/** * Resizes image boundaries * * @param Intervention\Image\Image $image * @return boolean */ public function execute($image) { $width = $this->argument(0)->type('integer')->required()->value(); $height = $this->argument(1)->type('integer')->required()->value(); $anchor = $this->argument(2)->value('center'); $relative = $this->argument(3)->type('boolean')->value(); $bgcolor = $this->argument(4)->value(); $original_width = $image->getWidth(); $original_height = $image->getHeight(); // check of only width or height is set $width = is_null($width) ? $original_width : intval($width); $height = is_null($height) ? $original_height : intval($height); // check on relative width/height if ($relative) { $width = $original_width + $width; $height = $original_height + $height; } // check for negative width/height $width = $width <= 0 ? $width + $original_width : $width; $height = $height <= 0 ? $height + $original_height : $height; // create new canvas $canvas = $image->getDriver()->newImage($width, $height, $bgcolor); // set copy position $canvas_size = $canvas->getSize()->align($anchor); $image_size = $image->getSize()->align($anchor); $canvas_pos = $image_size->relativePosition($canvas_size); $image_pos = $canvas_size->relativePosition($image_size); if ($width <= $original_width) { $dst_x = 0; $src_x = $canvas_pos->x; $src_w = $canvas_size->width; } else { $dst_x = $image_pos->x; $src_x = 0; $src_w = $original_width; } if ($height <= $original_height) { $dst_y = 0; $src_y = $canvas_pos->y; $src_h = $canvas_size->height; } else { $dst_y = $image_pos->y; $src_y = 0; $src_h = $original_height; } // make image area transparent to keep transparency // even if background-color is set $transparent = imagecolorallocatealpha($canvas->getCore(), 255, 255, 255, 127); imagealphablending($canvas->getCore(), false); // do not blend / just overwrite imagefilledrectangle($canvas->getCore(), $dst_x, $dst_y, $dst_x + $src_w - 1, $dst_y + $src_h - 1, $transparent); // copy image into new canvas imagecopy($canvas->getCore(), $image->getCore(), $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h); // set new core to canvas $image->setCore($canvas->getCore()); return true; }
/** * Applies a pixelation effect to a given image * * @param Intervention\Image\Image $image * @return boolean */ public function execute($image) { $size = $this->argument(0)->type('integer')->value(10); $width = $image->getWidth(); $height = $image->getHeight(); $image->getCore()->scaleImage(max(1, $width / $size), max(1, $height / $size)); $image->getCore()->scaleImage($width, $height); return true; }
/** * Fills image with color or pattern * * @param Intervention\Image\Image $image * @return boolean */ public function execute($image) { $filling = $this->argument(0)->value(); $x = $this->argument(1)->type('integer')->value(); $y = $this->argument(2)->type('integer')->value(); $width = $image->getWidth(); $height = $image->getHeight(); $resource = $image->getCore(); try { // set image tile filling $source = new Decoder(); $tile = $source->init($filling); imagesettile($image->getCore(), $tile->getCore()); $filling = IMG_COLOR_TILED; } catch (\Intervention\Image\Exception\NotReadableException $e) { // set solid color filling $color = new Color($filling); $filling = $color->getInt(); } imagealphablending($resource, true); if (is_int($x) && is_int($y)) { // resource should be visible through transparency $base = $image->getDriver()->newImage($width, $height)->getCore(); imagecopy($base, $resource, 0, 0, 0, 0, $width, $height); // floodfill if exact position is defined imagefill($resource, $x, $y, $filling); // copy filled original over base imagecopy($base, $resource, 0, 0, 0, 0, $width, $height); // set base as new resource-core $image->setCore($base); imagedestroy($resource); } else { // fill whole image otherwise imagefilledrectangle($resource, 0, 0, $width - 1, $height - 1, $filling); } isset($tile) ? imagedestroy($tile->getCore()) : null; return true; }
/** * Resizes image boundaries * * @param Intervention\Image\Image $image * @return boolean */ public function execute($image) { $width = $this->argument(0)->type('integer')->required()->value(); $height = $this->argument(1)->type('integer')->required()->value(); $anchor = $this->argument(2)->value('center'); $relative = $this->argument(3)->type('boolean')->value(); $bgcolor = $this->argument(4)->value(); $original_width = $image->getWidth(); $original_height = $image->getHeight(); // check of only width or height is set $width = is_null($width) ? $original_width : intval($width); $height = is_null($height) ? $original_height : intval($height); // check on relative width/height if ($relative) { $width = $original_width + $width; $height = $original_height + $height; } // check for negative width/height $width = $width <= 0 ? $width + $original_width : $width; $height = $height <= 0 ? $height + $original_height : $height; // create new canvas $canvas = $image->getDriver()->newImage($width, $height, $bgcolor); // set copy position $canvas_size = $canvas->getSize()->align($anchor); $image_size = $image->getSize()->align($anchor); $canvas_pos = $image_size->relativePosition($canvas_size); $image_pos = $canvas_size->relativePosition($image_size); if ($width <= $original_width) { $dst_x = 0; $src_x = $canvas_pos->x; $src_w = $canvas_size->width; } else { $dst_x = $image_pos->x; $src_x = 0; $src_w = $original_width; } if ($height <= $original_height) { $dst_y = 0; $src_y = $canvas_pos->y; $src_h = $canvas_size->height; } else { $dst_y = $image_pos->y; $src_y = 0; $src_h = $original_height; } // make image area transparent to keep transparency // even if background-color is set $rect = new \ImagickDraw(); $fill = $canvas->pickColor(0, 0, 'hex'); $fill = $fill == '#ff0000' ? '#00ff00' : '#ff0000'; $rect->setFillColor($fill); $rect->rectangle($dst_x, $dst_y, $dst_x + $src_w - 1, $dst_y + $src_h - 1); $canvas->getCore()->drawImage($rect); $canvas->getCore()->transparentPaintImage($fill, 0, 0, false); // copy image into new canvas $image->getCore()->cropImage($src_w, $src_h, $src_x, $src_y); $canvas->getCore()->compositeImage($image->getCore(), \Imagick::COMPOSITE_DEFAULT, $dst_x, $dst_y); $canvas->getCore()->setImagePage(0, 0, 0, 0); // set new core to canvas $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 = 0; $checkColor->g = 0; $checkColor->b = 0; } 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 = 0; $checkColor->g = 0; $checkColor->b = 0; } 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 = 0; $checkColor->g = 0; $checkColor->b = 0; } 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 = 0; $checkColor->g = 0; $checkColor->b = 0; } 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); }
/** * 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; }