/** * 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; }
/** * 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; }
/** * Returns an image with only specified channels copied * * @param WideImage_Image $img * @param array $channels * @return WideImage_Image */ function execute($img, $channels) { $blank = array('red' => 0, 'green' => 0, 'blue' => 0, 'alpha' => 0); $width = $img->getWidth(); $height = $img->getHeight(); $copy = WideImage_TrueColorImage::create($width, $height); if (count($channels) > 0) { for ($x = 0; $x < $width; $x++) { for ($y = 0; $y < $height; $y++) { $RGBA = $img->getRGBAt($x, $y); $newRGBA = $blank; foreach ($channels as $channel) { $newRGBA[$channel] = $RGBA[$channel]; } $color = $copy->getExactColorAlpha($newRGBA); if ($color == -1) { $color = $copy->allocateColorAlpha($newRGBA); } $copy->setColorAt($x, $y, $color); } } } return $copy; }
/** * Returns sharpened image * * @param WideImage_Image $image * @param float $amount * @param int $radius * @param float $threshold * @return WideImage_Image */ function execute($image, $amount, $radius, $threshold) { // Attempt to calibrate the parameters to Photoshop: if ($amount > 500) { $amount = 500; } $amount = $amount * 0.016; if ($radius > 50) { $radius = 50; } $radius = $radius * 2; if ($threshold > 255) { $threshold = 255; } $radius = abs(round($radius)); // Only integers make sense. if ($radius == 0) { return $image; } // Gaussian blur matrix $matrix = array(array(1, 2, 1), array(2, 4, 2), array(1, 2, 1)); $blurred = $image->applyConvolution($matrix, 16, 0); if ($threshold > 0) { // Calculate the difference between the blurred pixels and the original // and set the pixels for ($x = 0; $x < $image->getWidth(); $x++) { for ($y = 0; $y < $image->getHeight(); $y++) { $rgbOrig = $image->getRGBAt($x, $y); $rOrig = $rgbOrig["red"]; $gOrig = $rgbOrig["green"]; $bOrig = $rgbOrig["blue"]; $rgbBlur = $blurred->getRGBAt($x, $y); $rBlur = $rgbBlur["red"]; $gBlur = $rgbBlur["green"]; $bBlur = $rgbBlur["blue"]; // When the masked pixels differ less from the original // than the threshold specifies, they are set to their original value. $rNew = abs($rOrig - $rBlur) >= $threshold ? max(0, min(255, $amount * ($rOrig - $rBlur) + $rOrig)) : $rOrig; $gNew = abs($gOrig - $gBlur) >= $threshold ? max(0, min(255, $amount * ($gOrig - $gBlur) + $gOrig)) : $gOrig; $bNew = abs($bOrig - $bBlur) >= $threshold ? max(0, min(255, $amount * ($bOrig - $bBlur) + $bOrig)) : $bOrig; $rgbNew = array("red" => $rNew, "green" => $gNew, "blue" => $bNew, "alpha" => 0); if ($rOrig != $rNew || $gOrig != $gNew || $bOrig != $bNew) { $image->setRGBAt($x, $y, $rgbNew); } } } } else { $w = $image->getWidth(); $h = $image->getHeight(); for ($x = 0; $x < $w; $x++) { for ($y = 0; $y < $h; $y++) { $rgbOrig = $image->getRGBAt($x, $y); $rOrig = $rgbOrig["red"]; $gOrig = $rgbOrig["green"]; $bOrig = $rgbOrig["blue"]; $rgbBlur = $blurred->getRGBAt($x, $y); $rBlur = $rgbBlur["red"]; $gBlur = $rgbBlur["green"]; $bBlur = $rgbBlur["blue"]; $rNew = $amount * ($rOrig - $rBlur) + $rOrig; if ($rNew > 255) { $rNew = 255; } elseif ($rNew < 0) { $rNew = 0; } $gNew = $amount * ($gOrig - $gBlur) + $gOrig; if ($gNew > 255) { $gNew = 255; } elseif ($gNew < 0) { $gNew = 0; } $bNew = $amount * ($bOrig - $bBlur) + $bOrig; if ($bNew > 255) { $bNew = 255; } elseif ($bNew < 0) { $bNew = 0; } $rgbNew = array("red" => $rNew, "green" => $gNew, "blue" => $bNew, "alpha" => 0); $image->setRGBAt($x, $y, $rgbNew); } } } return $image; }
/** * 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); }