Beispiel #1
0
 /**
  * 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;
 }
Beispiel #2
0
 /**
  * Applies a pixelation effect to a given image
  *
  * @param  \Intervention\Image\Image $image
  * @return boolean
  */
 public function execute($image)
 {
     $size = $this->argument(0)->type('digit')->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;
 }
 /**
  * Draw ellipse instance on given image
  *
  * @param  Image   $image
  * @param  integer $x
  * @param  integer $y
  * @return boolean
  */
 public function applyToImage(Image $image, $x = 0, $y = 0)
 {
     $background = new Color($this->background);
     imagefilledellipse($image->getCore(), $x, $y, $this->width, $this->height, $background->getInt());
     if ($this->hasBorder()) {
         $border_color = new Color($this->border_color);
         imagesetthickness($image->getCore(), $this->border_width);
         imageellipse($image->getCore(), $x, $y, $this->width, $this->height, $border_color->getInt());
     }
     return true;
 }
Beispiel #4
0
 /**
  * Draw polygon on given image
  *
  * @param  Image   $image
  * @param  integer $x
  * @param  integer $y
  * @return boolean
  */
 public function applyToImage(Image $image, $x = 0, $y = 0)
 {
     $background = new Color($this->background);
     imagefilledpolygon($image->getCore(), $this->points, intval(count($this->points) / 2), $background->getInt());
     if ($this->hasBorder()) {
         $border_color = new Color($this->border_color);
         imagesetthickness($image->getCore(), $this->border_width);
         imagepolygon($image->getCore(), $this->points, intval(count($this->points) / 2), $border_color->getInt());
     }
     return true;
 }
Beispiel #5
0
 /**
  * Mirrors an image
  *
  * @param  \Intervention\Image\Image $image
  * @return boolean
  */
 public function execute($image)
 {
     $mode = $this->argument(0)->value('h');
     if (in_array(strtolower($mode), array(2, 'v', 'vert', 'vertical'))) {
         // flip vertical
         return $image->getCore()->flipImage();
     } else {
         // flip horizontal
         return $image->getCore()->flopImage();
     }
 }
 /**
  * Draw rectangle to given image at certain position
  *
  * @param  Image   $image
  * @param  integer $x
  * @param  integer $y
  * @return boolean
  */
 public function applyToImage(Image $image, $x = 0, $y = 0)
 {
     $background = new Color($this->background);
     imagefilledrectangle($image->getCore(), $this->x1, $this->y1, $this->x2, $this->y2, $background->getInt());
     if ($this->hasBorder()) {
         $border_color = new Color($this->border_color);
         imagesetthickness($image->getCore(), $this->border_width);
         imagerectangle($image->getCore(), $this->x1, $this->y1, $this->x2, $this->y2, $border_color->getInt());
     }
     return true;
 }
 /**
  * Removes all frames of an animation except one
  *
  * @param  \Intervention\Image\Image $image
  * @return boolean
  */
 public function execute($image)
 {
     $keepIndex = $this->argument(0)->type('int')->value(0);
     foreach ($image as $key => $frame) {
         if ($keepIndex == $key) {
             break;
         }
     }
     $frame = $image->getDriver()->init($image->getCore()->getImageBlob());
     // remove old core
     $image->getCore()->clear();
     // set new core
     $image->setContainer($frame->getContainer());
     return true;
 }
 /**
  * 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;
 }
Beispiel #9
0
 /**
  * Reads size of given image instance in pixels
  *
  * @param  \Intervention\Image\Image $image
  * @return boolean
  */
 public function execute($image)
 {
     /** @var \Imagick $core */
     $core = $image->getCore();
     $this->setOutput(new Size($core->getImageWidth(), $core->getImageHeight()));
     return true;
 }
Beispiel #10
0
 /**
  * Read color information from a certain position
  *
  * @param  \Intervention\Image\Image $image
  * @return boolean
  */
 public function execute($image)
 {
     $x = $this->argument(0)->type('digit')->required()->value();
     $y = $this->argument(1)->type('digit')->required()->value();
     $format = $this->argument(2)->type('string')->value('array');
     // pick color
     $color = imagecolorat($image->getCore(), $x, $y);
     if (!imageistruecolor($image->getCore())) {
         $color = imagecolorsforindex($image->getCore(), $color);
         $color['alpha'] = round(1 - $color['alpha'] / 127, 2);
     }
     $color = new Color($color);
     // format to output
     $this->setOutput($color->format($format));
     return true;
 }
Beispiel #11
0
 /**
  * Changes balance of different RGB color channels
  *
  * @param  \Intervention\Image\Image $image
  * @return boolean
  */
 public function execute($image)
 {
     $red = $this->argument(0)->between(-100, 100)->required()->value();
     $green = $this->argument(1)->between(-100, 100)->required()->value();
     $blue = $this->argument(2)->between(-100, 100)->required()->value();
     // normalize colorize levels
     $red = $this->normalizeLevel($red);
     $green = $this->normalizeLevel($green);
     $blue = $this->normalizeLevel($blue);
     $qrange = $image->getCore()->getQuantumRange();
     // apply
     $image->getCore()->levelImage(0, $red, $qrange['quantumRangeLong'], \Imagick::CHANNEL_RED);
     $image->getCore()->levelImage(0, $green, $qrange['quantumRangeLong'], \Imagick::CHANNEL_GREEN);
     $image->getCore()->levelImage(0, $blue, $qrange['quantumRangeLong'], \Imagick::CHANNEL_BLUE);
     return true;
 }
