Example #1
0
 /**
  * Returns a mask
  *
  * @param WideImage_Image $image
  * @return WideImage_Image
  */
 function execute($image)
 {
     $width = $image->getWidth();
     $height = $image->getHeight();
     $mask = WideImage_TrueColorImage::create($width, $height);
     $mask->setTransparentColor(-1);
     $mask->alphaBlending(false);
     $mask->saveAlpha(false);
     for ($i = 0; $i <= 255; $i++) {
         $greyscale[$i] = ImageColorAllocate($mask->getHandle(), $i, $i, $i);
     }
     imagefilledrectangle($mask->getHandle(), 0, 0, $width, $height, $greyscale[255]);
     $transparentColor = $image->getTransparentColor();
     $alphaToGreyRatio = 255 / 127;
     for ($x = 0; $x < $width; $x++) {
         for ($y = 0; $y < $height; $y++) {
             $color = $image->getColorAt($x, $y);
             if ($color == $transparentColor) {
                 $rgba['alpha'] = 127;
             } else {
                 $rgba = $image->getColorRGB($color);
             }
             imagesetpixel($mask->getHandle(), $x, $y, $greyscale[255 - round($rgba['alpha'] * $alphaToGreyRatio)]);
         }
     }
     return $mask;
 }
Example #2
0
 /**
  * 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($image->getWidth(), $left);
     $top = WideImage_Coordinate::fix($image->getHeight(), $top);
     $width = $image->getWidth();
     if ($width > $mask->getWidth()) {
         $width = $mask->getWidth();
     }
     $height = $image->getHeight();
     if ($height > $mask->getHeight()) {
         $height = $mask->getHeight();
     }
     $result = $image->asTrueColor();
     $result->alphaBlending(false);
     $result->saveAlpha(true);
     $srcTransparentColor = $image->getTransparentColor();
     if ($srcTransparentColor >= 0) {
         $trgb = $image->getColorRGB($srcTransparentColor);
         $trgb['alpha'] = 127;
         $destTransparentColor = $result->allocateColorAlpha($trgb);
         $result->setTransparentColor($destTransparentColor);
     } else {
         $destTransparentColor = $result->allocateColorAlpha(255, 255, 255, 127);
     }
     for ($x = 0; $x < $width; $x++) {
         for ($y = 0; $y < $height; $y++) {
             if ($left + $x < $image->getWidth() && $top + $y < $image->getHeight()) {
                 $srcColor = $image->getColorAt($left + $x, $top + $y);
                 if ($srcColor == $srcTransparentColor) {
                     $destColor = $destTransparentColor;
                 } else {
                     $maskRGB = $mask->getRGBAt($x, $y);
                     if ($maskRGB['red'] == 0) {
                         $destColor = $destTransparentColor;
                     } elseif ($srcColor >= 0) {
                         $imageRGB = $image->getRGBAt($left + $x, $top + $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($left + $x, $top + $y, $destColor);
             }
         }
     }
     return $result;
 }
Example #3
0
 /**
  * Executes the auto-crop operation on the $img
  * 
  * @param WideImage_Image $img 
  * @param int $rgb_threshold The difference in RGB from $base_color
  * @param int $pixel_cutoff The number of pixels on each border that must be over $rgb_threshold
  * @param int $base_color The color that will get cropped
  * @return WideImage_Image resulting auto-cropped image
  */
 function execute($img, $margin, $rgb_threshold, $pixel_cutoff, $base_color)
 {
     $margin = intval($margin);
     $rgb_threshold = intval($rgb_threshold);
     if ($rgb_threshold < 0) {
         $rgb_threshold = 0;
     }
     $pixel_cutoff = intval($pixel_cutoff);
     if ($pixel_cutoff <= 1) {
         $pixel_cutoff = 1;
     }
     if ($base_color === null) {
         $rgb_base = $img->getRGBAt(0, 0);
     } else {
         if ($base_color < 0) {
             return $img->copy();
         }
         $rgb_base = $img->getColorRGB($base_color);
     }
     $cut_rect = array('left' => 0, 'top' => 0, 'right' => $img->getWidth() - 1, 'bottom' => $img->getHeight() - 1);
     for ($y = 0; $y <= $cut_rect['bottom']; $y++) {
         $count = 0;
         for ($x = 0; $x <= $cut_rect['right']; $x++) {
             $rgb = $img->getRGBAt($x, $y);
             $diff = abs($rgb['red'] - $rgb_base['red']) + abs($rgb['green'] - $rgb_base['green']) + abs($rgb['blue'] - $rgb_base['blue']);
             if ($diff > $rgb_threshold) {
                 $count++;
                 if ($count >= $pixel_cutoff) {
                     $cut_rect['top'] = $y;
                     break 2;
                 }
             }
         }
     }
     for ($y = $img->getHeight() - 1; $y >= $cut_rect['top']; $y--) {
         $count = 0;
         for ($x = 0; $x <= $cut_rect['right']; $x++) {
             $rgb = $img->getRGBAt($x, $y);
             $diff = abs($rgb['red'] - $rgb_base['red']) + abs($rgb['green'] - $rgb_base['green']) + abs($rgb['blue'] - $rgb_base['blue']);
             if ($diff > $rgb_threshold) {
                 $count++;
                 if ($count >= $pixel_cutoff) {
                     $cut_rect['bottom'] = $y;
                     break 2;
                 }
             }
         }
     }
     for ($x = 0; $x <= $cut_rect['right']; $x++) {
         $count = 0;
         for ($y = $cut_rect['top']; $y <= $cut_rect['bottom']; $y++) {
             $rgb = $img->getRGBAt($x, $y);
             $diff = abs($rgb['red'] - $rgb_base['red']) + abs($rgb['green'] - $rgb_base['green']) + abs($rgb['blue'] - $rgb_base['blue']);
             if ($diff > $rgb_threshold) {
                 $count++;
                 if ($count >= $pixel_cutoff) {
                     $cut_rect['left'] = $x;
                     break 2;
                 }
             }
         }
     }
     for ($x = $cut_rect['right']; $x >= $cut_rect['left']; $x--) {
         $count = 0;
         for ($y = $cut_rect['top']; $y <= $cut_rect['bottom']; $y++) {
             $rgb = $img->getRGBAt($x, $y);
             $diff = abs($rgb['red'] - $rgb_base['red']) + abs($rgb['green'] - $rgb_base['green']) + abs($rgb['blue'] - $rgb_base['blue']);
             if ($diff > $rgb_threshold) {
                 $count++;
                 if ($count >= $pixel_cutoff) {
                     $cut_rect['right'] = $x;
                     break 2;
                 }
             }
         }
     }
     $cut_rect = array('left' => $cut_rect['left'] - $margin, 'top' => $cut_rect['top'] - $margin, 'right' => $cut_rect['right'] + $margin, 'bottom' => $cut_rect['bottom'] + $margin);
     if ($cut_rect['left'] < 0) {
         $cut_rect['left'] = 0;
     }
     if ($cut_rect['top'] < 0) {
         $cut_rect['top'] = 0;
     }
     if ($cut_rect['right'] >= $img->getWidth()) {
         $cut_rect['right'] = $img->getWidth() - 1;
     }
     if ($cut_rect['bottom'] >= $img->getHeight()) {
         $cut_rect['bottom'] = $img->getHeight() - 1;
     }
     return $img->crop($cut_rect['left'], $cut_rect['top'], $cut_rect['right'] - $cut_rect['left'] + 1, $cut_rect['bottom'] - $cut_rect['top'] + 1);
 }