/** * @return string */ public function getAveragePixelColor() { $this->intervention->resize(1, 1); $color = substr($this->intervention->pickColor(0, 0, 'hex'), 1); $this->intervention->reset(); return $color; }
/** * Calculates checksum of given image * * @param \Intervention\Image\Image $image * @return boolean */ public function execute($image) { $colors = array(); $size = $image->getSize(); for ($x = 0; $x <= $size->width - 1; $x++) { for ($y = 0; $y <= $size->height - 1; $y++) { $colors[] = $image->pickColor($x, $y, 'array'); } } $this->setOutput(md5(serialize($colors))); return true; }
/** * Applies an alpha mask to an image * * @param \Intervention\Image\Image $image * @return boolean */ public function execute($image) { $mask_source = $this->argument(0)->value(); $mask_w_alpha = $this->argument(1)->type('bool')->value(false); $image_size = $image->getSize(); // create empty canvas $canvas = $image->getDriver()->newImage($image_size->width, $image_size->height, array(0, 0, 0, 0)); // build mask image from source $mask = $image->getDriver()->init($mask_source); $mask_size = $mask->getSize(); // resize mask to size of current image (if necessary) if ($mask_size != $image_size) { $mask->resize($image_size->width, $image_size->height); } imagealphablending($canvas->getCore(), false); if (!$mask_w_alpha) { // mask from greyscale image imagefilter($mask->getCore(), IMG_FILTER_GRAYSCALE); } // redraw old image pixel by pixel considering alpha map for ($x = 0; $x < $image_size->width; $x++) { for ($y = 0; $y < $image_size->height; $y++) { $color = $image->pickColor($x, $y, 'array'); $alpha = $mask->pickColor($x, $y, 'array'); if ($mask_w_alpha) { $alpha = $alpha[3]; // use alpha channel as mask } else { if ($alpha[3] == 0) { // transparent as black $alpha = 0; } else { // $alpha = floatval(round((($alpha[0] + $alpha[1] + $alpha[3]) / 3) / 255, 2)); // image is greyscale, so channel doesn't matter (use red channel) $alpha = floatval(round($alpha[0] / 255, 2)); } } // preserve alpha of original image... if ($color[3] < $alpha) { $alpha = $color[3]; } // replace alpha value $color[3] = $alpha; // redraw pixel $canvas->pixel($color, $x, $y); } } // replace current image with masked instance $image->setCore($canvas->getCore()); return true; }
/** * Trims away parts of an image * * @param \Intervention\Image\Image $image * @return boolean */ public function execute($image) { $base = $this->argument(0)->type('string')->value(); $away = $this->argument(1)->value(); $tolerance = $this->argument(2)->type('numeric')->value(0); $feather = $this->argument(3)->type('numeric')->value(0); $width = $image->getWidth(); $height = $image->getHeight(); // default values $checkTransparency = false; // define borders to trim away if (is_null($away)) { $away = array('top', 'right', 'bottom', 'left'); } elseif (is_string($away)) { $away = array($away); } // lower border names foreach ($away as $key => $value) { $away[$key] = strtolower($value); } // define base color position switch (strtolower($base)) { case 'transparent': case 'trans': $checkTransparency = true; $base_x = 0; $base_y = 0; break; case 'bottom-right': case 'right-bottom': $base_x = $width - 1; $base_y = $height - 1; break; default: case 'top-left': case 'left-top': $base_x = 0; $base_y = 0; break; } // pick base color if ($checkTransparency) { $color = new Color(); // color will only be used to compare alpha channel } else { $color = $image->pickColor($base_x, $base_y, 'object'); } $top_x = 0; $top_y = 0; $bottom_x = $width; $bottom_y = $height; // search upper part of image for colors to trim away if (in_array('top', $away)) { for ($y = 0; $y < ceil($height / 2); $y++) { for ($x = 0; $x < $width; $x++) { $checkColor = $image->pickColor($x, $y, 'object'); if ($checkTransparency) { $checkColor->r = $color->r; $checkColor->g = $color->g; $checkColor->b = $color->b; } if ($color->differs($checkColor, $tolerance)) { $top_y = max(0, $y - $feather); break 2; } } } } // search left part of image for colors to trim away if (in_array('left', $away)) { for ($x = 0; $x < ceil($width / 2); $x++) { for ($y = $top_y; $y < $height; $y++) { $checkColor = $image->pickColor($x, $y, 'object'); if ($checkTransparency) { $checkColor->r = $color->r; $checkColor->g = $color->g; $checkColor->b = $color->b; } if ($color->differs($checkColor, $tolerance)) { $top_x = max(0, $x - $feather); break 2; } } } } // search lower part of image for colors to trim away if (in_array('bottom', $away)) { for ($y = $height - 1; $y >= floor($height / 2) - 1; $y--) { for ($x = $top_x; $x < $width; $x++) { $checkColor = $image->pickColor($x, $y, 'object'); if ($checkTransparency) { $checkColor->r = $color->r; $checkColor->g = $color->g; $checkColor->b = $color->b; } if ($color->differs($checkColor, $tolerance)) { $bottom_y = min($height, $y + 1 + $feather); break 2; } } } } // search right part of image for colors to trim away if (in_array('right', $away)) { for ($x = $width - 1; $x >= floor($width / 2) - 1; $x--) { for ($y = $top_y; $y < $bottom_y; $y++) { $checkColor = $image->pickColor($x, $y, 'object'); if ($checkTransparency) { $checkColor->r = $color->r; $checkColor->g = $color->g; $checkColor->b = $color->b; } if ($color->differs($checkColor, $tolerance)) { $bottom_x = min($width, $x + 1 + $feather); break 2; } } } } // trim parts of image return $this->modify($image, 0, 0, $top_x, $top_y, $bottom_x - $top_x, $bottom_y - $top_y, $bottom_x - $top_x, $bottom_y - $top_y); }
public function testBackupKeepTransparency($value = '') { $img = new Image('public/circle.png'); $img->backup(); $img->reset(); $transparent = array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0.0); $this->assertEquals($transparent, $img->pickColor(0, 0, 'array')); }
/** * Picks and formats color at position * * @param int $x * @param int $y * @param string $format * @return mixed * @static */ public static function pickColor($x, $y, $format = null) { return \Intervention\Image\Image::pickColor($x, $y, $format); }
/** * Trims away parts of an image * * @param \Intervention\Image\Image $image * @return boolean */ public function execute($image) { $base = $this->argument(0)->type('string')->value(); $away = $this->argument(1)->value(); $tolerance = $this->argument(2)->type('numeric')->value(0); $feather = $this->argument(3)->type('numeric')->value(0); $width = $image->getWidth(); $height = $image->getHeight(); $checkTransparency = false; // define borders to trim away if (is_null($away)) { $away = array('top', 'right', 'bottom', 'left'); } elseif (is_string($away)) { $away = array($away); } // lower border names foreach ($away as $key => $value) { $away[$key] = strtolower($value); } // define base color position switch (strtolower($base)) { case 'transparent': case 'trans': $checkTransparency = true; $base_x = 0; $base_y = 0; break; case 'bottom-right': case 'right-bottom': $base_x = $width - 1; $base_y = $height - 1; break; default: case 'top-left': case 'left-top': $base_x = 0; $base_y = 0; break; } // pick base color if ($checkTransparency) { $base_color = new Color(); // color will only be used to compare alpha channel } else { $base_color = $image->pickColor($base_x, $base_y, 'object'); } // trim on clone to get only coordinates $trimed = clone $image->getCore(); // add border to trim specific color $trimed->borderImage($base_color->getPixel(), 1, 1); // trim image $trimed->trimImage(65850 / 100 * $tolerance); // get coordinates of trim $imagePage = $trimed->getImagePage(); list($crop_x, $crop_y) = array($imagePage['x'] - 1, $imagePage['y'] - 1); // $trimed->setImagePage(0, 0, 0, 0); list($crop_width, $crop_height) = array($trimed->width, $trimed->height); // adjust settings if right should not be trimed if (!in_array('right', $away)) { $crop_width = $crop_width + ($width - ($width - $crop_x)); } // adjust settings if bottom should not be trimed if (!in_array('bottom', $away)) { $crop_height = $crop_height + ($height - ($height - $crop_y)); } // adjust settings if left should not be trimed if (!in_array('left', $away)) { $crop_width = $crop_width + $crop_x; $crop_x = 0; } // adjust settings if top should not be trimed if (!in_array('top', $away)) { $crop_height = $crop_height + $crop_y; $crop_y = 0; } // add feather $crop_width = min($width, $crop_width + $feather * 2); $crop_height = min($height, $crop_height + $feather * 2); $crop_x = max(0, $crop_x - $feather); $crop_y = max(0, $crop_y - $feather); // finally crop based on page $image->getCore()->cropImage($crop_width, $crop_height, $crop_x, $crop_y); $image->getCore()->setImagePage(0, 0, 0, 0); $trimed->destroy(); return true; }
public function testInsertPng8WithAlphaChannel() { $img = new Image(null, 16, 16, '#ff0000'); $img->insert('public/png8.png'); $this->assertEquals('#ff0000', $img->pickColor(0, 0, 'hex')); $this->assertEquals('#8c8c8c', $img->pickColor(10, 10, 'hex')); }
public function testBackup() { $img = new Image(null, 800, 600, '#0000ff'); $img->fill('#00ff00'); $img->backup(); $img->resize(200, 200); $img->reset(); $this->assertInternalType('int', $img->width); $this->assertInternalType('int', $img->height); $this->assertEquals($img->width, 800); $this->assertEquals($img->height, 600); $this->assertEquals('#00ff00', $img->pickColor(0, 0, 'hex')); $img = new Image('public/tile.png'); $img->resize(10, 10); $img->fill('#00ff00'); $img->backup(); $img->resize(5, 5); $img->reset(); $this->assertInternalType('int', $img->width); $this->assertInternalType('int', $img->height); $this->assertEquals($img->width, 10); $this->assertEquals($img->height, 10); $this->assertEquals('#00ff00', $img->pickColor(0, 0, 'hex')); }