36 protected function crop($width, $height, $x = 0, $y = 0)
38 if (!$this->
bypassTest($width, $height, $x, $y)) {
39 $newImage = $this->createCanvas($width, $height);
44 ($x > 0) ? 0 : abs($x),
45 ($y > 0) ? 0 : abs($y),
48 $this->size[0] - abs($x),
49 $this->size[1] - abs($y)
52 $this->image = $newImage;
53 $this->size = array($width, $height);
65 protected function resize($width, $height)
69 if (!$this->
bypassTest($newSize[0], $newSize[1])) {
70 $newImage = $this->createCanvas($newSize[0], $newSize[1]);
71 imagecopyresampled($newImage, $this->image, 0, 0, 0, 0, $newSize[0], $newSize[1], $this->size[0], $this->size[1]);
73 $this->image = $newImage;
74 $this->size = $newSize;
86 protected function thumb($width, $height)
88 list($width, $height) = $this->
dimensions($width, $height);
93 if ($newSize[1] > $height) {
95 $xOffset = round(($width - $newSize[0]) / 2);
99 $yOffset = round(($height - $newSize[1]) / 2);
102 $newImage = $this->createCanvas($width, $height);
104 imagecopyresampled($newImage, $this->image, $xOffset, $yOffset, 0, 0, $newSize[0], $newSize[1], $this->size[0], $this->size[1]);
106 $this->image = $newImage;
107 $this->size = array($width, $height);
121 protected function thumbfill($width, $height, $centerX = 50, $centerY = 50)
123 list($width, $height) = $this->
dimensions($width, $height);
125 if (!$this->
bypassTest($width, $height, $centerX - 50, $centerY - 50)) {
130 if ($newSize[1] < $height) {
132 $xOffset = round(($width - $newSize[0]) * $centerX);
136 $yOffset = round(($height - $newSize[1]) * $centerY);
139 $newImage = $this->createCanvas($width, $height);
141 imagecopyresampled($newImage, $this->image, $xOffset, $yOffset, 0, 0, $newSize[0], $newSize[1], $this->size[0], $this->size[1]);
143 $this->image = $newImage;
144 $this->size = array($width, $height);
157 if ($this->inputFormat ==
'gif' && function_exists(
'imagecreatefromgif')) {
159 $this->image = imagecreatefromgif($this->input);
160 } elseif ($this->inputFormat ==
'jpg') {
162 $this->image = imagecreatefromjpeg($this->input);
163 } elseif ($this->inputFormat ==
'png') {
165 $this->image = imagecreatefrompng($this->input);
166 } elseif ($this->inputFormat ==
'webp' && function_exists(
'imagecreatefromwebp')) {
168 $this->image = imagecreatefromwebp($this->input);
170 throw new \Depage\Graphics\Exceptions\Exception(
'Unknown image format.');
182 $bg = $this->createBackground($this->size[0], $this->size[1]);
183 imagecopy($bg, $this->image, 0, 0, 0, 0, $this->size[0], $this->size[1]);
187 if ($this->outputFormat ==
'gif' && function_exists(
'imagegif')) {
188 $result = imagegif($this->image, $this->output);
189 } elseif ($this->outputFormat ==
'jpg') {
190 $result = imagejpeg($this->image, $this->output, $this->
getQuality());
191 } elseif ($this->outputFormat ==
'png') {
193 $result = imagepng($this->image, $this->output,
$quality, PNG_ALL_FILTERS);
194 } elseif ($this->outputFormat ==
'webp' && function_exists(
'imagewebp')) {
195 $result = imagewebp($this->image, $this->output, $this->
getQuality());
198 throw new \Depage\Graphics\Exceptions\Exception(
'Could not save output image.');
209 return getimagesize($this->input);
228 if ($this->otherRender && file_exists($this->output)) {
231 && $this->inputFormat == $this->outputFormat
237 if ($this->optimize) {
242 parent::renderFinished();
252 private function createCanvas($width, $height)
254 $canvas = imagecreatetruecolor($width, $height);
255 $bg = imagecolorallocatealpha($canvas, 255, 255, 255, 127);
256 imagefill($canvas, 0, 0, $bg);
269 private function createBackground($width, $height)
271 $newImage = imagecreatetruecolor($width, $height);
273 if ($this->background[0] ==
'#') {
277 $color = substr($this->background, 1);
279 if (strlen($color) == 6) {
280 list($r, $g, $b) = array(
285 } elseif (strlen($color) == 3) {
286 list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
289 $r = hexdec($r); $g = hexdec($g); $b = hexdec($b);
291 imagefill($newImage, 0, 0, imagecolorallocate($newImage, $r, $g, $b));
292 } elseif ($this->background ==
'checkerboard') {
294 $transColor = array();
295 $transColor[0] = imagecolorallocate ($newImage, 153, 153, 153);
296 $transColor[1] = imagecolorallocate ($newImage, 102, 102, 102);
297 for ($i = 0; $i * $transLen < $width; $i++) {
298 for ($j = 0; $j * $transLen < $height; $j++) {
299 imagefilledrectangle(
303 ($i + 1) * $transLen,
304 ($j + 1) * $transLen,
305 $transColor[$j % 2 == 0 ? $i % 2 : ($i % 2 == 0 ? 1 : 0)]
309 } elseif ($this->background ==
'transparent') {
310 imagefill($newImage, 0, 0, imagecolorallocatealpha($newImage, 255, 255, 255, 127));
311 if ($this->outputFormat ==
'gif') imagecolortransparent($newImage, imagecolorallocatealpha($newImage, 255, 255, 255, 127));
312 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.