Esempio n. 1
0
 /**
  * (non-PHPdoc)
  * @see Imagine\ImageInterface::crop()
  */
 public function crop(PointInterface $start, BoxInterface $size)
 {
     if (!$start->in($size)) {
         throw new OutOfBoundsException('Crop coordinates must start at minimum 0, 0 position from ' . 'top left corner, crop height and width must be positive ' . 'integers and must not exceed the current image borders');
     }
     try {
         $this->gmagick->cropimage($size->getWidth(), $size->getHeight(), $start->getX(), $start->getY());
     } catch (\GmagickException $e) {
         throw new RuntimeException('Crop operation failed', $e->getCode(), $e);
     }
     return $this;
 }
Esempio n. 2
0
 public function crop(PointInterface $start, BoxInterface $size)
 {
     if ($this->image === null) {
         $this->load();
     }
     $width = $size->getWidth();
     $height = $size->getHeight();
     try {
         $this->image->getGmagick()->cropimage($width, $height, $start->getX(), $start->getY());
     } catch (\GmagickException $e) {
         throw new \Imagine\Exception\RuntimeException("Gmagick: Crop operation failed. {$e->getMessage()}", $e->getCode(), $e);
     }
     $this->size = array($width, $height);
     return $this;
 }
Esempio n. 3
0
 /**
  * {@inheritdoc}
  */
 public function crop(PointInterface $start, BoxInterface $size)
 {
     if (!$start->in($this->getSize())) {
         throw new OutOfBoundsException('Crop coordinates must start at minimum 0, 0 position from top left corner, crop height and width ' . 'must be positive integers and must not exceed the current image borders');
     }
     $this->fixViewBox();
     $svg = $this->document->documentElement;
     $svg->setAttribute('x', -$start->getX());
     $svg->setAttribute('y', -$start->getY());
     $svgWrap = $this->document->createElementNS('http://www.w3.org/2000/svg', 'svg');
     $svgWrap->setAttribute('version', '1.1');
     $svgWrap->setAttribute('width', $size->getWidth());
     $svgWrap->setAttribute('height', $size->getHeight());
     $svgWrap->appendChild($svg);
     $this->document->appendChild($svgWrap);
     return $this;
 }
Esempio n. 4
0
 public function crop(PointInterface $start, BoxInterface $size)
 {
     if ($this->image === null) {
         $this->load();
     }
     $width = $size->getWidth();
     $height = $size->getHeight();
     $magick = $this->image->getImagick();
     try {
         $magick->cropImage($width, $height, $start->getX(), $start->getY());
         $magick->setImagePage(0, 0, 0, 0);
         // Reset canvas for gif format
     } catch (\ImagickException $e) {
         throw new \Imagine\Exception\RuntimeException('Imagick: Crop operation failed', $e->getCode(), $e);
     }
     $this->size = array($width, $height);
     return $this;
 }
Esempio n. 5
0
 /**
  * (non-PHPdoc)
  * @see Imagine\ImageInterface::crop()
  */
 public final function crop(PointInterface $start, BoxInterface $size)
 {
     if (!$start->in($this->getSize())) {
         throw new OutOfBoundsException('Crop coordinates must start at minimum 0, 0 position from ' . 'top  left corner, crop height and width must be positive ' . 'integers and must not exceed the current image borders');
     }
     $width = $size->getWidth();
     $height = $size->getHeight();
     $dest = imagecreatetruecolor($width, $height);
     imagealphablending($dest, false);
     imagesavealpha($dest, true);
     if (function_exists('imageantialias')) {
         imageantialias($dest, true);
     }
     if (false === imagecopymerge($dest, $this->resource, 0, 0, $start->getX(), $start->getY(), $width, $height, 100)) {
         throw new RuntimeException('Image crop operation failed');
     }
     imagedestroy($this->resource);
     $this->resource = $dest;
     return $this;
 }
Esempio n. 6
0
 /**
  * {@inheritdoc}
  */
 public function isEqualTo($coordinates)
 {
     if ($coordinates instanceof BoxInterface) {
         $coordinates = new static($coordinates, new Point(0, 0), $coordinates);
     }
     if (!$coordinates instanceof ResizeCoordinatesInterface) {
         throw new \InvalidArgumentException(sprintf('$coordinates must be an instance of ResizeCoordinatesInterface or BoxInterface, "%s" given', get_class($coordinates)));
     }
     /* @var ResizeCoordinatesInterface $coordinates */
     return $this->cropStart->getX() === $coordinates->getCropStart()->getX() && $this->cropStart->getY() === $coordinates->getCropStart()->getY() && $this->cropSize->getWidth() === $coordinates->getCropSize()->getWidth() && $this->cropSize->getHeight() === $coordinates->getCropSize()->getHeight() && $this->size->getWidth() === $coordinates->getSize()->getWidth() && $this->size->getHeight() === $coordinates->getSize()->getHeight();
 }
