/**
  * 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
  */
 function execute($image, $mask, $left = 0, $top = 0)
 {
     $left = WideImage_Coordinate::fix($left, $image->getWidth(), $mask->getWidth());
     $top = WideImage_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) {
         # this was here. works without.
         #$trgb = $image->getColorRGB($srcTransparentColor);
         #$trgb['alpha'] = 127;
         #$destTransparentColor = $result->allocateColorAlpha($trgb);
         #$result->setTransparentColor($destTransparentColor);
         $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;
 }
Example #2
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
  */
 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 = WideImage_Coordinate::fix($x, $image->getWidth(), $obox['width']);
     $y = WideImage_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);
 }
 /**
  * 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
  */
 function execute($img, $width, $height, $left, $top, $color, $scale, $merge)
 {
     $new_width = WideImage_Coordinate::fix($width, $img->getWidth());
     $new_height = WideImage_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 = WideImage_Coordinate::fix($left, $new->getWidth(), $img->getWidth());
     $y = WideImage_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;
 }
 /**
  * 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) {
         $width = $img->getWidth();
         $height = $img->getHeight();
     }
     if ($width !== null) {
         $width = WideImage_Coordinate::fix($width, $img->getWidth());
     }
     if ($height !== null) {
         $height = WideImage_Coordinate::fix($height, $img->getHeight());
     }
     if ($width === null) {
         $width = floor($img->getWidth() * $height / $img->getHeight());
     }
     if ($height === null) {
         $height = floor($img->getHeight() * $width / $img->getWidth());
     }
     if ($width === 0 || $height === 0) {
         return array('width' => 0, 'height' => 0);
     }
     if ($fit == null) {
         $fit = 'inside';
     }
     $dim = array();
     if ($fit == 'fill') {
         $dim['width'] = $width;
         $dim['height'] = $height;
     } elseif ($fit == 'inside' || $fit == 'outside') {
         $rx = $img->getWidth() / $width;
         $ry = $img->getHeight() / $height;
         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 WideImage_Operation_InvalidFitMethodException("{$fit} is not a valid resize-fit method.");
     }
     return $dim;
 }
 /**
  * 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
  */
 function execute($base, $overlay, $left, $top, $pct)
 {
     $x = WideImage_Coordinate::fix($left, $base->getWidth(), $overlay->getWidth());
     $y = WideImage_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 WideImage_GDFunctionResultException("imagecopymerge() returned false");
         }
     } else {
         if (!imagecopy($result->getHandle(), $overlay->getHandle(), $x, $y, 0, 0, $overlay->getWidth(), $overlay->getHeight())) {
             throw new WideImage_GDFunctionResultException("imagecopy() returned false");
         }
     }
     return $result;
 }
 /**
  * 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
  */
 function execute($img, $left, $top, $width, $height)
 {
     $width = WideImage_Coordinate::fix($width, $img->getWidth(), $width);
     $height = WideImage_Coordinate::fix($height, $img->getHeight(), $height);
     $left = WideImage_Coordinate::fix($left, $img->getWidth(), $width);
     $top = WideImage_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 WideImage_Exception("Can't crop outside of an image.");
     }
     $new = $img->doCreate($width, $height);
     if ($img->isTransparent() || $img instanceof WideImage_PaletteImage) {
         $new->copyTransparencyFrom($img);
         if (!imagecopyresized($new->getHandle(), $img->getHandle(), 0, 0, $left, $top, $width, $height, $width, $height)) {
             throw new WideImage_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 WideImage_GDFunctionResultException("imagecopyresampled() returned false");
         }
     }
     return $new;
 }