depage-graphics
Loading...
Searching...
No Matches
Graphicsmagick.php
Go to the documentation of this file.
1<?php
2
10
11namespace Depage\Graphics\Providers;
12
20{
32 protected function crop($width, $height, $x = 0, $y = 0)
33 {
34 if (!$this->bypassTest($width, $height, $x, $y)) {
35 // '+' for positive offset (the '-' is already there)
36 $x = ($x < 0) ? $x : '+' . $x;
37 $y = ($y < 0) ? $y : '+' . $y;
38
39 $xExtent = ($x > 0) ? "+0" : $x;
40 $yExtent = ($y > 0) ? "+0" : $y;
41 $this->command .= " -gravity NorthWest -crop {$width}x{$height}{$x}{$y}! -gravity NorthWest -extent {$width}x{$height}{$xExtent}{$yExtent}";
42 $this->size = array($width, $height);
43 }
44 }
45
51 protected function getImageSize()
52 {
53 $imageSize = false;
54 if (is_callable('getimagesize')) {
55 $imageSize = getimagesize($this->input);
56 }
57 if (!$imageSize) {
58 $pageNumber = $this->getPageNumber();
59 exec("{$this->executable} identify -ping -format \"%wx%h\" " . escapeshellarg($this->input) . $pageNumber . ' 2>&1', $commandOutput, $returnStatus);
60 if ($returnStatus === 0) {
61 $imageSize = explode('x', $commandOutput[0]);
62 } else {
63 $this->unlock();
64
65 throw new \Depage\Graphics\Exceptions\Exception(implode("\n", $commandOutput));
66 }
67 }
68
69 return $imageSize;
70 }
71
81 public function render($input, $output = null)
82 {
84
85 $pageNumber = $this->getPageNumber();
86
87 $this->command = $this->executable . " convert -auto-orient '+profile' '*' -auto-orient " . escapeshellarg($this->input) . "{$pageNumber} -background none";
88 $this->processQueue();
89
90 if ($this->otherRender && file_exists($this->output)) {
91 // do nothing file is already generated
92 } elseif (
93 $this->bypass
94 && $this->inputFormat == $this->outputFormat
95 ) {
96 $this->bypass();
97 } else {
98 $quality = $this->getQuality();
99 $optimize = $this->getOptimize();
100
101 if ($this->background === 'checkerboard') {
102 $tempFile = tempnam(sys_get_temp_dir(), 'depage-graphics-');
103 $this->command .= " miff:{$tempFile}";
104
105 $this->execCommand();
106
107 $canvasSize = $this->size[0] . "x" . $this->size[1];
108
109 $this->command = $this->executable . " convert";
110 $this->command .= " -page {$canvasSize} -size {$canvasSize} pattern:checkerboard";
111 $this->command .= " -page {$canvasSize} miff:{$tempFile} -colorspace rgb -flatten {$quality}{$optimize} +page {$this->outputFormat}:" . escapeshellarg($this->output);
112
113 $this->execCommand();
114 unlink($tempFile);
115 } else {
116 $background = $this->getBackground();
117 $this->command .= "{$background} -colorspace rgb {$quality}{$optimize} +page {$this->outputFormat}:" . escapeshellarg($this->output);
118
119 $this->execCommand();
120
121 if ($this->optimize) {
122 $this->optimizeImage($this->output);
123 }
124 }
125 }
126
127 parent::renderFinished();
128 }
129
135 protected function getBackground()
136 {
137 if ($this->background[0] === '#') {
138 $background = " -flatten -background {$this->background}";
139 } elseif ($this->outputFormat == 'jpg') {
140 $background = " -flatten -background #FFF";
141 } else {
142 $background = '';
143 }
144
145 return $background;
146 }
147}
148
149/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
$background
Image background string.
Definition Graphics.php:74
$quality
Image quality string.
Definition Graphics.php:78
processQueue()
Process action queue.
Definition Graphics.php:322
bypassTest($width, $height, $x=0, $y=0)
Tests if action would change current image.
Definition Graphics.php:551
$input
Input filename.
Definition Graphics.php:46
$output
Output filename.
Definition Graphics.php:50
getPageNumber()
getPageNumber
Definition Graphics.php:533
render($input, $output=null)
Main method for image handling.
Definition Graphics.php:340
optimizeImage($filename)
Opimizes final image through one of the optimization programs.
Definition Graphics.php:375
bypass()
Runs bypass (copies file)
Definition Graphics.php:579
$optimize
Optimize output images.
Definition Graphics.php:82
getBackground()
Generates background command.
getImageSize()
Determine size of input image.
render($input, $output=null)
Main method for image handling.
crop($width, $height, $x=0, $y=0)
Crop action.
getQuality()
Generates quality command.
getOptimize()
Generates optimization parameters.
execCommand()
Executes ImageMagick command.