Esempio n. 7
0
 /**
  * {@inheritdoc}
  */
 public function getColorAt(PointInterface $point)
 {
     if (!$point->in($this->getSize())) {
         throw new RuntimeException(sprintf('Error getting color at point [%s,%s]. The point must be inside the image of size [%s,%s]', $point->getX(), $point->getY(), $this->getSize()->getWidth(), $this->getSize()->getHeight()));
     }
     try {
         $cropped = clone $this->gmagick;
         $histogram = $cropped->cropImage(1, 1, $point->getX(), $point->getY())->getImageHistogram();
     } catch (\GmagickException $e) {
         throw new RuntimeException('Unable to get the pixel');
     }
     $pixel = array_shift($histogram);
     unset($histogram, $cropped);
     return $this->pixelToColor($pixel);
 }
Esempio n. 8
0
 /**
  * {@inheritdoc}
  */
 public function text($string, AbstractFont $font, PointInterface $position, $angle = 0)
 {
     try {
         $pixel = $this->getColor($font->getColor());
         $text = new \GmagickDraw();
         $text->setfont($font->getFile());
         /**
          * @see http://www.php.net/manual/en/imagick.queryfontmetrics.php#101027
          *
          * ensure font resolution is the same as GD's hard-coded 96
          */
         $text->setfontsize((int) ($font->getSize() * (96 / 72)));
         $text->setfillcolor($pixel);
         $info = $this->gmagick->queryfontmetrics($text, $string);
         $rad = deg2rad($angle);
         $cos = cos($rad);
         $sin = sin($rad);
         $x1 = round(0 * $cos - 0 * $sin);
         $x2 = round($info['textWidth'] * $cos - $info['textHeight'] * $sin);
         $y1 = round(0 * $sin + 0 * $cos);
         $y2 = round($info['textWidth'] * $sin + $info['textHeight'] * $cos);
         $xdiff = 0 - min($x1, $x2);
         $ydiff = 0 - min($y1, $y2);
         $this->gmagick->annotateimage($text, $position->getX() + $x1 + $xdiff, $position->getY() + $y2 + $ydiff, $angle, $string);
         $pixel = null;
         $text = null;
     } catch (\GmagickException $e) {
         throw new RuntimeException('Draw text operation failed', $e->getCode(), $e);
     }
     return $this;
 }
Esempio n. 9
0
 /**
  * {@inheritdoc}
  */
 public function getColorAt(PointInterface $point)
 {
     if (!$point->in($this->getSize())) {
         throw new RuntimeException(sprintf('Error getting color at point [%s,%s]. The point must be inside the image of size [%s,%s]', $point->getX(), $point->getY(), $this->getSize()->getWidth(), $this->getSize()->getHeight()));
     }
     $pixel = $this->imagick->getImagePixelColor($point->getX(), $point->getY());
     return new Color(array($pixel->getColorValue(\Imagick::COLOR_RED) * 255, $pixel->getColorValue(\Imagick::COLOR_GREEN) * 255, $pixel->getColorValue(\Imagick::COLOR_BLUE) * 255), (int) round($pixel->getColorValue(\Imagick::COLOR_ALPHA) * 100));
 }
Esempio n. 10
0
 /**
  * {@inheritdoc}
  */
 public function getColorAt(PointInterface $point)
 {
     if (!$point->in($this->getSize())) {
         throw new RuntimeException(sprintf('Error getting color at point [%s,%s]. The point must be inside the image of size [%s,%s]', $point->getX(), $point->getY(), $this->getSize()->getWidth(), $this->getSize()->getHeight()));
     }
     $index = imagecolorat($this->resource, $point->getX(), $point->getY());
     $info = imagecolorsforindex($this->resource, $index);
     return new Color(array($info['red'], $info['green'], $info['blue']), (int) round($info['alpha'] / 127 * 100));
 }
