/**
  * Returns a greyscale copy of an image
  *
  * @param \WideImage\Image $image
  * @return \WideImage\Image
  */
 public function execute($image)
 {
     $new = $image->asTrueColor();
     if (!imagefilter($new->getHandle(), IMG_FILTER_GRAYSCALE)) {
         throw new GDFunctionResultException("imagefilter() returned false");
     }
     if (!$image->isTrueColor()) {
         $new = $new->asPalette();
     }
     return $new;
 }
 /**
  * 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);
 }
 /**
  * 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;
 }
 /**
  * Returns a greyscale copy of an image
  *
  * @param \WideImage\Image $image
  * @return \WideImage\Image
  */
 public function execute($image)
 {
     $palette = !$image->isTrueColor();
     $transparent = $image->isTransparent();
     if ($palette && $transparent) {
         $tcrgb = $image->getTransparentColorRGB();
     }
     $new = $image->asTrueColor();
     if (!imagefilter($new->getHandle(), IMG_FILTER_NEGATE)) {
         throw new GDFunctionResultException("imagefilter() returned false");
     }
     if ($palette) {
         $new = $new->asPalette();
         if ($transparent) {
             $irgb = array('red' => 255 - $tcrgb['red'], 'green' => 255 - $tcrgb['green'], 'blue' => 255 - $tcrgb['blue'], 'alpha' => 127);
             // needs imagecolorexactalpha instead of imagecolorexact, otherwise doesn't work on some transparent GIF images
             $new_tci = imagecolorexactalpha($new->getHandle(), $irgb['red'], $irgb['green'], $irgb['blue'], 127);
             $new->setTransparentColor($new_tci);
         }
     }
     return $new;
 }