Ejemplo n.º 1
0
 /**
  * Writes text onto an image
  * 
  * @param \WideImage\Image $image
  * @param mixed $x smart coordinate
  * @param mixed $y smart coordinate
  * @param string $text
  * @param int $angle Angle in degrees clockwise
  */
 public function writeText($image, $x, $y, $text, $angle = 0)
 {
     if ($image->isTrueColor()) {
         $image->alphaBlending(true);
     }
     $box = imageftbbox($this->size, $angle, $this->face, $text);
     $obox = array('left' => min($box[0], $box[2], $box[4], $box[6]), 'top' => min($box[1], $box[3], $box[5], $box[7]), 'right' => max($box[0], $box[2], $box[4], $box[6]) - 1, 'bottom' => max($box[1], $box[3], $box[5], $box[7]) - 1);
     $obox['width'] = abs($obox['left']) + abs($obox['right']);
     $obox['height'] = abs($obox['top']) + abs($obox['bottom']);
     $x = Coordinate::fix($x, $image->getWidth(), $obox['width']);
     $y = Coordinate::fix($y, $image->getHeight(), $obox['height']);
     $fixed_x = $x - $obox['left'];
     $fixed_y = $y - $obox['top'];
     imagettftext($image->getHandle(), $this->size, $angle, $fixed_x, $fixed_y, $this->color, $this->face, $text);
 }
Ejemplo n.º 2
0
 /**
  * Prepares and corrects smart coordinates
  *
  * @param \WideImage\Image $img
  * @param smart_coordinate $width
  * @param smart_coordinate $height
  * @param string $fit
  * @return array
  */
 protected function prepareDimensions($img, $width, $height, $fit)
 {
     if ($width === null && $height === null) {
         return array('width' => $img->getWidth(), 'height' => $img->getHeight());
     }
     if ($width !== null) {
         $width = Coordinate::fix($width, $img->getWidth());
         $rx = $img->getWidth() / $width;
     } else {
         $rx = null;
     }
     if ($height !== null) {
         $height = Coordinate::fix($height, $img->getHeight());
         $ry = $img->getHeight() / $height;
     } else {
         $ry = null;
     }
     if ($rx === null && $ry !== null) {
         $rx = $ry;
         $width = round($img->getWidth() / $rx);
     }
     if ($ry === null && $rx !== null) {
         $ry = $rx;
         $height = round($img->getHeight() / $ry);
     }
     if ($width === 0 || $height === 0) {
         return array('width' => 0, 'height' => 0);
     }
     if ($fit == null) {
         $fit = 'inside';
     }
     $dim = [];
     if ($fit == 'fill') {
         $dim['width'] = $width;
         $dim['height'] = $height;
     } elseif ($fit == 'inside' || $fit == 'outside') {
         if ($fit == 'inside') {
             $ratio = $rx > $ry ? $rx : $ry;
         } else {
             $ratio = $rx < $ry ? $rx : $ry;
         }
         $dim['width'] = round($img->getWidth() / $ratio);
         $dim['height'] = round($img->getHeight() / $ratio);
     } else {
         throw new InvalidFitMethodException("{$fit} is not a valid resize-fit method.");
     }
     return $dim;
 }
 /**
  * Applies a mask on the copy of source image
  *
  * @param \WideImage\Image $image
  * @param \WideImage\Image $mask
  * @param smart_coordinate $left
  * @param smart_coordinate $top
  * @return \WideImage\Image
  */
 public function execute($image, $mask, $left = 0, $top = 0)
 {
     $left = Coordinate::fix($left, $image->getWidth(), $mask->getWidth());
     $top = Coordinate::fix($top, $image->getHeight(), $mask->getHeight());
     $width = $image->getWidth();
     $mask_width = $mask->getWidth();
     $height = $image->getHeight();
     $mask_height = $mask->getHeight();
     $result = $image->asTrueColor();
     $result->alphaBlending(false);
     $result->saveAlpha(true);
     $srcTransparentColor = $result->getTransparentColor();
     if ($srcTransparentColor >= 0) {
         $destTransparentColor = $srcTransparentColor;
     } else {
         $destTransparentColor = $result->allocateColorAlpha(255, 255, 255, 127);
     }
     for ($x = 0; $x < $width; $x++) {
         for ($y = 0; $y < $height; $y++) {
             $mx = $x - $left;
             $my = $y - $top;
             if ($mx >= 0 && $mx < $mask_width && $my >= 0 && $my < $mask_height) {
                 $srcColor = $image->getColorAt($x, $y);
                 if ($srcColor == $srcTransparentColor) {
                     $destColor = $destTransparentColor;
                 } else {
                     $maskRGB = $mask->getRGBAt($mx, $my);
                     if ($maskRGB['red'] == 0) {
                         $destColor = $destTransparentColor;
                     } elseif ($srcColor >= 0) {
                         $imageRGB = $image->getRGBAt($x, $y);
                         $level = $maskRGB['red'] / 255 * (1 - $imageRGB['alpha'] / 127);
                         $imageRGB['alpha'] = 127 - round($level * 127);
                         if ($imageRGB['alpha'] == 127) {
                             $destColor = $destTransparentColor;
                         } else {
                             $destColor = $result->allocateColorAlpha($imageRGB);
                         }
                     } else {
                         $destColor = $destTransparentColor;
                     }
                 }
                 $result->setColorAt($x, $y, $destColor);
             }
         }
     }
     return $result;
 }
 /**
  * Returns an image with a resized canvas
  * 
  * The image is filled with $color. Use $scale to determine, when to resize.
  *
  * @param \WideImage\Image $img
  * @param smart_coordinate $width
  * @param smart_coordinate $height
  * @param smart_coordinate $left
  * @param smart_coordinate $top
  * @param int $color
  * @param string $scale 'up', 'down', 'any'
  * @param boolean $merge
  * @return \WideImage\Image
  */
 public function execute($img, $width, $height, $left, $top, $color, $scale, $merge)
 {
     $new_width = Coordinate::fix($width, $img->getWidth());
     $new_height = Coordinate::fix($height, $img->getHeight());
     if ($scale == 'down') {
         $new_width = min($new_width, $img->getWidth());
         $new_height = min($new_height, $img->getHeight());
     } elseif ($scale == 'up') {
         $new_width = max($new_width, $img->getWidth());
         $new_height = max($new_height, $img->getHeight());
     }
     $new = WideImage::createTrueColorImage($new_width, $new_height);
     if ($img->isTrueColor()) {
         if ($color === null) {
             $color = $new->allocateColorAlpha(0, 0, 0, 127);
         }
     } else {
         imagepalettecopy($new->getHandle(), $img->getHandle());
         if ($img->isTransparent()) {
             $new->copyTransparencyFrom($img);
             $tc_rgb = $img->getTransparentColorRGB();
             $t_color = $new->allocateColorAlpha($tc_rgb);
         }
         if ($color === null) {
             if ($img->isTransparent()) {
                 $color = $t_color;
             } else {
                 $color = $new->allocateColorAlpha(255, 0, 127, 127);
             }
             imagecolortransparent($new->getHandle(), $color);
         }
     }
     $new->fill(0, 0, $color);
     $x = Coordinate::fix($left, $new->getWidth(), $img->getWidth());
     $y = Coordinate::fix($top, $new->getHeight(), $img->getHeight());
     // blending for truecolor images
     if ($img->isTrueColor()) {
         $new->alphaBlending($merge);
     }
     // not-blending for palette images
     if (!$merge && !$img->isTrueColor() && isset($t_color)) {
         $new->getCanvas()->filledRectangle($x, $y, $x + $img->getWidth(), $y + $img->getHeight(), $t_color);
     }
     $img->copyTo($new, $x, $y);
     return $new;
 }