Esempio n. 11
0
 /**
  * (non-PHPdoc)
  * @see Imagine\Draw\DrawerInterface::text()
  */
 public function text($string, AbstractFont $font, PointInterface $position, $angle = 0)
 {
     try {
         $pixel = $this->getColor($font->getColor());
         $text = new \ImagickDraw();
         $text->setFont($font->getFile());
         $text->setFontSize($font->getSize());
         $text->setFillColor($pixel);
         $text->setTextAntialias(true);
         $info = $this->imagick->queryFontMetrics($text, $string);
         $rad = deg2rad($angle);
         $cos = cos($rad);
         $sin = sin($rad);
         $x1 = round(0 * $cos - 0 * $sin);
         $x2 = round($info['textWidth'] * $cos - $info['textHeight'] * $sin);
         $y1 = round(0 * $sin + 0 * $cos);
         $y2 = round($info['textWidth'] * $sin + $info['textHeight'] * $cos);
         $xdiff = 0 - min($x1, $x2);
         $ydiff = 0 - min($y1, $y2);
         $this->imagick->annotateImage($text, $position->getX() + $x1 + $xdiff, $position->getY() + $y2 + $ydiff, $angle, $string);
         $pixel->clear();
         $pixel->destroy();
         $text->clear();
         $text->destroy();
     } catch (\ImagickException $e) {
         throw new RuntimeException('Draw text operation failed', $e->getCode(), $e);
     }
 }
Esempio n. 12
0
 /**
  * {@inheritdoc}
  */
 public function text($string, AbstractFont $font, PointInterface $position, $angle = 0, $width = null)
 {
     try {
         $pixel = $this->getColor($font->getColor());
         $text = new \ImagickDraw();
         $text->setFont($font->getFile());
         /**
          * @see http://www.php.net/manual/en/imagick.queryfontmetrics.php#101027
          *
          * ensure font resolution is the same as GD's hard-coded 96
          */
         if (version_compare(phpversion("imagick"), "3.0.2", ">=")) {
             $text->setResolution(96, 96);
             $text->setFontSize($font->getSize());
         } else {
             $text->setFontSize((int) ($font->getSize() * (96 / 72)));
         }
         $text->setFillColor($pixel);
         $text->setTextAntialias(true);
         $info = $this->imagick->queryFontMetrics($text, $string);
         $rad = deg2rad($angle);
         $cos = cos($rad);
         $sin = sin($rad);
         // round(0 * $cos - 0 * $sin)
         $x1 = 0;
         $x2 = round($info['characterWidth'] * $cos - $info['characterHeight'] * $sin);
         // round(0 * $sin + 0 * $cos)
         $y1 = 0;
         $y2 = round($info['characterWidth'] * $sin + $info['characterHeight'] * $cos);
         $xdiff = 0 - min($x1, $x2);
         $ydiff = 0 - min($y1, $y2);
         if ($width !== null) {
             $string = $this->wrapText($string, $text, $angle, $width);
         }
         $this->imagick->annotateImage($text, $position->getX() + $x1 + $xdiff, $position->getY() + $y2 + $ydiff, $angle, $string);
         $pixel->clear();
         $pixel->destroy();
         $text->clear();
         $text->destroy();
     } catch (\ImagickException $e) {
         throw new RuntimeException('Draw text operation failed', $e->getCode(), $e);
     }
     return $this;
 }
Esempio n. 13
0
 /**
  * (non-PHPdoc)
  * @see Imagine\Draw\DrawerInterface::text()
  */
 public function text($string, AbstractFont $font, PointInterface $position, $angle = 0)
 {
     $angle = -1 * $angle;
     $fontsize = $font->getSize();
     $fontfile = $font->getFile();
     $info = imageftbbox($fontsize, $angle, $fontfile, $string);
     $xs = array($info[0], $info[2], $info[4], $info[6]);
     $ys = array($info[1], $info[3], $info[5], $info[7]);
     $width = abs(max($xs) - min($xs));
     $height = abs(max($ys) - min($ys));
     $xdiff = 0 - min($xs) + $position->getX();
     $ydiff = 0 - min($ys) + $position->getY();
     foreach ($xs as &$x) {
         $x += $xdiff;
     }
     foreach ($ys as &$y) {
         $y += $ydiff;
     }
     if (false === imagealphablending($this->resource, true)) {
         throw new RuntimeException('Font mask operation failed');
     }
     if (false === imagefttext($this->resource, $fontsize, $angle, $xs[0], $ys[0], $this->getColor($font->getColor()), $fontfile, $string)) {
         throw new RuntimeException('Font mask operation failed');
     }
     if (false === imagealphablending($this->resource, false)) {
         throw new RuntimeException('Font mask operation failed');
     }
     return $this;
 }
Esempio n. 14
0
 /**
  * @covers Imagine\Image\Point\Center::getX
  * @covers Imagine\Image\Point\Center::getY
  *
  * @dataProvider getSizesAndCoordinates
  *
  * @param Imagine\Image\BoxInterface   $box
  * @param Imagine\Image\PointInterface $expected
  */
 public function testShouldGetCenterCoordinates(BoxInterface $box, PointInterface $expected)
 {
     $point = new Center($box);
     $this->assertEquals($expected->getX(), $point->getX());
     $this->assertEquals($expected->getY(), $point->getY());
 }