Beispiel #12
0
 /**
  * Insert another image into given image
  *
  * @param  \Intervention\Image\Image $image
  * @return boolean
  */
 public function execute($image)
 {
     $source = $this->argument(0)->required()->value();
     $position = $this->argument(1)->type('string')->value();
     $x = $this->argument(2)->type('digit')->value(0);
     $y = $this->argument(3)->type('digit')->value(0);
     // build watermark
     $watermark = $image->getDriver()->init($source);
     // define insertion point
     $image_size = $image->getSize()->align($position, $x, $y);
     $watermark_size = $watermark->getSize()->align($position);
     $target = $image_size->relativePosition($watermark_size);
     // insert image at position
     imagealphablending($image->getCore(), true);
     return imagecopy($image->getCore(), $watermark->getCore(), $target->x, $target->y, 0, 0, $watermark_size->width, $watermark_size->height);
 }
 /**
  * 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;
 }
Beispiel #14
0
 /**
  * Draw ellipse instance on given image
  *
  * @param  Image   $image
  * @param  integer $x
  * @param  integer $y
  * @return boolean
  */
 public function applyToImage(Image $image, $x = 0, $y = 0)
 {
     // parse background color
     $background = new Color($this->background);
     if ($this->hasBorder()) {
         // slightly smaller ellipse to keep 1px bordered edges clean
         imagefilledellipse($image->getCore(), $x, $y, $this->width - 1, $this->height - 1, $background->getInt());
         $border_color = new Color($this->border_color);
         imagesetthickness($image->getCore(), $this->border_width);
         // gd's imageellipse doesn't respect imagesetthickness so i use imagearc with 359.9 degrees here
         imagearc($image->getCore(), $x, $y, $this->width, $this->height, 0, 359.99, $border_color->getInt());
     } else {
         imagefilledellipse($image->getCore(), $x, $y, $this->width, $this->height, $background->getInt());
     }
     return true;
 }
Beispiel #15
0
 /**
  * Saves a backups of current state of image core
  *
  * @param  \Intervention\Image\Image $image
  * @return boolean
  */
 public function execute($image)
 {
     $backupName = $this->argument(0)->value();
     // clone current image resource
     $image->setBackup(clone $image->getCore(), $backupName);
     return true;
 }
Beispiel #16
0
 /**
  * Crops and resized an image at the same time
  *
  * @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')->value($width);
     $constraints = $this->argument(2)->type('closure')->value();
     // calculate size
     $cropped = $image->getSize()->fit(new Size($width, $height));
     $resized = clone $cropped;
     $resized = $resized->resize($width, $height, $constraints);
     // crop image
     $image->getCore()->cropImage($cropped->width, $cropped->height, $cropped->pivot->x, $cropped->pivot->y);
     // resize image
     $image->getCore()->resizeImage($resized->getWidth(), $resized->getHeight(), \Imagick::FILTER_BOX, 1);
     $image->getCore()->setImagePage(0, 0, 0, 0);
     return true;
 }
Beispiel #17
0
 /**
  * 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);
     // get imagick
     $imagick = $image->getCore();
     // build mask image from source
     $mask = $image->getDriver()->init($mask_source);
     // resize mask to size of current image (if necessary)
     $image_size = $image->getSize();
     if ($mask->getSize() != $image_size) {
         $mask->resize($image_size->width, $image_size->height);
     }
     $imagick->setImageMatte(true);
     if ($mask_w_alpha) {
         // just mask with alpha map
         $imagick->compositeImage($mask->getCore(), \Imagick::COMPOSITE_DSTIN, 0, 0);
     } else {
         // get alpha channel of original as greyscale image
         $original_alpha = clone $imagick;
         $original_alpha->separateImageChannel(\Imagick::CHANNEL_ALPHA);
         // use red channel from mask ask alpha
         $mask_alpha = clone $mask->getCore();
         $mask_alpha->compositeImage($mask->getCore(), \Imagick::COMPOSITE_DEFAULT, 0, 0);
         // $mask_alpha->setImageAlphaChannel(\Imagick::ALPHACHANNEL_DEACTIVATE);
         $mask_alpha->separateImageChannel(\Imagick::CHANNEL_ALL);
         // combine both alphas from original and mask
         $original_alpha->compositeImage($mask_alpha, \Imagick::COMPOSITE_COPYOPACITY, 0, 0);
         // mask the image with the alpha combination
         $imagick->compositeImage($original_alpha, \Imagick::COMPOSITE_DSTIN, 0, 0);
     }
     return true;
 }
Beispiel #18
0
 /**
  * Draws font to given image at given position
  *
  * @param  Image   $image
  * @param  integer $posx
  * @param  integer $posy
  * @return void
  */
 public function applyToImage(Image $image, $posx = 0, $posy = 0)
 {
     // build draw object
     $draw = new \ImagickDraw();
     $draw->setStrokeAntialias(true);
     $draw->setTextAntialias(true);
     // set font file
     if ($this->hasApplicableFontFile()) {
         $draw->setFont($this->file);
     } else {
         throw new \Intervention\Image\Exception\RuntimeException("Font file must be provided to apply text to image.");
     }
     // parse text color
     $color = new Color($this->color);
     $draw->setFontSize($this->size);
     $draw->setFillColor($color->getPixel());
     // align horizontal
     switch (strtolower($this->align)) {
         case 'center':
             $align = \Imagick::ALIGN_CENTER;
             break;
         case 'right':
             $align = \Imagick::ALIGN_RIGHT;
             break;
         default:
             $align = \Imagick::ALIGN_LEFT;
             break;
     }
     $draw->setTextAlignment($align);
     // align vertical
     if (strtolower($this->valign) != 'bottom') {
         // calculate box size
         $dimensions = $image->getCore()->queryFontMetrics($draw, $this->text);
         // corrections on y-position
         switch (strtolower($this->valign)) {
             case 'center':
             case 'middle':
                 $posy = $posy + $dimensions['textHeight'] * 0.65 / 2;
                 break;
             case 'top':
                 $posy = $posy + $dimensions['textHeight'] * 0.65;
                 break;
         }
     }
     // apply to image
     $image->getCore()->annotateImage($draw, $posx, $posy, $this->angle * -1, $this->text);
 }
