31 protected function crop($width, $height, $x = 0, $y = 0)
33 if (!$this->
bypassTest($width, $height, $x, $y)) {
34 $newImage = $this->createCanvas($width, $height);
39 ($x > 0) ? 0 : abs($x),
40 ($y > 0) ? 0 : abs($y),
43 $this->size[0] - abs($x),
44 $this->size[1] - abs($y)
47 $this->image = $newImage;
48 $this->size = array($width, $height);
60 protected function resize($width, $height)
64 if (!$this->
bypassTest($newSize[0], $newSize[1])) {
65 $newImage = $this->createCanvas($newSize[0], $newSize[1]);
66 imagecopyresampled($newImage, $this->image, 0, 0, 0, 0, $newSize[0], $newSize[1], $this->size[0], $this->size[1]);
68 $this->image = $newImage;
69 $this->size = $newSize;
81 protected function thumb($width, $height)
83 list($width, $height) = $this->
dimensions($width, $height);
88 if ($newSize[1] > $height) {
90 $xOffset = round(($width - $newSize[0]) / 2);
94 $yOffset = round(($height - $newSize[1]) / 2);
97 $newImage = $this->createCanvas($width, $height);
99 imagecopyresampled($newImage, $this->image, $xOffset, $yOffset, 0, 0, $newSize[0], $newSize[1], $this->size[0], $this->size[1]);
101 $this->image = $newImage;
102 $this->size = array($width, $height);
116 protected function thumbfill($width, $height, $centerX = 50, $centerY = 50)
118 list($width, $height) = $this->
dimensions($width, $height);
120 if (!$this->
bypassTest($width, $height, $centerX - 50, $centerY - 50)) {
125 if ($newSize[1] < $height) {
127 $xOffset = round(($width - $newSize[0]) * $centerX);
131 $yOffset = round(($height - $newSize[1]) * $centerY);
134 $newImage = $this->createCanvas($width, $height);
136 imagecopyresampled($newImage, $this->image, $xOffset, $yOffset, 0, 0, $newSize[0], $newSize[1], $this->size[0], $this->size[1]);
138 $this->image = $newImage;
139 $this->size = array($width, $height);
152 if ($this->inputFormat ==
'gif' && function_exists(
'imagecreatefromgif')) {
154 $this->image = imagecreatefromgif($this->input);
155 } elseif ($this->inputFormat ==
'jpg') {
157 $this->image = imagecreatefromjpeg($this->input);
158 } elseif ($this->inputFormat ==
'png') {
160 $this->image = imagecreatefrompng($this->input);
161 } elseif ($this->inputFormat ==
'webp' && function_exists(
'imagecreatefromwebp')) {
163 $this->image = imagecreatefromwebp($this->input);
165 throw new \Depage\Graphics\Exceptions\Exception(
'Unknown image format.');
177 $bg = $this->createBackground($this->size[0], $this->size[1]);
178 imagecopy($bg, $this->image, 0, 0, 0, 0, $this->size[0], $this->size[1]);
182 if ($this->outputFormat ==
'gif' && function_exists(
'imagegif')) {
183 $result = imagegif($this->image, $this->output);
184 } elseif ($this->outputFormat ==
'jpg') {
185 $result = imagejpeg($this->image, $this->output, $this->
getQuality());
186 } elseif ($this->outputFormat ==
'png') {
188 $result = imagepng($this->image, $this->output,
$quality, PNG_ALL_FILTERS);
189 } elseif ($this->outputFormat ==
'webp' && function_exists(
'imagewebp')) {
190 $result = imagewebp($this->image, $this->output, $this->
getQuality());
193 throw new \Depage\Graphics\Exceptions\Exception(
'Could not save output image.');
204 return getimagesize($this->input);
223 if ($this->otherRender && file_exists($this->output)) {
226 && $this->inputFormat == $this->outputFormat
232 if ($this->optimize) {
237 parent::renderFinished();
247 private function createCanvas($width, $height)
249 $canvas = imagecreatetruecolor($width, $height);
250 $bg = imagecolorallocatealpha($canvas, 255, 255, 255, 127);
251 imagefill($canvas, 0, 0, $bg);
264 private function createBackground($width, $height)
266 $newImage = imagecreatetruecolor($width, $height);
268 if ($this->background[0] ==
'#') {
272 $color = substr($this->background, 1);
274 if (strlen($color) == 6) {
275 list($r, $g, $b) = array(
280 } elseif (strlen($color) == 3) {
281 list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
284 $r = hexdec($r); $g = hexdec($g); $b = hexdec($b);
286 imagefill($newImage, 0, 0, imagecolorallocate($newImage, $r, $g, $b));
287 } elseif ($this->background ==
'checkerboard') {
289 $transColor = array();
290 $transColor[0] = imagecolorallocate ($newImage, 153, 153, 153);
291 $transColor[1] = imagecolorallocate ($newImage, 102, 102, 102);
292 for ($i = 0; $i * $transLen < $width; $i++) {
293 for ($j = 0; $j * $transLen < $height; $j++) {
294 imagefilledrectangle(
298 ($i + 1) * $transLen,
299 ($j + 1) * $transLen,
300 $transColor[$j % 2 == 0 ? $i % 2 : ($i % 2 == 0 ? 1 : 0)]
304 } elseif ($this->background ==
'transparent') {
305 imagefill($newImage, 0, 0, imagecolorallocatealpha($newImage, 255, 255, 255, 127));
306 if ($this->outputFormat ==
'gif') imagecolortransparent($newImage, imagecolorallocatealpha($newImage, 255, 255, 255, 127));
307 imagesavealpha($newImage,
true);
$quality
Image quality string.
processQueue()
Process action queue.
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.
load()
Loads image from file.
getImageSize()
Determine size of input image.
render($input, $output=null)
Main method for image handling.
resize($width, $height)
Resize action.
crop($width, $height, $x=0, $y=0)
Crop action.
thumb($width, $height)
Thumb action.
save()
Saves image to file.