Esempio n. 15
0
 /**
  * (non-PHPdoc)
  * @see Imagine\Image\ImageInterface::getColorAt()
  */
 public function getColorAt(PointInterface $point)
 {
     if (!$point->in($this->getSize())) {
         throw new RuntimeException(sprintf('Error getting color at point [%s,%s]. The point must be inside the image of size [%s,%s]', $point->getX(), $point->getY(), $this->getSize()->getWidth(), $this->getSize()->getHeight()));
     }
     throw new RuntimeException('Not Implemented!');
 }
Esempio n. 16
0
 private function translatePoint(PointInterface $point, $x, $y)
 {
     return new Point($x - $point->getX(), $y - $point->getY());
 }
Esempio n. 17
0
 /**
  * {@inheritdoc}
  */
 public function getDistance(PointInterface $position)
 {
     return $position->getY();
 }
Esempio n. 18
0
 /**
  * {@inheritdoc}
  */
 public function text($string, AbstractFont $font, PointInterface $position, $angle = 0)
 {
     if (!$this->info['FreeType Support']) {
         throw new RuntimeException('GD is not compiled with FreeType support');
     }
     $angle = -1 * $angle;
     $fontsize = $font->getSize();
     $fontfile = $font->getFile();
     $x = $position->getX();
     $y = $position->getY() + $fontsize;
     if (false === imagealphablending($this->resource, true)) {
         throw new RuntimeException('Font mask operation failed');
     }
     if (false === imagefttext($this->resource, $fontsize, $angle, $x, $y, $this->getColor($font->getColor()), $fontfile, $string)) {
         throw new RuntimeException('Font mask operation failed');
     }
     if (false === imagealphablending($this->resource, false)) {
         throw new RuntimeException('Font mask operation failed');
     }
     return $this;
 }
Esempio n. 19
0
 /**
  * {@inheritdoc}
  */
 public function getColorAt(PointInterface $point)
 {
     if (!$point->in($this->getSize())) {
         throw new RuntimeException(sprintf('Error getting color at point [%s,%s]. The point must be inside the image of size [%s,%s]', $point->getX(), $point->getY(), $this->getSize()->getWidth(), $this->getSize()->getHeight()));
     }
     try {
         $pixel = $this->imagick->getImagePixelColor($point->getX(), $point->getY());
     } catch (\ImagickException $e) {
         throw new RuntimeException('Error while getting image pixel color', $e->getCode(), $e);
     }
     return $this->pixelToColor($pixel);
 }
Esempio n. 20
0
 /**
  * {@inheritdoc}
  */
 public function text($string, AbstractFont $font, PointInterface $position, $angle = 0, $width = null)
 {
     try {
         $pixel = $this->getColor($font->getColor());
         $text = new \GmagickDraw();
         $text->setfont($font->getFile());
         /**
          * @see http://www.php.net/manual/en/imagick.queryfontmetrics.php#101027
          *
          * ensure font resolution is the same as GD's hard-coded 96
          */
         $text->setfontsize((int) ($font->getSize() * (96 / 72)));
         $text->setfillcolor($pixel);
         $info = $this->gmagick->queryfontmetrics($text, $string);
         // $rad  = deg2rad($angle);
         // $cos  = cos($rad);
         // $sin  = sin($rad);
         // $x1 = round(0 * $cos - 0 * $sin);
         // $x2 = round($info['textWidth'] * $cos - $info['textHeight'] * $sin);
         // $y1 = round(0 * $sin + 0 * $cos);
         // $y2 = round($info['textWidth'] * $sin + $info['textHeight'] * $cos);
         // $xdiff = 0 - min($x1, $x2);
         // $ydiff = 0 - min($y1, $y2);
         if ($width !== null) {
             throw new NotSupportedException('Gmagick doesn\'t support queryfontmetrics function for multiline text', 1);
         }
         // $this->gmagick->annotateimage($text, $position->getX() + $x1 + $xdiff, $position->getY() + $y2 + $ydiff, $angle, $string);
         $deltaX = $info['characterWidth'] * sin(deg2rad($angle));
         $deltaY = $info['characterHeight'];
         $posX = $position->getX() - $deltaX;
         if ($posX < 0) {
             $posX = 0;
         }
         $this->gmagick->annotateimage($text, $posX, $position->getY() + $deltaY, $angle, $string);
         unset($pixel, $text);
     } catch (\GmagickException $e) {
         throw new RuntimeException('Draw text operation failed', $e->getCode(), $e);
     }
     return $this;
 }