/** * Reduces colors of a given image * * @param \Intervention\Image\Image $image * @return boolean */ public function execute($image) { $count = $this->argument(0)->value(); $matte = $this->argument(1)->value(); // get current image size $size = $image->getSize(); // create empty canvas $resource = imagecreatetruecolor($size->width, $size->height); // define matte if (is_null($matte)) { $matte = imagecolorallocatealpha($resource, 255, 255, 255, 127); } else { $matte = $image->getDriver()->parseColor($matte)->getInt(); } // fill with matte and copy original image imagefill($resource, 0, 0, $matte); // set transparency imagecolortransparent($resource, $matte); // copy original image imagecopy($resource, $image->getCore(), 0, 0, 0, 0, $size->width, $size->height); if (is_numeric($count) && $count <= 256) { // decrease colors imagetruecolortopalette($resource, true, $count); } // set new resource $image->setCore($resource); return true; }
/** * Applies filter effects to the given image * * @param Image\Image $image The image to filter. * * @return Image\Image The filtered image. * * @throws FilterException if the image filter algorithm fails. */ public function applyFilter(Image\Image $image) { if ($this->level <= 0) { $gd = $image->getCore(); $width = imagesx($gd); $height = imagesy($gd); for ($x = 0; $x < $width; ++$x) { for ($y = 0; $y < $height; ++$y) { $rgba = imagecolorsforindex($gd, imagecolorat($gd, $x, $y)); $r = $rgba['red']; $g = $rgba['green']; $b = $rgba['blue']; $a = $rgba['alpha']; $level = $this->level * -1; $max = max($r, $g, $b); $avg = ($r + $g + $b) / 3; $amt = abs($max - $avg) * 2 / 255 * $level / 100; if ($r !== $max) { $r += ($max - $r) * $amt; } if ($g !== $max) { $g += ($max - $g) * $amt; } if ($b !== $max) { $b += ($max - $b) * $amt; } imagesetpixel($gd, $x, $y, imagecolorallocatealpha($gd, $r, $g, $b, $a)); } } $image->setCore($gd); } else { $image->filter(new SaturateFilter($this->level)); } return $image; }
/** * Reduces colors of a given image * * @param \Intervention\Image\Image $image * @return boolean */ public function execute($image) { $count = $this->argument(0)->value(); $matte = $this->argument(1)->value(); // get current image size $size = $image->getSize(); // build 2 color alpha mask from original alpha $alpha = clone $image->getCore(); $alpha->separateImageChannel(\Imagick::CHANNEL_ALPHA); $alpha->transparentPaintImage('#ffffff', 0, 0, false); $alpha->separateImageChannel(\Imagick::CHANNEL_ALPHA); $alpha->negateImage(false); if ($matte) { // get matte color $mattecolor = $image->getDriver()->parseColor($matte)->getPixel(); // create matte image $canvas = new \Imagick(); $canvas->newImage($size->width, $size->height, $mattecolor, 'png'); // lower colors of original and copy to matte $image->getCore()->quantizeImage($count, \Imagick::COLORSPACE_RGB, 0, false, false); $canvas->compositeImage($image->getCore(), \Imagick::COMPOSITE_DEFAULT, 0, 0); // copy new alpha to canvas $canvas->compositeImage($alpha, \Imagick::COMPOSITE_COPYOPACITY, 0, 0); // replace core $image->setCore($canvas); } else { $image->getCore()->quantizeImage($count, \Imagick::COLORSPACE_RGB, 0, false, false); $image->getCore()->compositeImage($alpha, \Imagick::COMPOSITE_COPYOPACITY, 0, 0); } return true; }
/** * Applies filter effects to the given image * * @param Image\Image $image The image to filter. * * @return Image\Image The filtered image. * * @throws FilterException if the image filter algorithm fails. */ public function applyFilter(Image\Image $image) { $gd = $image->getCore(); imagefilter($gd, IMG_FILTER_GRAYSCALE); imagefilter($gd, IMG_FILTER_COLORIZE, 60, 30, 0); $image->setCore($gd); return $image; }
/** * Applies filter effects to the given image * * @param Image\Image $image The image to filter. * * @return Image\Image The filtered image. * * @throws FilterException if the image filter algorithm fails. */ public function applyFilter(Image\Image $image) { $gd = $image->getCore(); if (!imagefilter($gd, IMG_FILTER_EMBOSS)) { throw new FilterException('Er is een fout opgetreden bij het bewerken van de afbeelding. ' + 'Probeer het later alstublieft opnieuw.', 1); } $image->setCore($gd); return $image; }
/** * Resizes image boundaries * * @param \Intervention\Image\Image $image * @return boolean */ public function execute($image) { $width = $this->argument(0)->type('digit')->required()->value(); $height = $this->argument(1)->type('digit')->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; }
/** * Rotates image counter clockwise * * @param \Intervention\Image\Image $image * @return boolean */ public function execute($image) { $angle = $this->argument(0)->type('numeric')->required()->value(); $color = $this->argument(1)->value(); $color = new Color($color); // rotate image $image->setCore(imagerotate($image->getCore(), $angle, $color->getInt())); return true; }
/** * Resets given image to its backup state * * @param \Intervention\Image\Image $image * @return boolean */ public function execute($image) { if (is_resource($backup = $image->getBackup())) { // destroy old resource imagedestroy($image->getCore()); // reset to new resource $image->setCore($backup); return true; } throw new \Intervention\Image\Exception\RuntimeException("Backup not available. Call backup() before reset()."); }
/** * Resets given image to its backup state * * @param \Intervention\Image\Image $image * @return boolean */ public function execute($image) { $backup = $image->getBackup(); if ($backup instanceof \Imagick) { // destroy old core $image->getCore()->clear(); // reset to new resource $image->setCore($backup); return true; } throw new \Intervention\Image\Exception\RuntimeException("Backup not available. Call backup() before reset()."); }
/** * 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; }
/** * Resets given image to its backup state * * @param \Intervention\Image\Image $image * @return boolean */ public function execute($image) { $backupName = $this->argument(0)->value(); if (is_resource($backup = $image->getBackup($backupName))) { // destroy current resource imagedestroy($image->getCore()); // clone backup $backup = $image->getDriver()->cloneCore($backup); // reset to new resource $image->setCore($backup); return true; } throw new \Intervention\Image\Exception\RuntimeException("Backup not available. Call backup() before reset()."); }
/** * Resets given image to its backup state * * @param \Intervention\Image\Image $image * @return boolean */ public function execute($image) { $backupName = $this->argument(0)->value(); $backup = $image->getBackup($backupName); if ($backup instanceof \Imagick) { // destroy current core $image->getCore()->clear(); // clone backup $backup = clone $backup; // reset to new resource $image->setCore($backup); return true; } throw new \Intervention\Image\Exception\RuntimeException("Backup not available. Call backup({$backupName}) before reset()."); }
/** * 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('digit')->value(); $y = $this->argument(2)->type('digit')->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; }
/** * Applies filter effects to the given image * * @param Image\Image $image The image to filter. * * @return Image\Image The filtered image. * * @throws FilterException if the image filter algorithm fails. */ public function applyFilter(Image\Image $image) { $gd = $image->getCore(); $width = imagesx($gd); $height = imagesy($gd); for ($x = 0; $x < $width; ++$x) { for ($y = 0; $y < $height; ++$y) { $rgba = imagecolorsforindex($gd, imagecolorat($gd, $x, $y)); $r = $rgba['red']; $g = $rgba['green']; $b = $rgba['blue']; $a = $rgba['alpha']; list($h, $s, $l) = ColorConverter::rgbToHsl($r, $g, $b); $h += $this->hue / 100; if ($h > 1.0) { $h -= 1.0; } list($r, $g, $b) = ColorConverter::hslToRgb($h, $s, $l); imagesetpixel($gd, $x, $y, imagecolorallocatealpha($gd, $r, $g, $b, $s)); } } $image->setCore($gd); return $image; }
/** * 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('digit')->value(); $y = $this->argument(2)->type('digit')->value(); $imagick = $image->getCore(); try { // set image filling $source = new Decoder(); $filling = $source->init($filling); } catch (\Intervention\Image\Exception\NotReadableException $e) { // set solid color filling $filling = new Color($filling); } // flood fill if coordinates are set if (is_int($x) && is_int($y)) { // flood fill with texture if ($filling instanceof Image) { // create tile $tile = clone $image->getCore(); // mask away color at position $tile->transparentPaintImage($tile->getImagePixelColor($x, $y), 0, 0, false); // create canvas $canvas = clone $image->getCore(); // fill canvas with texture $canvas = $canvas->textureImage($filling->getCore()); // merge canvas and tile $canvas->compositeImage($tile, \Imagick::COMPOSITE_DEFAULT, 0, 0); // replace image core $image->setCore($canvas); // flood fill with color } elseif ($filling instanceof Color) { // create canvas with filling $canvas = new \Imagick(); $canvas->newImage($image->getWidth(), $image->getHeight(), $filling->getPixel(), 'png'); // create tile to put on top $tile = clone $image->getCore(); // mask away color at pos. $tile->transparentPaintImage($tile->getImagePixelColor($x, $y), 0, 0, false); // save alpha channel of original image $alpha = clone $image->getCore(); // merge original with canvas and tile $image->getCore()->compositeImage($canvas, \Imagick::COMPOSITE_DEFAULT, 0, 0); $image->getCore()->compositeImage($tile, \Imagick::COMPOSITE_DEFAULT, 0, 0); // restore alpha channel of original image $image->getCore()->compositeImage($alpha, \Imagick::COMPOSITE_COPYOPACITY, 0, 0); } } else { if ($filling instanceof Image) { // fill whole image with texture $image->setCore($image->getCore()->textureImage($filling->getCore())); } elseif ($filling instanceof Color) { // fill whole image with color $draw = new \ImagickDraw(); $draw->setFillColor($filling->getPixel()); $draw->rectangle(0, 0, $image->getWidth(), $image->getHeight()); $image->getCore()->drawImage($draw); } } return true; }
/** * Resizes image boundaries * * @param \Intervention\Image\Image $image * @return boolean */ public function execute($image) { $width = $this->argument(0)->type('digit')->required()->value(); $height = $this->argument(1)->type('digit')->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); $canvas->getCore()->setImageColorspace($image->getCore()->getImageColorspace()); // 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; }