Ejemplo n.º 5
0
 /**
  * Returns a merged image
  *
  * @param \WideImage\Image $base
  * @param \WideImage\Image $overlay
  * @param smart_coordinate $left
  * @param smart_coordinate $top
  * @param numeric $pct
  * @return \WideImage\Image
  */
 public function execute($base, $overlay, $left, $top, $pct)
 {
     $x = Coordinate::fix($left, $base->getWidth(), $overlay->getWidth());
     $y = Coordinate::fix($top, $base->getHeight(), $overlay->getHeight());
     $result = $base->asTrueColor();
     $result->alphaBlending(true);
     $result->saveAlpha(true);
     if ($pct <= 0) {
         return $result;
     }
     if ($pct < 100) {
         if (!imagecopymerge($result->getHandle(), $overlay->getHandle(), $x, $y, 0, 0, $overlay->getWidth(), $overlay->getHeight(), $pct)) {
             throw new GDFunctionResultException("imagecopymerge() returned false");
         }
     } else {
         if (!imagecopy($result->getHandle(), $overlay->getHandle(), $x, $y, 0, 0, $overlay->getWidth(), $overlay->getHeight())) {
             throw new GDFunctionResultException("imagecopy() returned false");
         }
     }
     return $result;
 }
Ejemplo n.º 6
0
 /**
  * Returns a cropped image
  *
  * @param \WideImage\Image $img
  * @param smart_coordinate $left
  * @param smart_coordinate $top
  * @param smart_coordinate $width
  * @param smart_coordinate $height
  * @return \WideImage\Image
  */
 public function execute($img, $left, $top, $width, $height)
 {
     $width = Coordinate::fix($width, $img->getWidth(), $width);
     $height = Coordinate::fix($height, $img->getHeight(), $height);
     $left = Coordinate::fix($left, $img->getWidth(), $width);
     $top = Coordinate::fix($top, $img->getHeight(), $height);
     if ($left < 0) {
         $width = $left + $width;
         $left = 0;
     }
     if ($width > $img->getWidth() - $left) {
         $width = $img->getWidth() - $left;
     }
     if ($top < 0) {
         $height = $top + $height;
         $top = 0;
     }
     if ($height > $img->getHeight() - $top) {
         $height = $img->getHeight() - $top;
     }
     if ($width <= 0 || $height <= 0) {
         throw new Exception("Can't crop outside of an image.");
     }
     $new = $img->doCreate($width, $height);
     if ($img->isTransparent() || $img instanceof PaletteImage) {
         $new->copyTransparencyFrom($img);
         if (!imagecopyresized($new->getHandle(), $img->getHandle(), 0, 0, $left, $top, $width, $height, $width, $height)) {
             throw new GDFunctionResultException("imagecopyresized() returned false");
         }
     } else {
         $new->alphaBlending(false);
         $new->saveAlpha(true);
         if (!imagecopyresampled($new->getHandle(), $img->getHandle(), 0, 0, $left, $top, $width, $height, $width, $height)) {
             throw new GDFunctionResultException("imagecopyresampled() returned false");
         }
     }
     return $new;
 }
 /**
  * @expectedException WideImage\Exception\InvalidCoordinateException
  */
 public function testInvalidSyntaxEndsWithOperator()
 {
     Coordinate::fix('5+2+', 10);
 }
Ejemplo n.º 8
0
 function getCoordinate($key, $default = 0)
 {
     $v = self::get($key);
     if (strlen($v) > 0 && \WideImage\Coordinate::parse($v) !== null) {
         return self::get($key);
     } else {
         return $default;
     }
 }