15namespace Depage\Graphics;
24 if (strpos($class, __NAMESPACE__ .
'\\') == 0) {
25 $class = str_replace(
'\\',
'/', str_replace(__NAMESPACE__ .
'\\',
'', $class));
26 $file = __DIR__ .
'/' . $class .
'.php';
28 if (file_exists($file)) {
34spl_autoload_register(__NAMESPACE__ .
'\autoload');
106 private $oldIgnoreUserAbort =
false;
118 $extension = (isset($options[
'extension'])) ? $options[
'extension'] :
'gd';
120 if ($extension ==
'imagick' && extension_loaded(
'imagick')) {
122 } elseif ($extension ==
'im' || $extension ==
'imagemagick') {
123 if (isset($options[
'executable'])) {
127 if ($executable ==
null) {
128 trigger_error(
"Cannot find ImageMagick, falling back to GD", E_USER_WARNING);
130 $options[
'executable'] = $executable;
135 } elseif ($extension ==
'gm' || $extension ==
'graphicsmagick') {
136 if (isset($options[
'executable'])) {
140 if ($executable ==
null) {
141 trigger_error(
"Cannot find GraphicsMagick, falling back to GD", E_USER_WARNING);
143 $options[
'executable'] = $executable;
159 $this->background = (isset($options[
'background'])) ? $options[
'background'] :
'transparent';
160 $this->quality = (isset($options[
'quality'])) ? intval($options[
'quality']) :
null;
161 $this->format = (isset($options[
'format'])) ? $options[
'format'] :
null;
162 $this->optimize = (isset($options[
'optimize'])) ? $options[
'optimize'] :
false;
163 $this->optimizers = (isset($options[
'optimizers'])) ? $options[
'optimizers'] : [];
164 $this->limits = (isset($options[
'limits'])) ? $options[
'limits'] : [
168 if (isset($this->limits[
'memory'])) {
169 putenv(
"MAGICK_MEMORY_LIMIT=" . $this->limits[
'memory']);
170 putenv(
"MAGICK_LIMIT_MEMORY=" . $this->limits[
'memory']);
172 if (isset($this->limits[
'map'])) {
173 putenv(
"MAGICK_MAP_LIMIT=" . $this->limits[
'map']);
174 putenv(
"MAGICK_LIMIT_MAP=" . $this->limits[
'map']);
186 return in_array($ext, [
'jpg',
'jpeg',
'png',
'gif',
'webp']);
196 return in_array($ext, [
'png']);
197 return in_array($ext, [
'jpg',
'jpeg',
'png',
'gif',
'webp']);
225 public function addCrop($width, $height, $x = 0, $y = 0): self
227 $this->queue[] = [
'crop', func_get_args()];
242 $this->queue[] = [
'resize', func_get_args()];
257 $this->queue[] = [
'thumb', func_get_args()];
272 public function addThumbfill($width, $height, $centerX = 50, $centerY = 50): self
274 $this->queue[] = [
'thumbfill', func_get_args()];
289 return (is_numeric($number)) ? intval($number) :
null;
303 if (!is_numeric($width) && !is_numeric($height)) {
306 } elseif (!is_numeric($height)) {
307 $height = round(($this->size[1] / $this->size[0]) * $width);
308 } elseif (!is_numeric($width)) {
309 $width = round(($this->size[0] / $this->size[1]) * $height);
312 return [$width, $height];
331 foreach ($this->queue as $task) {
333 $arguments = array_map([$this,
'escapeNumber'], $task[1]);
335 call_user_func_array([$this, $action], $arguments);
349 if (!file_exists(
$input)) {
358 $this->otherRender =
false;
362 $this->oldIgnoreUserAbort = ignore_user_abort();
363 ignore_user_abort(
true);
372 ignore_user_abort($this->oldIgnoreUserAbort);
384 if (!file_exists($filename)) {
389 $success = $optimizer->optimize($filename);
401 $this->outputLockFp = fopen($this->output .
".lock",
'w');
402 $locked = flock($this->outputLockFp, LOCK_EX | LOCK_NB, $wouldblock);
404 if (!$locked && $wouldblock) {
405 $this->otherRender =
true;
406 flock($this->outputLockFp, LOCK_EX);
417 if (isset($this->outputLockFp)) {
418 flock($this->outputLockFp, LOCK_UN);
419 unlink($this->output .
".lock");
421 $this->outputLockFp =
null;
433 $parts = explode(
'.', $fileName);
434 $extension = strtolower(end($parts));
436 if ($extension ==
'jpeg') {
440 && $extension !=
'png'
441 && $extension !=
'gif'
443 if (is_callable(
'getimagesize') && file_exists($fileName)) {
444 $info = getimagesize($fileName);
445 if (isset($info[2])) {
470 public static function which($binary)
472 exec(
'which ' . $binary, $commandOutput, $returnStatus);
473 if ($returnStatus === 0) {
474 return $commandOutput[0];
497 if ($this->outputFormat ==
'jpg') {
499 is_numeric($this->quality)
500 && $this->quality >= 0
501 && $this->quality <= 100
507 } elseif ($this->outputFormat ==
"webp") {
509 is_numeric($this->quality)
510 && $this->quality >= 0
511 && $this->quality <= 100
517 } elseif ($this->outputFormat ==
'png') {
519 is_numeric($this->quality)
520 && $this->quality >= 0
521 && $this->quality <= 95
522 && $this->quality % 10 <= 5
524 $quality = sprintf(
"%02d", $this->quality);
542 if ($this->inputFormat ==
"pdf") {
558 protected function bypassTest($width, $height, $x = 0, $y = 0)
561 ($width !==
null && $width < 1)
562 || ($height !==
null && $height < 1)
563 || ($width ==
null && $height ==
null)
571 $width == $this->size[0]
572 && $height == $this->size[1]
588 if ($this->input != $this->output) {
589 copy($this->input, $this->output);
general graphics related exception class
Input file not found exception.
$background
Image background string.
$quality
Image quality string.
canWrite($ext)
Checks if extension supports writing file type.
addThumbfill($width, $height, $centerX=50, $centerY=50)
Adds thumb-fill action.
processQueue()
Process action queue.
bypassTest($width, $height, $x=0, $y=0)
Tests if action would change current image.
$inputFormat
Input image format.
getQuality()
Returns quality-index for current image format.
addThumb($width, $height)
Adds thumb action.
$queue
Action queue array.
$otherRender
otherRender is set to true if another render process has already locked file
getPageNumber()
getPageNumber
canRead($ext)
Checks if extension support reading file type.
getImageSize()
Determine size of input image.
render($input, $output=null)
Main method for image handling.
$limits
limits (mainly for imagemagick and graphicsmagick)
static factory($options=[])
graphics object factory
addCrop($width, $height, $x=0, $y=0)
Adds crop action.
$outputFormat
Output image format.
setQuality($quality)
Sets quality parameter.
dimensions($width, $height)
Scales image dimensions.
optimizeImage($filename)
Opimizes final image through one of the optimization programs.
__construct($options=[])
graphics class constructor
obtainFormat($fileName)
Determines image format from file extension.
$optimizers
List of optimizer binaries.
static which($binary)
Executes "which" command.
$bypass
Process bypass bool.
renderFinished()
Called after rendering has finished.
addBackground($background)
Background "action".
addResize($width, $height)
Adds resize action.
bypass()
Runs bypass (copies file)
escapeNumber($number)
Validates integers.
$size
Image size array(width, height)
$optimize
Optimize output images.
PHP GD extension interface.
GraphicsMagick interface.
autoload($class)
PHP autoloader.