Example #1
0
 /**
  * Returns a cropped image
  *
  * @param GDImage_Image $img
  * @param smart_coordinate $left
  * @param smart_coordinate $top
  * @param smart_coordinate $width
  * @param smart_coordinate $height
  * @return GDImage_Image
  */
 function execute($img, $left, $top, $width, $height)
 {
     $width = GDImage_Coordinate::fix($width, $img->getWidth(), $width);
     $height = GDImage_Coordinate::fix($height, $img->getHeight(), $height);
     $left = GDImage_Coordinate::fix($left, $img->getWidth(), $width);
     $top = GDImage_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) {
         JError::raiseError(500, JText::_('JLIB_GDIMAGE_ERROR_CAN_NOT_CROP_OUTSIDE'));
         return false;
     }
     $new = $img->doCreate($width, $height);
     if ($img->isTransparent() || $img instanceof GDImage_PaletteImage) {
         $new->copyTransparencyFrom($img);
         imagecopyresized($new->getHandle(), $img->getHandle(), 0, 0, $left, $top, $width, $height, $width, $height);
     } else {
         $new->alphaBlending(false);
         $new->saveAlpha(true);
         imagecopyresampled($new->getHandle(), $img->getHandle(), 0, 0, $left, $top, $width, $height, $width, $height);
     }
     return $new;
 }
 /**
  * Applies a mask on the copy of source image
  *
  * @param GDImage_Image $image
  * @param GDImage_Image $mask
  * @param smart_coordinate $left
  * @param smart_coordinate $top
  * @return GDImage_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) {
         $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 a merged image
  *
  * @param GDImage_Image $base
  * @param GDImage_Image $overlay
  * @param smart_coordinate $left
  * @param smart_coordinate $top
  * @param numeric $pct
  * @return GDImage_Image
  */
 function execute($base, $overlay, $left, $top, $pct)
 {
     $x = GDImage_Coordinate::fix($left, $base->getWidth(), $overlay->getWidth());
     $y = GDImage_Coordinate::fix($top, $base->getHeight(), $overlay->getHeight());
     $result = $base->asTrueColor();
     $result->alphaBlending(true);
     $result->saveAlpha(true);
     if ($pct <= 0) {
         return $result;
     }
     if ($pct < 100) {
         imagecopymerge($result->getHandle(), $overlay->getHandle(), $x, $y, 0, 0, $overlay->getWidth(), $overlay->getHeight(), $pct);
     } else {
         imagecopy($result->getHandle(), $overlay->getHandle(), $x, $y, 0, 0, $overlay->getWidth(), $overlay->getHeight());
     }
     return $result;
 }
 /**
  * Fix a coordinate for a resize (limits by image weight and height)
  * 
  * @param GDImage_Image $img
  * @param int $width Width of the image
  * @param int $height Height of the image
  * @return array An array(width, height), fixed for resizing
  */
 static function fixForResize($img, $width, $height)
 {
     if ($width === null && $height === null) {
         return array($img->getWidth(), $img->getHeight());
     }
     if ($width !== null) {
         $width = self::fix($width, $img->getWidth());
     }
     if ($height !== null) {
         $height = self::fix($height, $img->getHeight());
     }
     if ($width === null) {
         $width = floor($img->getWidth() * $height / $img->getHeight());
     }
     if ($height === null) {
         $height = floor($img->getHeight() * $width / $img->getWidth());
     }
     return array($width, $height);
 }