Cheetah
ResizeCanvasCommand.php
Go to the documentation of this file.
1 <?php
2 
4 
6 {
13  public function execute($image)
14  {
15  $width = $this->argument(0)->type('digit')->required()->value();
16  $height = $this->argument(1)->type('digit')->required()->value();
17  $anchor = $this->argument(2)->value('center');
18  $relative = $this->argument(3)->type('boolean')->value();
19  $bgcolor = $this->argument(4)->value();
20 
21  $original_width = $image->getWidth();
22  $original_height = $image->getHeight();
23 
24  // check of only width or height is set
25  $width = is_null($width) ? $original_width : intval($width);
26  $height = is_null($height) ? $original_height : intval($height);
27 
28  // check on relative width/height
29  if ($relative) {
30  $width = $original_width + $width;
31  $height = $original_height + $height;
32  }
33 
34  // check for negative width/height
35  $width = ($width <= 0) ? $width + $original_width : $width;
36  $height = ($height <= 0) ? $height + $original_height : $height;
37 
38  // create new canvas
39  $canvas = $image->getDriver()->newImage($width, $height, $bgcolor);
40 
41  // set copy position
42  $canvas_size = $canvas->getSize()->align($anchor);
43  $image_size = $image->getSize()->align($anchor);
44  $canvas_pos = $image_size->relativePosition($canvas_size);
45  $image_pos = $canvas_size->relativePosition($image_size);
46 
47  if ($width <= $original_width) {
48  $dst_x = 0;
49  $src_x = $canvas_pos->x;
50  $src_w = $canvas_size->width;
51  } else {
52  $dst_x = $image_pos->x;
53  $src_x = 0;
54  $src_w = $original_width;
55  }
56 
57  if ($height <= $original_height) {
58  $dst_y = 0;
59  $src_y = $canvas_pos->y;
60  $src_h = $canvas_size->height;
61  } else {
62  $dst_y = $image_pos->y;
63  $src_y = 0;
64  $src_h = $original_height;
65  }
66 
67  // make image area transparent to keep transparency
68  // even if background-color is set
69  $rect = new \ImagickDraw;
70  $fill = $canvas->pickColor(0, 0, 'hex');
71  $fill = $fill == '#ff0000' ? '#00ff00' : '#ff0000';
72  $rect->setFillColor($fill);
73  $rect->rectangle($dst_x, $dst_y, $dst_x + $src_w - 1, $dst_y + $src_h - 1);
74  $canvas->getCore()->drawImage($rect);
75  $canvas->getCore()->transparentPaintImage($fill, 0, 0, false);
76 
77  $canvas->getCore()->setImageColorspace($image->getCore()->getImageColorspace());
78 
79  // copy image into new canvas
80  $image->getCore()->cropImage($src_w, $src_h, $src_x, $src_y);
81  $canvas->getCore()->compositeImage($image->getCore(), \Imagick::COMPOSITE_DEFAULT, $dst_x, $dst_y);
82  $canvas->getCore()->setImagePage(0,0,0,0);
83 
84  // set new core to canvas
85  $image->setCore($canvas->getCore());
86 
87  return true;
88  }
89 }
php
Intervention\Image\Imagick\Commands\ResizeCanvasCommand
Definition: ResizeCanvasCommand.php:6
Intervention\Image\Imagick\Commands\ResizeCanvasCommand\execute
execute($image)
Definition: ResizeCanvasCommand.php:13
Intervention\Image\Imagick\Commands
Definition: BackupCommand.php:3
Intervention\Image\Commands\AbstractCommand\argument
argument($key)
Definition: AbstractCommand.php:45
Intervention\Image\Commands\AbstractCommand
Definition: AbstractCommand.php:6