Mercurial > hg > mgallery
comparison mgtool.php @ 261:32c13f594e39
Add support for PHP GraphicsMagick bindings, though it kind of sucks due to
poor documentation of the said bindings.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 16 Dec 2018 16:48:30 +0200 |
parents | 67a807666405 |
children | 3c9ce1ad3c45 |
comparison
equal
deleted
inserted
replaced
260:50f8932ab1cb | 261:32c13f594e39 |
---|---|
46 | 46 |
47 | 47 |
48 // | 48 // |
49 // Convert and scale image file function, for generating | 49 // Convert and scale image file function, for generating |
50 // the intermediate size images and thumbnails. Uses the | 50 // the intermediate size images and thumbnails. Uses the |
51 // PHP ImageMagick bindings. | 51 // PHP ImageMagick or GraphicsMagick bindings. |
52 // | 52 // |
53 function mgConvertImage($inFilename, $outFilename, $outDim, $outFormat, $outQuality, $thumb) | 53 function mgConvertImage($inFilename, $outFilename, $outDim, $outFormat, $outQuality, $thumb) |
54 { | 54 { |
55 // Create conversion entity | 55 if (extension_loaded("imagick") && extension_loaded("imagick")) |
56 try | 56 { |
57 { | 57 return mgError("WARNING! Both ImageMagick AND GraphicsMagick modules enabled in PHP! This will cause problems! Refusing to work.\n"); |
58 $img = new Imagick($inFilename); | 58 } |
59 } | 59 |
60 catch (Exception $e) | 60 if (extension_loaded("imagick")) |
61 { | 61 { |
62 return mgError("ImageMagick exception for file '".$inFilename."':\n".$e->getMessage()."\n"); | 62 // Create conversion entity |
63 } | 63 try |
64 | 64 { |
65 if ($img === FALSE) | 65 $img = new Imagick($inFilename); |
66 return mgError("ImageMagick could not digest the file '".$inFilename."'.\n"); | 66 } |
67 | 67 catch (Exception $e) |
68 $profiles = $img->getImageProfiles("icc", true); | 68 { |
69 $img->setImageDepth(16); | 69 return mgError("ImageMagick exception for file '".$inFilename."':\n".$e->getMessage()."\n"); |
70 $img->transformImageColorspace(Imagick::COLORSPACE_SRGB); | 70 } |
71 $img->setImageColorspace(Imagick::COLORSPACE_SRGB); | 71 |
72 | 72 if ($img === FALSE) |
73 if ($outDim !== FALSE) | 73 return mgError("ImageMagick could not digest '".$inFilename."'.\n"); |
74 { | 74 |
75 // Get dimensions, setup background | 75 $profiles = $img->getImageProfiles("icc", true); |
76 $dim = $img->getImageGeometry(); | 76 $img->setImageDepth(16); |
77 //$img->setImageBackgroundColor(imagick::COLOR_BLACK); | 77 $img->transformImageColorspace(Imagick::COLORSPACE_SRGB); |
78 $img->setGravity(imagick::GRAVITY_CENTER); | 78 $img->setImageColorspace(Imagick::COLORSPACE_SRGB); |
79 | 79 |
80 | 80 if ($outDim !== FALSE) |
81 if ($dim["width"] < $dim["height"]) | 81 { |
82 { | 82 // Get dimensions, setup background |
83 $stmp = $outDim[0]; | 83 $dim = $img->getImageGeometry(); |
84 $outDim[0] = $outDim[1]; | 84 $img->setGravity(imagick::GRAVITY_CENTER); |
85 $outDim[1] = $stmp; | 85 |
86 } | 86 if ($dim["width"] < $dim["height"]) |
87 | 87 { |
88 if ($dim["width"] != $outDim[0] || $dim["height"] != $outDim[1]) | 88 $stmp = $outDim[0]; |
89 { | 89 $outDim[0] = $outDim[1]; |
90 $outDim[1] = ($dim["height"] * $outDim[0]) / $dim["width"]; | 90 $outDim[1] = $stmp; |
91 } | 91 } |
92 | 92 |
93 // Act based on image size vs. desired size and $thumb mode | 93 if ($dim["width"] != $outDim[0] || $dim["height"] != $outDim[1]) |
94 if ($thumb || $dim["width"] > $outDim[0] || $dim["height"] > $outDim[1]) | 94 { |
95 { | 95 $outDim[1] = ($dim["height"] * $outDim[0]) / $dim["width"]; |
96 // Image is larger | 96 } |
97 $img->resizeImage($outDim[0], $outDim[1], Imagick::FILTER_LANCZOS, 1); | 97 |
98 $img->setImageExtent($outDim[0], $outDim[1]); | 98 // Act based on image size vs. desired size and $thumb mode |
99 // $img->normalizeImage(); | 99 if ($thumb || $dim["width"] != $outDim[0] || $dim["height"] != $outDim[1]) |
100 $img->unsharpMaskImage(0, 0.5, 1, 0.05); | 100 { |
101 } | 101 $img->resizeImage($outDim[0], $outDim[1], Imagick::FILTER_LANCZOS, 1); |
102 if ($dim["width"] < $outDim[0] || $dim["height"] < $outDim[1]) | 102 $img->setImageExtent($outDim[0], $outDim[1]); |
103 { | 103 $img->unsharpMaskImage(0, 0.5, 1, 0.05); |
104 // Image is smaller than requested dimension(s)? | 104 } |
105 $img->resizeImage($outDim[0], $outDim[1], Imagick::FILTER_LANCZOS, 1); | 105 } |
106 $img->setImageExtent($outDim[0], $outDim[1]); | 106 |
107 // $img->normalizeImage(); | 107 $img->setImageDepth(8); |
108 $img->unsharpMaskImage(0, 0.5, 1, 0.05); | 108 $tfmt = strtolower($outFormat); |
109 } | 109 switch ($tfmt) |
110 } | 110 { |
111 | 111 case "jpeg": |
112 $img->setImageDepth(8); | 112 $img->setFormat("JPEG"); |
113 $tfmt = strtolower($outFormat); | 113 $img->setImageCompression(Imagick::COMPRESSION_JPEG); |
114 switch ($tfmt) | 114 break; |
115 { | 115 |
116 case "jpeg": | 116 case "webp": |
117 $img->setFormat("JPEG"); | 117 $img->setFormat("WEBP"); |
118 $img->setImageCompression(Imagick::COMPRESSION_JPEG); | 118 $img->setOption('webp:method', '6'); |
119 break; | 119 break; |
120 | 120 |
121 case "webp": | 121 default: |
122 $img->setFormat("WEBP"); | 122 return mgError("Unsupported MGallery med/tn format '".$tfmt."'.\n"); |
123 $img->setOption('webp:method', '6'); | 123 } |
124 break; | 124 |
125 | 125 $img->setImageCompressionQuality($outQuality); |
126 default: | 126 |
127 return mgError("Unsupported MGallery med/tn format '".$tfmt."'.\n"); | 127 $img->stripImage(); |
128 } | 128 if (!empty($profiles)) |
129 | 129 $img->profileImage("icc", $profiles["icc"]); |
130 $img->setImageCompressionQuality($outQuality); | 130 |
131 | 131 $img->writeImage($outFilename); |
132 $img->stripImage(); | 132 $img->removeImage(); |
133 if (!empty($profiles)) | 133 } |
134 $img->profileImage("icc", $profiles["icc"]); | 134 else |
135 | 135 if (extension_loaded("gmagick")) |
136 $img->writeImage($outFilename); | 136 { |
137 $img->removeImage(); | 137 // Create conversion entity |
138 try | |
139 { | |
140 $img = new Gmagick($inFilename); | |
141 } | |
142 catch (Exception $e) | |
143 { | |
144 return mgError("GraphicsMagick exception for file '".$inFilename."':\n".$e->getMessage()."\n"); | |
145 } | |
146 | |
147 if ($img === FALSE) | |
148 return mgError("GraphicsMagick could not digest '".$inFilename."'.\n"); | |
149 | |
150 // $profiles = $img->getImageProfile("icc"); | |
151 // $img->setImageDepth(16); | |
152 // $img->setImageColorspace(Gmagick::COLORSPACE_SRGB); | |
153 | |
154 if ($outDim !== FALSE) | |
155 { | |
156 // Get dimensions, setup background | |
157 $dim = $img->getImageGeometry(); | |
158 //$img->setGravity(Gmagick::GRAVITY_CENTER); | |
159 | |
160 if ($dim["width"] < $dim["height"]) | |
161 { | |
162 $stmp = $outDim[0]; | |
163 $outDim[0] = $outDim[1]; | |
164 $outDim[1] = $stmp; | |
165 } | |
166 | |
167 if ($dim["width"] != $outDim[0] || $dim["height"] != $outDim[1]) | |
168 { | |
169 $outDim[1] = ($dim["height"] * $outDim[0]) / $dim["width"]; | |
170 } | |
171 | |
172 // Act based on image size vs. desired size and $thumb mode | |
173 if ($thumb || $dim["width"] != $outDim[0] || $dim["height"] != $outDim[1]) | |
174 { | |
175 $img->resizeImage($outDim[0], $outDim[1], Gmagick::FILTER_LANCZOS, 1); | |
176 //$img->setImageExtent($outDim[0], $outDim[1]); | |
177 //$img->unsharpMaskImage(0, 0.5, 1, 0.05); | |
178 } | |
179 } | |
180 | |
181 $img->setImageDepth(8); | |
182 $tfmt = strtolower($outFormat); | |
183 switch ($tfmt) | |
184 { | |
185 case "jpeg": | |
186 $img->setFormat("JPEG"); | |
187 $img->setImageCompression(Gmagick::COMPRESSION_JPEG); | |
188 break; | |
189 | |
190 case "webp": | |
191 $img->setFormat("WEBP"); | |
192 // $img->setOption('webp:method', '6'); | |
193 break; | |
194 | |
195 default: | |
196 return mgError("Unsupported MGallery med/tn format '".$tfmt."'.\n"); | |
197 } | |
198 | |
199 $img->setCompressionQuality($outQuality); | |
200 | |
201 $img->stripImage(); | |
202 // if (!empty($profiles)) | |
203 // $img->profileImage("icc", $profiles); | |
204 | |
205 $img->writeImage($outFilename); | |
206 $img->removeImage(); | |
207 } | |
208 else | |
209 { | |
210 return mgError("No ImageMagick OR GraphicsMagick module in PHP!\n"); | |
211 } | |
212 | |
138 return TRUE; | 213 return TRUE; |
139 } | 214 } |
140 | 215 |
141 | 216 |
142 // | 217 // |