Beispiel #19
0
 /**
  * Draws one pixel to a given image
  *
  * @param  \Intervention\Image\Image $image
  * @return boolean
  */
 public function execute($image)
 {
     $color = $this->argument(0)->required()->value();
     $color = new Color($color);
     $x = $this->argument(1)->type('digit')->required()->value();
     $y = $this->argument(2)->type('digit')->required()->value();
     return imagesetpixel($image->getCore(), $x, $y, $color->getInt());
 }
 /**
  * Removes all frames of an animation except one
  *
  * @param  \Intervention\Image\Image $image
  * @return boolean
  */
 public function execute($image)
 {
     $keepIndex = $this->argument(0)->type('int')->value(0);
     $container = new Container();
     $container->add($image->getCore($keepIndex));
     $image->setContainer($container);
     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;
 }
Beispiel #22
0
 /**
  * Applies blur effect on image
  *
  * @param  \Intervention\Image\Image $image
  * @return boolean
  */
 public function execute($image)
 {
     $amount = $this->argument(0)->between(0, 100)->value(1);
     for ($i = 0; $i < intval($amount); $i++) {
         imagefilter($image->getCore(), IMG_FILTER_GAUSSIAN_BLUR);
     }
     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();
     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;
 }
Beispiel #24
0
 /**
  * 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->getCore()->rotateImage($color->getPixel(), $angle * -1);
     return true;
 }
Beispiel #25
0
 /**
  * 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;
 }
Beispiel #26
0
 /**
  * Destroys current image core and frees up memory
  *
  * @param  \Intervention\Image\Image $image
  * @return boolean
  */
 public function execute($image)
 {
     // destroy image core
     $image->getCore()->clear();
     // destroy backups
     foreach ($image->getBackups() as $backup) {
         $backup->clear();
     }
     return true;
 }
Beispiel #27
0
 /**
  * Draw current instance of line to given endpoint on given image
  *
  * @param  Image   $image
  * @param  integer $x
  * @param  integer $y
  * @return boolean
  */
 public function applyToImage(Image $image, $x = 0, $y = 0)
 {
     $line = new \ImagickDraw();
     $color = new Color($this->color);
     $line->setStrokeColor($color->getPixel());
     $line->setStrokeWidth($this->width);
     $line->line($this->x, $this->y, $x, $y);
     $image->getCore()->drawImage($line);
     return true;
 }
Beispiel #28
0
 /**
  * Destroys current image core and frees up memory
  *
  * @param  \Intervention\Image\Image $image
  * @return boolean
  */
 public function execute($image)
 {
     // destroy image core
     imagedestroy($image->getCore());
     // destroy backups
     foreach ($image->getBackups() as $backup) {
         imagedestroy($backup);
     }
     return true;
 }
Beispiel #29
0
 /**
  * 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().");
 }
Beispiel #30
0
 /**
  * Resizes image dimensions
  *
  * @param  \Intervention\Image\Image $image
  * @return boolean
  */
 public function execute($image)
 {
     $width = $this->argument(0)->value();
     $height = $this->argument(1)->value();
     $constraints = $this->argument(2)->type('closure')->value();
     // resize box
     $resized = $image->getSize()->resize($width, $height, $constraints);
     // modify image
     $image->getCore()->scaleImage($resized->getWidth(), $resized->getHeight());
     return true;
 }