37 protected function crop($width, $height, $x = 0, $y = 0)
39 if (!$this->
bypassTest($width, $height, $x, $y)) {
40 $newImage = $this->createCanvas($width, $height);
45 ($x > 0) ? 0 : abs($x),
46 ($y > 0) ? 0 : abs($y),
49 $this->size[0] - abs($x),
50 $this->size[1] - abs($y),
53 $this->image = $newImage;
54 $this->size = [$width, $height];
66 protected function resize($width, $height)
70 if (!$this->
bypassTest($newSize[0], $newSize[1])) {
71 $newImage = $this->createCanvas($newSize[0], $newSize[1]);
72 imagecopyresampled($newImage, $this->image, 0, 0, 0, 0, $newSize[0], $newSize[1], $this->size[0], $this->size[1]);
74 $this->image = $newImage;
75 $this->size = $newSize;
87 protected function thumb($width, $height)
89 list($width, $height) = $this->
dimensions($width, $height);
94 if ($newSize[1] > $height) {
96 $xOffset = round(($width - $newSize[0]) / 2);
100 $yOffset = round(($height - $newSize[1]) / 2);
103 $newImage = $this->createCanvas($width, $height);
105 imagecopyresampled($newImage, $this->image, $xOffset, $yOffset, 0, 0, $newSize[0], $newSize[1], $this->size[0], $this->size[1]);
107 $this->image = $newImage;
108 $this->size = [$width, $height];
122 protected function thumbfill($width, $height, $centerX = 50, $centerY = 50)
124 list($width, $height) = $this->
dimensions($width, $height);
126 if (!$this->
bypassTest($width, $height, $centerX - 50, $centerY - 50)) {
131 if ($newSize[1] < $height) {
133 $xOffset = round(($width - $newSize[0]) * $centerX);
137 $yOffset = round(($height - $newSize[1]) * $centerY);
140 $newImage = $this->createCanvas($width, $height);
142 imagecopyresampled($newImage, $this->image, $xOffset, $yOffset, 0, 0, $newSize[0], $newSize[1], $this->size[0], $this->size[1]);
144 $this->image = $newImage;
145 $this->size = [$width, $height];
158 if ($this->inputFormat ==
'gif' && function_exists(
'imagecreatefromgif')) {
160 $this->image = imagecreatefromgif($this->input);
161 } elseif ($this->inputFormat ==
'jpg') {
163 $this->image = imagecreatefromjpeg($this->input);
164 } elseif ($this->inputFormat ==
'png') {
166 $this->image = imagecreatefrompng($this->input);
167 } elseif ($this->inputFormat ==
'webp' && function_exists(
'imagecreatefromwebp')) {
169 $this->image = imagecreatefromwebp($this->input);
171 throw new \Depage\Graphics\Exceptions\Exception(
'Unknown image format.');
185 $bg = $this->createBackground($this->size[0], $this->size[1]);
186 imagecopy($bg, $this->image, 0, 0, 0, 0, $this->size[0], $this->size[1]);
191 if ($this->outputFormat ==
'gif' && function_exists(
'imagegif')) {
192 $result = imagegif($this->image, $this->output);
193 } elseif ($this->outputFormat ==
'jpg') {
194 $result = imagejpeg($this->image, $this->output, $this->
getQuality());
195 } elseif ($this->outputFormat ==
'png') {
197 $result = imagepng($this->image, $this->output,
$quality, PNG_ALL_FILTERS);
198 } elseif ($this->outputFormat ==
'webp' && function_exists(
'imagewebp')) {
199 $result = imagewebp($this->image, $this->output, $this->
getQuality());
202 throw new \Depage\Graphics\Exceptions\Exception(
'Could not save output image.');
213 $exif = @exif_read_data($this->input);
215 if (isset($exif[
'Orientation'])) {
216 switch ($exif[
'Orientation']) {
218 imageflip($this->image, IMG_FLIP_HORIZONTAL);
221 $this->image = imagerotate($this->image, 180, 0);
224 imageflip($this->image, IMG_FLIP_VERTICAL);
227 $this->image = imagerotate($this->image, -90, 0);
228 imageflip($this->image, IMG_FLIP_HORIZONTAL);
231 $this->image = imagerotate($this->image, -90, 0);
234 $this->image = imagerotate($this->image, -90, 0);
235 imageflip($this->image, IMG_FLIP_VERTICAL);
238 $this->image = imagerotate($this->image, 90, 0);
251 return getimagesize($this->input);
270 if ($this->otherRender && file_exists($this->output)) {
273 && $this->inputFormat == $this->outputFormat
279 if ($this->optimize) {
284 parent::renderFinished();
294 private function createCanvas($width, $height)
296 $canvas = imagecreatetruecolor($width, $height);
297 $bg = imagecolorallocatealpha($canvas, 255, 255, 255, 127);
298 imagefill($canvas, 0, 0, $bg);
311 private function createBackground($width, $height)
313 $newImage = imagecreatetruecolor($width, $height);
315 if ($this->background[0] ==
'#') {
319 $color = substr($this->background, 1);
321 if (strlen($color) == 6) {
323 $color[0] . $color[1],
324 $color[2] . $color[3],
325 $color[4] . $color[5],
327 } elseif (strlen($color) == 3) {
328 list($r, $g, $b) = [$color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2]];
335 imagefill($newImage, 0, 0, imagecolorallocate($newImage, $r, $g, $b));
336 } elseif ($this->background ==
'checkerboard') {
339 $transColor[0] = imagecolorallocate($newImage, 153, 153, 153);
340 $transColor[1] = imagecolorallocate($newImage, 102, 102, 102);
341 for ($i = 0; $i * $transLen < $width; $i++) {
342 for ($j = 0; $j * $transLen < $height; $j++) {
343 imagefilledrectangle(
347 ($i + 1) * $transLen,
348 ($j + 1) * $transLen,
349 $transColor[$j % 2 == 0 ? $i % 2 : ($i % 2 == 0 ? 1 : 0)],
353 } elseif ($this->background ==
'transparent') {
354 imagefill($newImage, 0, 0, imagecolorallocatealpha($newImage, 255, 255, 255, 127));
355 if ($this->outputFormat ==
'gif') {
356 imagecolortransparent($newImage, imagecolorallocatealpha($newImage, 255, 255, 255, 127));
358 imagesavealpha($newImage,
true);
bypassTest($width, $height, $x=0, $y=0)
Tests if action would change current image.
getQuality()
Returns quality-index for current image format.
dimensions($width, $height)
Scales image dimensions.
optimizeImage($filename)
Opimizes final image through one of the optimization programs.
bypass()
Runs bypass (copies file)
PHP GD extension interface.
thumbfill($width, $height, $centerX=50, $centerY=50)
Thumb-Fill action.
getImageSize()
Determine size of input image.
render($input, $output=null)
Main method for image handling.
$image
Image resource identifier.
crop($width, $height, $x=0, $y=0)
Crop action.