128 if ($this->resource ===
null)
133 $destinationWidth = $destination->
getWidth();
134 $destinationHeight = $destination->
getHeight();
136 if (($picture = imagecreatetruecolor($destinationWidth, $destinationHeight)))
138 $transparentColor = -1;
140 imagealphablending($picture,
false);
144 $color = imagecolorallocatealpha($picture, 0, 0, 0, 127);
145 imagefilledrectangle($picture, 0, 0, $destinationWidth, $destinationHeight, $color);
151 if ($transparentColor >= 0)
153 $rgb = imagecolorsforindex($this->resource, $transparentColor);
154 $transparentColor = imagecolorallocatealpha($picture, $rgb[
'red'], $rgb[
'green'], $rgb[
'blue'], 127);
155 imagefilledrectangle($picture, 0, 0, $destinationWidth, $destinationHeight, $transparentColor);
159 if (imagecopyresampled($picture, $this->resource, 0, 0, $source->
getX(), $source->
getY(), $destinationWidth, $destinationHeight, $source->
getWidth(), $source->
getHeight()))
162 $this->resource = $picture;
165 if ($transparentColor >= 0)
181 if ($this->resource ===
null)
186 $transparentColor = -1;
196 $result = imageconvolution($this->resource, $mask->
getValue(), 1, 0);
201 imagealphablending($this->resource,
false);
202 imagesetpixel($this->resource, 0, 0, $newPixel);
205 if ($transparentColor >= 0)
225 imagecolortransparent($this->resource, $transparentColor);
230 for ($y = 0; $y < $height; ++$y)
232 for ($x = 0; $x < $width; ++$x)
234 if (((imagecolorat($this->resource, $x, $y) >> 24) & 0x7F) >= 100)
236 imagesetpixel($this->resource, $x, $y, $transparentColor);
253 $alpha = (imagecolorat($this->resource, $x, $y) >> 24) & 0xFF;
254 $newR = $newG = $newB = 0;
256 for ($j = 0; $j < 3; ++$j)
263 elseif ($yv >= $height)
268 for ($i = 0; $i < 3; ++$i)
275 elseif ($xv >= $width)
281 $rgb = imagecolorat($this->resource, $xv, $yv);
283 $newR += (($rgb >> 16) & 0xFF) * $m;
284 $newG += (($rgb >> 8) & 0xFF) * $m;
285 $newB += ($rgb & 0xFF) * $m;
289 $newR = ($newR > 255 ? 255 : ($newR < 0 ? 0 : $newR));
290 $newG = ($newG > 255 ? 255 : ($newG < 0 ? 0 : $newG));
291 $newB = ($newB > 255 ? 255 : ($newB < 0 ? 0 : $newB));
293 return imagecolorallocatealpha($this->resource, $newR, $newG, $newB, $alpha);
301 if ($this->resource ===
null)
308 if (!file_exists($font))
318 if (($textWidth = $watermark->
getWidth()) > 0)
320 $textBox = imagettfbbox(20, 0, $font, $utfText);
321 if (!is_array($textBox))
326 $scale = $textWidth / ($textBox[2] - $textBox[0]);
327 $fontSize = 20 * $scale;
329 $position =
new Rectangle($textWidth, ($textBox[0] - $textBox[7]) * $scale);
335 $textBox = imagettfbbox($fontSize, 0, $font, $utfText);
337 $position =
new Rectangle(($textBox[2] - $textBox[0]), ($textBox[0] - $textBox[7]));
343 $textColor = imagecolorallocate($this->resource, $color->getRed(), $color->getGreen(), $color->getBlue());
350 $descender = $fontSize * 0.2;
351 $y = $position->getY() + $position->getHeight() - $descender;
355 $y = $position->getY() + $fontSize;
358 $result = imagettftext($this->resource, $fontSize, 0, $position->getX(), $y, $textColor, $font, $utfText);
360 return ($result !==
false);
368 if ($this->resource ===
null)
381 $watermarkWidth = $image->getWidth();
382 $watermarkHeight = $image->getHeight();
384 $position =
new Rectangle($watermarkWidth, $watermarkHeight);
388 $watermarkX = $position->getX();
389 $watermarkY = $position->getY();
391 $watermarkAlpha = $watermark->
getAlpha();
394 for ($y = 0; $y < $watermarkHeight; $y++)
396 for ($x = 0; $x < $watermarkWidth; $x++)
398 $posY = $watermarkY + $y;
401 $posX = $watermarkX + $x;
404 $alpha = $watermarkAlpha;
406 $mainRgb = imagecolorsforindex($this->resource, imagecolorat($this->resource, $posX, $posY));
407 $watermarkRgb = imagecolorsforindex($image->resource, imagecolorat($image->resource, $x, $y));
409 if ($watermarkRgb[
'alpha'] == 127)
415 if ($watermarkRgb[
'alpha'])
417 $alpha = round((( 127 - $watermarkRgb[
'alpha']) / 127), 2);
418 $alpha = $alpha * $watermarkAlpha;
422 foreach ([
'red',
'green',
'blue',
'alpha'] as $k)
424 $pixel[$k] = round(($mainRgb[$k] * (1 - $alpha)) + ($watermarkRgb[$k] * $alpha));
428 $color = imagecolorexactalpha($this->resource, $pixel[
'red'], $pixel[
'green'], $pixel[
'blue'], $pixel[
'alpha']);
431 $color = imagecolorallocatealpha($this->resource, $pixel[
'red'], $pixel[
'green'], $pixel[
'blue'], $pixel[
'alpha']);
432 if ($color ===
false)
434 $color = imagecolorclosestalpha($this->resource, $pixel[
'red'], $pixel[
'green'], $pixel[
'blue'], $pixel[
'alpha']);
438 imagesetpixel($this->resource, $posX, $posY, $color);
440 $posX += $watermarkWidth;
442 if (!$repeat || $posX > $width)
448 $posY += $watermarkHeight;
450 if (!$repeat || $posY > $height)
468 if ($this->resource ===
null)
482 case File\Image::FORMAT_GIF:
483 $result = imagegif($this->resource,
$file);
485 case File\Image::FORMAT_PNG:
486 imagealphablending($this->resource,
true);
487 imagesavealpha($this->resource,
true);
488 $result = imagepng($this->resource,
$file);
490 case File\Image::FORMAT_WEBP:
491 imagealphablending($this->resource,
true);
492 imagesavealpha($this->resource,
true);
493 $result = imagewebp($this->resource,
$file, $quality);
495 case File\Image::FORMAT_BMP:
496 $result = imagebmp($this->resource,
$file);
498 case File\Image::FORMAT_JPEG:
499 $result = imagejpeg($this->resource,
$file, $quality);