1 | <?php |
||||||
2 | ////////////////////////////////////////////////////////////// |
||||||
3 | // phpThumb() by James Heinrich <[email protected]> // |
||||||
4 | // available at http://phpthumb.sourceforge.net // |
||||||
5 | // and/or https://github.com/JamesHeinrich/phpThumb // |
||||||
6 | ////////////////////////////////////////////////////////////// |
||||||
7 | /// // |
||||||
8 | // See: phpthumb.readme.txt for usage instructions // |
||||||
9 | // /// |
||||||
10 | ////////////////////////////////////////////////////////////// |
||||||
11 | |||||||
12 | if (!class_exists('phpthumb_functions')) |
||||||
13 | { |
||||||
14 | ob_start(); |
||||||
15 | if(!include_once __DIR__ . '/phpthumb.functions.php') |
||||||
16 | { |
||||||
17 | ob_end_flush(); |
||||||
18 | die('failed to include_once("' . __DIR__ . '/phpthumb.functions.php")'); |
||||||
19 | } |
||||||
20 | ob_end_clean(); |
||||||
21 | } |
||||||
22 | |||||||
23 | class phpthumb { |
||||||
24 | |||||||
25 | // public: |
||||||
26 | // START PARAMETERS (for object mode and phpThumb.php) |
||||||
27 | // See phpthumb.readme.txt for descriptions of what each of these values are |
||||||
28 | public $src = null; // SouRCe filename |
||||||
29 | public $new = null; // NEW image (phpThumb.php only) |
||||||
30 | public $w = null; // Width |
||||||
31 | public $h = null; // Height |
||||||
32 | public $wp = null; // Width (Portrait Images Only) |
||||||
33 | public $hp = null; // Height (Portrait Images Only) |
||||||
34 | public $wl = null; // Width (Landscape Images Only) |
||||||
35 | public $hl = null; // Height (Landscape Images Only) |
||||||
36 | public $ws = null; // Width (Square Images Only) |
||||||
37 | public $hs = null; // Height (Square Images Only) |
||||||
38 | public $f = null; // output image Format |
||||||
39 | public $q = 75; // jpeg output Quality |
||||||
40 | public $sx = null; // Source crop top-left X position |
||||||
41 | public $sy = null; // Source crop top-left Y position |
||||||
42 | public $sw = null; // Source crop Width |
||||||
43 | public $sh = null; // Source crop Height |
||||||
44 | public $zc = null; // Zoom Crop |
||||||
45 | public $ica = null; // Image Crop Auto |
||||||
46 | public $bc = null; // Border Color |
||||||
47 | public $bg = null; // BackGround color |
||||||
48 | public $fltr = array(); // FiLTeRs |
||||||
49 | public $goto = null; // GO TO url after processing |
||||||
50 | public $err = null; // default ERRor image filename |
||||||
51 | public $xto = null; // extract eXif Thumbnail Only |
||||||
52 | public $ra = null; // Rotate by Angle |
||||||
53 | public $ar = null; // Auto Rotate |
||||||
54 | public $aoe = null; // Allow Output Enlargement |
||||||
55 | public $far = null; // Fixed Aspect Ratio |
||||||
56 | public $iar = null; // Ignore Aspect Ratio |
||||||
57 | public $maxb = null; // MAXimum Bytes |
||||||
58 | public $down = null; // DOWNload thumbnail filename |
||||||
59 | public $md5s = null; // MD5 hash of Source image |
||||||
60 | public $sfn = 0; // Source Frame Number |
||||||
61 | public $dpi = 150; // Dots Per Inch for vector source formats |
||||||
62 | public $sia = null; // Save Image As filename |
||||||
63 | |||||||
64 | public $file = null; // >>>deprecated, DO NOT USE, will be removed in future versions<<< |
||||||
65 | |||||||
66 | public $phpThumbDebug = null; |
||||||
67 | // END PARAMETERS |
||||||
68 | |||||||
69 | |||||||
70 | // public: |
||||||
71 | // START CONFIGURATION OPTIONS (for object mode only) |
||||||
72 | // See phpThumb.config.php for descriptions of what each of these settings do |
||||||
73 | |||||||
74 | // * Directory Configuration |
||||||
75 | public $config_cache_directory = null; |
||||||
76 | public $config_cache_directory_depth = 0; |
||||||
77 | public $config_cache_disable_warning = true; |
||||||
78 | public $config_cache_source_enabled = false; |
||||||
79 | public $config_cache_source_directory = null; |
||||||
80 | public $config_temp_directory = null; |
||||||
81 | public $config_document_root = null; |
||||||
82 | |||||||
83 | // * Default output configuration: |
||||||
84 | public $config_output_format = 'jpeg'; |
||||||
85 | public $config_output_maxwidth = 0; |
||||||
86 | public $config_output_maxheight = 0; |
||||||
87 | public $config_output_interlace = true; |
||||||
88 | |||||||
89 | // * Error message configuration |
||||||
90 | public $config_error_image_width = 400; |
||||||
91 | public $config_error_image_height = 100; |
||||||
92 | public $config_error_message_image_default = ''; |
||||||
93 | public $config_error_bgcolor = 'CCCCFF'; |
||||||
94 | public $config_error_textcolor = 'FF0000'; |
||||||
95 | public $config_error_fontsize = 1; |
||||||
96 | public $config_error_die_on_error = false; |
||||||
97 | public $config_error_silent_die_on_error = false; |
||||||
98 | public $config_error_die_on_source_failure = true; |
||||||
99 | |||||||
100 | // * Anti-Hotlink Configuration: |
||||||
101 | public $config_nohotlink_enabled = true; |
||||||
102 | public $config_nohotlink_valid_domains = array(); |
||||||
103 | public $config_nohotlink_erase_image = true; |
||||||
104 | public $config_nohotlink_text_message = 'Off-server thumbnailing is not allowed'; |
||||||
105 | // * Off-server Linking Configuration: |
||||||
106 | public $config_nooffsitelink_enabled = false; |
||||||
107 | public $config_nooffsitelink_valid_domains = array(); |
||||||
108 | public $config_nooffsitelink_require_refer = false; |
||||||
109 | public $config_nooffsitelink_erase_image = true; |
||||||
110 | public $config_nooffsitelink_watermark_src = ''; |
||||||
111 | public $config_nooffsitelink_text_message = 'Off-server linking is not allowed'; |
||||||
112 | |||||||
113 | // * Border & Background default colors |
||||||
114 | public $config_border_hexcolor = '000000'; |
||||||
115 | public $config_background_hexcolor = 'FFFFFF'; |
||||||
116 | |||||||
117 | // * TrueType Fonts |
||||||
118 | public $config_ttf_directory = './fonts'; |
||||||
119 | |||||||
120 | public $config_max_source_pixels = null; |
||||||
121 | public $config_use_exif_thumbnail_for_speed = false; |
||||||
122 | public $config_allow_local_http_src = false; |
||||||
123 | |||||||
124 | public $config_imagemagick_path = null; |
||||||
125 | public $config_prefer_imagemagick = true; |
||||||
126 | public $config_imagemagick_use_thumbnail = true; |
||||||
127 | |||||||
128 | public $config_cache_maxage = null; |
||||||
129 | public $config_cache_maxsize = null; |
||||||
130 | public $config_cache_maxfiles = null; |
||||||
131 | public $config_cache_source_filemtime_ignore_local = false; |
||||||
132 | public $config_cache_source_filemtime_ignore_remote = true; |
||||||
133 | public $config_cache_default_only_suffix = false; |
||||||
134 | public $config_cache_force_passthru = true; |
||||||
135 | public $config_cache_prefix = ''; // default value set in the constructor below |
||||||
136 | |||||||
137 | // * MySQL |
||||||
138 | public $config_mysql_extension = null; |
||||||
139 | public $config_mysql_query = null; |
||||||
140 | public $config_mysql_hostname = null; |
||||||
141 | public $config_mysql_username = null; |
||||||
142 | public $config_mysql_password = null; |
||||||
143 | public $config_mysql_database = null; |
||||||
144 | |||||||
145 | // * Security |
||||||
146 | public $config_high_security_enabled = true; |
||||||
147 | public $config_high_security_password = null; |
||||||
148 | public $config_high_security_url_separator = '&'; |
||||||
149 | public $config_disable_debug = true; |
||||||
150 | public $config_allow_src_above_docroot = false; |
||||||
151 | public $config_allow_src_above_phpthumb = true; |
||||||
152 | public $config_auto_allow_symlinks = true; // allow symlink target directories without explicitly whitelisting them |
||||||
153 | public $config_additional_allowed_dirs = array(); // additional directories to allow source images to be read from |
||||||
154 | public $config_file_create_mask = 0755; |
||||||
155 | public $config_dir_create_mask = 0755; |
||||||
156 | |||||||
157 | // * HTTP fopen |
||||||
158 | public $config_http_fopen_timeout = 10; |
||||||
159 | public $config_http_follow_redirect = true; |
||||||
160 | |||||||
161 | // * Compatability |
||||||
162 | public $config_disable_pathinfo_parsing = false; |
||||||
163 | public $config_disable_imagecopyresampled = false; |
||||||
164 | public $config_disable_onlycreateable_passthru = false; |
||||||
165 | public $config_disable_realpath = false; |
||||||
166 | |||||||
167 | public $config_http_user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7'; |
||||||
168 | |||||||
169 | // END CONFIGURATION OPTIONS |
||||||
170 | |||||||
171 | |||||||
172 | // public: error messages (read-only; persistant) |
||||||
173 | public $debugmessages = array(); |
||||||
174 | public $debugtiming = array(); |
||||||
175 | public $fatalerror = null; |
||||||
176 | |||||||
177 | |||||||
178 | // private: (should not be modified directly) |
||||||
179 | public $thumbnailQuality = 75; |
||||||
180 | public $thumbnailFormat = null; |
||||||
181 | |||||||
182 | public $sourceFilename = null; |
||||||
183 | public $rawImageData = null; |
||||||
184 | public $IMresizedData = null; |
||||||
185 | public $outputImageData = null; |
||||||
186 | |||||||
187 | public $useRawIMoutput = false; |
||||||
188 | |||||||
189 | public $gdimg_output = null; |
||||||
190 | public $gdimg_source = null; |
||||||
191 | |||||||
192 | public $getimagesizeinfo = null; |
||||||
193 | |||||||
194 | public $source_width = null; |
||||||
195 | public $source_height = null; |
||||||
196 | |||||||
197 | public $thumbnailCropX = null; |
||||||
198 | public $thumbnailCropY = null; |
||||||
199 | public $thumbnailCropW = null; |
||||||
200 | public $thumbnailCropH = null; |
||||||
201 | |||||||
202 | public $exif_thumbnail_width = null; |
||||||
203 | public $exif_thumbnail_height = null; |
||||||
204 | public $exif_thumbnail_type = null; |
||||||
205 | public $exif_thumbnail_data = null; |
||||||
206 | public $exif_raw_data = null; |
||||||
207 | |||||||
208 | public $thumbnail_width = null; |
||||||
209 | public $thumbnail_height = null; |
||||||
210 | public $thumbnail_image_width = null; |
||||||
211 | public $thumbnail_image_height = null; |
||||||
212 | |||||||
213 | public $tempFilesToDelete = array(); |
||||||
214 | public $cache_filename = null; |
||||||
215 | |||||||
216 | public $AlphaCapableFormats = array( 'png', 'ico', 'gif', 'webp'); |
||||||
217 | public $is_alpha = false; |
||||||
218 | |||||||
219 | public $iswindows = null; |
||||||
220 | public $issafemode = null; |
||||||
221 | public $php_memory_limit = null; |
||||||
222 | |||||||
223 | public $phpthumb_version = '1.7.16-202008101454'; |
||||||
224 | |||||||
225 | ////////////////////////////////////////////////////////////////////// |
||||||
226 | |||||||
227 | // public: constructor |
||||||
228 | public function __construct() { |
||||||
229 | $this->phpThumb(); |
||||||
230 | } |
||||||
231 | |||||||
232 | public function phpThumb() { |
||||||
233 | $this->DebugTimingMessage('phpThumb() constructor', __FILE__, __LINE__); |
||||||
234 | $this->DebugMessage('phpThumb() v'.$this->phpthumb_version, __FILE__, __LINE__); |
||||||
235 | |||||||
236 | foreach (array(ini_get('memory_limit'), get_cfg_var('memory_limit')) as $php_config_memory_limit) { |
||||||
237 | if (!empty($php_config_memory_limit)) { |
||||||
238 | if (strtoupper($php_config_memory_limit[ strlen($php_config_memory_limit) - 1 ]) == 'G') { // PHP memory limit expressed in Gigabytes |
||||||
239 | $php_config_memory_limit = (int) substr($php_config_memory_limit, 0, -1) * 1073741824; |
||||||
240 | } elseif (strtoupper($php_config_memory_limit[ strlen($php_config_memory_limit) - 1 ]) == 'M') { // PHP memory limit expressed in Megabytes |
||||||
241 | $php_config_memory_limit = (int) substr($php_config_memory_limit, 0, -1) * 1048576; |
||||||
242 | } |
||||||
243 | $this->php_memory_limit = max($this->php_memory_limit, $php_config_memory_limit); |
||||||
244 | } |
||||||
245 | } |
||||||
246 | if ($this->php_memory_limit > 0) { // could be "-1" for "no limit" |
||||||
247 | $this->config_max_source_pixels = round($this->php_memory_limit * 0.20); // 20% of memory_limit |
||||||
248 | } |
||||||
249 | |||||||
250 | $this->iswindows = (bool) (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN'); |
||||||
251 | $this->issafemode = (bool) preg_match('#(1|ON)#i', ini_get('safe_mode')); |
||||||
252 | $this->config_document_root = (!empty($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'] : $this->config_document_root); |
||||||
253 | $this->config_cache_prefix = ( isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'].'_' : ''); |
||||||
254 | |||||||
255 | $this->purgeTempFiles(); // purge existing temp files if re-initializing object |
||||||
256 | |||||||
257 | $php_sapi_name = strtolower(function_exists('php_sapi_name') ? PHP_SAPI : ''); |
||||||
258 | if ($php_sapi_name == 'cli') { |
||||||
259 | $this->config_allow_src_above_docroot = true; |
||||||
260 | } |
||||||
261 | |||||||
262 | if (!$this->config_disable_debug) { |
||||||
263 | // if debug mode is enabled, force phpThumbDebug output, do not allow normal thumbnails to be generated |
||||||
264 | $this->phpThumbDebug = (null === $this->phpThumbDebug ? 9 : max(1, (int) $this->phpThumbDebug)); |
||||||
265 | } |
||||||
266 | } |
||||||
267 | |||||||
268 | public function __destruct() { |
||||||
269 | $this->purgeTempFiles(); |
||||||
270 | } |
||||||
271 | |||||||
272 | // public: |
||||||
273 | public function purgeTempFiles() { |
||||||
274 | foreach ($this->tempFilesToDelete as $tempFileToDelete) { |
||||||
275 | if (file_exists($tempFileToDelete)) { |
||||||
276 | $this->DebugMessage('Deleting temp file "'.$tempFileToDelete.'"', __FILE__, __LINE__); |
||||||
277 | @unlink($tempFileToDelete); |
||||||
278 | } |
||||||
279 | } |
||||||
280 | $this->tempFilesToDelete = array(); |
||||||
281 | return true; |
||||||
282 | } |
||||||
283 | |||||||
284 | // public: |
||||||
285 | public function setSourceFilename($sourceFilename) { |
||||||
286 | //$this->resetObject(); |
||||||
287 | //$this->rawImageData = null; |
||||||
288 | $this->sourceFilename = $sourceFilename; |
||||||
289 | $this->src = $sourceFilename; |
||||||
290 | if (null === $this->config_output_format) { |
||||||
291 | $sourceFileExtension = strtolower(substr(strrchr($sourceFilename, '.'), 1)); |
||||||
292 | if (preg_match('#^[a-z]{3,4}$#', $sourceFileExtension)) { |
||||||
293 | $this->config_output_format = $sourceFileExtension; |
||||||
294 | $this->DebugMessage('setSourceFilename('.$sourceFilename.') set $this->config_output_format to "'.$sourceFileExtension.'"', __FILE__, __LINE__); |
||||||
295 | } else { |
||||||
296 | $this->DebugMessage('setSourceFilename('.$sourceFilename.') did NOT set $this->config_output_format to "'.$sourceFileExtension.'" because it did not seem like an appropriate image format', __FILE__, __LINE__); |
||||||
297 | } |
||||||
298 | } |
||||||
299 | $this->DebugMessage('setSourceFilename('.$sourceFilename.') set $this->sourceFilename to "'.$this->sourceFilename.'"', __FILE__, __LINE__); |
||||||
300 | return true; |
||||||
301 | } |
||||||
302 | |||||||
303 | // public: |
||||||
304 | public function setSourceData($rawImageData, $sourceFilename='') { |
||||||
305 | //$this->resetObject(); |
||||||
306 | //$this->sourceFilename = null; |
||||||
307 | $this->rawImageData = $rawImageData; |
||||||
308 | $this->DebugMessage('setSourceData() setting $this->rawImageData ('.strlen($this->rawImageData).' bytes; magic="'.substr($this->rawImageData, 0, 4).'" ('.phpthumb_functions::HexCharDisplay(substr($this->rawImageData, 0, 4)).'))', __FILE__, __LINE__); |
||||||
309 | if ($this->config_cache_source_enabled) { |
||||||
310 | $sourceFilename = ($sourceFilename ? $sourceFilename : md5($rawImageData)); |
||||||
311 | if (!is_dir($this->config_cache_source_directory)) { |
||||||
312 | $this->ErrorImage('$this->config_cache_source_directory ('.$this->config_cache_source_directory.') is not a directory'); |
||||||
313 | } elseif (!@is_writable($this->config_cache_source_directory)) { |
||||||
314 | $this->ErrorImage('$this->config_cache_source_directory ('.$this->config_cache_source_directory.') is not writable'); |
||||||
315 | } |
||||||
316 | $this->DebugMessage('setSourceData() attempting to save source image to "'.$this->config_cache_source_directory.DIRECTORY_SEPARATOR.urlencode($sourceFilename).'"', __FILE__, __LINE__); |
||||||
317 | if ($fp = @fopen($this->config_cache_source_directory.DIRECTORY_SEPARATOR.urlencode($sourceFilename), 'wb')) { |
||||||
318 | fwrite($fp, $rawImageData); |
||||||
319 | fclose($fp); |
||||||
320 | } elseif (!$this->phpThumbDebug) { |
||||||
321 | $this->ErrorImage('setSourceData() failed to write to source cache ('.$this->config_cache_source_directory.DIRECTORY_SEPARATOR.urlencode($sourceFilename).')'); |
||||||
322 | } |
||||||
323 | } |
||||||
324 | return true; |
||||||
325 | } |
||||||
326 | |||||||
327 | // public: |
||||||
328 | public function setSourceImageResource($gdimg) { |
||||||
329 | //$this->resetObject(); |
||||||
330 | $this->gdimg_source = $gdimg; |
||||||
331 | return true; |
||||||
332 | } |
||||||
333 | |||||||
334 | // public: |
||||||
335 | public function setParameter($param, $value) { |
||||||
336 | if ($param == 'src') { |
||||||
337 | $this->setSourceFilename($this->ResolveFilenameToAbsolute($value)); |
||||||
338 | } elseif (@is_array($this->$param)) { |
||||||
339 | if (is_array($value)) { |
||||||
340 | foreach ($value as $arraykey => $arrayvalue) { |
||||||
341 | array_push($this->$param, $arrayvalue); |
||||||
342 | } |
||||||
343 | } else { |
||||||
344 | array_push($this->$param, $value); |
||||||
345 | } |
||||||
346 | } else { |
||||||
347 | $this->$param = $value; |
||||||
348 | } |
||||||
349 | return true; |
||||||
350 | } |
||||||
351 | |||||||
352 | // public: |
||||||
353 | public function getParameter($param) { |
||||||
354 | //if (property_exists('phpThumb', $param)) { |
||||||
355 | return $this->$param; |
||||||
356 | //} |
||||||
357 | //$this->DebugMessage('setParameter() attempting to get non-existant parameter "'.$param.'"', __FILE__, __LINE__); |
||||||
358 | //return false; |
||||||
359 | } |
||||||
360 | |||||||
361 | |||||||
362 | // public: |
||||||
363 | public function GenerateThumbnail() { |
||||||
364 | |||||||
365 | $this->setOutputFormat(); |
||||||
366 | $this->phpThumbDebug('8a'); |
||||||
367 | $this->ResolveSource(); |
||||||
368 | $this->phpThumbDebug('8b'); |
||||||
369 | $this->SetCacheFilename(); |
||||||
370 | $this->phpThumbDebug('8c'); |
||||||
371 | $this->ExtractEXIFgetImageSize(); |
||||||
372 | $this->phpThumbDebug('8d'); |
||||||
373 | if ($this->useRawIMoutput) { |
||||||
374 | $this->DebugMessage('Skipping rest of GenerateThumbnail() because ($this->useRawIMoutput == true)', __FILE__, __LINE__); |
||||||
375 | return true; |
||||||
376 | } |
||||||
377 | $this->phpThumbDebug('8e'); |
||||||
378 | if (!$this->SourceImageToGD()) { |
||||||
379 | $this->DebugMessage('SourceImageToGD() failed', __FILE__, __LINE__); |
||||||
380 | return false; |
||||||
381 | } |
||||||
382 | $this->phpThumbDebug('8f'); |
||||||
383 | $this->ImageCropAuto(); |
||||||
384 | $this->phpThumbDebug('8h'); |
||||||
385 | $this->Rotate(); |
||||||
386 | $this->phpThumbDebug('8h'); |
||||||
387 | $this->CreateGDoutput(); |
||||||
388 | $this->phpThumbDebug('8i'); |
||||||
389 | |||||||
390 | // default values, also applicable for far="C" |
||||||
391 | $destination_offset_x = round(($this->thumbnail_width - $this->thumbnail_image_width) / 2); |
||||||
392 | $destination_offset_y = round(($this->thumbnail_height - $this->thumbnail_image_height) / 2); |
||||||
393 | if (($this->far == 'L') || ($this->far == 'TL') || ($this->far == 'BL')) { |
||||||
394 | $destination_offset_x = 0; |
||||||
395 | } |
||||||
396 | if (($this->far == 'R') || ($this->far == 'TR') || ($this->far == 'BR')) { |
||||||
397 | $destination_offset_x = round($this->thumbnail_width - $this->thumbnail_image_width); |
||||||
398 | } |
||||||
399 | if (($this->far == 'T') || ($this->far == 'TL') || ($this->far == 'TR')) { |
||||||
400 | $destination_offset_y = 0; |
||||||
401 | } |
||||||
402 | if (($this->far == 'B') || ($this->far == 'BL') || ($this->far == 'BR')) { |
||||||
403 | $destination_offset_y = round($this->thumbnail_height - $this->thumbnail_image_height); |
||||||
404 | } |
||||||
405 | |||||||
406 | // // copy/resize image to appropriate dimensions |
||||||
407 | // $borderThickness = 0; |
||||||
408 | // if (!empty($this->fltr)) { |
||||||
409 | // foreach ($this->fltr as $key => $value) { |
||||||
410 | // if (preg_match('#^bord\|([0-9]+)#', $value, $matches)) { |
||||||
411 | // $borderThickness = $matches[1]; |
||||||
412 | // break; |
||||||
413 | // } |
||||||
414 | // } |
||||||
415 | // } |
||||||
416 | // if ($borderThickness > 0) { |
||||||
417 | // //$this->DebugMessage('Skipping ImageResizeFunction() because BorderThickness="'.$borderThickness.'"', __FILE__, __LINE__); |
||||||
418 | // $this->thumbnail_image_height /= 2; |
||||||
419 | // } |
||||||
420 | $this->ImageResizeFunction( |
||||||
421 | $this->gdimg_output, |
||||||
422 | $this->gdimg_source, |
||||||
423 | $destination_offset_x, |
||||||
424 | $destination_offset_y, |
||||||
425 | $this->thumbnailCropX, |
||||||
426 | $this->thumbnailCropY, |
||||||
427 | $this->thumbnail_image_width, |
||||||
428 | $this->thumbnail_image_height, |
||||||
429 | $this->thumbnailCropW, |
||||||
430 | $this->thumbnailCropH |
||||||
431 | ); |
||||||
432 | |||||||
433 | $this->DebugMessage('memory_get_usage() after copy-resize = '.(function_exists('memory_get_usage') ? @memory_get_usage() : 'n/a'), __FILE__, __LINE__); |
||||||
434 | imagedestroy($this->gdimg_source); |
||||||
435 | $this->DebugMessage('memory_get_usage() after imagedestroy = '.(function_exists('memory_get_usage') ? @memory_get_usage() : 'n/a'), __FILE__, __LINE__); |
||||||
436 | |||||||
437 | $this->phpThumbDebug('8i'); |
||||||
438 | $this->AntiOffsiteLinking(); |
||||||
439 | $this->phpThumbDebug('8j'); |
||||||
440 | $this->ApplyFilters(); |
||||||
441 | $this->phpThumbDebug('8k'); |
||||||
442 | $this->AlphaChannelFlatten(); |
||||||
443 | $this->phpThumbDebug('8l'); |
||||||
444 | $this->MaxFileSize(); |
||||||
445 | $this->phpThumbDebug('8m'); |
||||||
446 | |||||||
447 | $this->DebugMessage('GenerateThumbnail() completed successfully', __FILE__, __LINE__); |
||||||
448 | return true; |
||||||
449 | } |
||||||
450 | |||||||
451 | |||||||
452 | // public: |
||||||
453 | public function RenderOutput() { |
||||||
454 | if (!$this->useRawIMoutput && !is_resource($this->gdimg_output)) { |
||||||
455 | $this->DebugMessage('RenderOutput() failed because !is_resource($this->gdimg_output)', __FILE__, __LINE__); |
||||||
456 | return false; |
||||||
457 | } |
||||||
458 | if (!$this->thumbnailFormat) { |
||||||
459 | $this->DebugMessage('RenderOutput() failed because $this->thumbnailFormat is empty', __FILE__, __LINE__); |
||||||
460 | return false; |
||||||
461 | } |
||||||
462 | if ($this->useRawIMoutput) { |
||||||
463 | $this->DebugMessage('RenderOutput copying $this->IMresizedData ('.strlen($this->IMresizedData).' bytes) to $this->outputImage', __FILE__, __LINE__); |
||||||
464 | $this->outputImageData = $this->IMresizedData; |
||||||
465 | return true; |
||||||
466 | } |
||||||
467 | |||||||
468 | $builtin_formats = array(); |
||||||
469 | if (function_exists('imagetypes')) { |
||||||
470 | $imagetypes = imagetypes(); |
||||||
471 | $builtin_formats['wbmp'] = (bool) ($imagetypes & IMG_WBMP); |
||||||
472 | $builtin_formats['jpg'] = (bool) ($imagetypes & IMG_JPG); |
||||||
473 | $builtin_formats['gif'] = (bool) ($imagetypes & IMG_GIF); |
||||||
474 | $builtin_formats['png'] = (bool) ($imagetypes & IMG_PNG); |
||||||
475 | if (defined('IMG_WEBP')) { |
||||||
476 | $builtin_formats['webp'] = (bool) ($imagetypes & IMG_WEBP); // PHP 5.6.25, 7.0.10 |
||||||
477 | } |
||||||
478 | if (defined('IMG_BMP')) { |
||||||
479 | $builtin_formats['bmp'] = (bool) ($imagetypes & IMG_BMP); // PHP 7.2.0 |
||||||
480 | } |
||||||
481 | } |
||||||
482 | |||||||
483 | $this->DebugMessage('imageinterlace($this->gdimg_output, '. (int) $this->config_output_interlace .')', __FILE__, __LINE__); |
||||||
484 | imageinterlace($this->gdimg_output, (int) $this->config_output_interlace); |
||||||
485 | |||||||
486 | $this->DebugMessage('RenderOutput() attempting image'.strtolower(@$this->thumbnailFormat).'($this->gdimg_output)', __FILE__, __LINE__); |
||||||
487 | ob_start(); |
||||||
488 | switch ($this->thumbnailFormat) { |
||||||
489 | case 'wbmp': |
||||||
490 | if (empty($builtin_formats['wbmp'])) { |
||||||
491 | $this->DebugMessage('GD does not have required built-in support for WBMP output', __FILE__, __LINE__); |
||||||
492 | ob_end_clean(); |
||||||
493 | return false; |
||||||
494 | } |
||||||
495 | imagewbmp($this->gdimg_output, null, $this->thumbnailQuality); |
||||||
496 | $this->outputImageData = ob_get_contents(); |
||||||
497 | break; |
||||||
498 | |||||||
499 | case 'jpeg': |
||||||
500 | case 'jpg': // should be "jpeg" not "jpg" but just in case... |
||||||
501 | if (empty($builtin_formats['jpg'])) { |
||||||
502 | $this->DebugMessage('GD does not have required built-in support for JPEG output', __FILE__, __LINE__); |
||||||
503 | ob_end_clean(); |
||||||
504 | return false; |
||||||
505 | } |
||||||
506 | imagejpeg($this->gdimg_output, null, $this->thumbnailQuality); |
||||||
507 | $this->outputImageData = ob_get_contents(); |
||||||
508 | break; |
||||||
509 | |||||||
510 | case 'png': |
||||||
511 | if (empty($builtin_formats['png'])) { |
||||||
512 | $this->DebugMessage('GD does not have required built-in support for PNG output', __FILE__, __LINE__); |
||||||
513 | ob_end_clean(); |
||||||
514 | return false; |
||||||
515 | } |
||||||
516 | if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.1.2', '>=')) { |
||||||
517 | // https://github.com/JamesHeinrich/phpThumb/issues/24 |
||||||
518 | |||||||
519 | /* http://php.net/manual/en/function.imagepng.php: |
||||||
520 | from php source (gd.h): |
||||||
521 | 2.0.12: Compression level: 0-9 or -1, where 0 is NO COMPRESSION at all, |
||||||
522 | :: 1 is FASTEST but produces larger files, 9 provides the best |
||||||
523 | :: compression (smallest files) but takes a long time to compress, and |
||||||
524 | :: -1 selects the default compiled into the zlib library. |
||||||
525 | Conclusion: Based on the Zlib manual (http://www.zlib.net/manual.html) the default compression level is set to 6. |
||||||
526 | */ |
||||||
527 | if (($this->thumbnailQuality >= -1) && ($this->thumbnailQuality <= 9)) { |
||||||
528 | $PNGquality = $this->thumbnailQuality; |
||||||
529 | } else { |
||||||
530 | $this->DebugMessage('Specified thumbnailQuality "'.$this->thumbnailQuality.'" is outside the accepted range (0-9, or -1). Using 6 as default value.', __FILE__, __LINE__); |
||||||
531 | $PNGquality = 6; |
||||||
532 | } |
||||||
533 | imagepng($this->gdimg_output, null, $PNGquality); |
||||||
534 | } else { |
||||||
535 | imagepng($this->gdimg_output); |
||||||
536 | } |
||||||
537 | $this->outputImageData = ob_get_contents(); |
||||||
538 | break; |
||||||
539 | |||||||
540 | case 'gif': |
||||||
541 | if (empty($builtin_formats['gif'])) { |
||||||
542 | $this->DebugMessage('GD does not have required built-in support for GIF output', __FILE__, __LINE__); |
||||||
543 | ob_end_clean(); |
||||||
544 | return false; |
||||||
545 | } |
||||||
546 | imagegif($this->gdimg_output); |
||||||
547 | $this->outputImageData = ob_get_contents(); |
||||||
548 | break; |
||||||
549 | |||||||
550 | case 'webp': |
||||||
551 | if (empty($builtin_formats['webp'])) { |
||||||
552 | $this->DebugMessage('GD does not have required built-in support for WebP output', __FILE__, __LINE__); |
||||||
553 | ob_end_clean(); |
||||||
554 | return false; |
||||||
555 | } |
||||||
556 | imagewebp($this->gdimg_output); |
||||||
557 | $this->outputImageData = ob_get_contents(); |
||||||
558 | break; |
||||||
559 | |||||||
560 | case 'bmp': |
||||||
561 | if (!empty($builtin_formats['bmp'])) { |
||||||
562 | imagebmp($this->gdimg_output); |
||||||
563 | $this->outputImageData = ob_get_contents(); |
||||||
564 | break; |
||||||
565 | } |
||||||
566 | $this->DebugMessage('GD does not have required built-in support for BMP output', __FILE__, __LINE__); |
||||||
567 | if (!@include_once __DIR__ .'/phpthumb.bmp.php' ) { |
||||||
568 | $this->DebugMessage('Error including "'. __DIR__ .'/phpthumb.bmp.php" which is required for BMP format output', __FILE__, __LINE__); |
||||||
569 | ob_end_clean(); |
||||||
570 | return false; |
||||||
571 | } |
||||||
572 | $phpthumb_bmp = new phpthumb_bmp(); |
||||||
573 | $this->outputImageData = $phpthumb_bmp->GD2BMPstring($this->gdimg_output); |
||||||
574 | unset($phpthumb_bmp); |
||||||
575 | break; |
||||||
576 | |||||||
577 | case 'ico': |
||||||
578 | if (!@include_once __DIR__ .'/phpthumb.ico.php' ) { |
||||||
579 | $this->DebugMessage('Error including "'. __DIR__ .'/phpthumb.ico.php" which is required for ICO format output', __FILE__, __LINE__); |
||||||
580 | ob_end_clean(); |
||||||
581 | return false; |
||||||
582 | } |
||||||
583 | $phpthumb_ico = new phpthumb_ico(); |
||||||
584 | $arrayOfOutputImages = array($this->gdimg_output); |
||||||
585 | $this->outputImageData = $phpthumb_ico->GD2ICOstring($arrayOfOutputImages); |
||||||
586 | unset($phpthumb_ico); |
||||||
587 | break; |
||||||
588 | |||||||
589 | default: |
||||||
590 | $this->DebugMessage('RenderOutput failed because $this->thumbnailFormat "'.$this->thumbnailFormat.'" is not valid', __FILE__, __LINE__); |
||||||
591 | ob_end_clean(); |
||||||
592 | return false; |
||||||
593 | } |
||||||
594 | ob_end_clean(); |
||||||
595 | if (!$this->outputImageData) { |
||||||
596 | $this->DebugMessage('RenderOutput() for "'.$this->thumbnailFormat.'" failed', __FILE__, __LINE__); |
||||||
597 | ob_end_clean(); |
||||||
598 | return false; |
||||||
599 | } |
||||||
600 | $this->DebugMessage('RenderOutput() completing with $this->outputImageData = '.strlen($this->outputImageData).' bytes', __FILE__, __LINE__); |
||||||
601 | return true; |
||||||
602 | } |
||||||
603 | |||||||
604 | |||||||
605 | // public: |
||||||
606 | public function RenderToFile($filename) { |
||||||
607 | if (preg_match('#^[a-z0-9]+://#i', $filename)) { |
||||||
608 | $this->DebugMessage('RenderToFile() failed because $filename ('.$filename.') is a URL', __FILE__, __LINE__); |
||||||
609 | return false; |
||||||
610 | } |
||||||
611 | // render thumbnail to this file only, do not cache, do not output to browser |
||||||
612 | //$renderfilename = $this->ResolveFilenameToAbsolute(dirname($filename)).DIRECTORY_SEPARATOR.basename($filename); |
||||||
613 | $renderfilename = $filename; |
||||||
614 | if (($filename[0] != '/') && ($filename[0] != '\\') && ($filename[1] != ':')) { |
||||||
615 | $renderfilename = $this->ResolveFilenameToAbsolute($renderfilename); |
||||||
616 | } |
||||||
617 | if (!@is_writable(dirname($renderfilename))) { |
||||||
618 | $this->DebugMessage('RenderToFile() failed because "'.dirname($renderfilename).'/" is not writable', __FILE__, __LINE__); |
||||||
619 | return false; |
||||||
620 | } |
||||||
621 | if (@is_file($renderfilename) && !@is_writable($renderfilename)) { |
||||||
622 | $this->DebugMessage('RenderToFile() failed because "'.$renderfilename.'" is not writable', __FILE__, __LINE__); |
||||||
623 | return false; |
||||||
624 | } |
||||||
625 | |||||||
626 | if ($this->RenderOutput()) { |
||||||
627 | if (file_put_contents($renderfilename, $this->outputImageData)) { |
||||||
628 | @chmod($renderfilename, $this->getParameter('config_file_create_mask')); |
||||||
629 | $this->DebugMessage('RenderToFile('.$renderfilename.') succeeded', __FILE__, __LINE__); |
||||||
630 | return true; |
||||||
631 | } |
||||||
632 | if (!@file_exists($renderfilename)) { |
||||||
633 | $this->DebugMessage('RenderOutput ['.$this->thumbnailFormat.'('.$renderfilename.')] did not appear to fail, but the output image does not exist either...', __FILE__, __LINE__); |
||||||
634 | } |
||||||
635 | } else { |
||||||
636 | $this->DebugMessage('RenderOutput ['.$this->thumbnailFormat.'('.$renderfilename.')] failed', __FILE__, __LINE__); |
||||||
637 | } |
||||||
638 | return false; |
||||||
639 | } |
||||||
640 | |||||||
641 | |||||||
642 | // public: |
||||||
643 | public function OutputThumbnail() { |
||||||
644 | $this->purgeTempFiles(); |
||||||
645 | |||||||
646 | if (!$this->useRawIMoutput && !is_resource($this->gdimg_output)) { |
||||||
647 | $this->DebugMessage('OutputThumbnail() failed because !is_resource($this->gdimg_output)', __FILE__, __LINE__); |
||||||
648 | return false; |
||||||
649 | } |
||||||
650 | if (headers_sent()) { |
||||||
651 | return $this->ErrorImage('OutputThumbnail() failed - headers already sent'); |
||||||
652 | } |
||||||
653 | |||||||
654 | $downloadfilename = phpthumb_functions::SanitizeFilename(is_string($this->sia) ? $this->sia : ($this->down ? $this->down : 'phpThumb_generated_thumbnail'.'.'.$this->thumbnailFormat)); |
||||||
655 | $this->DebugMessage('Content-Disposition header filename set to "'.$downloadfilename.'"', __FILE__, __LINE__); |
||||||
656 | if ($downloadfilename) { |
||||||
657 | header('Content-Disposition: '.($this->down ? 'attachment' : 'inline').'; filename="'.$downloadfilename.'"'); |
||||||
658 | } else { |
||||||
659 | $this->DebugMessage('failed to send Content-Disposition header because $downloadfilename is empty', __FILE__, __LINE__); |
||||||
660 | } |
||||||
661 | |||||||
662 | if ($this->useRawIMoutput) { |
||||||
663 | |||||||
664 | header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat)); |
||||||
665 | echo $this->IMresizedData; |
||||||
666 | |||||||
667 | } else { |
||||||
668 | |||||||
669 | $this->DebugMessage('imageinterlace($this->gdimg_output, '. (int) $this->config_output_interlace .')', __FILE__, __LINE__); |
||||||
670 | imageinterlace($this->gdimg_output, (int) $this->config_output_interlace); |
||||||
671 | switch ($this->thumbnailFormat) { |
||||||
672 | case 'jpeg': |
||||||
673 | header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat)); |
||||||
674 | $ImageOutFunction = 'image'.$this->thumbnailFormat; |
||||||
675 | @$ImageOutFunction($this->gdimg_output, null, $this->thumbnailQuality); |
||||||
676 | break; |
||||||
677 | |||||||
678 | case 'png': |
||||||
679 | case 'gif': |
||||||
680 | case 'webp': |
||||||
681 | $ImageOutFunction = 'image'.$this->thumbnailFormat; |
||||||
682 | if (!function_exists($ImageOutFunction)) { |
||||||
683 | $this->DebugMessage($ImageOutFunction.' is not available', __FILE__, __LINE__); |
||||||
684 | return false; |
||||||
685 | } |
||||||
686 | header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat)); |
||||||
687 | @$ImageOutFunction($this->gdimg_output); |
||||||
688 | break; |
||||||
689 | |||||||
690 | case 'bmp': |
||||||
691 | if (function_exists('imagebmp')) { |
||||||
692 | header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat)); |
||||||
693 | imagebmp($this->gdimg_output); |
||||||
694 | break; |
||||||
695 | } |
||||||
696 | if (!@include_once __DIR__ .'/phpthumb.bmp.php' ) { |
||||||
697 | $this->DebugMessage('Error including "'. __DIR__ .'/phpthumb.bmp.php" which is required for BMP format output', __FILE__, __LINE__); |
||||||
698 | return false; |
||||||
699 | } |
||||||
700 | $phpthumb_bmp = new phpthumb_bmp(); |
||||||
701 | if (is_object($phpthumb_bmp)) { |
||||||
702 | $bmp_data = $phpthumb_bmp->GD2BMPstring($this->gdimg_output); |
||||||
703 | unset($phpthumb_bmp); |
||||||
704 | if (!$bmp_data) { |
||||||
705 | $this->DebugMessage('$phpthumb_bmp->GD2BMPstring() failed', __FILE__, __LINE__); |
||||||
706 | return false; |
||||||
707 | } |
||||||
708 | header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat)); |
||||||
709 | echo $bmp_data; |
||||||
710 | } else { |
||||||
711 | $this->DebugMessage('new phpthumb_bmp() failed', __FILE__, __LINE__); |
||||||
712 | return false; |
||||||
713 | } |
||||||
714 | break; |
||||||
715 | |||||||
716 | case 'ico': |
||||||
717 | if (!@include_once __DIR__ .'/phpthumb.ico.php' ) { |
||||||
718 | $this->DebugMessage('Error including "'. __DIR__ .'/phpthumb.ico.php" which is required for ICO format output', __FILE__, __LINE__); |
||||||
719 | return false; |
||||||
720 | } |
||||||
721 | $phpthumb_ico = new phpthumb_ico(); |
||||||
722 | if (is_object($phpthumb_ico)) { |
||||||
723 | $arrayOfOutputImages = array($this->gdimg_output); |
||||||
724 | $ico_data = $phpthumb_ico->GD2ICOstring($arrayOfOutputImages); |
||||||
725 | unset($phpthumb_ico); |
||||||
726 | if (!$ico_data) { |
||||||
727 | $this->DebugMessage('$phpthumb_ico->GD2ICOstring() failed', __FILE__, __LINE__); |
||||||
728 | return false; |
||||||
729 | } |
||||||
730 | header('Content-Type: '.phpthumb_functions::ImageTypeToMIMEtype($this->thumbnailFormat)); |
||||||
731 | echo $ico_data; |
||||||
732 | } else { |
||||||
733 | $this->DebugMessage('new phpthumb_ico() failed', __FILE__, __LINE__); |
||||||
734 | return false; |
||||||
735 | } |
||||||
736 | break; |
||||||
737 | |||||||
738 | default: |
||||||
739 | $this->DebugMessage('OutputThumbnail failed because $this->thumbnailFormat "'.$this->thumbnailFormat.'" is not valid', __FILE__, __LINE__); |
||||||
740 | return false; |
||||||
741 | break; |
||||||
742 | } |
||||||
743 | |||||||
744 | } |
||||||
745 | return true; |
||||||
746 | } |
||||||
747 | |||||||
748 | |||||||
749 | // public: |
||||||
750 | public function CleanUpCacheDirectory() { |
||||||
751 | $this->DebugMessage('CleanUpCacheDirectory() set to purge ('.(null === $this->config_cache_maxage ? 'NULL' : number_format($this->config_cache_maxage / 86400, 1)).' days; '.(null === $this->config_cache_maxsize ? 'NULL' : number_format($this->config_cache_maxsize / 1048576, 2)).' MB; '.(null === $this->config_cache_maxfiles ? 'NULL' : number_format($this->config_cache_maxfiles)).' files)', __FILE__, __LINE__); |
||||||
752 | |||||||
753 | if (!is_writable($this->config_cache_directory)) { |
||||||
754 | $this->DebugMessage('CleanUpCacheDirectory() skipped because "'.$this->config_cache_directory.'" is not writable', __FILE__, __LINE__); |
||||||
755 | return true; |
||||||
756 | } |
||||||
757 | |||||||
758 | // cache status of cache directory for 1 hour to avoid hammering the filesystem functions |
||||||
759 | $phpThumbCacheStats_filename = $this->config_cache_directory.DIRECTORY_SEPARATOR.'phpThumbCacheStats.txt'; |
||||||
760 | if (file_exists($phpThumbCacheStats_filename) && is_readable($phpThumbCacheStats_filename) && (filemtime($phpThumbCacheStats_filename) >= (time() - 3600))) { |
||||||
761 | $this->DebugMessage('CleanUpCacheDirectory() skipped because "'.$phpThumbCacheStats_filename.'" is recently modified', __FILE__, __LINE__); |
||||||
762 | return true; |
||||||
763 | } |
||||||
764 | if (!@touch($phpThumbCacheStats_filename)) { |
||||||
765 | $this->DebugMessage('touch('.$phpThumbCacheStats_filename.') failed', __FILE__, __LINE__); |
||||||
766 | } |
||||||
767 | |||||||
768 | $DeletedKeys = array(); |
||||||
769 | $AllFilesInCacheDirectory = array(); |
||||||
770 | if (($this->config_cache_maxage > 0) || ($this->config_cache_maxsize > 0) || ($this->config_cache_maxfiles > 0)) { |
||||||
771 | $CacheDirOldFilesAge = array(); |
||||||
772 | $CacheDirOldFilesSize = array(); |
||||||
773 | $AllFilesInCacheDirectory = phpthumb_functions::GetAllFilesInSubfolders($this->config_cache_directory); |
||||||
774 | foreach ($AllFilesInCacheDirectory as $fullfilename) { |
||||||
775 | if (preg_match('#'.preg_quote($this->config_cache_prefix).'#i', $fullfilename) && file_exists($fullfilename)) { |
||||||
776 | $CacheDirOldFilesAge[$fullfilename] = @fileatime($fullfilename); |
||||||
777 | if ($CacheDirOldFilesAge[$fullfilename] == 0) { |
||||||
778 | $CacheDirOldFilesAge[$fullfilename] = @filemtime($fullfilename); |
||||||
779 | } |
||||||
780 | $CacheDirOldFilesSize[$fullfilename] = @filesize($fullfilename); |
||||||
781 | } |
||||||
782 | } |
||||||
783 | if (empty($CacheDirOldFilesSize)) { |
||||||
784 | $this->DebugMessage('CleanUpCacheDirectory() skipped because $CacheDirOldFilesSize is empty (phpthumb_functions::GetAllFilesInSubfolders('.$this->config_cache_directory.') found no files)', __FILE__, __LINE__); |
||||||
785 | return true; |
||||||
786 | } |
||||||
787 | $DeletedKeys['zerobyte'] = array(); |
||||||
788 | foreach ($CacheDirOldFilesSize as $fullfilename => $filesize) { |
||||||
789 | // purge all zero-size files more than an hour old (to prevent trying to delete just-created and/or in-use files) |
||||||
790 | $cutofftime = time() - 3600; |
||||||
791 | if (($filesize == 0) && ($CacheDirOldFilesAge[$fullfilename] < $cutofftime)) { |
||||||
792 | $this->DebugMessage('deleting "'.$fullfilename.'"', __FILE__, __LINE__); |
||||||
793 | if (@unlink($fullfilename)) { |
||||||
794 | $DeletedKeys['zerobyte'][] = $fullfilename; |
||||||
795 | unset($CacheDirOldFilesSize[$fullfilename]); |
||||||
796 | unset($CacheDirOldFilesAge[$fullfilename]); |
||||||
797 | } |
||||||
798 | } |
||||||
799 | } |
||||||
800 | $this->DebugMessage('CleanUpCacheDirectory() purged '.count($DeletedKeys['zerobyte']).' zero-byte files', __FILE__, __LINE__); |
||||||
801 | asort($CacheDirOldFilesAge); |
||||||
802 | |||||||
803 | if ($this->config_cache_maxfiles > 0) { |
||||||
804 | $TotalCachedFiles = count($CacheDirOldFilesAge); |
||||||
805 | $DeletedKeys['maxfiles'] = array(); |
||||||
806 | foreach ($CacheDirOldFilesAge as $fullfilename => $filedate) { |
||||||
807 | if ($TotalCachedFiles > $this->config_cache_maxfiles) { |
||||||
808 | $this->DebugMessage('deleting "'.$fullfilename.'"', __FILE__, __LINE__); |
||||||
809 | if (@unlink($fullfilename)) { |
||||||
810 | $TotalCachedFiles--; |
||||||
811 | $DeletedKeys['maxfiles'][] = $fullfilename; |
||||||
812 | } |
||||||
813 | } else { |
||||||
814 | // there are few enough files to keep the rest |
||||||
815 | break; |
||||||
816 | } |
||||||
817 | } |
||||||
818 | $this->DebugMessage('CleanUpCacheDirectory() purged '.count($DeletedKeys['maxfiles']).' files based on (config_cache_maxfiles='.$this->config_cache_maxfiles.')', __FILE__, __LINE__); |
||||||
819 | foreach ($DeletedKeys['maxfiles'] as $fullfilename) { |
||||||
820 | unset($CacheDirOldFilesAge[$fullfilename]); |
||||||
821 | unset($CacheDirOldFilesSize[$fullfilename]); |
||||||
822 | } |
||||||
823 | } |
||||||
824 | |||||||
825 | if ($this->config_cache_maxage > 0) { |
||||||
826 | $mindate = time() - $this->config_cache_maxage; |
||||||
827 | $DeletedKeys['maxage'] = array(); |
||||||
828 | foreach ($CacheDirOldFilesAge as $fullfilename => $filedate) { |
||||||
829 | if ($filedate > 0) { |
||||||
830 | if ($filedate < $mindate) { |
||||||
831 | $this->DebugMessage('deleting "'.$fullfilename.'"', __FILE__, __LINE__); |
||||||
832 | if (@unlink($fullfilename)) { |
||||||
833 | $DeletedKeys['maxage'][] = $fullfilename; |
||||||
834 | } |
||||||
835 | } else { |
||||||
836 | // the rest of the files are new enough to keep |
||||||
837 | break; |
||||||
838 | } |
||||||
839 | } |
||||||
840 | } |
||||||
841 | $this->DebugMessage('CleanUpCacheDirectory() purged '.count($DeletedKeys['maxage']).' files based on (config_cache_maxage='.$this->config_cache_maxage.')', __FILE__, __LINE__); |
||||||
842 | foreach ($DeletedKeys['maxage'] as $fullfilename) { |
||||||
843 | unset($CacheDirOldFilesAge[$fullfilename]); |
||||||
844 | unset($CacheDirOldFilesSize[$fullfilename]); |
||||||
845 | } |
||||||
846 | } |
||||||
847 | |||||||
848 | if ($this->config_cache_maxsize > 0) { |
||||||
849 | $TotalCachedFileSize = array_sum($CacheDirOldFilesSize); |
||||||
850 | $DeletedKeys['maxsize'] = array(); |
||||||
851 | foreach ($CacheDirOldFilesAge as $fullfilename => $filedate) { |
||||||
852 | if ($TotalCachedFileSize > $this->config_cache_maxsize) { |
||||||
853 | $this->DebugMessage('deleting "'.$fullfilename.'"', __FILE__, __LINE__); |
||||||
854 | if (@unlink($fullfilename)) { |
||||||
855 | $TotalCachedFileSize -= $CacheDirOldFilesSize[$fullfilename]; |
||||||
856 | $DeletedKeys['maxsize'][] = $fullfilename; |
||||||
857 | } |
||||||
858 | } else { |
||||||
859 | // the total filesizes are small enough to keep the rest of the files |
||||||
860 | break; |
||||||
861 | } |
||||||
862 | } |
||||||
863 | $this->DebugMessage('CleanUpCacheDirectory() purged '.count($DeletedKeys['maxsize']).' files based on (config_cache_maxsize='.$this->config_cache_maxsize.')', __FILE__, __LINE__); |
||||||
864 | foreach ($DeletedKeys['maxsize'] as $fullfilename) { |
||||||
865 | unset($CacheDirOldFilesAge[$fullfilename]); |
||||||
866 | unset($CacheDirOldFilesSize[$fullfilename]); |
||||||
867 | } |
||||||
868 | } |
||||||
869 | |||||||
870 | } else { |
||||||
871 | $this->DebugMessage('skipping CleanUpCacheDirectory() because config set to not use it', __FILE__, __LINE__); |
||||||
872 | } |
||||||
873 | $totalpurged = 0; |
||||||
874 | foreach ($DeletedKeys as $key => $value) { |
||||||
875 | $totalpurged += count($value); |
||||||
876 | } |
||||||
877 | $this->DebugMessage('CleanUpCacheDirectory() purged '.$totalpurged.' files (from '.count($AllFilesInCacheDirectory).') based on config settings', __FILE__, __LINE__); |
||||||
878 | if ($totalpurged > 0) { |
||||||
879 | $empty_dirs = array(); |
||||||
880 | foreach ($AllFilesInCacheDirectory as $fullfilename) { |
||||||
881 | if (is_dir($fullfilename)) { |
||||||
882 | $empty_dirs[$this->realPathSafe($fullfilename)] = 1; |
||||||
883 | } else { |
||||||
884 | unset($empty_dirs[$this->realPathSafe(dirname($fullfilename))]); |
||||||
885 | } |
||||||
886 | } |
||||||
887 | krsort($empty_dirs); |
||||||
888 | $totalpurgeddirs = 0; |
||||||
889 | foreach ($empty_dirs as $empty_dir => $dummy) { |
||||||
890 | if ($empty_dir == $this->config_cache_directory) { |
||||||
891 | // shouldn't happen, but just in case, don't let it delete actual cache directory |
||||||
892 | continue; |
||||||
893 | } elseif (@rmdir($empty_dir)) { |
||||||
894 | $totalpurgeddirs++; |
||||||
895 | } else { |
||||||
896 | $this->DebugMessage('failed to rmdir('.$empty_dir.')', __FILE__, __LINE__); |
||||||
897 | } |
||||||
898 | } |
||||||
899 | $this->DebugMessage('purged '.$totalpurgeddirs.' empty directories', __FILE__, __LINE__); |
||||||
900 | } |
||||||
901 | return true; |
||||||
902 | } |
||||||
903 | |||||||
904 | ////////////////////////////////////////////////////////////////////// |
||||||
905 | |||||||
906 | // private: re-initializator (call between rendering multiple images with one object) |
||||||
907 | public function resetObject() { |
||||||
908 | $class_vars = get_class_vars(get_class($this)); |
||||||
909 | foreach ($class_vars as $key => $value) { |
||||||
910 | // do not clobber debug or config info |
||||||
911 | if (!preg_match('#^(config_|debug|fatalerror)#i', $key)) { |
||||||
912 | $this->$key = $value; |
||||||
913 | } |
||||||
914 | } |
||||||
915 | $this->phpThumb(); // re-initialize some class variables |
||||||
916 | return true; |
||||||
917 | } |
||||||
918 | |||||||
919 | ////////////////////////////////////////////////////////////////////// |
||||||
920 | |||||||
921 | public function ResolveSource() { |
||||||
922 | if (is_resource($this->gdimg_source)) { |
||||||
923 | $this->DebugMessage('ResolveSource() exiting because is_resource($this->gdimg_source)', __FILE__, __LINE__); |
||||||
924 | return true; |
||||||
925 | } |
||||||
926 | if ($this->rawImageData) { |
||||||
927 | $this->sourceFilename = null; |
||||||
928 | $this->DebugMessage('ResolveSource() exiting because $this->rawImageData is set ('.number_format(strlen($this->rawImageData)).' bytes)', __FILE__, __LINE__); |
||||||
929 | return true; |
||||||
930 | } |
||||||
931 | if ($this->sourceFilename) { |
||||||
932 | $this->sourceFilename = $this->ResolveFilenameToAbsolute($this->sourceFilename); |
||||||
933 | $this->DebugMessage('$this->sourceFilename set to "'.$this->sourceFilename.'"', __FILE__, __LINE__); |
||||||
934 | } elseif ($this->src) { |
||||||
935 | $this->sourceFilename = $this->ResolveFilenameToAbsolute($this->src); |
||||||
936 | $this->DebugMessage('$this->sourceFilename set to "'.$this->sourceFilename.'" from $this->src ('.$this->src.')', __FILE__, __LINE__); |
||||||
937 | } else { |
||||||
938 | return $this->ErrorImage('$this->sourceFilename and $this->src are both empty'); |
||||||
939 | } |
||||||
940 | if ($this->iswindows && ((substr($this->sourceFilename, 0, 2) == '//') || (substr($this->sourceFilename, 0, 2) == '\\\\'))) { |
||||||
941 | // Windows \\share\filename.ext |
||||||
942 | } elseif (preg_match('#^[a-z0-9]+://#i', $this->sourceFilename, $protocol_matches)) { |
||||||
943 | if (preg_match('#^(f|ht)tps?\://#i', $this->sourceFilename)) { |
||||||
944 | // URL |
||||||
945 | if ($this->config_http_user_agent) { |
||||||
946 | ini_set('user_agent', $this->config_http_user_agent); |
||||||
947 | } |
||||||
948 | } else { |
||||||
949 | return $this->ErrorImage('only FTP and HTTP/HTTPS protocols are allowed, "'.$protocol_matches[1].'" is not'); |
||||||
950 | } |
||||||
951 | } elseif (!@file_exists($this->sourceFilename)) { |
||||||
952 | return $this->ErrorImage('"'.$this->sourceFilename.'" does not exist'); |
||||||
953 | } elseif (!@is_file($this->sourceFilename)) { |
||||||
954 | return $this->ErrorImage('"'.$this->sourceFilename.'" is not a file'); |
||||||
955 | } |
||||||
956 | return true; |
||||||
957 | } |
||||||
958 | |||||||
959 | |||||||
960 | public function setOutputFormat() { |
||||||
961 | static $alreadyCalled = false; |
||||||
962 | if ($this->thumbnailFormat && $alreadyCalled) { |
||||||
963 | return true; |
||||||
964 | } |
||||||
965 | $alreadyCalled = true; |
||||||
966 | |||||||
967 | $AvailableImageOutputFormats = array(); |
||||||
968 | $AvailableImageOutputFormats[] = 'text'; |
||||||
969 | if (@is_readable( __DIR__ .'/phpthumb.ico.php')) { |
||||||
970 | $AvailableImageOutputFormats[] = 'ico'; |
||||||
971 | } |
||||||
972 | if (@is_readable( __DIR__ .'/phpthumb.bmp.php')) { |
||||||
973 | $AvailableImageOutputFormats[] = 'bmp'; |
||||||
974 | } |
||||||
975 | |||||||
976 | $this->thumbnailFormat = 'ico'; |
||||||
977 | |||||||
978 | // Set default output format based on what image types are available |
||||||
979 | if (function_exists('imagetypes')) { |
||||||
980 | $imagetypes = imagetypes(); |
||||||
981 | if ($imagetypes & IMG_WBMP) { |
||||||
982 | $this->thumbnailFormat = 'wbmp'; |
||||||
983 | $AvailableImageOutputFormats[] = 'wbmp'; |
||||||
984 | } |
||||||
985 | if ($imagetypes & IMG_GIF) { |
||||||
986 | $this->thumbnailFormat = 'gif'; |
||||||
987 | $AvailableImageOutputFormats[] = 'gif'; |
||||||
988 | } |
||||||
989 | if ($imagetypes & IMG_WEBP) { |
||||||
990 | $this->thumbnailFormat = 'webp'; |
||||||
991 | $AvailableImageOutputFormats[] = 'webp'; |
||||||
992 | } |
||||||
993 | if ($imagetypes & IMG_PNG) { |
||||||
994 | $this->thumbnailFormat = 'png'; |
||||||
995 | $AvailableImageOutputFormats[] = 'png'; |
||||||
996 | } |
||||||
997 | if ($imagetypes & IMG_JPG) { |
||||||
998 | $this->thumbnailFormat = 'jpeg'; |
||||||
999 | $AvailableImageOutputFormats[] = 'jpeg'; |
||||||
1000 | } |
||||||
1001 | } else { |
||||||
1002 | $this->DebugMessage('imagetypes() does not exist - GD support might not be enabled?', __FILE__, __LINE__); |
||||||
1003 | } |
||||||
1004 | if ($this->ImageMagickVersion()) { |
||||||
1005 | $IMformats = array('jpeg', 'png', 'gif', 'bmp', 'ico', 'wbmp', 'webp'); |
||||||
1006 | $this->DebugMessage('Addding ImageMagick formats to $AvailableImageOutputFormats ('.implode(';', $AvailableImageOutputFormats).')', __FILE__, __LINE__); |
||||||
1007 | foreach ($IMformats as $key => $format) { |
||||||
1008 | $AvailableImageOutputFormats[] = $format; |
||||||
1009 | } |
||||||
1010 | } |
||||||
1011 | $AvailableImageOutputFormats = array_unique($AvailableImageOutputFormats); |
||||||
1012 | $this->DebugMessage('$AvailableImageOutputFormats = array('.implode(';', $AvailableImageOutputFormats).')', __FILE__, __LINE__); |
||||||
1013 | |||||||
1014 | $this->f = preg_replace('#[^a-z]#', '', strtolower($this->f)); |
||||||
1015 | if (strtolower($this->config_output_format) == 'jpg') { |
||||||
1016 | $this->config_output_format = 'jpeg'; |
||||||
1017 | } |
||||||
1018 | if (strtolower($this->f) == 'jpg') { |
||||||
1019 | $this->f = 'jpeg'; |
||||||
1020 | } |
||||||
1021 | if (phpthumb_functions::CaseInsensitiveInArray($this->config_output_format, $AvailableImageOutputFormats)) { |
||||||
1022 | // set output format to config default if that format is available |
||||||
1023 | $this->DebugMessage('$this->thumbnailFormat set to $this->config_output_format "'.strtolower($this->config_output_format).'"', __FILE__, __LINE__); |
||||||
1024 | $this->thumbnailFormat = strtolower($this->config_output_format); |
||||||
1025 | } elseif ($this->config_output_format) { |
||||||
1026 | $this->DebugMessage('$this->thumbnailFormat staying as "'.$this->thumbnailFormat.'" because $this->config_output_format ('.strtolower($this->config_output_format).') is not in $AvailableImageOutputFormats', __FILE__, __LINE__); |
||||||
1027 | } |
||||||
1028 | if ($this->f && phpthumb_functions::CaseInsensitiveInArray($this->f, $AvailableImageOutputFormats) ) { |
||||||
1029 | // override output format if $this->f is set and that format is available |
||||||
1030 | $this->DebugMessage('$this->thumbnailFormat set to $this->f "'.strtolower($this->f).'"', __FILE__, __LINE__); |
||||||
1031 | $this->thumbnailFormat = strtolower($this->f); |
||||||
1032 | } elseif ($this->f) { |
||||||
1033 | $this->DebugMessage('$this->thumbnailFormat staying as "'.$this->thumbnailFormat.'" because $this->f ('.strtolower($this->f).') is not in $AvailableImageOutputFormats', __FILE__, __LINE__); |
||||||
1034 | } |
||||||
1035 | |||||||
1036 | // for JPEG images, quality 1 (worst) to 99 (best) |
||||||
1037 | // quality < 25 is nasty, with not much size savings - not recommended |
||||||
1038 | // problems with 100 - invalid JPEG? |
||||||
1039 | $this->thumbnailQuality = max(1, min(99, ($this->q ? (int) $this->q : 75))); |
||||||
1040 | $this->DebugMessage('$this->thumbnailQuality set to "'.$this->thumbnailQuality.'"', __FILE__, __LINE__); |
||||||
1041 | |||||||
1042 | return true; |
||||||
1043 | } |
||||||
1044 | |||||||
1045 | |||||||
1046 | public function setCacheDirectory() { |
||||||
1047 | // resolve cache directory to absolute pathname |
||||||
1048 | $this->DebugMessage('setCacheDirectory() starting with config_cache_directory = "'.$this->config_cache_directory.'"', __FILE__, __LINE__); |
||||||
1049 | if ($this->config_cache_directory && ($this->config_cache_directory[0] == '.')) { |
||||||
1050 | if (preg_match('#^(f|ht)tps?\://#i', $this->src)) { |
||||||
1051 | if (!$this->config_cache_disable_warning) { |
||||||
1052 | $this->ErrorImage('$this->config_cache_directory ('.$this->config_cache_directory.') cannot be used for remote images. Adjust "cache_directory" or "cache_disable_warning" in phpThumb.config.php'); |
||||||
1053 | } |
||||||
1054 | } elseif ($this->src) { |
||||||
1055 | // resolve relative cache directory to source image |
||||||
1056 | $this->config_cache_directory = dirname($this->ResolveFilenameToAbsolute($this->src)).DIRECTORY_SEPARATOR.$this->config_cache_directory; |
||||||
1057 | } else { |
||||||
1058 | // $this->new is probably set |
||||||
1059 | } |
||||||
1060 | } |
||||||
1061 | if (substr($this->config_cache_directory, -1) == '/') { |
||||||
1062 | $this->config_cache_directory = substr($this->config_cache_directory, 0, -1); |
||||||
1063 | } |
||||||
1064 | if ($this->iswindows) { |
||||||
1065 | $this->config_cache_directory = str_replace('/', DIRECTORY_SEPARATOR, $this->config_cache_directory); |
||||||
1066 | } |
||||||
1067 | if ($this->config_cache_directory) { |
||||||
1068 | $real_cache_path = $this->realPathSafe($this->config_cache_directory); |
||||||
1069 | if (!$real_cache_path) { |
||||||
1070 | $this->DebugMessage('$this->realPathSafe($this->config_cache_directory) failed for "'.$this->config_cache_directory.'"', __FILE__, __LINE__); |
||||||
1071 | if (!is_dir($this->config_cache_directory)) { |
||||||
1072 | $this->DebugMessage('!is_dir('.$this->config_cache_directory.')', __FILE__, __LINE__); |
||||||
1073 | } |
||||||
1074 | } |
||||||
1075 | if ($real_cache_path) { |
||||||
1076 | $this->DebugMessage('setting config_cache_directory to $this->realPathSafe('.$this->config_cache_directory.') = "'.$real_cache_path.'"', __FILE__, __LINE__); |
||||||
1077 | $this->config_cache_directory = $real_cache_path; |
||||||
1078 | } |
||||||
1079 | } |
||||||
1080 | if (!is_dir($this->config_cache_directory)) { |
||||||
1081 | if (!$this->config_cache_disable_warning) { |
||||||
1082 | $this->ErrorImage('$this->config_cache_directory ('.$this->config_cache_directory.') does not exist. Adjust "cache_directory" or "cache_disable_warning" in phpThumb.config.php'); |
||||||
1083 | } |
||||||
1084 | $this->DebugMessage('$this->config_cache_directory ('.$this->config_cache_directory.') is not a directory', __FILE__, __LINE__); |
||||||
1085 | $this->config_cache_directory = null; |
||||||
1086 | } elseif (!@is_writable($this->config_cache_directory)) { |
||||||
1087 | $this->DebugMessage('$this->config_cache_directory is not writable ('.$this->config_cache_directory.')', __FILE__, __LINE__); |
||||||
1088 | } |
||||||
1089 | |||||||
1090 | $this->InitializeTempDirSetting(); |
||||||
1091 | if (!@is_dir($this->config_temp_directory) && !@is_writable($this->config_temp_directory) && @is_dir($this->config_cache_directory) && @is_writable($this->config_cache_directory)) { |
||||||
1092 | $this->DebugMessage('setting $this->config_temp_directory = $this->config_cache_directory ('.$this->config_cache_directory.')', __FILE__, __LINE__); |
||||||
1093 | $this->config_temp_directory = $this->config_cache_directory; |
||||||
1094 | } |
||||||
1095 | return true; |
||||||
1096 | } |
||||||
1097 | |||||||
1098 | /* Takes the array of path segments up to now, and the next segment (maybe a modifier: empty, . or ..) |
||||||
1099 | Applies it, adding or removing from $segments as a result. Returns nothing. */ |
||||||
1100 | // http://support.silisoftware.com/phpBB3/viewtopic.php?t=961 |
||||||
1101 | public function applyPathSegment(&$segments, $segment) { |
||||||
1102 | if ($segment == '.') { |
||||||
1103 | return; // always remove |
||||||
1104 | } |
||||||
1105 | if ($segment == '') { |
||||||
1106 | $test = array_pop($segments); |
||||||
1107 | if (null === $test) { |
||||||
1108 | $segments[] = $segment; // keep the first empty block |
||||||
1109 | } elseif ($test == '') { |
||||||
1110 | $test = array_pop($segments); |
||||||
1111 | if (null === $test) { |
||||||
1112 | $segments[] = $test; |
||||||
1113 | $segments[] = $segment; // keep the second one too |
||||||
1114 | } else { // put both back and ignore segment |
||||||
1115 | $segments[] = $test; |
||||||
1116 | $segments[] = $test; |
||||||
1117 | } |
||||||
1118 | } else { |
||||||
1119 | $segments[] = $test; // ignore empty blocks |
||||||
1120 | } |
||||||
1121 | } else { |
||||||
1122 | if ($segment == '..') { |
||||||
1123 | $test = array_pop($segments); |
||||||
1124 | if (null === $test) { |
||||||
1125 | $segments[] = $segment; |
||||||
1126 | } elseif ($test == '..') { |
||||||
1127 | $segments[] = $test; |
||||||
1128 | $segments[] = $segment; |
||||||
1129 | } else { |
||||||
1130 | if ($test == '') { |
||||||
1131 | $segments[] = $test; |
||||||
1132 | } // else nothing, remove both |
||||||
1133 | } |
||||||
1134 | } else { |
||||||
1135 | $segments[] = $segment; |
||||||
1136 | } |
||||||
1137 | } |
||||||
1138 | } |
||||||
1139 | |||||||
1140 | /* Takes array of path components, normalizes it: removes empty slots and '.', collapses '..' and folder names. Returns array. */ |
||||||
1141 | // http://support.silisoftware.com/phpBB3/viewtopic.php?t=961 |
||||||
1142 | public function normalizePath($segments) { |
||||||
1143 | $parts = array(); |
||||||
1144 | foreach ($segments as $segment) { |
||||||
1145 | $this->applyPathSegment($parts, $segment); |
||||||
1146 | } |
||||||
1147 | return $parts; |
||||||
1148 | } |
||||||
1149 | |||||||
1150 | /* True if the provided path points (without resolving symbolic links) into one of the allowed directories. */ |
||||||
1151 | // http://support.silisoftware.com/phpBB3/viewtopic.php?t=961 |
||||||
1152 | public function matchPath($path, $allowed_dirs) { |
||||||
1153 | if (!empty($allowed_dirs)) { |
||||||
1154 | foreach ($allowed_dirs as $one_dir) { |
||||||
1155 | if (preg_match('#^'.preg_quote(str_replace(DIRECTORY_SEPARATOR, '/', $this->realPathSafe($one_dir))).'#', $path)) { |
||||||
1156 | return true; |
||||||
1157 | } |
||||||
1158 | } |
||||||
1159 | } |
||||||
1160 | return false; |
||||||
1161 | } |
||||||
1162 | |||||||
1163 | /* True if the provided path points inside one of open_basedirs (or if open_basedirs are disabled) */ |
||||||
1164 | // http://support.silisoftware.com/phpBB3/viewtopic.php?t=961 |
||||||
1165 | public function isInOpenBasedir($path) { |
||||||
1166 | static $open_basedirs = null; |
||||||
1167 | if (null === $open_basedirs) { |
||||||
1168 | $ini_text = ini_get('open_basedir'); |
||||||
1169 | $this->DebugMessage('open_basedir: "'.$ini_text.'"', __FILE__, __LINE__); |
||||||
1170 | $open_basedirs = array(); |
||||||
1171 | if (strlen($ini_text) > 0) { |
||||||
1172 | foreach (preg_split('#[;:]#', $ini_text) as $key => $value) { |
||||||
1173 | $open_basedirs[$key] = $this->realPathSafe($value); |
||||||
1174 | } |
||||||
1175 | } |
||||||
1176 | } |
||||||
1177 | return (empty($open_basedirs) || $this->matchPath($path, $open_basedirs)); |
||||||
1178 | } |
||||||
1179 | |||||||
1180 | /* Resolves all symlinks in $path, checking that each continuous part ends in an allowed zone. Returns null, if any component leads outside of allowed zone. */ |
||||||
1181 | // http://support.silisoftware.com/phpBB3/viewtopic.php?t=961 |
||||||
1182 | public function resolvePath($path, $allowed_dirs) { |
||||||
1183 | $this->DebugMessage('resolvePath: '.$path.' (allowed_dirs: '.print_r($allowed_dirs, true).')', __FILE__, __LINE__); |
||||||
1184 | |||||||
1185 | // add base path to the top of the list |
||||||
1186 | if (!$this->config_allow_src_above_docroot) { |
||||||
1187 | array_unshift($allowed_dirs, $this->realPathSafe($this->config_document_root)); |
||||||
1188 | } else { |
||||||
1189 | if (!$this->config_allow_src_above_phpthumb) { |
||||||
1190 | array_unshift($allowed_dirs, $this->realPathSafe( __DIR__ )); |
||||||
1191 | } else { |
||||||
1192 | // no checks are needed, offload the work to realpath and forget about it |
||||||
1193 | $this->DebugMessage('resolvePath: checks disabled, returning '.$this->realPathSafe($path), __FILE__, __LINE__); |
||||||
1194 | return $this->realPathSafe($path); |
||||||
1195 | } |
||||||
1196 | } |
||||||
1197 | if ($path == '') { |
||||||
1198 | return null; // save us trouble |
||||||
1199 | } |
||||||
1200 | |||||||
1201 | do { |
||||||
1202 | $this->DebugMessage('resolvePath: iteration, path='.$path.', base path = '.$allowed_dirs[0], __FILE__, __LINE__); |
||||||
1203 | |||||||
1204 | $parts = array(); |
||||||
1205 | // do not use "cleaner" foreach version of this loop as later code relies on both $segments and $i |
||||||
1206 | // http://support.silisoftware.com/phpBB3/viewtopic.php?t=964 |
||||||
1207 | $segments = explode(DIRECTORY_SEPARATOR, $path); |
||||||
1208 | for ($i = 0, $iMax = count($segments); $i < $iMax; $i++) { |
||||||
1209 | $this->applyPathSegment($parts, $segments[$i]); |
||||||
1210 | $thispart = implode(DIRECTORY_SEPARATOR, $parts); |
||||||
1211 | if ($this->isInOpenBasedir($thispart)) { |
||||||
1212 | if (is_link($thispart)) { |
||||||
1213 | break; |
||||||
1214 | } |
||||||
1215 | } |
||||||
1216 | } |
||||||
1217 | |||||||
1218 | $this->DebugMessage('resolvePath: stop at component '.$i, __FILE__, __LINE__); |
||||||
1219 | // test the part up to here |
||||||
1220 | $path = implode(DIRECTORY_SEPARATOR, $parts); |
||||||
1221 | $this->DebugMessage('resolvePath: stop at path='.$path, __FILE__, __LINE__); |
||||||
1222 | if (!$this->matchPath($path, $allowed_dirs)) { |
||||||
1223 | $this->DebugMessage('resolvePath: no match, returning null', __FILE__, __LINE__); |
||||||
1224 | return null; |
||||||
1225 | } |
||||||
1226 | if ($i >= count($segments)) { // reached end |
||||||
1227 | $this->DebugMessage('resolvePath: path parsed, over', __FILE__, __LINE__); |
||||||
1228 | break; |
||||||
1229 | } |
||||||
1230 | // else it's symlink, rewrite path |
||||||
1231 | $path = readlink($path); |
||||||
1232 | $this->DebugMessage('resolvePath: symlink matched, target='.$path, __FILE__, __LINE__); |
||||||
1233 | |||||||
1234 | /* |
||||||
1235 | Replace base path with symlink target. |
||||||
1236 | Assuming: |
||||||
1237 | /www/img/external -> /external |
||||||
1238 | This is allowed: |
||||||
1239 | GET /www/img/external/../external/test/pic.jpg |
||||||
1240 | This isn't: |
||||||
1241 | GET /www/img/external/../www/img/pic.jpg |
||||||
1242 | So there's only one base path which is the last symlink target, but any number of stable whitelisted paths. |
||||||
1243 | */ |
||||||
1244 | if ($this->config_auto_allow_symlinks) { |
||||||
1245 | $allowed_dirs[0] = $path; |
||||||
1246 | } |
||||||
1247 | $path = $path.DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, array_slice($segments,$i + 1)); |
||||||
1248 | } while (true); |
||||||
1249 | return $path; |
||||||
1250 | } |
||||||
1251 | |||||||
1252 | |||||||
1253 | public function realPathSafe($filename) { |
||||||
1254 | // http://php.net/manual/en/function.realpath.php -- "Note: The running script must have executable permissions on all directories in the hierarchy, otherwise realpath() will return FALSE" |
||||||
1255 | // realPathSafe() provides a reasonable facsimile of realpath() but does not resolve symbolic links, nor does it check that the file/path actually exists |
||||||
1256 | if (!$this->config_disable_realpath) { |
||||||
1257 | return realpath($filename); |
||||||
1258 | } |
||||||
1259 | |||||||
1260 | // http://stackoverflow.com/questions/21421569 |
||||||
1261 | $newfilename = preg_replace('#[\\/]+#', DIRECTORY_SEPARATOR, $filename); |
||||||
1262 | if (!preg_match('#^'.DIRECTORY_SEPARATOR.'#', $newfilename)) { |
||||||
1263 | $newfilename = __DIR__ .DIRECTORY_SEPARATOR.$newfilename; |
||||||
1264 | } |
||||||
1265 | do { |
||||||
1266 | $beforeloop = $newfilename; |
||||||
1267 | |||||||
1268 | // Replace all sequences of more than one / with a single one [[ If you're working on a system that treats // at the start of a path as special, make sure you replace multiple / characters at the start with two of them. This is the only place where POSIX allows (but does not mandate) special handling for multiples, in all other cases, multiple / characters are equivalent to a single one.]] |
||||||
1269 | $newfilename = preg_replace('#'.DIRECTORY_SEPARATOR.'+#', DIRECTORY_SEPARATOR, $newfilename); |
||||||
1270 | |||||||
1271 | // Replace all occurrences of /./ with / |
||||||
1272 | $newfilename = preg_replace('#'.DIRECTORY_SEPARATOR.'\\.'.DIRECTORY_SEPARATOR.'#', DIRECTORY_SEPARATOR, $newfilename); |
||||||
1273 | |||||||
1274 | // Remove ./ if at the start |
||||||
1275 | $newfilename = preg_replace('#^\\.'.DIRECTORY_SEPARATOR.'#', '', $newfilename); |
||||||
1276 | |||||||
1277 | // Remove /. if at the end |
||||||
1278 | $newfilename = preg_replace('#'.DIRECTORY_SEPARATOR.'\\.$#', '', $newfilename); |
||||||
1279 | |||||||
1280 | // Replace /anything/../ with / |
||||||
1281 | $newfilename = preg_replace('#'.DIRECTORY_SEPARATOR.'[^'.DIRECTORY_SEPARATOR.']+'.DIRECTORY_SEPARATOR.'\\.\\.'.DIRECTORY_SEPARATOR.'#', DIRECTORY_SEPARATOR, $newfilename); |
||||||
1282 | |||||||
1283 | // Remove /anything/.. if at the end |
||||||
1284 | $newfilename = preg_replace('#'.DIRECTORY_SEPARATOR.'[^'.DIRECTORY_SEPARATOR.']+'.DIRECTORY_SEPARATOR.'\\.\\.$#', '', $newfilename); |
||||||
1285 | |||||||
1286 | } while ($newfilename != $beforeloop); |
||||||
1287 | return $newfilename; |
||||||
1288 | } |
||||||
1289 | |||||||
1290 | |||||||
1291 | public function ResolveFilenameToAbsolute($filename) { |
||||||
1292 | if (empty($filename)) { |
||||||
1293 | return false; |
||||||
1294 | } |
||||||
1295 | |||||||
1296 | if (preg_match('#^[a-z0-9]+\\:/{1,2}#i', $filename)) { |
||||||
1297 | // eg: http://host/path/file.jpg (HTTP URL) |
||||||
1298 | // eg: ftp://host/path/file.jpg (FTP URL) |
||||||
1299 | // eg: data1:/path/file.jpg (Netware path) |
||||||
1300 | |||||||
1301 | //$AbsoluteFilename = $filename; |
||||||
1302 | return $filename; |
||||||
1303 | |||||||
1304 | } elseif ($this->iswindows && isset($filename[1]) && ($filename[1] == ':')) { |
||||||
1305 | |||||||
1306 | // absolute pathname (Windows) |
||||||
1307 | $AbsoluteFilename = $filename; |
||||||
1308 | |||||||
1309 | } elseif ($this->iswindows && ((substr($filename, 0, 2) == '//') || (substr($filename, 0, 2) == '\\\\'))) { |
||||||
1310 | |||||||
1311 | // absolute pathname (Windows) |
||||||
1312 | $AbsoluteFilename = $filename; |
||||||
1313 | |||||||
1314 | } elseif ($filename[0] == '/') { |
||||||
1315 | |||||||
1316 | if (@is_readable($filename) && !@is_readable($this->config_document_root.$filename)) { |
||||||
1317 | |||||||
1318 | // absolute filename (*nix) |
||||||
1319 | $AbsoluteFilename = $filename; |
||||||
1320 | |||||||
1321 | } elseif (isset($filename[1]) && ($filename[1] == '~')) { |
||||||
1322 | |||||||
1323 | // /~user/path |
||||||
1324 | if ($ApacheLookupURIarray = phpthumb_functions::ApacheLookupURIarray($filename)) { |
||||||
1325 | $AbsoluteFilename = $ApacheLookupURIarray['filename']; |
||||||
1326 | } else { |
||||||
1327 | $AbsoluteFilename = $this->realPathSafe($filename); |
||||||
1328 | if (@is_readable($AbsoluteFilename)) { |
||||||
1329 | $this->DebugMessage('phpthumb_functions::ApacheLookupURIarray() failed for "'.$filename.'", but the correct filename ('.$AbsoluteFilename.') seems to have been resolved with $this->realPathSafe($filename)', __FILE__, __LINE__); |
||||||
1330 | } elseif (is_dir(dirname($AbsoluteFilename))) { |
||||||
1331 | $this->DebugMessage('phpthumb_functions::ApacheLookupURIarray() failed for "'.dirname($filename).'", but the correct directory ('.dirname($AbsoluteFilename).') seems to have been resolved with $this->realPathSafe(.)', __FILE__, __LINE__); |
||||||
1332 | } else { |
||||||
1333 | return $this->ErrorImage('phpthumb_functions::ApacheLookupURIarray() failed for "'.$filename.'". This has been known to fail on Apache2 - try using the absolute filename for the source image (ex: "/home/user/httpdocs/image.jpg" instead of "/~user/image.jpg")'); |
||||||
1334 | } |
||||||
1335 | } |
||||||
1336 | |||||||
1337 | } else { |
||||||
1338 | |||||||
1339 | // relative filename (any OS) |
||||||
1340 | if (preg_match('#^'.preg_quote($this->config_document_root).'#', $filename)) { |
||||||
1341 | $AbsoluteFilename = $filename; |
||||||
1342 | $this->DebugMessage('ResolveFilenameToAbsolute() NOT prepending $this->config_document_root ('.$this->config_document_root.') to $filename ('.$filename.') resulting in ($AbsoluteFilename = "'.$AbsoluteFilename.'")', __FILE__, __LINE__); |
||||||
1343 | } else { |
||||||
1344 | $AbsoluteFilename = $this->config_document_root.$filename; |
||||||
1345 | $this->DebugMessage('ResolveFilenameToAbsolute() prepending $this->config_document_root ('.$this->config_document_root.') to $filename ('.$filename.') resulting in ($AbsoluteFilename = "'.$AbsoluteFilename.'")', __FILE__, __LINE__); |
||||||
1346 | } |
||||||
1347 | |||||||
1348 | } |
||||||
1349 | |||||||
1350 | } else { |
||||||
1351 | |||||||
1352 | // relative to current directory (any OS) |
||||||
1353 | $AbsoluteFilename = __DIR__ .DIRECTORY_SEPARATOR.preg_replace('#[/\\\\]#', DIRECTORY_SEPARATOR, $filename); |
||||||
1354 | |||||||
1355 | if (substr(dirname(@$_SERVER['PHP_SELF']), 0, 2) == '/~') { |
||||||
1356 | if ($ApacheLookupURIarray = phpthumb_functions::ApacheLookupURIarray(dirname(@$_SERVER['PHP_SELF']))) { |
||||||
1357 | $AbsoluteFilename = $ApacheLookupURIarray['filename'].DIRECTORY_SEPARATOR.$filename; |
||||||
1358 | } else { |
||||||
1359 | $AbsoluteFilename = $this->realPathSafe('.').DIRECTORY_SEPARATOR.$filename; |
||||||
1360 | if (@is_readable($AbsoluteFilename)) { |
||||||
1361 | $this->DebugMessage('phpthumb_functions::ApacheLookupURIarray() failed for "'.dirname(@$_SERVER['PHP_SELF']).'", but the correct filename ('.$AbsoluteFilename.') seems to have been resolved with $this->realPathSafe(.)/$filename', __FILE__, __LINE__); |
||||||
1362 | } elseif (is_dir(dirname($AbsoluteFilename))) { |
||||||
1363 | $this->DebugMessage('phpthumb_functions::ApacheLookupURIarray() failed for "'.dirname(@$_SERVER['PHP_SELF']).'", but the correct directory ('.dirname($AbsoluteFilename).') seems to have been resolved with $this->realPathSafe(.)', __FILE__, __LINE__); |
||||||
1364 | } else { |
||||||
1365 | return $this->ErrorImage('phpthumb_functions::ApacheLookupURIarray() failed for "'.dirname(@$_SERVER['PHP_SELF']).'". This has been known to fail on Apache2 - try using the absolute filename for the source image'); |
||||||
1366 | } |
||||||
1367 | } |
||||||
1368 | } |
||||||
1369 | |||||||
1370 | } |
||||||
1371 | /* |
||||||
1372 | // removed 2014-May-30: http://support.silisoftware.com/phpBB3/viewtopic.php?t=961 |
||||||
1373 | if (is_link($AbsoluteFilename)) { |
||||||
1374 | $this->DebugMessage('is_link()==true, changing "'.$AbsoluteFilename.'" to "'.readlink($AbsoluteFilename).'"', __FILE__, __LINE__); |
||||||
1375 | $AbsoluteFilename = readlink($AbsoluteFilename); |
||||||
1376 | } |
||||||
1377 | if ($this->realPathSafe($AbsoluteFilename)) { |
||||||
1378 | $AbsoluteFilename = $this->realPathSafe($AbsoluteFilename); |
||||||
1379 | } |
||||||
1380 | */ |
||||||
1381 | if ($this->iswindows) { |
||||||
1382 | $AbsoluteFilename = preg_replace('#^'.preg_quote($this->realPathSafe($this->config_document_root)).'#i', str_replace('\\', '\\\\', $this->realPathSafe($this->config_document_root)), $AbsoluteFilename); |
||||||
1383 | $AbsoluteFilename = str_replace(DIRECTORY_SEPARATOR, '/', $AbsoluteFilename); |
||||||
1384 | } |
||||||
1385 | $resolvedAbsoluteFilename = $this->resolvePath($AbsoluteFilename, $this->config_additional_allowed_dirs); |
||||||
1386 | if (!$this->config_allow_src_above_docroot && !preg_match('#^'.preg_quote(str_replace(DIRECTORY_SEPARATOR, '/', $this->realPathSafe($this->config_document_root))).'#', $resolvedAbsoluteFilename)) { |
||||||
1387 | $this->DebugMessage('!$this->config_allow_src_above_docroot therefore setting "'.$AbsoluteFilename.'" (outside "'.$this->realPathSafe($this->config_document_root).'") to null', __FILE__, __LINE__); |
||||||
1388 | return false; |
||||||
1389 | } |
||||||
1390 | if (!$this->config_allow_src_above_phpthumb && !preg_match('#^'.preg_quote(str_replace(DIRECTORY_SEPARATOR, '/', __DIR__ )).'#', $resolvedAbsoluteFilename)) { |
||||||
1391 | $this->DebugMessage('!$this->config_allow_src_above_phpthumb therefore setting "'.$AbsoluteFilename.'" (outside "'. __DIR__ .'") to null', __FILE__, __LINE__); |
||||||
1392 | return false; |
||||||
1393 | } |
||||||
1394 | return $resolvedAbsoluteFilename; |
||||||
1395 | } |
||||||
1396 | |||||||
1397 | |||||||
1398 | public function file_exists_ignoreopenbasedir($filename, $cached=true) { |
||||||
1399 | static $open_basedirs = null; |
||||||
1400 | static $file_exists_cache = array(); |
||||||
1401 | if (!$cached || !isset($file_exists_cache[$filename])) { |
||||||
1402 | if (null === $open_basedirs) { |
||||||
1403 | $open_basedirs = preg_split('#[;:]#', ini_get('open_basedir')); |
||||||
1404 | } |
||||||
1405 | if (empty($open_basedirs) || in_array(dirname($filename), $open_basedirs)) { |
||||||
1406 | $file_exists_cache[$filename] = file_exists($filename); |
||||||
1407 | } elseif ($this->iswindows) { |
||||||
1408 | $ls_filename = trim(phpthumb_functions::SafeExec('dir /b '.phpthumb_functions::escapeshellarg_replacement($filename))); |
||||||
1409 | $file_exists_cache[$filename] = ($ls_filename == basename($filename)); // command dir /b return only filename without path |
||||||
1410 | } else { |
||||||
1411 | $ls_filename = trim(phpthumb_functions::SafeExec('ls '.phpthumb_functions::escapeshellarg_replacement($filename))); |
||||||
1412 | $file_exists_cache[$filename] = ($ls_filename == $filename); |
||||||
1413 | } |
||||||
1414 | } |
||||||
1415 | return $file_exists_cache[$filename]; |
||||||
1416 | } |
||||||
1417 | |||||||
1418 | |||||||
1419 | public function ImageMagickWhichConvert() { |
||||||
1420 | static $WhichConvert = null; |
||||||
1421 | if (null === $WhichConvert) { |
||||||
1422 | if ($this->iswindows) { |
||||||
1423 | $WhichConvert = false; |
||||||
1424 | } else { |
||||||
1425 | $IMwhichConvertCacheFilename = $this->config_cache_directory.DIRECTORY_SEPARATOR.'phpThumbCacheIMwhichConvert.txt'; |
||||||
1426 | if (($cachedwhichconvertstring = @file_get_contents($IMwhichConvertCacheFilename)) !== false) { |
||||||
1427 | $WhichConvert = $cachedwhichconvertstring; |
||||||
1428 | } else { |
||||||
1429 | $WhichConvert = trim(phpthumb_functions::SafeExec('which convert')); |
||||||
1430 | @file_put_contents($IMwhichConvertCacheFilename, $WhichConvert); |
||||||
1431 | @chmod($IMwhichConvertCacheFilename, $this->getParameter('config_file_create_mask')); |
||||||
1432 | } |
||||||
1433 | } |
||||||
1434 | } |
||||||
1435 | return $WhichConvert; |
||||||
1436 | } |
||||||
1437 | |||||||
1438 | |||||||
1439 | public function ImageMagickCommandlineBase() { |
||||||
1440 | static $commandline = null; |
||||||
1441 | if (null === $commandline) { |
||||||
1442 | if ($this->issafemode) { |
||||||
1443 | $commandline = ''; |
||||||
1444 | return $commandline; |
||||||
1445 | } |
||||||
1446 | |||||||
1447 | $IMcommandlineBaseCacheFilename = $this->config_cache_directory.DIRECTORY_SEPARATOR.'phpThumbCacheIMcommandlineBase.txt'; |
||||||
1448 | if (($commandline = @file_get_contents($IMcommandlineBaseCacheFilename)) !== false) { |
||||||
1449 | return $commandline; |
||||||
1450 | } |
||||||
1451 | |||||||
1452 | $commandline = (null !== $this->config_imagemagick_path ? $this->config_imagemagick_path : ''); |
||||||
1453 | |||||||
1454 | if ($this->config_imagemagick_path && ($this->config_imagemagick_path != $this->realPathSafe($this->config_imagemagick_path))) { |
||||||
1455 | if (@is_executable($this->realPathSafe($this->config_imagemagick_path))) { |
||||||
1456 | $this->DebugMessage('Changing $this->config_imagemagick_path ('.$this->config_imagemagick_path.') to $this->realPathSafe($this->config_imagemagick_path) ('.$this->realPathSafe($this->config_imagemagick_path).')', __FILE__, __LINE__); |
||||||
1457 | $this->config_imagemagick_path = $this->realPathSafe($this->config_imagemagick_path); |
||||||
1458 | } else { |
||||||
1459 | $this->DebugMessage('Leaving $this->config_imagemagick_path as ('.$this->config_imagemagick_path.') because !is_execuatable($this->realPathSafe($this->config_imagemagick_path)) ('.$this->realPathSafe($this->config_imagemagick_path).')', __FILE__, __LINE__); |
||||||
1460 | } |
||||||
1461 | } |
||||||
1462 | $this->DebugMessage(' file_exists('.$this->config_imagemagick_path.') = '. (int) (@file_exists($this->config_imagemagick_path)), __FILE__, __LINE__); |
||||||
1463 | $this->DebugMessage('file_exists_ignoreopenbasedir('.$this->config_imagemagick_path.') = '. (int) $this->file_exists_ignoreopenbasedir($this->config_imagemagick_path), __FILE__, __LINE__); |
||||||
1464 | $this->DebugMessage(' is_file('.$this->config_imagemagick_path.') = '. (int) (@is_file($this->config_imagemagick_path)), __FILE__, __LINE__); |
||||||
1465 | $this->DebugMessage(' is_executable('.$this->config_imagemagick_path.') = '. (int) (@is_executable($this->config_imagemagick_path)), __FILE__, __LINE__); |
||||||
1466 | |||||||
1467 | if ($this->file_exists_ignoreopenbasedir($this->config_imagemagick_path)) { |
||||||
1468 | |||||||
1469 | $this->DebugMessage('using ImageMagick path from $this->config_imagemagick_path ('.$this->config_imagemagick_path.')', __FILE__, __LINE__); |
||||||
1470 | if ($this->iswindows) { |
||||||
1471 | $commandline = substr($this->config_imagemagick_path, 0, 2).' && cd '.phpthumb_functions::escapeshellarg_replacement(str_replace('/', DIRECTORY_SEPARATOR, substr(dirname($this->config_imagemagick_path), 2))).' && '.phpthumb_functions::escapeshellarg_replacement(basename($this->config_imagemagick_path)); |
||||||
1472 | } else { |
||||||
1473 | $commandline = phpthumb_functions::escapeshellarg_replacement($this->config_imagemagick_path); |
||||||
1474 | } |
||||||
1475 | |||||||
1476 | } else { |
||||||
1477 | |||||||
1478 | $which_convert = $this->ImageMagickWhichConvert(); |
||||||
1479 | $IMversion = $this->ImageMagickVersion(); |
||||||
1480 | |||||||
1481 | if ($which_convert && ($which_convert[0] == '/') && $this->file_exists_ignoreopenbasedir($which_convert)) { |
||||||
1482 | |||||||
1483 | // `which convert` *should* return the path if "convert" exist, or nothing if it doesn't |
||||||
1484 | // other things *may* get returned, like "sh: convert: not found" or "no convert in /usr/local/bin /usr/sbin /usr/bin /usr/ccs/bin" |
||||||
1485 | // so only do this if the value returned exists as a file |
||||||
1486 | $this->DebugMessage('using ImageMagick path from `which convert` ('.$which_convert.')', __FILE__, __LINE__); |
||||||
1487 | $commandline = 'convert'; |
||||||
1488 | |||||||
1489 | } elseif ($IMversion) { |
||||||
1490 | |||||||
1491 | $this->DebugMessage('setting ImageMagick path to $this->config_imagemagick_path ('.$this->config_imagemagick_path.') ['.$IMversion.']', __FILE__, __LINE__); |
||||||
1492 | $commandline = $this->config_imagemagick_path; |
||||||
1493 | |||||||
1494 | } else { |
||||||
1495 | |||||||
1496 | $this->DebugMessage('ImageMagickThumbnailToGD() aborting because cannot find convert in $this->config_imagemagick_path ('.$this->config_imagemagick_path.'), and `which convert` returned ('.$which_convert.')', __FILE__, __LINE__); |
||||||
1497 | $commandline = ''; |
||||||
1498 | |||||||
1499 | } |
||||||
1500 | |||||||
1501 | } |
||||||
1502 | |||||||
1503 | @file_put_contents($IMcommandlineBaseCacheFilename, $commandline); |
||||||
1504 | @chmod($IMcommandlineBaseCacheFilename, $this->getParameter('config_file_create_mask')); |
||||||
1505 | } |
||||||
1506 | return $commandline; |
||||||
1507 | } |
||||||
1508 | |||||||
1509 | |||||||
1510 | public function ImageMagickVersion($returnRAW=false) { |
||||||
1511 | static $versionstring = null; |
||||||
1512 | if (null === $versionstring) { |
||||||
1513 | $versionstring = array(0=>false, 1=>false); |
||||||
1514 | |||||||
1515 | $IMversionCacheFilename = $this->config_cache_directory.DIRECTORY_SEPARATOR.'phpThumbCacheIMversion.txt'; |
||||||
1516 | if ($cachedversionstring = @file_get_contents($IMversionCacheFilename)) { |
||||||
1517 | |||||||
1518 | $versionstring = explode("\n", $cachedversionstring, 2); |
||||||
1519 | $versionstring[0] = ($versionstring[0] ? $versionstring[0] : false); // "false" is stored as an empty string in the cache file |
||||||
1520 | $versionstring[1] = ($versionstring[1] ? $versionstring[1] : false); // "false" is stored as an empty string in the cache file |
||||||
1521 | |||||||
1522 | } else { |
||||||
1523 | |||||||
1524 | $commandline = $this->ImageMagickCommandlineBase(); |
||||||
1525 | $commandline = (null !== $commandline ? $commandline : ''); |
||||||
1526 | if ($commandline) { |
||||||
1527 | $commandline .= ' --version'; |
||||||
1528 | $this->DebugMessage('ImageMagick version checked with "'.$commandline.'"', __FILE__, __LINE__); |
||||||
1529 | $versionstring[1] = trim(phpthumb_functions::SafeExec($commandline)); |
||||||
1530 | if (preg_match('#^Version: [^\d]*([ 0-9\\.\\:Q/\\-]+)#i', $versionstring[1], $matches)) { |
||||||
1531 | $versionstring[0] = trim($matches[1]); |
||||||
1532 | } else { |
||||||
1533 | $versionstring[0] = false; |
||||||
1534 | $this->DebugMessage('ImageMagick did not return recognized version string ('.$versionstring[1].')', __FILE__, __LINE__); |
||||||
1535 | } |
||||||
1536 | $this->DebugMessage('ImageMagick convert --version says "'.@$matches[0].'"', __FILE__, __LINE__); |
||||||
1537 | } |
||||||
1538 | |||||||
1539 | @file_put_contents($IMversionCacheFilename, $versionstring[0]."\n".$versionstring[1]); |
||||||
1540 | @chmod($IMversionCacheFilename, $this->getParameter('config_file_create_mask')); |
||||||
1541 | |||||||
1542 | } |
||||||
1543 | } |
||||||
1544 | return $versionstring[ (int) $returnRAW ]; |
||||||
1545 | } |
||||||
1546 | |||||||
1547 | |||||||
1548 | public function ImageMagickSwitchAvailable($switchname) { |
||||||
1549 | static $IMoptions = null; |
||||||
1550 | if (null === $IMoptions) { |
||||||
1551 | $IMoptions = array(); |
||||||
1552 | $commandline = $this->ImageMagickCommandlineBase(); |
||||||
1553 | if (null !== $commandline) { |
||||||
1554 | $commandline .= ' -help'; |
||||||
1555 | $IMhelp_lines = explode("\n", phpthumb_functions::SafeExec($commandline)); |
||||||
1556 | foreach ($IMhelp_lines as $line) { |
||||||
1557 | if (preg_match('#^[\\+\\-]([a-z\\-]+) #', trim($line), $matches)) { |
||||||
1558 | $IMoptions[$matches[1]] = true; |
||||||
1559 | } |
||||||
1560 | } |
||||||
1561 | } |
||||||
1562 | } |
||||||
1563 | if (is_array($switchname)) { |
||||||
1564 | $allOK = true; |
||||||
1565 | foreach ($switchname as $key => $value) { |
||||||
1566 | if (!isset($IMoptions[$value])) { |
||||||
1567 | $allOK = false; |
||||||
1568 | break; |
||||||
1569 | } |
||||||
1570 | } |
||||||
1571 | $this->DebugMessage('ImageMagickSwitchAvailable('.implode(';', $switchname).') = '. (int) $allOK .'', __FILE__, __LINE__); |
||||||
1572 | } else { |
||||||
1573 | $allOK = isset($IMoptions[$switchname]); |
||||||
1574 | $this->DebugMessage('ImageMagickSwitchAvailable('.$switchname.') = '. (int) $allOK .'', __FILE__, __LINE__); |
||||||
1575 | } |
||||||
1576 | return $allOK; |
||||||
1577 | } |
||||||
1578 | |||||||
1579 | |||||||
1580 | public function ImageMagickFormatsList() { |
||||||
1581 | static $IMformatsList = null; |
||||||
1582 | if (null === $IMformatsList) { |
||||||
1583 | $IMformatsList = ''; |
||||||
1584 | $commandline = $this->ImageMagickCommandlineBase(); |
||||||
1585 | if (null !== $commandline) { |
||||||
1586 | $commandline = dirname($commandline).DIRECTORY_SEPARATOR.str_replace('convert', 'identify', basename($commandline)); |
||||||
1587 | $commandline .= ' -list format'; |
||||||
1588 | $IMformatsList = phpthumb_functions::SafeExec($commandline); |
||||||
1589 | } |
||||||
1590 | } |
||||||
1591 | return $IMformatsList; |
||||||
1592 | } |
||||||
1593 | |||||||
1594 | |||||||
1595 | public function SourceDataToTempFile() { |
||||||
1596 | if ($IMtempSourceFilename = $this->phpThumb_tempnam()) { |
||||||
1597 | $IMtempSourceFilename = $this->realPathSafe($IMtempSourceFilename); |
||||||
1598 | ob_start(); |
||||||
1599 | $fp_tempfile = fopen($IMtempSourceFilename, 'wb'); |
||||||
1600 | $tempfile_open_error = ob_get_contents(); |
||||||
1601 | ob_end_clean(); |
||||||
1602 | if ($fp_tempfile) { |
||||||
1603 | fwrite($fp_tempfile, $this->rawImageData); |
||||||
1604 | fclose($fp_tempfile); |
||||||
1605 | @chmod($IMtempSourceFilename, $this->getParameter('config_file_create_mask')); |
||||||
1606 | $this->sourceFilename = $IMtempSourceFilename; |
||||||
1607 | $this->DebugMessage('ImageMagickThumbnailToGD() setting $this->sourceFilename to "'.$IMtempSourceFilename.'" from $this->rawImageData ('.strlen($this->rawImageData).' bytes)', __FILE__, __LINE__); |
||||||
1608 | } else { |
||||||
1609 | $this->DebugMessage('ImageMagickThumbnailToGD() FAILED setting $this->sourceFilename to "'.$IMtempSourceFilename.'" (failed to open for writing: "'.$tempfile_open_error.'")', __FILE__, __LINE__); |
||||||
1610 | } |
||||||
1611 | unset($tempfile_open_error, $IMtempSourceFilename); |
||||||
1612 | return true; |
||||||
1613 | } |
||||||
1614 | $this->DebugMessage('SourceDataToTempFile() FAILED because $this->phpThumb_tempnam() failed', __FILE__, __LINE__); |
||||||
1615 | return false; |
||||||
1616 | } |
||||||
1617 | |||||||
1618 | |||||||
1619 | public function ImageMagickThumbnailToGD() { |
||||||
1620 | // http://www.imagemagick.org/script/command-line-options.php |
||||||
1621 | |||||||
1622 | $this->useRawIMoutput = true; |
||||||
1623 | if (phpthumb_functions::gd_version()) { |
||||||
1624 | // if GD is not available, must use whatever ImageMagick can output |
||||||
1625 | |||||||
1626 | // $CannotMagickParameters contains options that cannot be used with ImageMagick |
||||||
1627 | $CannotMagickParameters = array('ica'); |
||||||
1628 | foreach ($CannotMagickParameters as $parameter) { |
||||||
1629 | if (isset($this->$parameter)) { |
||||||
1630 | $this->DebugMessage('cannot process with ImageMagick because "'.$parameter.'" is set', __FILE__, __LINE__); |
||||||
1631 | $this->useRawIMoutput = false; |
||||||
1632 | return false; |
||||||
1633 | } |
||||||
1634 | } |
||||||
1635 | |||||||
1636 | // $UnAllowedParameters contains options that can only be processed in GD, not ImageMagick |
||||||
1637 | // note: 'fltr' *may* need to be processed by GD, but we'll check that in more detail below |
||||||
1638 | $UnAllowedParameters = array('xto', 'ar', 'bg', 'bc'); |
||||||
1639 | // 'ra' may be part of this list, if not a multiple of 90 degrees |
||||||
1640 | foreach ($UnAllowedParameters as $parameter) { |
||||||
1641 | if (isset($this->$parameter)) { |
||||||
1642 | $this->DebugMessage('$this->useRawIMoutput=false because "'.$parameter.'" is set', __FILE__, __LINE__); |
||||||
1643 | $this->useRawIMoutput = false; |
||||||
1644 | break; |
||||||
1645 | } |
||||||
1646 | } |
||||||
1647 | } |
||||||
1648 | $this->DebugMessage('$this->useRawIMoutput='.($this->useRawIMoutput ? 'true' : 'false').' after checking $UnAllowedParameters', __FILE__, __LINE__); |
||||||
1649 | $ImageCreateFunction = ''; |
||||||
1650 | $outputFormat = $this->thumbnailFormat; |
||||||
1651 | if (phpthumb_functions::gd_version()) { |
||||||
1652 | if ($this->useRawIMoutput) { |
||||||
1653 | switch ($this->thumbnailFormat) { |
||||||
1654 | case 'gif': |
||||||
1655 | $ImageCreateFunction = 'imagecreatefromgif'; |
||||||
1656 | $this->is_alpha = true; |
||||||
1657 | break; |
||||||
1658 | case 'png': |
||||||
1659 | $ImageCreateFunction = 'imagecreatefrompng'; |
||||||
1660 | $this->is_alpha = true; |
||||||
1661 | break; |
||||||
1662 | case 'jpg': |
||||||
1663 | case 'jpeg': |
||||||
1664 | $ImageCreateFunction = 'imagecreatefromjpeg'; |
||||||
1665 | break; |
||||||
1666 | case 'webp': |
||||||
1667 | $ImageCreateFunction = 'imagecreatefromwebp'; |
||||||
1668 | $this->is_alpha = true; |
||||||
1669 | break; |
||||||
1670 | default: |
||||||
1671 | $this->DebugMessage('Forcing output to PNG because $this->thumbnailFormat ('.$this->thumbnailFormat.' is not a GD-supported format)', __FILE__, __LINE__); |
||||||
1672 | $outputFormat = 'png'; |
||||||
1673 | $ImageCreateFunction = 'imagecreatefrompng'; |
||||||
1674 | $this->is_alpha = true; |
||||||
1675 | $this->useRawIMoutput = false; |
||||||
1676 | break; |
||||||
1677 | } |
||||||
1678 | if (!function_exists($ImageCreateFunction)) { |
||||||
1679 | // ImageMagickThumbnailToGD() depends on imagecreatefrompng/imagecreatefromgif |
||||||
1680 | //$this->DebugMessage('ImageMagickThumbnailToGD() aborting because '.@$ImageCreateFunction.'() is not available', __FILE__, __LINE__); |
||||||
1681 | $this->useRawIMoutput = true; |
||||||
1682 | //return false; |
||||||
1683 | } |
||||||
1684 | } else { |
||||||
1685 | $outputFormat = 'png'; |
||||||
1686 | $ImageCreateFunction = 'imagecreatefrompng'; |
||||||
1687 | $this->is_alpha = true; |
||||||
1688 | $this->useRawIMoutput = false; |
||||||
1689 | } |
||||||
1690 | } |
||||||
1691 | |||||||
1692 | // http://freealter.org/doc_distrib/ImageMagick-5.1.1/www/convert.html |
||||||
1693 | if (!$this->sourceFilename && $this->rawImageData) { |
||||||
1694 | $this->SourceDataToTempFile(); |
||||||
1695 | } |
||||||
1696 | if (!$this->sourceFilename) { |
||||||
1697 | $this->DebugMessage('ImageMagickThumbnailToGD() aborting because $this->sourceFilename is empty', __FILE__, __LINE__); |
||||||
1698 | $this->useRawIMoutput = false; |
||||||
1699 | return false; |
||||||
1700 | } |
||||||
1701 | if ($this->issafemode) { |
||||||
1702 | $this->DebugMessage('ImageMagickThumbnailToGD() aborting because safe_mode is enabled', __FILE__, __LINE__); |
||||||
1703 | $this->useRawIMoutput = false; |
||||||
1704 | return false; |
||||||
1705 | } |
||||||
1706 | // TO BE FIXED |
||||||
1707 | //if (true) { |
||||||
1708 | // $this->DebugMessage('ImageMagickThumbnailToGD() aborting it is broken right now', __FILE__, __LINE__); |
||||||
1709 | // $this->useRawIMoutput = false; |
||||||
1710 | // return false; |
||||||
1711 | //} |
||||||
1712 | |||||||
1713 | $commandline = $this->ImageMagickCommandlineBase(); |
||||||
1714 | if ($commandline) { |
||||||
1715 | $commandline .= ' '.phpthumb_functions::escapeshellarg_replacement(preg_replace('#[/\\\\]#', DIRECTORY_SEPARATOR, $this->sourceFilename).(($outputFormat == 'gif') ? '' : '['. (int) $this->sfn .']')); // [0] means first frame of (GIF) animation, can be ignored |
||||||
1716 | if ($IMtempfilename = $this->phpThumb_tempnam()) { |
||||||
1717 | $IMtempfilename = $this->realPathSafe($IMtempfilename); |
||||||
1718 | |||||||
1719 | $IMuseExplicitImageOutputDimensions = false; |
||||||
1720 | if ($this->ImageMagickSwitchAvailable('thumbnail') && $this->config_imagemagick_use_thumbnail) { |
||||||
1721 | $IMresizeParameter = 'thumbnail'; |
||||||
1722 | } else { |
||||||
1723 | $IMresizeParameter = 'resize'; |
||||||
1724 | |||||||
1725 | // some (older? around 2002) versions of IM won't accept "-resize 100x" but require "-resize 100x100" |
||||||
1726 | $commandline_test = $this->ImageMagickCommandlineBase().' logo: -resize 1x '.phpthumb_functions::escapeshellarg_replacement($IMtempfilename).' 2>&1'; |
||||||
1727 | $IMresult_test = phpthumb_functions::SafeExec($commandline_test); |
||||||
1728 | $IMuseExplicitImageOutputDimensions = preg_match('#image dimensions are zero#i', $IMresult_test); |
||||||
1729 | $this->DebugMessage('IMuseExplicitImageOutputDimensions = '. (int) $IMuseExplicitImageOutputDimensions, __FILE__, __LINE__); |
||||||
1730 | if ($fp_im_temp = @fopen($IMtempfilename, 'wb')) { |
||||||
1731 | // erase temp image so ImageMagick logo doesn't get output if other processing fails |
||||||
1732 | fclose($fp_im_temp); |
||||||
1733 | @chmod($IMtempfilename, $this->getParameter('config_file_create_mask')); |
||||||
1734 | } |
||||||
1735 | } |
||||||
1736 | |||||||
1737 | |||||||
1738 | ob_start(); |
||||||
1739 | $getimagesize = getimagesize($this->sourceFilename); |
||||||
1740 | $GetImageSizeError = ob_get_contents(); |
||||||
1741 | ob_end_clean(); |
||||||
1742 | if (is_array($getimagesize)) { |
||||||
1743 | $this->DebugMessage('getimagesize('.$this->sourceFilename.') SUCCEEDED: '.print_r($getimagesize, true), __FILE__, __LINE__); |
||||||
1744 | } else { |
||||||
1745 | $this->DebugMessage('getimagesize('.$this->sourceFilename.') FAILED with error "'.$GetImageSizeError.'"', __FILE__, __LINE__); |
||||||
1746 | } |
||||||
1747 | if (null !== $this->dpi && $this->ImageMagickSwitchAvailable('density')) { |
||||||
1748 | // for vector source formats only (WMF, PDF, etc) |
||||||
1749 | if (is_array($getimagesize) && isset($getimagesize[2]) && ($getimagesize[2] == IMAGETYPE_PNG)) { |
||||||
1750 | // explicitly exclude PNG from "-flatten" to make sure transparency is preserved |
||||||
1751 | // https://github.com/JamesHeinrich/phpThumb/issues/65 |
||||||
1752 | } else { |
||||||
1753 | $commandline .= ' -flatten'; |
||||||
1754 | $commandline .= ' -density '.phpthumb_functions::escapeshellarg_replacement($this->dpi); |
||||||
1755 | } |
||||||
1756 | } |
||||||
1757 | if (is_array($getimagesize)) { |
||||||
1758 | $this->DebugMessage('getimagesize('.$this->sourceFilename.') returned [w='.$getimagesize[0].';h='.$getimagesize[1].';f='.$getimagesize[2].']', __FILE__, __LINE__); |
||||||
1759 | $this->source_width = $getimagesize[0]; |
||||||
1760 | $this->source_height = $getimagesize[1]; |
||||||
1761 | $this->DebugMessage('source dimensions set to '.$this->source_width.'x'.$this->source_height, __FILE__, __LINE__); |
||||||
1762 | $this->SetOrientationDependantWidthHeight(); |
||||||
1763 | |||||||
1764 | if (!preg_match('#('.implode('|', $this->AlphaCapableFormats).')#i', $outputFormat)) { |
||||||
1765 | // not a transparency-capable format |
||||||
1766 | $commandline .= ' -background '.phpthumb_functions::escapeshellarg_replacement('#'.($this->bg ? $this->bg : 'FFFFFF')); |
||||||
1767 | if (!stristr($commandline, ' -flatten')) { |
||||||
1768 | $commandline .= ' -flatten'; |
||||||
1769 | } |
||||||
1770 | } else { |
||||||
1771 | if ($getimagesize[2] == IMAGETYPE_PNG && !$this->bg) { |
||||||
1772 | $commandline .= ' -background none'; |
||||||
1773 | } |
||||||
1774 | } |
||||||
1775 | if ($getimagesize[2] == IMAGETYPE_GIF) { |
||||||
1776 | $commandline .= ' -coalesce'; // may be needed for animated GIFs |
||||||
1777 | } |
||||||
1778 | if ($this->source_width || $this->source_height) { |
||||||
1779 | if ($this->zc) { |
||||||
1780 | |||||||
1781 | $borderThickness = 0; |
||||||
1782 | if (!empty($this->fltr)) { |
||||||
1783 | foreach ($this->fltr as $key => $value) { |
||||||
1784 | if (preg_match('#^bord\|([\d]+)#', $value, $matches)) { |
||||||
1785 | $borderThickness = $matches[1]; |
||||||
1786 | break; |
||||||
1787 | } |
||||||
1788 | } |
||||||
1789 | } |
||||||
1790 | $wAll = (int) max($this->w, $this->wp, $this->wl, $this->ws) - (2 * $borderThickness); |
||||||
1791 | $hAll = (int) max($this->h, $this->hp, $this->hl, $this->hs) - (2 * $borderThickness); |
||||||
1792 | $imAR = $this->source_width / $this->source_height; |
||||||
1793 | $zcAR = (($wAll && $hAll) ? $wAll / $hAll : 1); |
||||||
1794 | $side = phpthumb_functions::nonempty_min($this->source_width, $this->source_height, max($wAll, $hAll)); |
||||||
1795 | $sideX = phpthumb_functions::nonempty_min($this->source_width, $wAll, round($hAll * $zcAR)); |
||||||
1796 | $sideY = phpthumb_functions::nonempty_min( $this->source_height, $hAll, round($wAll / $zcAR)); |
||||||
1797 | |||||||
1798 | $thumbnailH = round(max($sideY, ($sideY * $zcAR) / $imAR)); |
||||||
1799 | if ($this->aoe == 1) { |
||||||
1800 | $commandline .= ' -'.$IMresizeParameter.' "'.$wAll.'x'.$hAll.'^"'; |
||||||
1801 | } else { |
||||||
1802 | $commandline .= ' -'.$IMresizeParameter.' '.phpthumb_functions::escapeshellarg_replacement(($IMuseExplicitImageOutputDimensions ? $thumbnailH : '').'x'.$thumbnailH); |
||||||
1803 | } |
||||||
1804 | |||||||
1805 | switch (strtoupper($this->zc)) { |
||||||
1806 | case 'T': |
||||||
1807 | $commandline .= ' -gravity north'; |
||||||
1808 | break; |
||||||
1809 | case 'B': |
||||||
1810 | $commandline .= ' -gravity south'; |
||||||
1811 | break; |
||||||
1812 | case 'L': |
||||||
1813 | $commandline .= ' -gravity west'; |
||||||
1814 | break; |
||||||
1815 | case 'R': |
||||||
1816 | $commandline .= ' -gravity east'; |
||||||
1817 | break; |
||||||
1818 | case 'TL': |
||||||
1819 | $commandline .= ' -gravity northwest'; |
||||||
1820 | break; |
||||||
1821 | case 'TR': |
||||||
1822 | $commandline .= ' -gravity northeast'; |
||||||
1823 | break; |
||||||
1824 | case 'BL': |
||||||
1825 | $commandline .= ' -gravity southwest'; |
||||||
1826 | break; |
||||||
1827 | case 'BR': |
||||||
1828 | $commandline .= ' -gravity southeast'; |
||||||
1829 | break; |
||||||
1830 | case '1': |
||||||
1831 | case 'C': |
||||||
1832 | default: |
||||||
1833 | $commandline .= ' -gravity center'; |
||||||
1834 | break; |
||||||
1835 | } |
||||||
1836 | |||||||
1837 | if (($wAll > 0) && ($hAll > 0)) { |
||||||
1838 | $commandline .= ' -crop '.phpthumb_functions::escapeshellarg_replacement($wAll.'x'.$hAll.'+0+0'); |
||||||
1839 | } else { |
||||||
1840 | $commandline .= ' -crop '.phpthumb_functions::escapeshellarg_replacement($side.'x'.$side.'+0+0'); |
||||||
1841 | } |
||||||
1842 | if ($this->ImageMagickSwitchAvailable('repage')) { |
||||||
1843 | $commandline .= ' +repage'; |
||||||
1844 | } else { |
||||||
1845 | $this->DebugMessage('Skipping "+repage" because ImageMagick (v'.$this->ImageMagickVersion().') does not support it', __FILE__, __LINE__); |
||||||
1846 | } |
||||||
1847 | |||||||
1848 | } elseif ($this->sw || $this->sh || $this->sx || $this->sy) { |
||||||
1849 | |||||||
1850 | $crop_param = ''; |
||||||
1851 | $crop_param .= ($this->sw ? (($this->sw < 2) ? round($this->sw * $this->source_width) : $this->sw) : $this->source_width); |
||||||
1852 | $crop_param .= 'x'.($this->sh ? (($this->sh < 2) ? round($this->sh * $this->source_height) : $this->sh) : $this->source_height); |
||||||
1853 | $crop_param .= '+'.(($this->sx < 2) ? round($this->sx * $this->source_width) : $this->sx); |
||||||
1854 | $crop_param .= '+'.(($this->sy < 2) ? round($this->sy * $this->source_height) : $this->sy); |
||||||
1855 | // TO BE FIXED |
||||||
1856 | // makes 1x1 output |
||||||
1857 | // http://trainspotted.com/phpThumb/phpThumb.php?src=/content/CNR/47/CNR-4728-LD-L-20110723-898.jpg&w=100&h=100&far=1&f=png&fltr[]=lvl&sx=0.05&sy=0.25&sw=0.92&sh=0.42 |
||||||
1858 | // '/usr/bin/convert' -density 150 -thumbnail 100x100 -contrast-stretch '0.1%' '/var/www/vhosts/trainspotted.com/httpdocs/content/CNR/47/CNR-4728-LD-L-20110723-898.jpg[0]' png:'/var/www/vhosts/trainspotted.com/httpdocs/phpThumb/_cache/pThumbIIUlvj' |
||||||
1859 | $commandline .= ' -crop '.phpthumb_functions::escapeshellarg_replacement($crop_param); |
||||||
1860 | |||||||
1861 | // this is broken for aoe=1, but unsure how to fix. Send advice to [email protected] |
||||||
1862 | if ($this->w || $this->h) { |
||||||
1863 | //if ($this->ImageMagickSwitchAvailable('repage')) { |
||||||
1864 | if (false) { |
||||||
1865 | // TO BE FIXED |
||||||
1866 | // newer versions of ImageMagick require -repage <geometry> |
||||||
1867 | $commandline .= ' -repage'; |
||||||
1868 | } else { |
||||||
1869 | $this->DebugMessage('Skipping "-repage" because ImageMagick (v'.$this->ImageMagickVersion().') does not support it', __FILE__, __LINE__); |
||||||
1870 | } |
||||||
1871 | if ($IMuseExplicitImageOutputDimensions) { |
||||||
1872 | if ($this->w && !$this->h) { |
||||||
1873 | $this->h = ceil($this->w / ($this->source_width / $this->source_height)); |
||||||
1874 | } elseif ($this->h && !$this->w) { |
||||||
1875 | $this->w = ceil($this->h * ($this->source_width / $this->source_height)); |
||||||
1876 | } |
||||||
1877 | } |
||||||
1878 | $commandline .= ' -'.$IMresizeParameter.' '.phpthumb_functions::escapeshellarg_replacement($this->w.'x'.$this->h); |
||||||
1879 | } |
||||||
1880 | |||||||
1881 | } else { |
||||||
1882 | |||||||
1883 | if ($this->iar && ((int) $this->w > 0) && ((int) $this->h > 0)) { |
||||||
1884 | |||||||
1885 | list($nw, $nh) = phpthumb_functions::TranslateWHbyAngle($this->w, $this->h, $this->ra); |
||||||
1886 | $nw = ((round($nw) != 0) ? round($nw) : ''); |
||||||
1887 | $nh = ((round($nh) != 0) ? round($nh) : ''); |
||||||
1888 | $commandline .= ' -'.$IMresizeParameter.' '.phpthumb_functions::escapeshellarg_replacement($nw.'x'.$nh.'!'); |
||||||
1889 | |||||||
1890 | } elseif ($this->far && ((int) $this->w > 0) && ((int) $this->h > 0)) { |
||||||
1891 | |||||||
1892 | $commandline .= ' -'.$IMresizeParameter.' '.phpthumb_functions::escapeshellarg_replacement(phpthumb_functions::nonempty_min($this->w, $getimagesize[0]).'x'.phpthumb_functions::nonempty_min($this->h, $getimagesize[1])); |
||||||
1893 | $commandline .= ' -gravity center'; |
||||||
1894 | if ($this->bg) { |
||||||
1895 | $commandline .= ' -background ' . phpthumb_functions::escapeshellarg_replacement('#' . $this->bg); |
||||||
1896 | } else { |
||||||
1897 | $commandline .= ' -background none'; |
||||||
1898 | } |
||||||
1899 | $commandline .= ' -extent '.phpthumb_functions::escapeshellarg_replacement($this->w.'x'.$this->h); |
||||||
1900 | |||||||
1901 | } else { |
||||||
1902 | |||||||
1903 | $this->w = (($this->aoe && $this->w) ? $this->w : ($this->w ? phpthumb_functions::nonempty_min($this->w, $getimagesize[0]) : '')); |
||||||
1904 | $this->h = (($this->aoe && $this->h) ? $this->h : ($this->h ? phpthumb_functions::nonempty_min($this->h, $getimagesize[1]) : '')); |
||||||
1905 | if ($this->w || $this->h) { |
||||||
1906 | if ($IMuseExplicitImageOutputDimensions) { |
||||||
1907 | if ($this->w && !$this->h) { |
||||||
1908 | $this->h = ceil($this->w / ($this->source_width / $this->source_height)); |
||||||
1909 | } elseif ($this->h && !$this->w) { |
||||||
1910 | $this->w = ceil($this->h * ($this->source_width / $this->source_height)); |
||||||
1911 | } |
||||||
1912 | } |
||||||
1913 | list($nw, $nh) = phpthumb_functions::TranslateWHbyAngle($this->w, $this->h, $this->ra); |
||||||
1914 | $nw = ((round($nw) != 0) ? round($nw) : ''); |
||||||
1915 | $nh = ((round($nh) != 0) ? round($nh) : ''); |
||||||
1916 | $commandline .= ' -'.$IMresizeParameter.' '.phpthumb_functions::escapeshellarg_replacement($nw.'x'.$nh); |
||||||
1917 | } |
||||||
1918 | |||||||
1919 | } |
||||||
1920 | } |
||||||
1921 | } |
||||||
1922 | |||||||
1923 | } else { |
||||||
1924 | |||||||
1925 | $this->DebugMessage('getimagesize('.$this->sourceFilename.') failed', __FILE__, __LINE__); |
||||||
1926 | if ($this->w || $this->h) { |
||||||
1927 | $exactDimensionsBang = (($this->iar && ((int) $this->w > 0) && ((int) $this->h > 0)) ? '!' : ''); |
||||||
1928 | if ($IMuseExplicitImageOutputDimensions) { |
||||||
1929 | // unknown source aspect ratio, just put large number and hope IM figures it out |
||||||
1930 | $commandline .= ' -'.$IMresizeParameter.' '.phpthumb_functions::escapeshellarg_replacement(($this->w ? $this->w : '9999').'x'.($this->h ? $this->h : '9999').$exactDimensionsBang); |
||||||
1931 | } else { |
||||||
1932 | $commandline .= ' -'.$IMresizeParameter.' '.phpthumb_functions::escapeshellarg_replacement($this->w.'x'.$this->h.$exactDimensionsBang); |
||||||
1933 | } |
||||||
1934 | } |
||||||
1935 | |||||||
1936 | } |
||||||
1937 | |||||||
1938 | if ($this->ra) { |
||||||
1939 | $this->ra = (int) $this->ra; |
||||||
1940 | if ($this->ImageMagickSwitchAvailable('rotate')) { |
||||||
1941 | if (!preg_match('#('.implode('|', $this->AlphaCapableFormats).')#i', $outputFormat) || phpthumb_functions::version_compare_replacement($this->ImageMagickVersion(), '6.3.7', '>=')) { |
||||||
1942 | $this->DebugMessage('Using ImageMagick rotate', __FILE__, __LINE__); |
||||||
1943 | $commandline .= ' -rotate '.phpthumb_functions::escapeshellarg_replacement($this->ra); |
||||||
1944 | if (($this->ra % 90) != 0) { |
||||||
1945 | if (preg_match('#('.implode('|', $this->AlphaCapableFormats).')#i', $outputFormat)) { |
||||||
1946 | // alpha-capable format |
||||||
1947 | $commandline .= ' -background rgba(255,255,255,0)'; |
||||||
1948 | } else { |
||||||
1949 | $commandline .= ' -background '.phpthumb_functions::escapeshellarg_replacement('#'.($this->bg ? $this->bg : 'FFFFFF')); |
||||||
1950 | } |
||||||
1951 | } |
||||||
1952 | $this->ra = 0; |
||||||
1953 | } else { |
||||||
1954 | $this->DebugMessage('Not using ImageMagick rotate because alpha background buggy before v6.3.7', __FILE__, __LINE__); |
||||||
1955 | } |
||||||
1956 | } else { |
||||||
1957 | $this->DebugMessage('Not using ImageMagick rotate because not supported', __FILE__, __LINE__); |
||||||
1958 | } |
||||||
1959 | } |
||||||
1960 | |||||||
1961 | $successfullyProcessedFilters = array(); |
||||||
1962 | foreach ($this->fltr as $filterkey => $filtercommand) { |
||||||
1963 | @list($command, $parameter) = explode('|', $filtercommand, 2); |
||||||
1964 | switch ($command) { |
||||||
1965 | case 'brit': |
||||||
1966 | if ($this->ImageMagickSwitchAvailable('modulate')) { |
||||||
1967 | $commandline .= ' -modulate '.phpthumb_functions::escapeshellarg_replacement((100 + (int) $parameter).',100,100'); |
||||||
1968 | $successfullyProcessedFilters[] = $filterkey; |
||||||
1969 | } |
||||||
1970 | break; |
||||||
1971 | |||||||
1972 | case 'cont': |
||||||
1973 | if ($this->ImageMagickSwitchAvailable('contrast')) { |
||||||
1974 | $contDiv10 = round((int) $parameter / 10); |
||||||
1975 | if ($contDiv10 > 0) { |
||||||
1976 | $contDiv10 = min($contDiv10, 100); |
||||||
1977 | for ($i = 0; $i < $contDiv10; $i++) { |
||||||
1978 | $commandline .= ' -contrast'; // increase contrast by 10% |
||||||
1979 | } |
||||||
1980 | } elseif ($contDiv10 < 0) { |
||||||
1981 | $contDiv10 = max($contDiv10, -100); |
||||||
1982 | for ($i = $contDiv10; $i < 0; $i++) { |
||||||
1983 | $commandline .= ' +contrast'; // decrease contrast by 10% |
||||||
1984 | } |
||||||
1985 | } else { |
||||||
1986 | // do nothing |
||||||
1987 | } |
||||||
1988 | $successfullyProcessedFilters[] = $filterkey; |
||||||
1989 | } |
||||||
1990 | break; |
||||||
1991 | |||||||
1992 | case 'ds': |
||||||
1993 | if ($this->ImageMagickSwitchAvailable(array('colorspace', 'modulate'))) { |
||||||
1994 | if ($parameter == 100) { |
||||||
1995 | $commandline .= ' -colorspace GRAY'; |
||||||
1996 | $commandline .= ' -modulate 100,0,100'; |
||||||
1997 | } else { |
||||||
1998 | $commandline .= ' -modulate '.phpthumb_functions::escapeshellarg_replacement('100,'.(100 - (int) $parameter).',100'); |
||||||
1999 | } |
||||||
2000 | $successfullyProcessedFilters[] = $filterkey; |
||||||
2001 | } |
||||||
2002 | break; |
||||||
2003 | |||||||
2004 | case 'sat': |
||||||
2005 | if ($this->ImageMagickSwitchAvailable(array('colorspace', 'modulate'))) { |
||||||
2006 | if ($parameter == -100) { |
||||||
2007 | $commandline .= ' -colorspace GRAY'; |
||||||
2008 | $commandline .= ' -modulate 100,0,100'; |
||||||
2009 | } else { |
||||||
2010 | $commandline .= ' -modulate '.phpthumb_functions::escapeshellarg_replacement('100,'.(100 + (int) $parameter).',100'); |
||||||
2011 | } |
||||||
2012 | $successfullyProcessedFilters[] = $filterkey; |
||||||
2013 | } |
||||||
2014 | break; |
||||||
2015 | |||||||
2016 | case 'gray': |
||||||
2017 | if ($this->ImageMagickSwitchAvailable(array('colorspace', 'modulate'))) { |
||||||
2018 | $commandline .= ' -colorspace GRAY'; |
||||||
2019 | $commandline .= ' -modulate 100,0,100'; |
||||||
2020 | $successfullyProcessedFilters[] = $filterkey; |
||||||
2021 | } |
||||||
2022 | break; |
||||||
2023 | |||||||
2024 | case 'clr': |
||||||
2025 | if ($this->ImageMagickSwitchAvailable(array('fill', 'colorize'))) { |
||||||
2026 | @list($amount, $color) = explode('|', $parameter); |
||||||
2027 | $commandline .= ' -fill '.phpthumb_functions::escapeshellarg_replacement('#'.preg_replace('#[^0-9A-F]#i', '', $color)); |
||||||
2028 | $commandline .= ' -colorize '.phpthumb_functions::escapeshellarg_replacement(min(max((int) $amount, 0), 100)); |
||||||
2029 | $successfullyProcessedFilters[] = $filterkey; |
||||||
2030 | } |
||||||
2031 | break; |
||||||
2032 | |||||||
2033 | case 'sep': |
||||||
2034 | if ($this->ImageMagickSwitchAvailable('sepia-tone')) { |
||||||
2035 | @list($amount, $color) = explode('|', $parameter); |
||||||
2036 | $amount = ($amount ? $amount : 80); |
||||||
2037 | if (!$color) { |
||||||
2038 | $commandline .= ' -sepia-tone '.phpthumb_functions::escapeshellarg_replacement(min(max((int) $amount, 0), 100).'%'); |
||||||
2039 | $successfullyProcessedFilters[] = $filterkey; |
||||||
2040 | } |
||||||
2041 | } |
||||||
2042 | break; |
||||||
2043 | |||||||
2044 | case 'gam': |
||||||
2045 | @list($amount) = explode('|', $parameter); |
||||||
2046 | $amount = min(max((float) $amount, 0.001), 10); |
||||||
2047 | if (number_format($amount, 3) != '1.000') { |
||||||
2048 | if ($this->ImageMagickSwitchAvailable('gamma')) { |
||||||
2049 | $commandline .= ' -gamma '.phpthumb_functions::escapeshellarg_replacement($amount); |
||||||
2050 | $successfullyProcessedFilters[] = $filterkey; |
||||||
2051 | } |
||||||
2052 | } |
||||||
2053 | break; |
||||||
2054 | |||||||
2055 | case 'neg': |
||||||
2056 | if ($this->ImageMagickSwitchAvailable('negate')) { |
||||||
2057 | $commandline .= ' -negate'; |
||||||
2058 | $successfullyProcessedFilters[] = $filterkey; |
||||||
2059 | } |
||||||
2060 | break; |
||||||
2061 | |||||||
2062 | case 'th': |
||||||
2063 | @list($amount) = explode('|', $parameter); |
||||||
2064 | if ($this->ImageMagickSwitchAvailable(array('threshold', 'dither', 'monochrome'))) { |
||||||
2065 | $commandline .= ' -threshold '.phpthumb_functions::escapeshellarg_replacement(round(min(max((int) $amount, 0), 255) / 2.55).'%'); |
||||||
2066 | $commandline .= ' -dither'; |
||||||
2067 | $commandline .= ' -monochrome'; |
||||||
2068 | $successfullyProcessedFilters[] = $filterkey; |
||||||
2069 | } |
||||||
2070 | break; |
||||||
2071 | |||||||
2072 | case 'rcd': |
||||||
2073 | if ($this->ImageMagickSwitchAvailable(array('colors', 'dither'))) { |
||||||
2074 | @list($colors, $dither) = explode('|', $parameter); |
||||||
2075 | $colors = ($colors ? (int) $colors : 256); |
||||||
2076 | $dither = ((strlen($dither) > 0) ? (bool) $dither : true); |
||||||
2077 | $commandline .= ' -colors '.phpthumb_functions::escapeshellarg_replacement(max($colors, 8)); // ImageMagick will otherwise fail with "cannot quantize to fewer than 8 colors" |
||||||
2078 | $commandline .= ($dither ? ' -dither' : ' +dither'); |
||||||
2079 | $successfullyProcessedFilters[] = $filterkey; |
||||||
2080 | } |
||||||
2081 | break; |
||||||
2082 | |||||||
2083 | case 'flip': |
||||||
2084 | if ($this->ImageMagickSwitchAvailable(array('flip', 'flop'))) { |
||||||
2085 | if (strpos(strtolower($parameter), 'x') !== false) { |
||||||
2086 | $commandline .= ' -flop'; |
||||||
2087 | } |
||||||
2088 | if (strpos(strtolower($parameter), 'y') !== false) { |
||||||
2089 | $commandline .= ' -flip'; |
||||||
2090 | } |
||||||
2091 | $successfullyProcessedFilters[] = $filterkey; |
||||||
2092 | } |
||||||
2093 | break; |
||||||
2094 | |||||||
2095 | case 'edge': |
||||||
2096 | if ($this->ImageMagickSwitchAvailable('edge')) { |
||||||
2097 | $parameter = (!empty($parameter) ? $parameter : 2); |
||||||
2098 | $commandline .= ' -edge '.phpthumb_functions::escapeshellarg_replacement(!empty($parameter) ? (int) $parameter : 1); |
||||||
2099 | $successfullyProcessedFilters[] = $filterkey; |
||||||
2100 | } |
||||||
2101 | break; |
||||||
2102 | |||||||
2103 | case 'emb': |
||||||
2104 | if ($this->ImageMagickSwitchAvailable(array('emboss', 'negate'))) { |
||||||
2105 | $parameter = (!empty($parameter) ? $parameter : 2); |
||||||
2106 | $commandline .= ' -emboss '.phpthumb_functions::escapeshellarg_replacement((int) $parameter); |
||||||
2107 | if ($parameter < 2) { |
||||||
2108 | $commandline .= ' -negate'; // ImageMagick negates the image for some reason with '-emboss 1'; |
||||||
2109 | } |
||||||
2110 | $successfullyProcessedFilters[] = $filterkey; |
||||||
2111 | } |
||||||
2112 | break; |
||||||
2113 | |||||||
2114 | case 'lvl': |
||||||
2115 | @list($band, $method, $threshold) = explode('|', $parameter); |
||||||
2116 | $band = ($band ? preg_replace('#[^RGBA\\*]#', '', strtoupper($band)) : '*'); |
||||||
2117 | $method = ((strlen($method) > 0) ? (int) $method : 2); |
||||||
2118 | $threshold = ((strlen($threshold) > 0) ? min(max((float) $threshold, 0), 100) : 0.1); |
||||||
2119 | |||||||
2120 | $band = preg_replace('#[^RGBA\\*]#', '', strtoupper($band)); |
||||||
2121 | |||||||
2122 | if (($method > 1) && !$this->ImageMagickSwitchAvailable(array('channel', 'contrast-stretch'))) { |
||||||
2123 | // Because ImageMagick processing happens before PHP-GD filters, and because some |
||||||
2124 | // clipping is involved in the "lvl" filter, if "lvl" happens before "wb" then the |
||||||
2125 | // "wb" filter will have (almost) no effect. Therefore, if "wb" is enabled then |
||||||
2126 | // force the "lvl" filter to be processed by GD, not ImageMagick. |
||||||
2127 | foreach ($this->fltr as $fltr_key => $fltr_value) { |
||||||
2128 | list($fltr_cmd) = explode('|', $fltr_value); |
||||||
2129 | if ($fltr_cmd == 'wb') { |
||||||
2130 | $this->DebugMessage('Setting "lvl" filter method to "0" (from "'.$method.'") because white-balance filter also enabled', __FILE__, __LINE__); |
||||||
2131 | $method = 0; |
||||||
2132 | } |
||||||
2133 | } |
||||||
2134 | } |
||||||
2135 | |||||||
2136 | switch ($method) { |
||||||
2137 | case 0: // internal RGB |
||||||
2138 | case 1: // internal grayscale |
||||||
2139 | break; |
||||||
2140 | case 2: // ImageMagick "contrast-stretch" |
||||||
2141 | if ($this->ImageMagickSwitchAvailable('contrast-stretch')) { |
||||||
2142 | if ($band != '*') { |
||||||
2143 | $commandline .= ' -channel '.phpthumb_functions::escapeshellarg_replacement(strtoupper($band)); |
||||||
2144 | } |
||||||
2145 | $threshold = preg_replace('#[^0-9\\.]#', '', $threshold); // should be unneccesary, but just to be double-sure |
||||||
2146 | //$commandline .= ' -contrast-stretch '.phpthumb_functions::escapeshellarg_replacement($threshold.'%'); |
||||||
2147 | $commandline .= ' -contrast-stretch \''.$threshold.'%\''; |
||||||
2148 | if ($band != '*') { |
||||||
2149 | $commandline .= ' +channel'; |
||||||
2150 | } |
||||||
2151 | $successfullyProcessedFilters[] = $filterkey; |
||||||
2152 | } |
||||||
2153 | break; |
||||||
2154 | case 3: // ImageMagick "normalize" |
||||||
2155 | if ($this->ImageMagickSwitchAvailable('normalize')) { |
||||||
2156 | if ($band != '*') { |
||||||
2157 | $commandline .= ' -channel '.phpthumb_functions::escapeshellarg_replacement(strtoupper($band)); |
||||||
2158 | } |
||||||
2159 | $commandline .= ' -normalize'; |
||||||
2160 | if ($band != '*') { |
||||||
2161 | $commandline .= ' +channel'; |
||||||
2162 | } |
||||||
2163 | $successfullyProcessedFilters[] = $filterkey; |
||||||
2164 | } |
||||||
2165 | break; |
||||||
2166 | default: |
||||||
2167 | $this->DebugMessage('unsupported method ('.$method.') for "lvl" filter', __FILE__, __LINE__); |
||||||
2168 | break; |
||||||
2169 | } |
||||||
2170 | if (isset($this->fltr[$filterkey]) && ($method > 1)) { |
||||||
2171 | $this->fltr[$filterkey] = $command.'|'.$band.'|0|'.$threshold; |
||||||
2172 | $this->DebugMessage('filter "lvl" remapped from method "'.$method.'" to method "0" because ImageMagick support is missing', __FILE__, __LINE__); |
||||||
2173 | } |
||||||
2174 | break; |
||||||
2175 | |||||||
2176 | case 'wb': |
||||||
2177 | if ($this->ImageMagickSwitchAvailable(array('channel', 'contrast-stretch'))) { |
||||||
2178 | @list($threshold) = explode('|', $parameter); |
||||||
2179 | $threshold = (!empty($threshold) ? min(max((float) $threshold, 0), 100) : 0.1); |
||||||
2180 | $threshold = preg_replace('#[^0-9\\.]#', '', $threshold); // should be unneccesary, but just to be double-sure |
||||||
2181 | //$commandline .= ' -channel R -contrast-stretch '.phpthumb_functions::escapeshellarg_replacement($threshold.'%'); // doesn't work on Windows because most versions of PHP do not properly |
||||||
2182 | //$commandline .= ' -channel G -contrast-stretch '.phpthumb_functions::escapeshellarg_replacement($threshold.'%'); // escape special characters (such as %) and just replace them with spaces |
||||||
2183 | //$commandline .= ' -channel B -contrast-stretch '.phpthumb_functions::escapeshellarg_replacement($threshold.'%'); // https://bugs.php.net/bug.php?id=43261 |
||||||
2184 | $commandline .= ' -channel R -contrast-stretch \''.$threshold.'%\''; |
||||||
2185 | $commandline .= ' -channel G -contrast-stretch \''.$threshold.'%\''; |
||||||
2186 | $commandline .= ' -channel B -contrast-stretch \''.$threshold.'%\''; |
||||||
2187 | $commandline .= ' +channel'; |
||||||
2188 | $successfullyProcessedFilters[] = $filterkey; |
||||||
2189 | } |
||||||
2190 | break; |
||||||
2191 | |||||||
2192 | case 'blur': |
||||||
2193 | if ($this->ImageMagickSwitchAvailable('blur')) { |
||||||
2194 | @list($radius) = explode('|', $parameter); |
||||||
2195 | $radius = (!empty($radius) ? min(max((int) $radius, 0), 25) : 1); |
||||||
2196 | $commandline .= ' -blur '.phpthumb_functions::escapeshellarg_replacement($radius); |
||||||
2197 | $successfullyProcessedFilters[] = $filterkey; |
||||||
2198 | } |
||||||
2199 | break; |
||||||
2200 | |||||||
2201 | case 'gblr': |
||||||
2202 | @list($radius) = explode('|', $parameter); |
||||||
2203 | $radius = (!empty($radius) ? min(max((int) $radius, 0), 25) : 1); |
||||||
2204 | // "-gaussian" changed to "-gaussian-blur" sometime around 2009 |
||||||
2205 | if ($this->ImageMagickSwitchAvailable('gaussian-blur')) { |
||||||
2206 | $commandline .= ' -gaussian-blur '.phpthumb_functions::escapeshellarg_replacement($radius); |
||||||
2207 | $successfullyProcessedFilters[] = $filterkey; |
||||||
2208 | } elseif ($this->ImageMagickSwitchAvailable('gaussian')) { |
||||||
2209 | $commandline .= ' -gaussian '.phpthumb_functions::escapeshellarg_replacement($radius); |
||||||
2210 | $successfullyProcessedFilters[] = $filterkey; |
||||||
2211 | } |
||||||
2212 | break; |
||||||
2213 | |||||||
2214 | case 'usm': |
||||||
2215 | if ($this->ImageMagickSwitchAvailable('unsharp')) { |
||||||
2216 | @list($amount, $radius, $threshold) = explode('|', $parameter); |
||||||
2217 | $amount = ($amount ? min(max((int) $amount, 0), 255) : 80); |
||||||
2218 | $radius = ($radius ? min(max((int) $radius, 0), 10) : 0.5); |
||||||
2219 | $threshold = ('' !== $threshold ? min(max((int) $threshold, 0), 50) : 3); |
||||||
2220 | $commandline .= ' -unsharp '.phpthumb_functions::escapeshellarg_replacement(number_format(($radius * 2) - 1, 2, '.', '').'x1+'.number_format($amount / 100, 2, '.', '').'+'.number_format($threshold / 100, 2, '.', '')); |
||||||
2221 | $successfullyProcessedFilters[] = $filterkey; |
||||||
2222 | } |
||||||
2223 | break; |
||||||
2224 | |||||||
2225 | case 'bord': |
||||||
2226 | if ($this->ImageMagickSwitchAvailable(array('border', 'bordercolor', 'thumbnail', 'crop'))) { |
||||||
2227 | if (!$this->zc) { |
||||||
2228 | @list($width, $rX, $rY, $color) = explode('|', $parameter); |
||||||
2229 | $width = (int) $width; |
||||||
2230 | $rX = (int) $rX; |
||||||
2231 | $rY = (int) $rY; |
||||||
2232 | if ($width && !$rX && !$rY) { |
||||||
2233 | if (!phpthumb_functions::IsHexColor($color)) { |
||||||
2234 | $color = ((!empty($this->bc) && phpthumb_functions::IsHexColor($this->bc)) ? $this->bc : '000000'); |
||||||
2235 | } |
||||||
2236 | $commandline .= ' -border '.phpthumb_functions::escapeshellarg_replacement((int) $width); |
||||||
2237 | $commandline .= ' -bordercolor '.phpthumb_functions::escapeshellarg_replacement('#'.$color); |
||||||
2238 | |||||||
2239 | if (preg_match('# \\-crop "([\d]+)x([\d]+)\\+0\\+0" #', $commandline, $matches)) { |
||||||
2240 | $commandline = str_replace(' -crop "'.$matches[1].'x'.$matches[2].'+0+0" ', ' -crop '.phpthumb_functions::escapeshellarg_replacement(($matches[1] - (2 * $width)).'x'.($matches[2] - (2 * $width)).'+0+0').' ', $commandline); |
||||||
2241 | } elseif (preg_match('# \\-'.$IMresizeParameter.' "([0-9]+)x([0-9]+)" #', $commandline, $matches)) { |
||||||
2242 | $commandline = str_replace(' -'.$IMresizeParameter.' "'.$matches[1].'x'.$matches[2].'" ', ' -'.$IMresizeParameter.' '.phpthumb_functions::escapeshellarg_replacement(($matches[1] - (2 * $width)).'x'.($matches[2] - (2 * $width))).' ', $commandline); |
||||||
2243 | } |
||||||
2244 | $successfullyProcessedFilters[] = $filterkey; |
||||||
2245 | } |
||||||
2246 | } |
||||||
2247 | } |
||||||
2248 | break; |
||||||
2249 | |||||||
2250 | case 'crop': |
||||||
2251 | break; |
||||||
2252 | |||||||
2253 | case 'sblr': |
||||||
2254 | break; |
||||||
2255 | |||||||
2256 | case 'mean': |
||||||
2257 | break; |
||||||
2258 | |||||||
2259 | case 'smth': |
||||||
2260 | break; |
||||||
2261 | |||||||
2262 | case 'bvl': |
||||||
2263 | break; |
||||||
2264 | |||||||
2265 | case 'wmi': |
||||||
2266 | break; |
||||||
2267 | |||||||
2268 | case 'wmt': |
||||||
2269 | break; |
||||||
2270 | |||||||
2271 | case 'over': |
||||||
2272 | break; |
||||||
2273 | |||||||
2274 | case 'hist': |
||||||
2275 | break; |
||||||
2276 | |||||||
2277 | case 'fram': |
||||||
2278 | break; |
||||||
2279 | |||||||
2280 | case 'drop': |
||||||
2281 | break; |
||||||
2282 | |||||||
2283 | case 'mask': |
||||||
2284 | break; |
||||||
2285 | |||||||
2286 | case 'elip': |
||||||
2287 | break; |
||||||
2288 | |||||||
2289 | case 'ric': |
||||||
2290 | break; |
||||||
2291 | |||||||
2292 | case 'stc': |
||||||
2293 | break; |
||||||
2294 | |||||||
2295 | case 'size': |
||||||
2296 | break; |
||||||
2297 | |||||||
2298 | default: |
||||||
2299 | $this->DebugMessage('Unknown $this->fltr['.$filterkey.'] ('.$filtercommand.') -- deleting filter command', __FILE__, __LINE__); |
||||||
2300 | $successfullyProcessedFilters[] = $filterkey; |
||||||
2301 | break; |
||||||
2302 | } |
||||||
2303 | if (!isset($this->fltr[$filterkey])) { |
||||||
2304 | $this->DebugMessage('Processed $this->fltr['.$filterkey.'] ('.$filtercommand.') with ImageMagick', __FILE__, __LINE__); |
||||||
2305 | } else { |
||||||
2306 | $this->DebugMessage('Skipping $this->fltr['.$filterkey.'] ('.$filtercommand.') with ImageMagick', __FILE__, __LINE__); |
||||||
2307 | } |
||||||
2308 | } |
||||||
2309 | $this->DebugMessage('Remaining $this->fltr after ImageMagick: ('.$this->phpThumbDebugVarDump($this->fltr).')', __FILE__, __LINE__); |
||||||
2310 | if (count($this->fltr) > 0) { |
||||||
2311 | $this->useRawIMoutput = false; |
||||||
2312 | } |
||||||
2313 | |||||||
2314 | if (preg_match('#jpe?g#i', $outputFormat) && $this->q) { |
||||||
2315 | if ($this->ImageMagickSwitchAvailable(array('quality', 'interlace'))) { |
||||||
2316 | $commandline .= ' -quality '.phpthumb_functions::escapeshellarg_replacement($this->thumbnailQuality); |
||||||
2317 | if ($this->config_output_interlace) { |
||||||
2318 | // causes weird things with animated GIF... leave for JPEG only |
||||||
2319 | $commandline .= ' -interlace line '; // Use Line or Plane to create an interlaced PNG or GIF or progressive JPEG image |
||||||
2320 | } |
||||||
2321 | } |
||||||
2322 | } |
||||||
2323 | $commandline .= ' '.$outputFormat.':'.phpthumb_functions::escapeshellarg_replacement($IMtempfilename); |
||||||
2324 | if (!$this->iswindows) { |
||||||
2325 | $commandline .= ' 2>&1'; |
||||||
2326 | } |
||||||
2327 | $this->DebugMessage('ImageMagick called as ('.$commandline.')', __FILE__, __LINE__); |
||||||
2328 | $IMresult = phpthumb_functions::SafeExec($commandline); |
||||||
2329 | clearstatcache(); |
||||||
2330 | if (!@file_exists($IMtempfilename) || !@filesize($IMtempfilename)) { |
||||||
2331 | $this->FatalError('ImageMagick failed with message ('.trim($IMresult).')'); |
||||||
2332 | $this->DebugMessage('ImageMagick failed with message ('.trim($IMresult).')', __FILE__, __LINE__); |
||||||
2333 | if ($this->iswindows && !$IMresult) { |
||||||
2334 | $this->DebugMessage('Check to make sure that PHP has read+write permissions to "'.dirname($IMtempfilename).'"', __FILE__, __LINE__); |
||||||
2335 | } |
||||||
2336 | |||||||
2337 | } else { |
||||||
2338 | |||||||
2339 | foreach ($successfullyProcessedFilters as $dummy => $filterkey) { |
||||||
2340 | unset($this->fltr[$filterkey]); |
||||||
2341 | } |
||||||
2342 | $this->IMresizedData = file_get_contents($IMtempfilename); |
||||||
2343 | $getimagesize_imresized = @getimagesize($IMtempfilename); |
||||||
2344 | $this->DebugMessage('getimagesize('.$IMtempfilename.') returned [w='.$getimagesize_imresized[0].';h='.$getimagesize_imresized[1].';f='.$getimagesize_imresized[2].']', __FILE__, __LINE__); |
||||||
2345 | if (($this->config_max_source_pixels > 0) && (($getimagesize_imresized[0] * $getimagesize_imresized[1]) > $this->config_max_source_pixels)) { |
||||||
2346 | $this->DebugMessage('skipping ImageMagickThumbnailToGD::'.$ImageCreateFunction.'() because IM output is too large ('.$getimagesize_imresized[0].'x'.$getimagesize_imresized[0].' = '.($getimagesize_imresized[0] * $getimagesize_imresized[1]).' > '.$this->config_max_source_pixels.')', __FILE__, __LINE__); |
||||||
2347 | } elseif (function_exists(@$ImageCreateFunction) && ($this->gdimg_source = @$ImageCreateFunction($IMtempfilename))) { |
||||||
2348 | $this->source_width = imagesx($this->gdimg_source); |
||||||
2349 | $this->source_height = imagesy($this->gdimg_source); |
||||||
2350 | $this->DebugMessage('ImageMagickThumbnailToGD::'.$ImageCreateFunction.'() succeeded, $this->gdimg_source is now ('.$this->source_width.'x'.$this->source_height.')', __FILE__, __LINE__); |
||||||
2351 | $this->DebugMessage('ImageMagickThumbnailToGD() returning $this->IMresizedData ('.strlen($this->IMresizedData).' bytes)', __FILE__, __LINE__); |
||||||
2352 | } else { |
||||||
2353 | $this->useRawIMoutput = true; |
||||||
2354 | $this->DebugMessage('$this->useRawIMoutput set to TRUE because '.@$ImageCreateFunction.'('.$IMtempfilename.') failed', __FILE__, __LINE__); |
||||||
2355 | } |
||||||
2356 | if (file_exists($IMtempfilename)) { |
||||||
2357 | $this->DebugMessage('deleting "'.$IMtempfilename.'"', __FILE__, __LINE__); |
||||||
2358 | @unlink($IMtempfilename); |
||||||
2359 | } |
||||||
2360 | return true; |
||||||
2361 | |||||||
2362 | } |
||||||
2363 | if (file_exists($IMtempfilename)) { |
||||||
2364 | $this->DebugMessage('deleting "'.$IMtempfilename.'"', __FILE__, __LINE__); |
||||||
2365 | @unlink($IMtempfilename); |
||||||
2366 | } |
||||||
2367 | |||||||
2368 | } elseif ($this->issafemode) { |
||||||
2369 | $this->DebugMessage('ImageMagickThumbnailToGD() aborting because PHP safe_mode is enabled and phpThumb_tempnam() failed', __FILE__, __LINE__); |
||||||
2370 | $this->useRawIMoutput = false; |
||||||
2371 | } else { |
||||||
2372 | if (file_exists($IMtempfilename)) { |
||||||
2373 | $this->DebugMessage('deleting "'.$IMtempfilename.'"', __FILE__, __LINE__); |
||||||
2374 | @unlink($IMtempfilename); |
||||||
2375 | } |
||||||
2376 | $this->DebugMessage('ImageMagickThumbnailToGD() aborting, phpThumb_tempnam() failed', __FILE__, __LINE__); |
||||||
2377 | } |
||||||
2378 | } else { |
||||||
2379 | $this->DebugMessage('ImageMagickThumbnailToGD() aborting because ImageMagickCommandlineBase() failed', __FILE__, __LINE__); |
||||||
2380 | } |
||||||
2381 | $this->useRawIMoutput = false; |
||||||
2382 | return false; |
||||||
2383 | } |
||||||
2384 | |||||||
2385 | |||||||
2386 | public function Rotate() { |
||||||
2387 | if ($this->ra || $this->ar) { |
||||||
2388 | if (!function_exists('imagerotate')) { |
||||||
2389 | $this->DebugMessage('!function_exists(imagerotate)', __FILE__, __LINE__); |
||||||
2390 | return false; |
||||||
2391 | } |
||||||
2392 | if (!include_once __DIR__ .'/phpthumb.filters.php' ) { |
||||||
2393 | $this->DebugMessage('Error including "'. __DIR__ .'/phpthumb.filters.php" which is required for applying filters ('.implode(';', $this->fltr).')', __FILE__, __LINE__); |
||||||
2394 | return false; |
||||||
2395 | } |
||||||
2396 | |||||||
2397 | $this->config_background_hexcolor = ($this->bg ? $this->bg : $this->config_background_hexcolor); |
||||||
2398 | if (!phpthumb_functions::IsHexColor($this->config_background_hexcolor)) { |
||||||
2399 | return $this->ErrorImage('Invalid hex color string "'.$this->config_background_hexcolor.'" for parameter "bg"'); |
||||||
2400 | } |
||||||
2401 | |||||||
2402 | $rotate_angle = 0; |
||||||
2403 | if ($this->ra) { |
||||||
2404 | |||||||
2405 | $rotate_angle = (float) $this->ra; |
||||||
2406 | |||||||
2407 | } else { |
||||||
2408 | |||||||
2409 | if ($this->ar == 'x') { |
||||||
2410 | if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '4.2.0', '>=')) { |
||||||
2411 | if ($this->sourceFilename) { |
||||||
2412 | if (function_exists('exif_read_data')) { |
||||||
2413 | if ($exif_data = @exif_read_data($this->sourceFilename, 'IFD0')) { |
||||||
2414 | // http://sylvana.net/jpegcrop/exif_orientation.html |
||||||
2415 | switch (@$exif_data['Orientation']) { |
||||||
2416 | case 1: |
||||||
2417 | $rotate_angle = 0; |
||||||
2418 | break; |
||||||
2419 | case 3: |
||||||
2420 | $rotate_angle = 180; |
||||||
2421 | break; |
||||||
2422 | case 6: |
||||||
2423 | $rotate_angle = 270; |
||||||
2424 | break; |
||||||
2425 | case 8: |
||||||
2426 | $rotate_angle = 90; |
||||||
2427 | break; |
||||||
2428 | |||||||
2429 | default: |
||||||
2430 | $this->DebugMessage('EXIF auto-rotate failed because unknown $exif_data[Orientation] "'.@$exif_data['Orientation'].'"', __FILE__, __LINE__); |
||||||
2431 | return false; |
||||||
2432 | break; |
||||||
2433 | } |
||||||
2434 | $this->DebugMessage('EXIF auto-rotate set to '.$rotate_angle.' degrees ($exif_data[Orientation] = "'.@$exif_data['Orientation'].'")', __FILE__, __LINE__); |
||||||
2435 | } else { |
||||||
2436 | $this->DebugMessage('failed: exif_read_data('.$this->sourceFilename.')', __FILE__, __LINE__); |
||||||
2437 | return false; |
||||||
2438 | } |
||||||
2439 | } else { |
||||||
2440 | $this->DebugMessage('!function_exists(exif_read_data)', __FILE__, __LINE__); |
||||||
2441 | return false; |
||||||
2442 | } |
||||||
2443 | } else { |
||||||
2444 | $this->DebugMessage('Cannot auto-rotate from EXIF data because $this->sourceFilename is empty', __FILE__, __LINE__); |
||||||
2445 | return false; |
||||||
2446 | } |
||||||
2447 | } else { |
||||||
2448 | $this->DebugMessage('Cannot auto-rotate from EXIF data because PHP is less than v4.2.0 ('. PHP_VERSION .')', __FILE__, __LINE__); |
||||||
2449 | return false; |
||||||
2450 | } |
||||||
2451 | } elseif (($this->ar == 'l') && ($this->source_height > $this->source_width)) { |
||||||
2452 | $rotate_angle = 270; |
||||||
2453 | } elseif (($this->ar == 'L') && ($this->source_height > $this->source_width)) { |
||||||
2454 | $rotate_angle = 90; |
||||||
2455 | } elseif (($this->ar == 'p') && ($this->source_width > $this->source_height)) { |
||||||
2456 | $rotate_angle = 90; |
||||||
2457 | } elseif (($this->ar == 'P') && ($this->source_width > $this->source_height)) { |
||||||
2458 | $rotate_angle = 270; |
||||||
2459 | } |
||||||
2460 | |||||||
2461 | } |
||||||
2462 | if ($rotate_angle % 90) { |
||||||
2463 | $this->is_alpha = true; |
||||||
2464 | } |
||||||
2465 | phpthumb_filters::ImprovedImageRotate($this->gdimg_source, $rotate_angle, $this->config_background_hexcolor, $this->bg, $this); |
||||||
2466 | $this->source_width = imagesx($this->gdimg_source); |
||||||
2467 | $this->source_height = imagesy($this->gdimg_source); |
||||||
2468 | } |
||||||
2469 | return true; |
||||||
2470 | } |
||||||
2471 | |||||||
2472 | |||||||
2473 | public function FixedAspectRatio() { |
||||||
2474 | // optional fixed-dimension images (regardless of aspect ratio) |
||||||
2475 | |||||||
2476 | if (!$this->far) { |
||||||
2477 | // do nothing |
||||||
2478 | return true; |
||||||
2479 | } |
||||||
2480 | |||||||
2481 | if (!$this->w || !$this->h) { |
||||||
2482 | return false; |
||||||
2483 | } |
||||||
2484 | $this->thumbnail_width = $this->w; |
||||||
2485 | $this->thumbnail_height = $this->h; |
||||||
2486 | $this->is_alpha = true; |
||||||
2487 | if ($this->thumbnail_image_width >= $this->thumbnail_width) { |
||||||
2488 | |||||||
2489 | $aspectratio = $this->thumbnail_image_height / $this->thumbnail_image_width; |
||||||
2490 | if ($this->w) { |
||||||
2491 | $this->thumbnail_image_height = round($this->thumbnail_image_width * $aspectratio); |
||||||
2492 | $this->thumbnail_height = ($this->h ? $this->h : $this->thumbnail_image_height); |
||||||
2493 | } elseif ($this->thumbnail_image_height < $this->thumbnail_height) { |
||||||
2494 | $this->thumbnail_image_height = $this->thumbnail_height; |
||||||
2495 | $this->thumbnail_image_width = round($this->thumbnail_image_height / $aspectratio); |
||||||
2496 | } |
||||||
2497 | |||||||
2498 | } else { |
||||||
2499 | |||||||
2500 | $aspectratio = $this->thumbnail_image_width / $this->thumbnail_image_height; |
||||||
2501 | if ($this->h) { |
||||||
2502 | $this->thumbnail_image_width = round($this->thumbnail_image_height * $aspectratio); |
||||||
2503 | } elseif ($this->thumbnail_image_width < $this->thumbnail_width) { |
||||||
2504 | $this->thumbnail_image_width = $this->thumbnail_width; |
||||||
2505 | $this->thumbnail_image_height = round($this->thumbnail_image_width / $aspectratio); |
||||||
2506 | } |
||||||
2507 | |||||||
2508 | } |
||||||
2509 | return true; |
||||||
2510 | } |
||||||
2511 | |||||||
2512 | |||||||
2513 | public function OffsiteDomainIsAllowed($hostname, $allowed_domains) { |
||||||
2514 | static $domain_is_allowed = array(); |
||||||
2515 | $hostname = strtolower($hostname); |
||||||
2516 | if (!isset($domain_is_allowed[$hostname])) { |
||||||
2517 | $domain_is_allowed[$hostname] = false; |
||||||
2518 | foreach ($allowed_domains as $valid_domain) { |
||||||
2519 | $starpos = strpos($valid_domain, '*'); |
||||||
2520 | if ($starpos !== false) { |
||||||
2521 | $valid_domain = substr($valid_domain, $starpos + 1); |
||||||
2522 | if (preg_match('#'.preg_quote($valid_domain).'$#', $hostname)) { |
||||||
2523 | $domain_is_allowed[$hostname] = true; |
||||||
2524 | break; |
||||||
2525 | } |
||||||
2526 | } else { |
||||||
2527 | if (strtolower($valid_domain) === $hostname) { |
||||||
2528 | $domain_is_allowed[$hostname] = true; |
||||||
2529 | break; |
||||||
2530 | } |
||||||
2531 | } |
||||||
2532 | } |
||||||
2533 | } |
||||||
2534 | return $domain_is_allowed[$hostname]; |
||||||
2535 | } |
||||||
2536 | |||||||
2537 | |||||||
2538 | public function AntiOffsiteLinking() { |
||||||
2539 | // Optional anti-offsite hijacking of the thumbnail script |
||||||
2540 | $allow = true; |
||||||
2541 | if ($allow && $this->config_nooffsitelink_enabled && (@$_SERVER['HTTP_REFERER'] || $this->config_nooffsitelink_require_refer)) { |
||||||
2542 | $this->DebugMessage('AntiOffsiteLinking() checking $_SERVER[HTTP_REFERER] "'.@$_SERVER['HTTP_REFERER'].'"', __FILE__, __LINE__); |
||||||
2543 | foreach ($this->config_nooffsitelink_valid_domains as $key => $valid_domain) { |
||||||
2544 | // $_SERVER['HTTP_HOST'] contains the port number, so strip it out here to make default configuration work |
||||||
2545 | list($clean_domain) = explode(':', $valid_domain); |
||||||
2546 | $this->config_nooffsitelink_valid_domains[$key] = $clean_domain; |
||||||
2547 | } |
||||||
2548 | $parsed_url = phpthumb_functions::ParseURLbetter(@$_SERVER['HTTP_REFERER']); |
||||||
2549 | if (!$this->OffsiteDomainIsAllowed(@$parsed_url['host'], $this->config_nooffsitelink_valid_domains)) { |
||||||
2550 | $allow = false; |
||||||
2551 | //$this->DebugMessage('AntiOffsiteLinking() - "'.@$parsed_url['host'].'" is NOT in $this->config_nooffsitelink_valid_domains ('.implode(';', $this->config_nooffsitelink_valid_domains).')', __FILE__, __LINE__); |
||||||
2552 | $this->ErrorImage('AntiOffsiteLinking() - "'.@$parsed_url['host'].'" is NOT in $this->config_nooffsitelink_valid_domains ('.implode(';', $this->config_nooffsitelink_valid_domains).')'); |
||||||
2553 | } else { |
||||||
2554 | $this->DebugMessage('AntiOffsiteLinking() - "'.@$parsed_url['host'].'" is in $this->config_nooffsitelink_valid_domains ('.implode(';', $this->config_nooffsitelink_valid_domains).')', __FILE__, __LINE__); |
||||||
2555 | } |
||||||
2556 | } |
||||||
2557 | |||||||
2558 | if ($allow && $this->config_nohotlink_enabled && preg_match('#^(f|ht)tps?\://#i', $this->src)) { |
||||||
2559 | $parsed_url = phpthumb_functions::ParseURLbetter($this->src); |
||||||
2560 | //if (!phpthumb_functions::CaseInsensitiveInArray(@$parsed_url['host'], $this->config_nohotlink_valid_domains)) { |
||||||
2561 | if (!$this->OffsiteDomainIsAllowed(@$parsed_url['host'], $this->config_nohotlink_valid_domains)) { |
||||||
2562 | // This domain is not allowed |
||||||
2563 | $allow = false; |
||||||
2564 | $this->DebugMessage('AntiOffsiteLinking() - "'.$parsed_url['host'].'" is NOT in $this->config_nohotlink_valid_domains ('.implode(';', $this->config_nohotlink_valid_domains).')', __FILE__, __LINE__); |
||||||
2565 | } else { |
||||||
2566 | $this->DebugMessage('AntiOffsiteLinking() - "'.$parsed_url['host'].'" is in $this->config_nohotlink_valid_domains ('.implode(';', $this->config_nohotlink_valid_domains).')', __FILE__, __LINE__); |
||||||
2567 | } |
||||||
2568 | } |
||||||
2569 | |||||||
2570 | if ($allow) { |
||||||
2571 | $this->DebugMessage('AntiOffsiteLinking() says this is allowed', __FILE__, __LINE__); |
||||||
2572 | return true; |
||||||
2573 | } |
||||||
2574 | |||||||
2575 | if (!phpthumb_functions::IsHexColor($this->config_error_bgcolor)) { |
||||||
2576 | return $this->ErrorImage('Invalid hex color string "'.$this->config_error_bgcolor.'" for $this->config_error_bgcolor'); |
||||||
2577 | } |
||||||
2578 | if (!phpthumb_functions::IsHexColor($this->config_error_textcolor)) { |
||||||
2579 | return $this->ErrorImage('Invalid hex color string "'.$this->config_error_textcolor.'" for $this->config_error_textcolor'); |
||||||
2580 | } |
||||||
2581 | if ($this->config_nooffsitelink_erase_image) { |
||||||
2582 | |||||||
2583 | return $this->ErrorImage($this->config_nooffsitelink_text_message, $this->thumbnail_width, $this->thumbnail_height); |
||||||
2584 | |||||||
2585 | } else { |
||||||
2586 | |||||||
2587 | $this->config_nooffsitelink_watermark_src = $this->ResolveFilenameToAbsolute($this->config_nooffsitelink_watermark_src); |
||||||
2588 | if (is_file($this->config_nooffsitelink_watermark_src)) { |
||||||
2589 | |||||||
2590 | if (!include_once __DIR__ .'/phpthumb.filters.php' ) { |
||||||
2591 | $this->DebugMessage('Error including "'. __DIR__ .'/phpthumb.filters.php" which is required for applying watermark', __FILE__, __LINE__); |
||||||
2592 | return false; |
||||||
2593 | } |
||||||
2594 | $watermark_img = $this->ImageCreateFromStringReplacement(file_get_contents($this->config_nooffsitelink_watermark_src)); |
||||||
2595 | $phpthumbFilters = new phpthumb_filters(); |
||||||
2596 | $phpthumbFilters->phpThumbObject = &$this; |
||||||
2597 | $opacity = 50; |
||||||
2598 | $margin = 5; |
||||||
2599 | $phpthumbFilters->WatermarkOverlay($this->gdimg_output, $watermark_img, '*', $opacity, $margin); |
||||||
2600 | imagedestroy($watermark_img); |
||||||
2601 | unset($phpthumbFilters); |
||||||
2602 | |||||||
2603 | } else { |
||||||
2604 | |||||||
2605 | $nohotlink_text_array = explode("\n", wordwrap($this->config_nooffsitelink_text_message, floor($this->thumbnail_width / imagefontwidth($this->config_error_fontsize)), "\n")); |
||||||
2606 | $nohotlink_text_color = phpthumb_functions::ImageHexColorAllocate($this->gdimg_output, $this->config_error_textcolor); |
||||||
2607 | |||||||
2608 | $topoffset = round(($this->thumbnail_height - (count($nohotlink_text_array) * imagefontheight($this->config_error_fontsize))) / 2); |
||||||
2609 | |||||||
2610 | $rowcounter = 0; |
||||||
2611 | $this->DebugMessage('AntiOffsiteLinking() writing '.count($nohotlink_text_array).' lines of text "'.$this->config_nooffsitelink_text_message.'" (in #'.$this->config_error_textcolor.') on top of image', __FILE__, __LINE__); |
||||||
2612 | foreach ($nohotlink_text_array as $textline) { |
||||||
2613 | $leftoffset = max(0, round(($this->thumbnail_width - (strlen($textline) * imagefontwidth($this->config_error_fontsize))) / 2)); |
||||||
2614 | imagestring($this->gdimg_output, $this->config_error_fontsize, $leftoffset, $topoffset + ($rowcounter++ * imagefontheight($this->config_error_fontsize)), $textline, $nohotlink_text_color); |
||||||
2615 | } |
||||||
2616 | |||||||
2617 | } |
||||||
2618 | |||||||
2619 | } |
||||||
2620 | return true; |
||||||
2621 | } |
||||||
2622 | |||||||
2623 | |||||||
2624 | public function AlphaChannelFlatten() { |
||||||
2625 | if (!$this->is_alpha) { |
||||||
2626 | // image doesn't have alpha transparency, no need to flatten |
||||||
2627 | $this->DebugMessage('skipping AlphaChannelFlatten() because !$this->is_alpha', __FILE__, __LINE__); |
||||||
2628 | return false; |
||||||
2629 | } |
||||||
2630 | switch ($this->thumbnailFormat) { |
||||||
2631 | case 'png': |
||||||
2632 | case 'webp': |
||||||
2633 | case 'ico': |
||||||
2634 | // image has alpha transparency, but output as PNG, WEBP or ICO which can handle it |
||||||
2635 | $this->DebugMessage('skipping AlphaChannelFlatten() because ($this->thumbnailFormat == "'.$this->thumbnailFormat.'")', __FILE__, __LINE__); |
||||||
2636 | return false; |
||||||
2637 | break; |
||||||
2638 | |||||||
2639 | case 'gif': |
||||||
2640 | // image has alpha transparency, but output as GIF which can handle only single-color transparency |
||||||
2641 | $CurrentImageColorTransparent = imagecolortransparent($this->gdimg_output); |
||||||
2642 | if ($CurrentImageColorTransparent == -1) { |
||||||
2643 | // no transparent color defined |
||||||
2644 | |||||||
2645 | if (phpthumb_functions::gd_version() < 2.0) { |
||||||
2646 | $this->DebugMessage('AlphaChannelFlatten() failed because GD version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__); |
||||||
2647 | return false; |
||||||
2648 | } |
||||||
2649 | |||||||
2650 | if ($img_alpha_mixdown_dither = @imagecreatetruecolor(imagesx($this->gdimg_output), imagesy($this->gdimg_output))) { |
||||||
2651 | |||||||
2652 | $dither_color = array(); |
||||||
2653 | for ($i = 0; $i <= 255; $i++) { |
||||||
2654 | $dither_color[$i] = imagecolorallocate($img_alpha_mixdown_dither, $i, $i, $i); |
||||||
2655 | } |
||||||
2656 | |||||||
2657 | // scan through current truecolor image copy alpha channel to temp image as grayscale |
||||||
2658 | for ($x = 0; $x < $this->thumbnail_width; $x++) { |
||||||
2659 | for ($y = 0; $y < $this->thumbnail_height; $y++) { |
||||||
2660 | $PixelColor = phpthumb_functions::GetPixelColor($this->gdimg_output, $x, $y); |
||||||
2661 | imagesetpixel($img_alpha_mixdown_dither, $x, $y, $dither_color[ $PixelColor[ 'alpha'] * 2 ]); |
||||||
2662 | } |
||||||
2663 | } |
||||||
2664 | |||||||
2665 | // dither alpha channel grayscale version down to 2 colors |
||||||
2666 | imagetruecolortopalette($img_alpha_mixdown_dither, true, 2); |
||||||
2667 | |||||||
2668 | // reduce color palette to 256-1 colors (leave one palette position for transparent color) |
||||||
2669 | imagetruecolortopalette($this->gdimg_output, true, 255); |
||||||
2670 | |||||||
2671 | // allocate a new color for transparent color index |
||||||
2672 | $TransparentColor = imagecolorallocate($this->gdimg_output, 1, 254, 253); |
||||||
2673 | imagecolortransparent($this->gdimg_output, $TransparentColor); |
||||||
2674 | |||||||
2675 | // scan through alpha channel image and note pixels with >50% transparency |
||||||
2676 | for ($x = 0; $x < $this->thumbnail_width; $x++) { |
||||||
2677 | for ($y = 0; $y < $this->thumbnail_height; $y++) { |
||||||
2678 | $AlphaChannelPixel = phpthumb_functions::GetPixelColor($img_alpha_mixdown_dither, $x, $y); |
||||||
2679 | if ($AlphaChannelPixel['red'] > 127) { |
||||||
2680 | imagesetpixel($this->gdimg_output, $x, $y, $TransparentColor); |
||||||
2681 | } |
||||||
2682 | } |
||||||
2683 | } |
||||||
2684 | imagedestroy($img_alpha_mixdown_dither); |
||||||
2685 | |||||||
2686 | $this->DebugMessage('AlphaChannelFlatten() set image to 255+1 colors with transparency for GIF output', __FILE__, __LINE__); |
||||||
2687 | return true; |
||||||
2688 | |||||||
2689 | } else { |
||||||
2690 | $this->DebugMessage('AlphaChannelFlatten() failed imagecreate('.imagesx($this->gdimg_output).', '.imagesy($this->gdimg_output).')', __FILE__, __LINE__); |
||||||
2691 | return false; |
||||||
2692 | } |
||||||
2693 | |||||||
2694 | } else { |
||||||
2695 | // a single transparent color already defined, leave as-is |
||||||
2696 | $this->DebugMessage('skipping AlphaChannelFlatten() because ($this->thumbnailFormat == "'.$this->thumbnailFormat.'") and imagecolortransparent() returned "'.$CurrentImageColorTransparent.'"', __FILE__, __LINE__); |
||||||
2697 | return true; |
||||||
2698 | } |
||||||
2699 | break; |
||||||
2700 | } |
||||||
2701 | $this->DebugMessage('continuing AlphaChannelFlatten() for output format "'.$this->thumbnailFormat.'"', __FILE__, __LINE__); |
||||||
2702 | // image has alpha transparency, and is being output in a format that doesn't support it -- flatten |
||||||
2703 | if ($gdimg_flatten_temp = phpthumb_functions::ImageCreateFunction($this->thumbnail_width, $this->thumbnail_height)) { |
||||||
2704 | |||||||
2705 | $this->config_background_hexcolor = ($this->bg ? $this->bg : $this->config_background_hexcolor); |
||||||
2706 | if (!phpthumb_functions::IsHexColor($this->config_background_hexcolor)) { |
||||||
2707 | return $this->ErrorImage('Invalid hex color string "'.$this->config_background_hexcolor.'" for parameter "bg"'); |
||||||
2708 | } |
||||||
2709 | $background_color = phpthumb_functions::ImageHexColorAllocate($this->gdimg_output, $this->config_background_hexcolor); |
||||||
2710 | imagefilledrectangle($gdimg_flatten_temp, 0, 0, $this->thumbnail_width, $this->thumbnail_height, $background_color); |
||||||
2711 | imagecopy($gdimg_flatten_temp, $this->gdimg_output, 0, 0, 0, 0, $this->thumbnail_width, $this->thumbnail_height); |
||||||
2712 | |||||||
2713 | imagealphablending($this->gdimg_output, true); |
||||||
2714 | imagesavealpha($this->gdimg_output, false); |
||||||
2715 | imagecolortransparent($this->gdimg_output, -1); |
||||||
2716 | imagecopy($this->gdimg_output, $gdimg_flatten_temp, 0, 0, 0, 0, $this->thumbnail_width, $this->thumbnail_height); |
||||||
2717 | |||||||
2718 | imagedestroy($gdimg_flatten_temp); |
||||||
2719 | return true; |
||||||
2720 | |||||||
2721 | } else { |
||||||
2722 | $this->DebugMessage('ImageCreateFunction() failed', __FILE__, __LINE__); |
||||||
2723 | } |
||||||
2724 | return false; |
||||||
2725 | } |
||||||
2726 | |||||||
2727 | |||||||
2728 | public function ApplyFilters() { |
||||||
2729 | if ($this->fltr && is_array($this->fltr)) { |
||||||
2730 | if (!include_once __DIR__ .'/phpthumb.filters.php' ) { |
||||||
2731 | $this->DebugMessage('Error including "'. __DIR__ .'/phpthumb.filters.php" which is required for applying filters ('.implode(';', $this->fltr).')', __FILE__, __LINE__); |
||||||
2732 | return false; |
||||||
2733 | } |
||||||
2734 | $phpthumbFilters = new phpthumb_filters(); |
||||||
2735 | $phpthumbFilters->phpThumbObject = &$this; |
||||||
2736 | foreach ($this->fltr as $filtercommand) { |
||||||
2737 | @list($command, $parameter) = explode('|', $filtercommand, 2); |
||||||
2738 | $this->DebugMessage('Attempting to process filter command "'.$command.'('.$parameter.')"', __FILE__, __LINE__); |
||||||
2739 | switch ($command) { |
||||||
2740 | case 'brit': // Brightness |
||||||
2741 | $phpthumbFilters->Brightness($this->gdimg_output, $parameter); |
||||||
2742 | break; |
||||||
2743 | |||||||
2744 | case 'cont': // Contrast |
||||||
2745 | $phpthumbFilters->Contrast($this->gdimg_output, $parameter); |
||||||
2746 | break; |
||||||
2747 | |||||||
2748 | case 'ds': // Desaturation |
||||||
2749 | $phpthumbFilters->Desaturate($this->gdimg_output, $parameter, ''); |
||||||
2750 | break; |
||||||
2751 | |||||||
2752 | case 'sat': // Saturation |
||||||
2753 | $phpthumbFilters->Saturation($this->gdimg_output, $parameter, ''); |
||||||
2754 | break; |
||||||
2755 | |||||||
2756 | case 'gray': // Grayscale |
||||||
2757 | $phpthumbFilters->Grayscale($this->gdimg_output); |
||||||
2758 | break; |
||||||
2759 | |||||||
2760 | case 'clr': // Colorize |
||||||
2761 | if (phpthumb_functions::gd_version() < 2) { |
||||||
2762 | $this->DebugMessage('Skipping Colorize() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__); |
||||||
2763 | break; |
||||||
2764 | } |
||||||
2765 | @list($amount, $color) = explode('|', $parameter, 2); |
||||||
2766 | $phpthumbFilters->Colorize($this->gdimg_output, $amount, $color); |
||||||
2767 | break; |
||||||
2768 | |||||||
2769 | case 'sep': // Sepia |
||||||
2770 | if (phpthumb_functions::gd_version() < 2) { |
||||||
2771 | $this->DebugMessage('Skipping Sepia() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__); |
||||||
2772 | break; |
||||||
2773 | } |
||||||
2774 | @list($amount, $color) = explode('|', $parameter, 2); |
||||||
2775 | $phpthumbFilters->Sepia($this->gdimg_output, $amount, $color); |
||||||
2776 | break; |
||||||
2777 | |||||||
2778 | case 'gam': // Gamma correction |
||||||
2779 | $phpthumbFilters->Gamma($this->gdimg_output, $parameter); |
||||||
2780 | break; |
||||||
2781 | |||||||
2782 | case 'neg': // Negative colors |
||||||
2783 | $phpthumbFilters->Negative($this->gdimg_output); |
||||||
2784 | break; |
||||||
2785 | |||||||
2786 | case 'th': // Threshold |
||||||
2787 | $phpthumbFilters->Threshold($this->gdimg_output, $parameter); |
||||||
2788 | break; |
||||||
2789 | |||||||
2790 | case 'rcd': // ReduceColorDepth |
||||||
2791 | if (phpthumb_functions::gd_version() < 2) { |
||||||
2792 | $this->DebugMessage('Skipping ReduceColorDepth() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__); |
||||||
2793 | break; |
||||||
2794 | } |
||||||
2795 | @list($colors, $dither) = explode('|', $parameter, 2); |
||||||
2796 | $colors = ($colors ? (int) $colors : 256); |
||||||
2797 | $dither = ((strlen($dither) > 0) ? (bool) $dither : true); |
||||||
2798 | $phpthumbFilters->ReduceColorDepth($this->gdimg_output, $colors, $dither); |
||||||
2799 | break; |
||||||
2800 | |||||||
2801 | case 'flip': // Flip |
||||||
2802 | $phpthumbFilters->Flip($this->gdimg_output, strpos(strtolower($parameter), 'x') !== false, strpos(strtolower($parameter), 'y') !== false); |
||||||
2803 | break; |
||||||
2804 | |||||||
2805 | case 'edge': // EdgeDetect |
||||||
2806 | $phpthumbFilters->EdgeDetect($this->gdimg_output); |
||||||
2807 | break; |
||||||
2808 | |||||||
2809 | case 'emb': // Emboss |
||||||
2810 | $phpthumbFilters->Emboss($this->gdimg_output); |
||||||
2811 | break; |
||||||
2812 | |||||||
2813 | case 'bvl': // Bevel |
||||||
2814 | @list($width, $color1, $color2) = explode('|', $parameter, 3); |
||||||
2815 | $phpthumbFilters->Bevel($this->gdimg_output, $width, $color1, $color2); |
||||||
2816 | break; |
||||||
2817 | |||||||
2818 | case 'lvl': // autoLevels |
||||||
2819 | @list($band, $method, $threshold) = explode('|', $parameter, 3); |
||||||
2820 | $band = ($band ? preg_replace('#[^RGBA\\*]#', '', strtoupper($band)) : '*'); |
||||||
2821 | $method = ((strlen($method) > 0) ? (int) $method : 2); |
||||||
2822 | $threshold = ((strlen($threshold) > 0) ? (float) $threshold : 0.1); |
||||||
2823 | |||||||
2824 | $phpthumbFilters->HistogramStretch($this->gdimg_output, $band, $method, $threshold); |
||||||
2825 | break; |
||||||
2826 | |||||||
2827 | case 'wb': // WhiteBalance |
||||||
2828 | $phpthumbFilters->WhiteBalance($this->gdimg_output, $parameter); |
||||||
2829 | break; |
||||||
2830 | |||||||
2831 | case 'hist': // Histogram overlay |
||||||
2832 | if (phpthumb_functions::gd_version() < 2) { |
||||||
2833 | $this->DebugMessage('Skipping HistogramOverlay() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__); |
||||||
2834 | break; |
||||||
2835 | } |
||||||
2836 | @list($bands, $colors, $width, $height, $alignment, $opacity, $margin_x, $margin_y) = explode('|', $parameter, 8); |
||||||
2837 | $bands = ($bands ? $bands : '*'); |
||||||
2838 | $colors = ($colors ? $colors : ''); |
||||||
2839 | $width = ($width ? $width : 0.25); |
||||||
2840 | $height = ($height ? $height : 0.25); |
||||||
2841 | $alignment = ($alignment ? $alignment : 'BR'); |
||||||
2842 | $opacity = ($opacity ? $opacity : 50); |
||||||
2843 | $margin_x = ($margin_x ? $margin_x : 5); |
||||||
2844 | // $margin_y -- it wasn't forgotten, let the value always pass unchanged |
||||||
2845 | $phpthumbFilters->HistogramOverlay($this->gdimg_output, $bands, $colors, $width, $height, $alignment, $opacity, $margin_x, $margin_y); |
||||||
2846 | break; |
||||||
2847 | |||||||
2848 | case 'fram': // Frame |
||||||
2849 | @list($frame_width, $edge_width, $color_frame, $color1, $color2) = explode('|', $parameter, 5); |
||||||
2850 | $phpthumbFilters->Frame($this->gdimg_output, $frame_width, $edge_width, $color_frame, $color1, $color2); |
||||||
2851 | break; |
||||||
2852 | |||||||
2853 | case 'drop': // DropShadow |
||||||
2854 | if (phpthumb_functions::gd_version() < 2) { |
||||||
2855 | $this->DebugMessage('Skipping DropShadow() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__); |
||||||
2856 | return false; |
||||||
2857 | } |
||||||
2858 | $this->is_alpha = true; |
||||||
2859 | @list($distance, $width, $color, $angle, $fade) = explode('|', $parameter, 5); |
||||||
2860 | $phpthumbFilters->DropShadow($this->gdimg_output, $distance, $width, $color, $angle, $fade); |
||||||
2861 | break; |
||||||
2862 | |||||||
2863 | case 'mask': // Mask cropping |
||||||
2864 | if (phpthumb_functions::gd_version() < 2) { |
||||||
2865 | $this->DebugMessage('Skipping Mask() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__); |
||||||
2866 | return false; |
||||||
2867 | } |
||||||
2868 | @list($mask_filename, $invert) = explode('|', $parameter, 2); |
||||||
2869 | $mask_filename = $this->ResolveFilenameToAbsolute($mask_filename); |
||||||
2870 | if (@is_readable($mask_filename) && ($fp_mask = @fopen($mask_filename, 'rb'))) { |
||||||
2871 | $MaskImageData = ''; |
||||||
2872 | do { |
||||||
2873 | $buffer = fread($fp_mask, 8192); |
||||||
2874 | $MaskImageData .= $buffer; |
||||||
2875 | } while (strlen($buffer) > 0); |
||||||
2876 | fclose($fp_mask); |
||||||
2877 | if ($gdimg_mask = $this->ImageCreateFromStringReplacement($MaskImageData)) { |
||||||
2878 | if ($invert && phpthumb_functions::version_compare_replacement(PHP_VERSION, '5.0.0', '>=') && phpthumb_functions::gd_is_bundled()) { |
||||||
2879 | imagefilter($gdimg_mask, IMG_FILTER_NEGATE); |
||||||
2880 | } |
||||||
2881 | $this->is_alpha = true; |
||||||
2882 | $phpthumbFilters->ApplyMask($gdimg_mask, $this->gdimg_output); |
||||||
2883 | imagedestroy($gdimg_mask); |
||||||
2884 | } else { |
||||||
2885 | $this->DebugMessage('ImageCreateFromStringReplacement() failed for "'.$mask_filename.'"', __FILE__, __LINE__); |
||||||
2886 | } |
||||||
2887 | } else { |
||||||
2888 | $this->DebugMessage('Cannot open mask file "'.$mask_filename.'"', __FILE__, __LINE__); |
||||||
2889 | } |
||||||
2890 | break; |
||||||
2891 | |||||||
2892 | case 'elip': // Ellipse cropping |
||||||
2893 | if (phpthumb_functions::gd_version() < 2) { |
||||||
2894 | $this->DebugMessage('Skipping Ellipse() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__); |
||||||
2895 | return false; |
||||||
2896 | } |
||||||
2897 | $this->is_alpha = true; |
||||||
2898 | $phpthumbFilters->Ellipse($this->gdimg_output); |
||||||
2899 | break; |
||||||
2900 | |||||||
2901 | case 'ric': // RoundedImageCorners |
||||||
2902 | if (phpthumb_functions::gd_version() < 2) { |
||||||
2903 | $this->DebugMessage('Skipping RoundedImageCorners() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__); |
||||||
2904 | return false; |
||||||
2905 | } |
||||||
2906 | @list($radius_x, $radius_y) = explode('|', $parameter, 2); |
||||||
2907 | if (($radius_x < 1) || ($radius_y < 1)) { |
||||||
2908 | $this->DebugMessage('Skipping RoundedImageCorners('.$radius_x.', '.$radius_y.') because x/y radius is less than 1', __FILE__, __LINE__); |
||||||
2909 | break; |
||||||
2910 | } |
||||||
2911 | $this->is_alpha = true; |
||||||
2912 | $phpthumbFilters->RoundedImageCorners($this->gdimg_output, $radius_x, $radius_y); |
||||||
2913 | break; |
||||||
2914 | |||||||
2915 | case 'crop': // Crop |
||||||
2916 | @list($left, $right, $top, $bottom) = explode('|', $parameter, 4); |
||||||
2917 | $phpthumbFilters->Crop($this->gdimg_output, $left, $right, $top, $bottom); |
||||||
2918 | break; |
||||||
2919 | |||||||
2920 | case 'bord': // Border |
||||||
2921 | @list($border_width, $radius_x, $radius_y, $hexcolor_border) = explode('|', $parameter, 4); |
||||||
2922 | $this->is_alpha = true; |
||||||
2923 | $phpthumbFilters->ImageBorder($this->gdimg_output, $border_width, $radius_x, $radius_y, $hexcolor_border); |
||||||
2924 | break; |
||||||
2925 | |||||||
2926 | case 'over': // Overlay |
||||||
2927 | @list($filename, $underlay, $margin, $opacity) = explode('|', $parameter, 4); |
||||||
2928 | $underlay = (bool) ($underlay ? $underlay : false); |
||||||
2929 | $margin = ((strlen($margin) > 0) ? $margin : ($underlay ? 0.1 : 0.0)); |
||||||
2930 | $opacity = ((strlen($opacity) > 0) ? $opacity : 100); |
||||||
2931 | if (($margin > 0) && ($margin < 1)) { |
||||||
2932 | $margin = min(0.499, $margin); |
||||||
2933 | } elseif (($margin > -1) && ($margin < 0)) { |
||||||
2934 | $margin = max(-0.499, $margin); |
||||||
2935 | } |
||||||
2936 | |||||||
2937 | $filename = $this->ResolveFilenameToAbsolute($filename); |
||||||
2938 | if (@is_readable($filename) && ($fp_watermark = @fopen($filename, 'rb'))) { |
||||||
2939 | $WatermarkImageData = ''; |
||||||
2940 | do { |
||||||
2941 | $buffer = fread($fp_watermark, 8192); |
||||||
2942 | $WatermarkImageData .= $buffer; |
||||||
2943 | } while (strlen($buffer) > 0); |
||||||
2944 | fclose($fp_watermark); |
||||||
2945 | if ($img_watermark = $this->ImageCreateFromStringReplacement($WatermarkImageData)) { |
||||||
2946 | if (($margin > 0) && ($margin < 1)) { |
||||||
2947 | $resized_x = max(1, imagesx($this->gdimg_output) - round(2 * (imagesx($this->gdimg_output) * $margin))); |
||||||
2948 | $resized_y = max(1, imagesy($this->gdimg_output) - round(2 * (imagesy($this->gdimg_output) * $margin))); |
||||||
2949 | } else { |
||||||
2950 | $resized_x = max(1, imagesx($this->gdimg_output) - round(2 * $margin)); |
||||||
2951 | $resized_y = max(1, imagesy($this->gdimg_output) - round(2 * $margin)); |
||||||
2952 | } |
||||||
2953 | |||||||
2954 | if ($underlay) { |
||||||
2955 | |||||||
2956 | if ($img_watermark_resized = phpthumb_functions::ImageCreateFunction(imagesx($this->gdimg_output), imagesy($this->gdimg_output))) { |
||||||
2957 | imagealphablending($img_watermark_resized, false); |
||||||
2958 | imagesavealpha($img_watermark_resized, true); |
||||||
2959 | $this->ImageResizeFunction($img_watermark_resized, $img_watermark, 0, 0, 0, 0, imagesx($img_watermark_resized), imagesy($img_watermark_resized), imagesx($img_watermark), imagesy($img_watermark)); |
||||||
2960 | if ($img_source_resized = phpthumb_functions::ImageCreateFunction($resized_x, $resized_y)) { |
||||||
2961 | imagealphablending($img_source_resized, false); |
||||||
2962 | imagesavealpha($img_source_resized, true); |
||||||
2963 | $this->ImageResizeFunction($img_source_resized, $this->gdimg_output, 0, 0, 0, 0, imagesx($img_source_resized), imagesy($img_source_resized), imagesx($this->gdimg_output), imagesy($this->gdimg_output)); |
||||||
2964 | $phpthumbFilters->WatermarkOverlay($img_watermark_resized, $img_source_resized, 'C', $opacity, $margin); |
||||||
2965 | imagecopy($this->gdimg_output, $img_watermark_resized, 0, 0, 0, 0, imagesx($this->gdimg_output), imagesy($this->gdimg_output)); |
||||||
2966 | } else { |
||||||
2967 | $this->DebugMessage('phpthumb_functions::ImageCreateFunction('.$resized_x.', '.$resized_y.')', __FILE__, __LINE__); |
||||||
2968 | } |
||||||
2969 | imagedestroy($img_watermark_resized); |
||||||
2970 | } else { |
||||||
2971 | $this->DebugMessage('phpthumb_functions::ImageCreateFunction('.imagesx($this->gdimg_output).', '.imagesy($this->gdimg_output).')', __FILE__, __LINE__); |
||||||
2972 | } |
||||||
2973 | |||||||
2974 | } else { // overlay |
||||||
2975 | |||||||
2976 | if ($img_watermark_resized = phpthumb_functions::ImageCreateFunction($resized_x, $resized_y)) { |
||||||
2977 | imagealphablending($img_watermark_resized, false); |
||||||
2978 | imagesavealpha($img_watermark_resized, true); |
||||||
2979 | $this->ImageResizeFunction($img_watermark_resized, $img_watermark, 0, 0, 0, 0, imagesx($img_watermark_resized), imagesy($img_watermark_resized), imagesx($img_watermark), imagesy($img_watermark)); |
||||||
2980 | $phpthumbFilters->WatermarkOverlay($this->gdimg_output, $img_watermark_resized, 'C', $opacity, $margin); |
||||||
2981 | imagedestroy($img_watermark_resized); |
||||||
2982 | } else { |
||||||
2983 | $this->DebugMessage('phpthumb_functions::ImageCreateFunction('.$resized_x.', '.$resized_y.')', __FILE__, __LINE__); |
||||||
2984 | } |
||||||
2985 | |||||||
2986 | } |
||||||
2987 | imagedestroy($img_watermark); |
||||||
2988 | |||||||
2989 | } else { |
||||||
2990 | $this->DebugMessage('ImageCreateFromStringReplacement() failed for "'.$filename.'"', __FILE__, __LINE__); |
||||||
2991 | } |
||||||
2992 | } else { |
||||||
2993 | $this->DebugMessage('Cannot open overlay file "'.$filename.'"', __FILE__, __LINE__); |
||||||
2994 | } |
||||||
2995 | break; |
||||||
2996 | |||||||
2997 | case 'wmi': // WaterMarkImage |
||||||
2998 | @list($filename, $alignment, $opacity, $margin['x'], $margin['y'], $rotate_angle) = explode('|', $parameter, 6); |
||||||
2999 | // $margin can be pixel margin or percent margin if $alignment is text, or max width/height if $alignment is position like "50x75" |
||||||
3000 | $alignment = ($alignment ? $alignment : 'BR'); |
||||||
3001 | $opacity = ('' != $opacity ? (int) $opacity : 50); |
||||||
3002 | $rotate_angle = ('' != $rotate_angle ? (int) $rotate_angle : 0); |
||||||
3003 | if (!preg_match('#^([0-9\\.\\-]*)x([0-9\\.\\-]*)$#i', $alignment, $matches)) { |
||||||
3004 | $margins = array('x', 'y'); |
||||||
3005 | foreach ($margins as $xy) { |
||||||
3006 | $margin[$xy] = ('' !== $margin[ $xy ] ? $margin[ $xy] : 5); |
||||||
3007 | if (($margin[$xy] > 0) && ($margin[$xy] < 1)) { |
||||||
3008 | $margin[$xy] = min(0.499, $margin[$xy]); |
||||||
3009 | } elseif (($margin[$xy] > -1) && ($margin[$xy] < 0)) { |
||||||
3010 | $margin[$xy] = max(-0.499, $margin[$xy]); |
||||||
3011 | } |
||||||
3012 | } |
||||||
3013 | } |
||||||
3014 | |||||||
3015 | $filename = $this->ResolveFilenameToAbsolute($filename); |
||||||
3016 | if (@is_readable($filename)) { |
||||||
3017 | if ($img_watermark = $this->ImageCreateFromFilename($filename)) { |
||||||
3018 | if ($rotate_angle !== 0) { |
||||||
3019 | $phpthumbFilters->ImprovedImageRotate($img_watermark, $rotate_angle, 'FFFFFF', null, $this); |
||||||
3020 | } |
||||||
3021 | if (preg_match('#^([0-9\\.\\-]*)x([0-9\\.\\-]*)$#i', $alignment, $matches)) { |
||||||
3022 | $watermark_max_width = (int) ($margin[ 'x'] ? $margin[ 'x'] : imagesx($img_watermark)); |
||||||
3023 | $watermark_max_height = (int) ($margin[ 'y'] ? $margin[ 'y'] : imagesy($img_watermark)); |
||||||
3024 | $scale = phpthumb_functions::ScaleToFitInBox(imagesx($img_watermark), imagesy($img_watermark), $watermark_max_width, $watermark_max_height, true, true); |
||||||
3025 | $this->DebugMessage('Scaling watermark by a factor of '.number_format($scale, 4), __FILE__, __LINE__); |
||||||
3026 | if (($scale > 1) || ($scale < 1)) { |
||||||
3027 | if ($img_watermark2 = phpthumb_functions::ImageCreateFunction($scale * imagesx($img_watermark), $scale * imagesy($img_watermark))) { |
||||||
3028 | imagealphablending($img_watermark2, false); |
||||||
3029 | imagesavealpha($img_watermark2, true); |
||||||
3030 | $this->ImageResizeFunction($img_watermark2, $img_watermark, 0, 0, 0, 0, imagesx($img_watermark2), imagesy($img_watermark2), imagesx($img_watermark), imagesy($img_watermark)); |
||||||
3031 | $img_watermark = $img_watermark2; |
||||||
3032 | } else { |
||||||
3033 | $this->DebugMessage('ImageCreateFunction('.($scale * imagesx($img_watermark)).', '.($scale * imagesx($img_watermark)).') failed', __FILE__, __LINE__); |
||||||
3034 | } |
||||||
3035 | } |
||||||
3036 | $watermark_dest_x = round($matches[1] - (imagesx($img_watermark) / 2)); |
||||||
3037 | $watermark_dest_y = round($matches[2] - (imagesy($img_watermark) / 2)); |
||||||
3038 | $alignment = $watermark_dest_x.'x'.$watermark_dest_y; |
||||||
3039 | } |
||||||
3040 | $phpthumbFilters->WatermarkOverlay($this->gdimg_output, $img_watermark, $alignment, $opacity, $margin['x'], $margin['y']); |
||||||
3041 | imagedestroy($img_watermark); |
||||||
3042 | if (isset($img_watermark2) && is_resource($img_watermark2)) { |
||||||
3043 | imagedestroy($img_watermark2); |
||||||
3044 | } |
||||||
3045 | } else { |
||||||
3046 | $this->DebugMessage('ImageCreateFromFilename() failed for "'.$filename.'"', __FILE__, __LINE__); |
||||||
3047 | } |
||||||
3048 | } else { |
||||||
3049 | $this->DebugMessage('!is_readable('.$filename.')', __FILE__, __LINE__); |
||||||
3050 | } |
||||||
3051 | break; |
||||||
3052 | |||||||
3053 | case 'wmt': // WaterMarkText |
||||||
3054 | @list($text, $size, $alignment, $hex_color, $ttffont, $opacity, $margin, $angle, $bg_color, $bg_opacity, $fillextend, $lineheight) = explode('|', $parameter, 12); |
||||||
3055 | $text = ($text ? $text : ''); |
||||||
3056 | $size = ($size ? $size : 3); |
||||||
3057 | $alignment = ($alignment ? $alignment : 'BR'); |
||||||
3058 | $hex_color = ($hex_color ? $hex_color : '000000'); |
||||||
3059 | $ttffont = ($ttffont ? $ttffont : ''); |
||||||
3060 | $opacity = ('' != $opacity ? $opacity : 50); |
||||||
3061 | $margin = ('' != $margin ? $margin : 5); |
||||||
3062 | $angle = ('' != $angle ? $angle : 0); |
||||||
3063 | $bg_color = ($bg_color ? $bg_color : false); |
||||||
3064 | $bg_opacity = ($bg_opacity ? $bg_opacity : 0); |
||||||
3065 | $fillextend = ($fillextend ? $fillextend : ''); |
||||||
3066 | $lineheight = ($lineheight ? $lineheight : 1.0); |
||||||
3067 | |||||||
3068 | if (basename($ttffont) == $ttffont) { |
||||||
3069 | $ttffont = $this->realPathSafe($this->config_ttf_directory.DIRECTORY_SEPARATOR.$ttffont); |
||||||
3070 | } else { |
||||||
3071 | $ttffont = $this->ResolveFilenameToAbsolute($ttffont); |
||||||
3072 | } |
||||||
3073 | $phpthumbFilters->WatermarkText($this->gdimg_output, $text, $size, $alignment, $hex_color, $ttffont, $opacity, $margin, $angle, $bg_color, $bg_opacity, $fillextend, $lineheight); |
||||||
3074 | break; |
||||||
3075 | |||||||
3076 | case 'blur': // Blur |
||||||
3077 | @list($radius) = explode('|', $parameter, 1); |
||||||
3078 | $radius = ($radius ? $radius : 1); |
||||||
3079 | if (phpthumb_functions::gd_version() >= 2) { |
||||||
3080 | $phpthumbFilters->Blur($this->gdimg_output, $radius); |
||||||
3081 | } else { |
||||||
3082 | $this->DebugMessage('Skipping Blur() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__); |
||||||
3083 | } |
||||||
3084 | break; |
||||||
3085 | |||||||
3086 | case 'gblr': // Gaussian Blur |
||||||
3087 | $phpthumbFilters->BlurGaussian($this->gdimg_output); |
||||||
3088 | break; |
||||||
3089 | |||||||
3090 | case 'sblr': // Selective Blur |
||||||
3091 | $phpthumbFilters->BlurSelective($this->gdimg_output); |
||||||
3092 | break; |
||||||
3093 | |||||||
3094 | case 'mean': // MeanRemoval blur |
||||||
3095 | $phpthumbFilters->MeanRemoval($this->gdimg_output); |
||||||
3096 | break; |
||||||
3097 | |||||||
3098 | case 'smth': // Smooth blur |
||||||
3099 | $phpthumbFilters->Smooth($this->gdimg_output, $parameter); |
||||||
3100 | break; |
||||||
3101 | |||||||
3102 | case 'usm': // UnSharpMask sharpening |
||||||
3103 | @list($amount, $radius, $threshold) = explode('|', $parameter, 3); |
||||||
3104 | $amount = ($amount ? $amount : 80); |
||||||
3105 | $radius = ($radius ? $radius : 0.5); |
||||||
3106 | $threshold = ('' !== $threshold ? $threshold : 3); |
||||||
3107 | if (phpthumb_functions::gd_version() >= 2.0) { |
||||||
3108 | ob_start(); |
||||||
3109 | if (!@include_once __DIR__ .'/phpthumb.unsharp.php' ) { |
||||||
3110 | $include_error = ob_get_contents(); |
||||||
3111 | if ($include_error) { |
||||||
3112 | $this->DebugMessage('include_once("'. __DIR__ .'/phpthumb.unsharp.php") generated message: "'.$include_error.'"', __FILE__, __LINE__); |
||||||
3113 | } |
||||||
3114 | $this->DebugMessage('Error including "'. __DIR__ .'/phpthumb.unsharp.php" which is required for unsharp masking', __FILE__, __LINE__); |
||||||
3115 | ob_end_clean(); |
||||||
3116 | return false; |
||||||
3117 | } |
||||||
3118 | ob_end_clean(); |
||||||
3119 | phpUnsharpMask::applyUnsharpMask($this->gdimg_output, $amount, $radius, $threshold); |
||||||
3120 | } else { |
||||||
3121 | $this->DebugMessage('Skipping unsharp mask because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__); |
||||||
3122 | return false; |
||||||
3123 | } |
||||||
3124 | break; |
||||||
3125 | |||||||
3126 | case 'size': // Resize |
||||||
3127 | @list($newwidth, $newheight, $stretch) = explode('|', $parameter); |
||||||
3128 | $newwidth = (!$newwidth ? imagesx($this->gdimg_output) : ((($newwidth > 0) && ($newwidth < 1)) ? round($newwidth * imagesx($this->gdimg_output)) : round($newwidth))); |
||||||
3129 | $newheight = (!$newheight ? imagesy($this->gdimg_output) : ((($newheight > 0) && ($newheight < 1)) ? round($newheight * imagesy($this->gdimg_output)) : round($newheight))); |
||||||
3130 | $stretch = ($stretch ? true : false); |
||||||
3131 | if ($stretch) { |
||||||
3132 | $scale_x = phpthumb_functions::ScaleToFitInBox(imagesx($this->gdimg_output), imagesx($this->gdimg_output), $newwidth, $newwidth, true, true); |
||||||
3133 | $scale_y = phpthumb_functions::ScaleToFitInBox(imagesy($this->gdimg_output), imagesy($this->gdimg_output), $newheight, $newheight, true, true); |
||||||
3134 | } else { |
||||||
3135 | $scale_x = phpthumb_functions::ScaleToFitInBox(imagesx($this->gdimg_output), imagesy($this->gdimg_output), $newwidth, $newheight, true, true); |
||||||
3136 | $scale_y = $scale_x; |
||||||
3137 | } |
||||||
3138 | $this->DebugMessage('Scaling watermark ('.($stretch ? 'with' : 'without').' stretch) by a factor of "'.number_format($scale_x, 4).' x '.number_format($scale_y, 4).'"', __FILE__, __LINE__); |
||||||
3139 | if (($scale_x > 1) || ($scale_x < 1) || ($scale_y > 1) || ($scale_y < 1)) { |
||||||
3140 | if ($img_temp = phpthumb_functions::ImageCreateFunction(imagesx($this->gdimg_output), imagesy($this->gdimg_output))) { |
||||||
3141 | imagecopy($img_temp, $this->gdimg_output, 0, 0, 0, 0, imagesx($this->gdimg_output), imagesy($this->gdimg_output)); |
||||||
3142 | if ($this->gdimg_output = phpthumb_functions::ImageCreateFunction($scale_x * imagesx($img_temp), $scale_y * imagesy($img_temp))) { |
||||||
3143 | imagealphablending($this->gdimg_output, false); |
||||||
3144 | imagesavealpha($this->gdimg_output, true); |
||||||
3145 | $this->ImageResizeFunction($this->gdimg_output, $img_temp, 0, 0, 0, 0, imagesx($this->gdimg_output), imagesy($this->gdimg_output), imagesx($img_temp), imagesy($img_temp)); |
||||||
3146 | } else { |
||||||
3147 | $this->DebugMessage('ImageCreateFunction('.($scale_x * imagesx($img_temp)).', '.($scale_y * imagesy($img_temp)).') failed', __FILE__, __LINE__); |
||||||
3148 | } |
||||||
3149 | imagedestroy($img_temp); |
||||||
3150 | } else { |
||||||
3151 | $this->DebugMessage('ImageCreateFunction('.imagesx($this->gdimg_output).', '.imagesy($this->gdimg_output).') failed', __FILE__, __LINE__); |
||||||
3152 | } |
||||||
3153 | } |
||||||
3154 | break; |
||||||
3155 | |||||||
3156 | case 'rot': // ROTate |
||||||
3157 | @list($angle, $bgcolor) = explode('|', $parameter, 2); |
||||||
3158 | $phpthumbFilters->ImprovedImageRotate($this->gdimg_output, $angle, $bgcolor, null, $this); |
||||||
3159 | break; |
||||||
3160 | |||||||
3161 | case 'stc': // Source Transparent Color |
||||||
3162 | @list($hexcolor, $min_limit, $max_limit) = explode('|', $parameter, 3); |
||||||
3163 | if (!phpthumb_functions::IsHexColor($hexcolor)) { |
||||||
3164 | $this->DebugMessage('Skipping SourceTransparentColor hex color is invalid ('.$hexcolor.')', __FILE__, __LINE__); |
||||||
3165 | return false; |
||||||
3166 | } |
||||||
3167 | $min_limit = ('' !== $min_limit ? $min_limit : 5); |
||||||
3168 | $max_limit = ('' !== $max_limit ? $max_limit : 10); |
||||||
3169 | if ($gdimg_mask = $phpthumbFilters->SourceTransparentColorMask($this->gdimg_output, $hexcolor, $min_limit, $max_limit)) { |
||||||
3170 | $this->is_alpha = true; |
||||||
3171 | $phpthumbFilters->ApplyMask($gdimg_mask, $this->gdimg_output); |
||||||
3172 | imagedestroy($gdimg_mask); |
||||||
3173 | } else { |
||||||
3174 | $this->DebugMessage('SourceTransparentColorMask() failed for "'.$hexcolor.','.$min_limit.','.$max_limit.'"', __FILE__, __LINE__); |
||||||
3175 | } |
||||||
3176 | break; |
||||||
3177 | } |
||||||
3178 | $this->DebugMessage('Finished processing filter command "'.$command.'('.$parameter.')"', __FILE__, __LINE__); |
||||||
3179 | } |
||||||
3180 | } |
||||||
3181 | return true; |
||||||
3182 | } |
||||||
3183 | |||||||
3184 | |||||||
3185 | public function MaxFileSize() { |
||||||
3186 | if (phpthumb_functions::gd_version() < 2) { |
||||||
3187 | $this->DebugMessage('Skipping MaxFileSize() because gd_version is "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__); |
||||||
3188 | return false; |
||||||
3189 | } |
||||||
3190 | if ($this->maxb > 0) { |
||||||
3191 | switch ($this->thumbnailFormat) { |
||||||
3192 | case 'png': |
||||||
3193 | case 'gif': |
||||||
3194 | $imgRenderFunction = 'image'.$this->thumbnailFormat; |
||||||
3195 | |||||||
3196 | ob_start(); |
||||||
3197 | $imgRenderFunction($this->gdimg_output); |
||||||
3198 | $imgdata = ob_get_contents(); |
||||||
3199 | ob_end_clean(); |
||||||
3200 | |||||||
3201 | if (strlen($imgdata) > $this->maxb) { |
||||||
3202 | for ($i = 8; $i >= 1; $i--) { |
||||||
3203 | $tempIMG = imagecreatetruecolor(imagesx($this->gdimg_output), imagesy($this->gdimg_output)); |
||||||
3204 | imagecopy($tempIMG, $this->gdimg_output, 0, 0, 0, 0, imagesx($this->gdimg_output), imagesy($this->gdimg_output)); |
||||||
3205 | imagetruecolortopalette($tempIMG, true, pow(2, $i)); |
||||||
3206 | ob_start(); |
||||||
3207 | $imgRenderFunction($tempIMG); |
||||||
3208 | $imgdata = ob_get_contents(); |
||||||
3209 | ob_end_clean(); |
||||||
3210 | |||||||
3211 | if (strlen($imgdata) <= $this->maxb) { |
||||||
3212 | imagetruecolortopalette($this->gdimg_output, true, pow(2, $i)); |
||||||
3213 | break; |
||||||
3214 | } |
||||||
3215 | } |
||||||
3216 | } |
||||||
3217 | break; |
||||||
3218 | |||||||
3219 | case 'jpeg': |
||||||
3220 | ob_start(); |
||||||
3221 | imagejpeg($this->gdimg_output); |
||||||
3222 | $imgdata = ob_get_contents(); |
||||||
3223 | ob_end_clean(); |
||||||
3224 | |||||||
3225 | if (strlen($imgdata) > $this->maxb) { |
||||||
3226 | for ($i = 3; $i < 20; $i++) { |
||||||
3227 | $q = round(100 * (1 - log10($i / 2))); |
||||||
3228 | ob_start(); |
||||||
3229 | imagejpeg($this->gdimg_output, null, $q); |
||||||
3230 | $imgdata = ob_get_contents(); |
||||||
3231 | ob_end_clean(); |
||||||
3232 | |||||||
3233 | $this->thumbnailQuality = $q; |
||||||
3234 | if (strlen($imgdata) <= $this->maxb) { |
||||||
3235 | break; |
||||||
3236 | } |
||||||
3237 | } |
||||||
3238 | } |
||||||
3239 | if (strlen($imgdata) > $this->maxb) { |
||||||
3240 | return false; |
||||||
3241 | } |
||||||
3242 | break; |
||||||
3243 | |||||||
3244 | default: |
||||||
3245 | return false; |
||||||
3246 | } |
||||||
3247 | } |
||||||
3248 | return true; |
||||||
3249 | } |
||||||
3250 | |||||||
3251 | |||||||
3252 | public function CalculateThumbnailDimensions() { |
||||||
3253 | $this->DebugMessage('CalculateThumbnailDimensions() starting with [W,H,sx,sy,sw,sh] initially set to ['.$this->source_width.','.$this->source_height.','.$this->sx.','.$this->sy.','.$this->sw.','.$this->sh.']', __FILE__, __LINE__); |
||||||
3254 | //echo $this->source_width.'x'.$this->source_height.'<hr>'; |
||||||
3255 | $this->thumbnailCropX = ($this->sx ? (($this->sx >= 2) ? $this->sx : round($this->sx * $this->source_width)) : 0); |
||||||
3256 | //echo $this->thumbnailCropX.'<br>'; |
||||||
3257 | $this->thumbnailCropY = ($this->sy ? (($this->sy >= 2) ? $this->sy : round($this->sy * $this->source_height)) : 0); |
||||||
3258 | //echo $this->thumbnailCropY.'<br>'; |
||||||
3259 | $this->thumbnailCropW = ($this->sw ? (($this->sw >= 2) ? $this->sw : round($this->sw * $this->source_width)) : $this->source_width); |
||||||
3260 | //echo $this->thumbnailCropW.'<br>'; |
||||||
3261 | $this->thumbnailCropH = ($this->sh ? (($this->sh >= 2) ? $this->sh : round($this->sh * $this->source_height)) : $this->source_height); |
||||||
3262 | //echo $this->thumbnailCropH.'<hr>'; |
||||||
3263 | |||||||
3264 | // limit source area to original image area |
||||||
3265 | $this->thumbnailCropW = max(1, min($this->thumbnailCropW, $this->source_width - $this->thumbnailCropX)); |
||||||
3266 | $this->thumbnailCropH = max(1, min($this->thumbnailCropH, $this->source_height - $this->thumbnailCropY)); |
||||||
3267 | |||||||
3268 | $this->DebugMessage('CalculateThumbnailDimensions() starting with [x,y,w,h] initially set to ['.$this->thumbnailCropX.','.$this->thumbnailCropY.','.$this->thumbnailCropW.','.$this->thumbnailCropH.']', __FILE__, __LINE__); |
||||||
3269 | |||||||
3270 | |||||||
3271 | if ($this->zc && $this->w && $this->h) { |
||||||
3272 | // Zoom Crop |
||||||
3273 | // retain proportional resizing we did above, but crop off larger dimension so smaller |
||||||
3274 | // dimension fully fits available space |
||||||
3275 | |||||||
3276 | $scaling_X = $this->source_width / $this->w; |
||||||
3277 | $scaling_Y = $this->source_height / $this->h; |
||||||
3278 | if ($scaling_X > $scaling_Y) { |
||||||
3279 | // some of the width will need to be cropped |
||||||
3280 | $allowable_width = $this->source_width / $scaling_X * $scaling_Y; |
||||||
3281 | $this->thumbnailCropW = round($allowable_width); |
||||||
3282 | $this->thumbnailCropX = round(($this->source_width - $allowable_width) / 2); |
||||||
3283 | |||||||
3284 | } elseif ($scaling_Y > $scaling_X) { |
||||||
3285 | // some of the height will need to be cropped |
||||||
3286 | $allowable_height = $this->source_height / $scaling_Y * $scaling_X; |
||||||
3287 | $this->thumbnailCropH = round($allowable_height); |
||||||
3288 | $this->thumbnailCropY = round(($this->source_height - $allowable_height) / 2); |
||||||
3289 | |||||||
3290 | } else { |
||||||
3291 | // image fits perfectly, no cropping needed |
||||||
3292 | } |
||||||
3293 | $this->thumbnail_width = $this->w; |
||||||
3294 | $this->thumbnail_height = $this->h; |
||||||
3295 | $this->thumbnail_image_width = $this->thumbnail_width; |
||||||
3296 | $this->thumbnail_image_height = $this->thumbnail_height; |
||||||
3297 | |||||||
3298 | } elseif ($this->iar && $this->w && $this->h) { |
||||||
3299 | |||||||
3300 | // Ignore Aspect Ratio |
||||||
3301 | // stretch image to fit exactly 'w' x 'h' |
||||||
3302 | $this->thumbnail_width = $this->w; |
||||||
3303 | $this->thumbnail_height = $this->h; |
||||||
3304 | $this->thumbnail_image_width = $this->thumbnail_width; |
||||||
3305 | $this->thumbnail_image_height = $this->thumbnail_height; |
||||||
3306 | |||||||
3307 | } else { |
||||||
3308 | |||||||
3309 | $original_aspect_ratio = $this->thumbnailCropW / $this->thumbnailCropH; |
||||||
3310 | if ($this->aoe) { |
||||||
3311 | if ($this->w && $this->h) { |
||||||
3312 | $maxwidth = min($this->w, $this->h * $original_aspect_ratio); |
||||||
3313 | $maxheight = min($this->h, $this->w / $original_aspect_ratio); |
||||||
3314 | } elseif ($this->w) { |
||||||
3315 | $maxwidth = $this->w; |
||||||
3316 | $maxheight = $this->w / $original_aspect_ratio; |
||||||
3317 | } elseif ($this->h) { |
||||||
3318 | $maxwidth = $this->h * $original_aspect_ratio; |
||||||
3319 | $maxheight = $this->h; |
||||||
3320 | } else { |
||||||
3321 | $maxwidth = $this->thumbnailCropW; |
||||||
3322 | $maxheight = $this->thumbnailCropH; |
||||||
3323 | } |
||||||
3324 | } else { |
||||||
3325 | $maxwidth = phpthumb_functions::nonempty_min($this->w, $this->thumbnailCropW, $this->config_output_maxwidth); |
||||||
3326 | $maxheight = phpthumb_functions::nonempty_min($this->h, $this->thumbnailCropH, $this->config_output_maxheight); |
||||||
3327 | //echo $maxwidth.'x'.$maxheight.'<br>'; |
||||||
3328 | $maxwidth = min($maxwidth, $maxheight * $original_aspect_ratio); |
||||||
3329 | $maxheight = min($maxheight, $maxwidth / $original_aspect_ratio); |
||||||
3330 | //echo $maxwidth.'x'.$maxheight.'<hr>'; |
||||||
3331 | } |
||||||
3332 | |||||||
3333 | $this->thumbnail_image_width = $maxwidth; |
||||||
3334 | $this->thumbnail_image_height = $maxheight; |
||||||
3335 | $this->thumbnail_width = $maxwidth; |
||||||
3336 | $this->thumbnail_height = $maxheight; |
||||||
3337 | |||||||
3338 | $this->FixedAspectRatio(); |
||||||
3339 | } |
||||||
3340 | |||||||
3341 | $this->thumbnail_width = max(1, floor($this->thumbnail_width)); |
||||||
3342 | $this->thumbnail_height = max(1, floor($this->thumbnail_height)); |
||||||
3343 | return true; |
||||||
3344 | } |
||||||
3345 | |||||||
3346 | |||||||
3347 | public function CreateGDoutput() { |
||||||
3348 | $this->CalculateThumbnailDimensions(); |
||||||
3349 | |||||||
3350 | // create the GD image (either true-color or 256-color, depending on GD version) |
||||||
3351 | $this->gdimg_output = phpthumb_functions::ImageCreateFunction($this->thumbnail_width, $this->thumbnail_height); |
||||||
3352 | |||||||
3353 | // images that have transparency must have the background filled with the configured 'bg' color otherwise the transparent color will appear as black |
||||||
3354 | imagesavealpha($this->gdimg_output, true); |
||||||
3355 | if ($this->is_alpha && phpthumb_functions::gd_version() >= 2) { |
||||||
3356 | |||||||
3357 | imagealphablending($this->gdimg_output, false); |
||||||
3358 | $output_full_alpha = phpthumb_functions::ImageColorAllocateAlphaSafe($this->gdimg_output, 255, 255, 255, 127); |
||||||
3359 | imagefilledrectangle($this->gdimg_output, 0, 0, $this->thumbnail_width, $this->thumbnail_height, $output_full_alpha); |
||||||
3360 | |||||||
3361 | } else { |
||||||
3362 | |||||||
3363 | $current_transparent_color = imagecolortransparent($this->gdimg_source); |
||||||
3364 | if ($this->bg || (@$current_transparent_color >= 0)) { |
||||||
3365 | |||||||
3366 | $this->config_background_hexcolor = ($this->bg ? $this->bg : $this->config_background_hexcolor); |
||||||
3367 | if (!phpthumb_functions::IsHexColor($this->config_background_hexcolor)) { |
||||||
3368 | return $this->ErrorImage('Invalid hex color string "'.$this->config_background_hexcolor.'" for parameter "bg"'); |
||||||
3369 | } |
||||||
3370 | $background_color = phpthumb_functions::ImageHexColorAllocate($this->gdimg_output, $this->config_background_hexcolor); |
||||||
3371 | imagefilledrectangle($this->gdimg_output, 0, 0, $this->thumbnail_width, $this->thumbnail_height, $background_color); |
||||||
3372 | |||||||
3373 | } |
||||||
3374 | |||||||
3375 | } |
||||||
3376 | $this->DebugMessage('CreateGDoutput() returning canvas "'.$this->thumbnail_width.'x'.$this->thumbnail_height.'"', __FILE__, __LINE__); |
||||||
3377 | return true; |
||||||
3378 | } |
||||||
3379 | |||||||
3380 | public function SetOrientationDependantWidthHeight() { |
||||||
3381 | $this->DebugMessage('SetOrientationDependantWidthHeight() starting with "'.$this->source_width.'"x"'.$this->source_height.'"', __FILE__, __LINE__); |
||||||
3382 | if ($this->source_height > $this->source_width) { |
||||||
3383 | // portrait |
||||||
3384 | $this->w = phpthumb_functions::OneOfThese($this->wp, $this->w, $this->ws, $this->wl); |
||||||
3385 | $this->h = phpthumb_functions::OneOfThese($this->hp, $this->h, $this->hs, $this->hl); |
||||||
3386 | } elseif ($this->source_height < $this->source_width) { |
||||||
3387 | // landscape |
||||||
3388 | $this->w = phpthumb_functions::OneOfThese($this->wl, $this->w, $this->ws, $this->wp); |
||||||
3389 | $this->h = phpthumb_functions::OneOfThese($this->hl, $this->h, $this->hs, $this->hp); |
||||||
3390 | } else { |
||||||
3391 | // square |
||||||
3392 | $this->w = phpthumb_functions::OneOfThese($this->ws, $this->w, $this->wl, $this->wp); |
||||||
3393 | $this->h = phpthumb_functions::OneOfThese($this->hs, $this->h, $this->hl, $this->hp); |
||||||
3394 | } |
||||||
3395 | //$this->w = round($this->w ? $this->w : (($this->h && $this->source_height) ? $this->h * $this->source_width / $this->source_height : $this->w)); |
||||||
3396 | //$this->h = round($this->h ? $this->h : (($this->w && $this->source_width) ? $this->w * $this->source_height / $this->source_width : $this->h)); |
||||||
3397 | $this->DebugMessage('SetOrientationDependantWidthHeight() setting w="'. (int) $this->w .'", h="'. (int) $this->h .'"', __FILE__, __LINE__); |
||||||
3398 | return true; |
||||||
3399 | } |
||||||
3400 | |||||||
3401 | public function ExtractEXIFgetImageSize() { |
||||||
3402 | $this->DebugMessage('starting ExtractEXIFgetImageSize()', __FILE__, __LINE__); |
||||||
3403 | |||||||
3404 | if (preg_match('#^http:#i', $this->src) && !$this->sourceFilename && $this->rawImageData) { |
||||||
3405 | $this->SourceDataToTempFile(); |
||||||
3406 | } |
||||||
3407 | if (null === $this->getimagesizeinfo) { |
||||||
3408 | if ($this->sourceFilename) { |
||||||
3409 | if ($this->getimagesizeinfo = @getimagesize($this->sourceFilename)) { |
||||||
3410 | $this->source_width = $this->getimagesizeinfo[0]; |
||||||
3411 | $this->source_height = $this->getimagesizeinfo[1]; |
||||||
3412 | $this->DebugMessage('getimagesize('.$this->sourceFilename.') says image is '.$this->source_width.'x'.$this->source_height, __FILE__, __LINE__); |
||||||
3413 | } else { |
||||||
3414 | $this->DebugMessage('getimagesize('.$this->sourceFilename.') failed', __FILE__, __LINE__); |
||||||
3415 | } |
||||||
3416 | } else { |
||||||
3417 | $this->DebugMessage('skipping getimagesize() because $this->sourceFilename is empty', __FILE__, __LINE__); |
||||||
3418 | } |
||||||
3419 | } else { |
||||||
3420 | $this->DebugMessage('skipping getimagesize() because !is_null($this->getimagesizeinfo)', __FILE__, __LINE__); |
||||||
3421 | } |
||||||
3422 | |||||||
3423 | if (is_resource($this->gdimg_source)) { |
||||||
3424 | |||||||
3425 | $this->source_width = imagesx($this->gdimg_source); |
||||||
3426 | $this->source_height = imagesy($this->gdimg_source); |
||||||
3427 | |||||||
3428 | $this->SetOrientationDependantWidthHeight(); |
||||||
3429 | |||||||
3430 | } elseif ($this->rawImageData && !$this->sourceFilename) { |
||||||
3431 | |||||||
3432 | if ($this->SourceImageIsTooLarge($this->source_width, $this->source_height)) { |
||||||
3433 | $this->DebugMessage('NOT bypassing EXIF and getimagesize sections because source image is too large for GD ('.$this->source_width.'x'.$this->source_width.'='.($this->source_width * $this->source_height * 5).'MB)', __FILE__, __LINE__); |
||||||
3434 | } else { |
||||||
3435 | $this->DebugMessage('bypassing EXIF and getimagesize sections because $this->rawImageData is set, and $this->sourceFilename is not set, and source image is not too large for GD ('.$this->source_width.'x'.$this->source_width.'='.($this->source_width * $this->source_height * 5).'MB)', __FILE__, __LINE__); |
||||||
3436 | } |
||||||
3437 | |||||||
3438 | } |
||||||
3439 | |||||||
3440 | if (!empty($this->getimagesizeinfo)) { |
||||||
3441 | // great |
||||||
3442 | $this->getimagesizeinfo['filesize'] = @filesize($this->sourceFilename); |
||||||
3443 | } elseif (!$this->rawImageData) { |
||||||
3444 | $this->DebugMessage('getimagesize("'.$this->sourceFilename.'") failed', __FILE__, __LINE__); |
||||||
3445 | } |
||||||
3446 | |||||||
3447 | if ($this->config_prefer_imagemagick) { |
||||||
3448 | if ($this->ImageMagickThumbnailToGD()) { |
||||||
3449 | return true; |
||||||
3450 | } |
||||||
3451 | $this->DebugMessage('ImageMagickThumbnailToGD() failed', __FILE__, __LINE__); |
||||||
3452 | } |
||||||
3453 | |||||||
3454 | if (isset($this->getimagesizeinfo[1])) { |
||||||
3455 | $this->source_width = $this->getimagesizeinfo[0]; |
||||||
3456 | $this->source_height = $this->getimagesizeinfo[1]; |
||||||
3457 | } |
||||||
3458 | |||||||
3459 | $this->SetOrientationDependantWidthHeight(); |
||||||
3460 | |||||||
3461 | if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '4.2.0', '>=') && function_exists('exif_read_data')) { |
||||||
3462 | switch (@$this->getimagesizeinfo[2]) { |
||||||
3463 | case IMAGETYPE_JPEG: |
||||||
3464 | case IMAGETYPE_TIFF_II: |
||||||
3465 | case IMAGETYPE_TIFF_MM: |
||||||
3466 | $this->exif_raw_data = @exif_read_data($this->sourceFilename, 0, true); |
||||||
3467 | break; |
||||||
3468 | } |
||||||
3469 | } |
||||||
3470 | if (function_exists('exif_thumbnail') && (@$this->getimagesizeinfo[2] == IMAGETYPE_JPEG)) { |
||||||
3471 | // Extract EXIF info from JPEGs |
||||||
3472 | |||||||
3473 | $this->exif_thumbnail_width = ''; |
||||||
3474 | $this->exif_thumbnail_height = ''; |
||||||
3475 | $this->exif_thumbnail_type = ''; |
||||||
3476 | |||||||
3477 | // The parameters width, height and imagetype are available since PHP v4.3.0 |
||||||
3478 | if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '4.3.0', '>=')) { |
||||||
3479 | |||||||
3480 | $this->exif_thumbnail_data = @exif_thumbnail($this->sourceFilename, $this->exif_thumbnail_width, $this->exif_thumbnail_height, $this->exif_thumbnail_type); |
||||||
3481 | |||||||
3482 | } else { |
||||||
3483 | |||||||
3484 | // older versions of exif_thumbnail output an error message but NOT return false on failure |
||||||
3485 | ob_start(); |
||||||
3486 | $this->exif_thumbnail_data = exif_thumbnail($this->sourceFilename); |
||||||
3487 | $exit_thumbnail_error = ob_get_contents(); |
||||||
3488 | ob_end_clean(); |
||||||
3489 | if (!$exit_thumbnail_error && $this->exif_thumbnail_data) { |
||||||
3490 | |||||||
3491 | if ($gdimg_exif_temp = $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data, false)) { |
||||||
3492 | $this->exif_thumbnail_width = imagesx($gdimg_exif_temp); |
||||||
3493 | $this->exif_thumbnail_height = imagesy($gdimg_exif_temp); |
||||||
3494 | $this->exif_thumbnail_type = 2; // (2 == JPEG) before PHP v4.3.0 only JPEG format EXIF thumbnails are returned |
||||||
3495 | unset($gdimg_exif_temp); |
||||||
3496 | } else { |
||||||
3497 | return $this->ErrorImage('Failed - $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data) in '.__FILE__.' on line '.__LINE__); |
||||||
3498 | } |
||||||
3499 | |||||||
3500 | } |
||||||
3501 | |||||||
3502 | } |
||||||
3503 | |||||||
3504 | } elseif (!function_exists('exif_thumbnail')) { |
||||||
3505 | |||||||
3506 | $this->DebugMessage('exif_thumbnail() does not exist, cannot extract EXIF thumbnail', __FILE__, __LINE__); |
||||||
3507 | |||||||
3508 | } |
||||||
3509 | |||||||
3510 | $this->DebugMessage('EXIF thumbnail extraction: (size='.strlen($this->exif_thumbnail_data).'; type="'.$this->exif_thumbnail_type.'"; '. (int) $this->exif_thumbnail_width .'x'. (int) $this->exif_thumbnail_height .')', __FILE__, __LINE__); |
||||||
3511 | |||||||
3512 | // see if EXIF thumbnail can be used directly with no processing |
||||||
3513 | if ($this->config_use_exif_thumbnail_for_speed && $this->exif_thumbnail_data) { |
||||||
3514 | while (true) { |
||||||
3515 | if (!$this->xto) { |
||||||
3516 | $source_ar = $this->source_width / $this->source_height; |
||||||
3517 | $exif_ar = $this->exif_thumbnail_width / $this->exif_thumbnail_height; |
||||||
3518 | if (number_format($source_ar, 2) != number_format($exif_ar, 2)) { |
||||||
3519 | $this->DebugMessage('not using EXIF thumbnail because $source_ar != $exif_ar ('.$source_ar.' != '.$exif_ar.')', __FILE__, __LINE__); |
||||||
3520 | break; |
||||||
3521 | } |
||||||
3522 | if ($this->w && ($this->w != $this->exif_thumbnail_width)) { |
||||||
3523 | $this->DebugMessage('not using EXIF thumbnail because $this->w != $this->exif_thumbnail_width ('.$this->w.' != '.$this->exif_thumbnail_width.')', __FILE__, __LINE__); |
||||||
3524 | break; |
||||||
3525 | } |
||||||
3526 | if ($this->h && ($this->h != $this->exif_thumbnail_height)) { |
||||||
3527 | $this->DebugMessage('not using EXIF thumbnail because $this->h != $this->exif_thumbnail_height ('.$this->h.' != '.$this->exif_thumbnail_height.')', __FILE__, __LINE__); |
||||||
3528 | break; |
||||||
3529 | } |
||||||
3530 | $CannotBeSetParameters = array('sx', 'sy', 'sh', 'sw', 'far', 'bg', 'bc', 'fltr', 'phpThumbDebug'); |
||||||
3531 | foreach ($CannotBeSetParameters as $parameter) { |
||||||
3532 | if ($this->$parameter) { |
||||||
3533 | break 2; |
||||||
3534 | } |
||||||
3535 | } |
||||||
3536 | } |
||||||
3537 | |||||||
3538 | $this->DebugMessage('setting $this->gdimg_source = $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data)', __FILE__, __LINE__); |
||||||
3539 | $this->gdimg_source = $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data); |
||||||
3540 | $this->source_width = imagesx($this->gdimg_source); |
||||||
3541 | $this->source_height = imagesy($this->gdimg_source); |
||||||
3542 | return true; |
||||||
3543 | } |
||||||
3544 | } |
||||||
3545 | |||||||
3546 | if (($this->config_max_source_pixels > 0) && (($this->source_width * $this->source_height) > $this->config_max_source_pixels)) { |
||||||
3547 | |||||||
3548 | // Source image is larger than would fit in available PHP memory. |
||||||
3549 | // If ImageMagick is installed, use it to generate the thumbnail. |
||||||
3550 | // Else, if an EXIF thumbnail is available, use that as the source image. |
||||||
3551 | // Otherwise, no choice but to fail with an error message |
||||||
3552 | $this->DebugMessage('image is '.$this->source_width.'x'.$this->source_height.' and therefore contains more pixels ('.($this->source_width * $this->source_height).') than $this->config_max_source_pixels setting ('.$this->config_max_source_pixels.')', __FILE__, __LINE__); |
||||||
3553 | if (!$this->config_prefer_imagemagick && $this->ImageMagickThumbnailToGD()) { |
||||||
3554 | // excellent, we have a thumbnailed source image |
||||||
3555 | return true; |
||||||
3556 | } |
||||||
3557 | |||||||
3558 | } |
||||||
3559 | return true; |
||||||
3560 | } |
||||||
3561 | |||||||
3562 | |||||||
3563 | public function SetCacheFilename() { |
||||||
3564 | if (null !== $this->cache_filename) { |
||||||
3565 | $this->DebugMessage('$this->cache_filename already set, skipping SetCacheFilename()', __FILE__, __LINE__); |
||||||
3566 | return true; |
||||||
3567 | } |
||||||
3568 | if (null === $this->config_cache_directory) { |
||||||
3569 | $this->setCacheDirectory(); |
||||||
3570 | if (!$this->config_cache_directory) { |
||||||
3571 | $this->DebugMessage('SetCacheFilename() failed because $this->config_cache_directory is empty', __FILE__, __LINE__); |
||||||
3572 | return false; |
||||||
3573 | } |
||||||
3574 | } |
||||||
3575 | $this->setOutputFormat(); |
||||||
3576 | |||||||
3577 | if (!$this->sourceFilename && !$this->rawImageData && $this->src) { |
||||||
3578 | $this->sourceFilename = $this->ResolveFilenameToAbsolute($this->src); |
||||||
3579 | } |
||||||
3580 | |||||||
3581 | if ($this->config_cache_default_only_suffix && $this->sourceFilename) { |
||||||
3582 | // simplified cache filenames: |
||||||
3583 | // only use default parameters in phpThumb.config.php |
||||||
3584 | // substitute source filename into * in $this->config_cache_default_only_suffix |
||||||
3585 | // (eg: '*_thumb' becomes 'picture_thumb.jpg') |
||||||
3586 | if (strpos($this->config_cache_default_only_suffix, '*') === false) { |
||||||
3587 | $this->DebugMessage('aborting simplified caching filename because no * in "'.$this->config_cache_default_only_suffix.'"', __FILE__, __LINE__); |
||||||
3588 | } else { |
||||||
3589 | preg_match('#(.+)(\\.[a-z0-9]+)?$#i', basename($this->sourceFilename), $matches); |
||||||
3590 | $this->cache_filename = $this->config_cache_directory.DIRECTORY_SEPARATOR.rawurlencode(str_replace('*', @$matches[1], $this->config_cache_default_only_suffix)).'.'.strtolower($this->thumbnailFormat); |
||||||
3591 | return true; |
||||||
3592 | } |
||||||
3593 | } |
||||||
3594 | |||||||
3595 | $this->cache_filename = ''; |
||||||
3596 | if ($this->new) { |
||||||
3597 | $broad_directory_name = strtolower(md5($this->new)); |
||||||
3598 | $this->cache_filename .= '_new'.$broad_directory_name; |
||||||
3599 | } elseif ($this->md5s) { |
||||||
3600 | // source image MD5 hash provided |
||||||
3601 | $this->DebugMessage('SetCacheFilename() _raw set from $this->md5s = "'.$this->md5s.'"', __FILE__, __LINE__); |
||||||
3602 | $broad_directory_name = $this->md5s; |
||||||
3603 | $this->cache_filename .= '_raw'.$this->md5s; |
||||||
3604 | } elseif (!$this->src && $this->rawImageData) { |
||||||
3605 | $this->DebugMessage('SetCacheFilename() _raw set from md5($this->rawImageData) = "'.md5($this->rawImageData).'"', __FILE__, __LINE__); |
||||||
3606 | $broad_directory_name = strtolower(md5($this->rawImageData)); |
||||||
3607 | $this->cache_filename .= '_raw'.$broad_directory_name; |
||||||
3608 | } else { |
||||||
3609 | $this->DebugMessage('SetCacheFilename() _src set from md5($this->sourceFilename) "'.$this->sourceFilename.'" = "'.md5($this->sourceFilename).'"', __FILE__, __LINE__); |
||||||
3610 | $broad_directory_name = strtolower(md5($this->sourceFilename)); |
||||||
3611 | $this->cache_filename .= '_src'.$broad_directory_name; |
||||||
3612 | } |
||||||
3613 | if (!empty($_SERVER['HTTP_REFERER']) && $this->config_nooffsitelink_enabled) { |
||||||
3614 | $parsed_url1 = @phpthumb_functions::ParseURLbetter(@$_SERVER['HTTP_REFERER']); |
||||||
3615 | $parsed_url2 = @phpthumb_functions::ParseURLbetter('http://'.@$_SERVER['HTTP_HOST']); |
||||||
3616 | if (@$parsed_url1['host'] && @$parsed_url2['host'] && ($parsed_url1['host'] != $parsed_url2['host'])) { |
||||||
3617 | // include "_offsite" only if nooffsitelink_enabled and if referrer doesn't match the domain of the current server |
||||||
3618 | $this->cache_filename .= '_offsite'; |
||||||
3619 | } |
||||||
3620 | } |
||||||
3621 | |||||||
3622 | $ParametersString = ''; |
||||||
3623 | if ($this->fltr && is_array($this->fltr)) { |
||||||
3624 | $ParametersString .= '_fltr'.implode('_fltr', $this->fltr); |
||||||
3625 | } |
||||||
3626 | $FilenameParameters1 = array('ar', 'bg', 'bc', 'far', 'sx', 'sy', 'sw', 'sh', 'zc'); |
||||||
3627 | foreach ($FilenameParameters1 as $key) { |
||||||
3628 | if ($this->$key) { |
||||||
3629 | $ParametersString .= '_'.$key.$this->$key; |
||||||
3630 | } |
||||||
3631 | } |
||||||
3632 | $FilenameParameters2 = array('h', 'w', 'wl', 'wp', 'ws', 'hp', 'hs', 'xto', 'ra', 'iar', 'aoe', 'maxb', 'sfn', 'dpi'); |
||||||
3633 | foreach ($FilenameParameters2 as $key) { |
||||||
3634 | if ($this->$key) { |
||||||
3635 | $ParametersString .= '_'.$key. (int) $this->$key; |
||||||
3636 | } |
||||||
3637 | } |
||||||
3638 | $FilenameParameters3 = array('ica'); |
||||||
3639 | foreach ($FilenameParameters3 as $key) { |
||||||
3640 | if ($this->$key) { |
||||||
3641 | $ParametersString .= '_'.$key.substr(md5($this->$key), 0, 4); |
||||||
3642 | } |
||||||
3643 | } |
||||||
3644 | if ($this->thumbnailFormat == 'jpeg') { |
||||||
3645 | // only JPEG output has variable quality option |
||||||
3646 | $ParametersString .= '_q'. (int) $this->thumbnailQuality; |
||||||
3647 | } |
||||||
3648 | $this->DebugMessage('SetCacheFilename() _par set from md5('.$ParametersString.')', __FILE__, __LINE__); |
||||||
3649 | $this->cache_filename .= '_par'.strtolower(md5($ParametersString)); |
||||||
3650 | |||||||
3651 | if ($this->md5s) { |
||||||
3652 | // source image MD5 hash provided |
||||||
3653 | // do not source image modification date -- |
||||||
3654 | // cached image will be used even if file was modified or removed |
||||||
3655 | } elseif (!$this->config_cache_source_filemtime_ignore_remote && preg_match('#^(f|ht)tps?\://#i', $this->src)) { |
||||||
3656 | $this->cache_filename .= '_dat'. (int) phpthumb_functions::filedate_remote($this->src); |
||||||
3657 | } elseif (!$this->config_cache_source_filemtime_ignore_local && $this->src && !$this->rawImageData) { |
||||||
3658 | $this->cache_filename .= '_dat'. (int) (@filemtime($this->sourceFilename)); |
||||||
3659 | } |
||||||
3660 | |||||||
3661 | $this->cache_filename .= '.'.strtolower($this->thumbnailFormat); |
||||||
3662 | $broad_directories = ''; |
||||||
3663 | for ($i = 0; $i < $this->config_cache_directory_depth; $i++) { |
||||||
3664 | $broad_directories .= DIRECTORY_SEPARATOR.substr($broad_directory_name, 0, $i + 1); |
||||||
3665 | } |
||||||
3666 | |||||||
3667 | $this->cache_filename = $this->config_cache_directory.$broad_directories.DIRECTORY_SEPARATOR.$this->config_cache_prefix.rawurlencode($this->cache_filename); |
||||||
3668 | return true; |
||||||
3669 | } |
||||||
3670 | |||||||
3671 | |||||||
3672 | public function SourceImageIsTooLarge($width, $height) { |
||||||
3673 | if (!$this->config_max_source_pixels) { |
||||||
3674 | return false; |
||||||
3675 | } |
||||||
3676 | if ($this->php_memory_limit && function_exists('memory_get_usage')) { |
||||||
3677 | $available_memory = $this->php_memory_limit - memory_get_usage(); |
||||||
3678 | return (bool) (($width * $height * 5) > $available_memory); |
||||||
3679 | } |
||||||
3680 | return (bool) (($width * $height) > $this->config_max_source_pixels); |
||||||
3681 | } |
||||||
3682 | |||||||
3683 | public function ImageCreateFromFilename($filename) { |
||||||
3684 | // try to create GD image source directly via GD, if possible, |
||||||
3685 | // rather than buffering to memory and creating with imagecreatefromstring |
||||||
3686 | $ImageCreateWasAttempted = false; |
||||||
3687 | $gd_image = false; |
||||||
3688 | |||||||
3689 | $this->DebugMessage('starting ImageCreateFromFilename('.$filename.')', __FILE__, __LINE__); |
||||||
3690 | if ($filename && ($getimagesizeinfo = @getimagesize($filename))) { |
||||||
3691 | if (!$this->SourceImageIsTooLarge($getimagesizeinfo[0], $getimagesizeinfo[1])) { |
||||||
3692 | $ImageCreateFromFunction = array( |
||||||
3693 | 1 => 'imagecreatefromgif', |
||||||
3694 | 2 => 'imagecreatefromjpeg', |
||||||
3695 | 3 => 'imagecreatefrompng', |
||||||
3696 | 15 => 'imagecreatefromwbmp', |
||||||
3697 | ); |
||||||
3698 | $this->DebugMessage('ImageCreateFromFilename found ($getimagesizeinfo[2]=='.@$getimagesizeinfo[2].')', __FILE__, __LINE__); |
||||||
3699 | switch (@$getimagesizeinfo[2]) { |
||||||
3700 | case 1: // GIF |
||||||
3701 | case 2: // JPEG |
||||||
3702 | case 3: // PNG |
||||||
3703 | case 15: // WBMP |
||||||
3704 | $ImageCreateFromFunctionName = $ImageCreateFromFunction[$getimagesizeinfo[2]]; |
||||||
3705 | if (function_exists($ImageCreateFromFunctionName)) { |
||||||
3706 | $this->DebugMessage('Calling '.$ImageCreateFromFunctionName.'('.$filename.')', __FILE__, __LINE__); |
||||||
3707 | $ImageCreateWasAttempted = true; |
||||||
3708 | $gd_image = $ImageCreateFromFunctionName($filename); |
||||||
3709 | } else { |
||||||
3710 | $this->DebugMessage('NOT calling '.$ImageCreateFromFunctionName.'('.$filename.') because !function_exists('.$ImageCreateFromFunctionName.')', __FILE__, __LINE__); |
||||||
3711 | } |
||||||
3712 | break; |
||||||
3713 | |||||||
3714 | case 4: // SWF |
||||||
3715 | case 5: // PSD |
||||||
3716 | case 6: // BMP |
||||||
3717 | case 7: // TIFF (LE) |
||||||
3718 | case 8: // TIFF (BE) |
||||||
3719 | case 9: // JPC |
||||||
3720 | case 10: // JP2 |
||||||
3721 | case 11: // JPX |
||||||
3722 | case 12: // JB2 |
||||||
3723 | case 13: // SWC |
||||||
3724 | case 14: // IFF |
||||||
3725 | case 16: // XBM |
||||||
3726 | $this->DebugMessage('No built-in image creation function for image type "'.@$getimagesizeinfo[2].'" ($getimagesizeinfo[2])', __FILE__, __LINE__); |
||||||
3727 | break; |
||||||
3728 | |||||||
3729 | default: |
||||||
3730 | $this->DebugMessage('Unknown value for $getimagesizeinfo[2]: "'.@$getimagesizeinfo[2].'"', __FILE__, __LINE__); |
||||||
3731 | break; |
||||||
3732 | } |
||||||
3733 | } else { |
||||||
3734 | $this->DebugMessage('image is '.$getimagesizeinfo[0].'x'.$getimagesizeinfo[1].' and therefore contains more pixels ('.($getimagesizeinfo[0] * $getimagesizeinfo[1]).') than $this->config_max_source_pixels setting ('.$this->config_max_source_pixels.')', __FILE__, __LINE__); |
||||||
3735 | return false; |
||||||
3736 | } |
||||||
3737 | } else { |
||||||
3738 | $this->DebugMessage('empty $filename or getimagesize('.$filename.') failed', __FILE__, __LINE__); |
||||||
3739 | } |
||||||
3740 | |||||||
3741 | if (!$gd_image) { |
||||||
3742 | // cannot create from filename, attempt to create source image with imagecreatefromstring, if possible |
||||||
3743 | if ($ImageCreateWasAttempted) { |
||||||
3744 | $this->DebugMessage($ImageCreateFromFunctionName.'() was attempted but FAILED', __FILE__, __LINE__); |
||||||
3745 | } |
||||||
3746 | $this->DebugMessage('Populating $rawimagedata', __FILE__, __LINE__); |
||||||
3747 | $rawimagedata = ''; |
||||||
3748 | if ($fp = @fopen($filename, 'rb')) { |
||||||
3749 | $filesize = filesize($filename); |
||||||
3750 | $blocksize = 8192; |
||||||
3751 | $blockreads = ceil($filesize / $blocksize); |
||||||
3752 | for ($i = 0; $i < $blockreads; $i++) { |
||||||
3753 | $rawimagedata .= fread($fp, $blocksize); |
||||||
3754 | } |
||||||
3755 | fclose($fp); |
||||||
3756 | } else { |
||||||
3757 | $this->DebugMessage('cannot fopen('.$filename.')', __FILE__, __LINE__); |
||||||
3758 | } |
||||||
3759 | if ($rawimagedata) { |
||||||
3760 | $this->DebugMessage('attempting ImageCreateFromStringReplacement($rawimagedata ('.strlen($rawimagedata).' bytes), true)', __FILE__, __LINE__); |
||||||
3761 | $gd_image = $this->ImageCreateFromStringReplacement($rawimagedata, true); |
||||||
3762 | } |
||||||
3763 | } |
||||||
3764 | return $gd_image; |
||||||
3765 | } |
||||||
3766 | |||||||
3767 | public function SourceImageToGD() { |
||||||
3768 | if (is_resource($this->gdimg_source)) { |
||||||
3769 | $this->source_width = imagesx($this->gdimg_source); |
||||||
3770 | $this->source_height = imagesy($this->gdimg_source); |
||||||
3771 | $this->DebugMessage('skipping SourceImageToGD() because $this->gdimg_source is already a resource ('.$this->source_width.'x'.$this->source_height.')', __FILE__, __LINE__); |
||||||
3772 | return true; |
||||||
3773 | } |
||||||
3774 | $this->DebugMessage('starting SourceImageToGD()', __FILE__, __LINE__); |
||||||
3775 | |||||||
3776 | if ($this->config_prefer_imagemagick) { |
||||||
3777 | if (empty($this->sourceFilename) && !empty($this->rawImageData)) { |
||||||
3778 | $this->DebugMessage('Copying raw image data to temp file and trying again with ImageMagick', __FILE__, __LINE__); |
||||||
3779 | if ($tempnam = $this->phpThumb_tempnam()) { |
||||||
3780 | if (file_put_contents($tempnam, $this->rawImageData)) { |
||||||
3781 | $this->sourceFilename = $tempnam; |
||||||
3782 | if ($this->ImageMagickThumbnailToGD()) { |
||||||
3783 | // excellent, we have a thumbnailed source image |
||||||
3784 | $this->DebugMessage('ImageMagickThumbnailToGD() succeeded', __FILE__, __LINE__); |
||||||
3785 | } else { |
||||||
3786 | $this->DebugMessage('ImageMagickThumbnailToGD() failed', __FILE__, __LINE__); |
||||||
3787 | } |
||||||
3788 | @chmod($tempnam, $this->getParameter('config_file_create_mask')); |
||||||
3789 | } else { |
||||||
3790 | $this->DebugMessage('failed to put $this->rawImageData into temp file "'.$tempnam.'"', __FILE__, __LINE__); |
||||||
3791 | } |
||||||
3792 | } else { |
||||||
3793 | $this->DebugMessage('failed to generate temp file name', __FILE__, __LINE__); |
||||||
3794 | } |
||||||
3795 | } |
||||||
3796 | } |
||||||
3797 | if (!$this->gdimg_source && $this->rawImageData) { |
||||||
3798 | |||||||
3799 | if ($this->SourceImageIsTooLarge($this->source_width, $this->source_height)) { |
||||||
3800 | $memory_get_usage = (function_exists('memory_get_usage') ? memory_get_usage() : 0); |
||||||
3801 | return $this->ErrorImage('Source image is too large ('.$this->source_width.'x'.$this->source_height.' = '.number_format($this->source_width * $this->source_height / 1000000, 1).'Mpx, max='.number_format($this->config_max_source_pixels / 1000000, 1).'Mpx) for GD creation (either install ImageMagick or increase PHP memory_limit to at least '.ceil(($memory_get_usage + (5 * $this->source_width * $this->source_height)) / 1048576).'M).'); |
||||||
3802 | } |
||||||
3803 | if ($this->md5s && ($this->md5s != md5($this->rawImageData))) { |
||||||
3804 | return $this->ErrorImage('$this->md5s != md5($this->rawImageData)'."\n".'"'.$this->md5s.'" != '."\n".'"'.md5($this->rawImageData).'"'); |
||||||
3805 | } |
||||||
3806 | //if ($this->issafemode) { |
||||||
3807 | // return $this->ErrorImage('Cannot generate thumbnails from raw image data when PHP SAFE_MODE enabled'); |
||||||
3808 | //} |
||||||
3809 | $this->gdimg_source = $this->ImageCreateFromStringReplacement($this->rawImageData); |
||||||
3810 | if (!$this->gdimg_source) { |
||||||
3811 | if (substr($this->rawImageData, 0, 2) === 'BM') { |
||||||
3812 | $this->getimagesizeinfo[2] = 6; // BMP |
||||||
3813 | } elseif (substr($this->rawImageData, 0, 4) === 'II'."\x2A\x00") { |
||||||
3814 | $this->getimagesizeinfo[2] = 7; // TIFF (littlendian) |
||||||
3815 | } elseif (substr($this->rawImageData, 0, 4) === 'MM'."\x00\x2A") { |
||||||
3816 | $this->getimagesizeinfo[2] = 8; // TIFF (bigendian) |
||||||
3817 | } |
||||||
3818 | $this->DebugMessage('SourceImageToGD.ImageCreateFromStringReplacement() failed with unknown image type "'.substr($this->rawImageData, 0, 4).'" ('.phpthumb_functions::HexCharDisplay(substr($this->rawImageData, 0, 4)).')', __FILE__, __LINE__); |
||||||
3819 | // return $this->ErrorImage('Unknown image type identified by "'.substr($this->rawImageData, 0, 4).'" ('.phpthumb_functions::HexCharDisplay(substr($this->rawImageData, 0, 4)).') in SourceImageToGD()['.__LINE__.']'); |
||||||
3820 | } |
||||||
3821 | |||||||
3822 | } elseif (!$this->gdimg_source && $this->sourceFilename) { |
||||||
3823 | |||||||
3824 | if ($this->md5s && ($this->md5s != phpthumb_functions::md5_file_safe($this->sourceFilename))) { |
||||||
3825 | return $this->ErrorImage('$this->md5s != md5(sourceFilename)'."\n".'"'.$this->md5s.'" != '."\n".'"'.phpthumb_functions::md5_file_safe($this->sourceFilename).'"'); |
||||||
3826 | } |
||||||
3827 | switch (@$this->getimagesizeinfo[2]) { |
||||||
3828 | case 1: |
||||||
3829 | case 3: |
||||||
3830 | // GIF or PNG input file may have transparency |
||||||
3831 | $this->is_alpha = true; |
||||||
3832 | break; |
||||||
3833 | } |
||||||
3834 | if (!$this->SourceImageIsTooLarge($this->source_width, $this->source_height)) { |
||||||
3835 | $this->gdimg_source = $this->ImageCreateFromFilename($this->sourceFilename); |
||||||
3836 | } |
||||||
3837 | |||||||
3838 | } |
||||||
3839 | |||||||
3840 | while (true) { |
||||||
3841 | if ($this->gdimg_source) { |
||||||
3842 | $this->DebugMessage('Not using EXIF thumbnail data because $this->gdimg_source is already set', __FILE__, __LINE__); |
||||||
3843 | break; |
||||||
3844 | } |
||||||
3845 | if (!$this->exif_thumbnail_data) { |
||||||
3846 | $this->DebugMessage('Not using EXIF thumbnail data because $this->exif_thumbnail_data is empty', __FILE__, __LINE__); |
||||||
3847 | break; |
||||||
3848 | } |
||||||
3849 | if (ini_get('safe_mode')) { |
||||||
3850 | if (!$this->SourceImageIsTooLarge($this->source_width, $this->source_height)) { |
||||||
3851 | $this->DebugMessage('Using EXIF thumbnail data because source image too large and safe_mode enabled', __FILE__, __LINE__); |
||||||
3852 | $this->aoe = true; |
||||||
3853 | } else { |
||||||
3854 | break; |
||||||
3855 | } |
||||||
3856 | } else { |
||||||
3857 | if (!$this->config_use_exif_thumbnail_for_speed) { |
||||||
3858 | $this->DebugMessage('Not using EXIF thumbnail data because $this->config_use_exif_thumbnail_for_speed is FALSE', __FILE__, __LINE__); |
||||||
3859 | break; |
||||||
3860 | } |
||||||
3861 | if (($this->thumbnailCropX != 0) || ($this->thumbnailCropY != 0)) { |
||||||
3862 | $this->DebugMessage('Not using EXIF thumbnail data because source cropping is enabled ('.$this->thumbnailCropX.','.$this->thumbnailCropY.')', __FILE__, __LINE__); |
||||||
3863 | break; |
||||||
3864 | } |
||||||
3865 | if (($this->w > $this->exif_thumbnail_width) || ($this->h > $this->exif_thumbnail_height)) { |
||||||
3866 | $this->DebugMessage('Not using EXIF thumbnail data because EXIF thumbnail is too small ('.$this->exif_thumbnail_width.'x'.$this->exif_thumbnail_height.' vs '.$this->w.'x'.$this->h.')', __FILE__, __LINE__); |
||||||
3867 | break; |
||||||
3868 | } |
||||||
3869 | $source_ar = $this->source_width / $this->source_height; |
||||||
3870 | $exif_ar = $this->exif_thumbnail_width / $this->exif_thumbnail_height; |
||||||
3871 | if (number_format($source_ar, 2) != number_format($exif_ar, 2)) { |
||||||
3872 | $this->DebugMessage('not using EXIF thumbnail because $source_ar != $exif_ar ('.$source_ar.' != '.$exif_ar.')', __FILE__, __LINE__); |
||||||
3873 | break; |
||||||
3874 | } |
||||||
3875 | } |
||||||
3876 | |||||||
3877 | // EXIF thumbnail exists, and is equal to or larger than destination thumbnail, and will be use as source image |
||||||
3878 | $this->DebugMessage('Trying to use EXIF thumbnail as source image', __FILE__, __LINE__); |
||||||
3879 | |||||||
3880 | if ($gdimg_exif_temp = $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data, false)) { |
||||||
3881 | |||||||
3882 | $this->DebugMessage('Successfully using EXIF thumbnail as source image', __FILE__, __LINE__); |
||||||
3883 | $this->gdimg_source = $gdimg_exif_temp; |
||||||
3884 | $this->source_width = $this->exif_thumbnail_width; |
||||||
3885 | $this->source_height = $this->exif_thumbnail_height; |
||||||
3886 | $this->thumbnailCropW = $this->source_width; |
||||||
3887 | $this->thumbnailCropH = $this->source_height; |
||||||
3888 | return true; |
||||||
3889 | |||||||
3890 | } else { |
||||||
3891 | $this->DebugMessage('$this->ImageCreateFromStringReplacement($this->exif_thumbnail_data, false) failed', __FILE__, __LINE__); |
||||||
3892 | } |
||||||
3893 | |||||||
3894 | break; |
||||||
3895 | } |
||||||
3896 | |||||||
3897 | if (!$this->gdimg_source) { |
||||||
3898 | $this->DebugMessage('$this->gdimg_source is still empty', __FILE__, __LINE__); |
||||||
3899 | |||||||
3900 | $this->DebugMessage('ImageMagickThumbnailToGD() failed', __FILE__, __LINE__); |
||||||
3901 | |||||||
3902 | $imageHeader = ''; |
||||||
3903 | $gd_info = gd_info(); |
||||||
3904 | $GDreadSupport = false; |
||||||
3905 | switch (@$this->getimagesizeinfo[2]) { |
||||||
3906 | case 1: |
||||||
3907 | $imageHeader = 'Content-Type: image/gif'; |
||||||
3908 | $GDreadSupport = (bool) @$gd_info['GIF Read Support']; |
||||||
3909 | break; |
||||||
3910 | case 2: |
||||||
3911 | $imageHeader = 'Content-Type: image/jpeg'; |
||||||
3912 | $GDreadSupport = (bool) @$gd_info['JPG Support']; |
||||||
3913 | break; |
||||||
3914 | case 3: |
||||||
3915 | $imageHeader = 'Content-Type: image/png'; |
||||||
3916 | $GDreadSupport = (bool) @$gd_info['PNG Support']; |
||||||
3917 | break; |
||||||
3918 | } |
||||||
3919 | if ($imageHeader) { |
||||||
3920 | // cannot create image for whatever reason (maybe imagecreatefromjpeg et al are not available?) |
||||||
3921 | // and ImageMagick is not available either, no choice but to output original (not resized/modified) data and exit |
||||||
3922 | if ($this->config_error_die_on_source_failure) { |
||||||
3923 | $errormessages = array(); |
||||||
3924 | $errormessages[] = 'All attempts to create GD image source failed.'; |
||||||
3925 | if ($this->fatalerror) { |
||||||
3926 | $errormessages[] = $this->fatalerror; |
||||||
3927 | } |
||||||
3928 | if ($this->issafemode) { |
||||||
3929 | $errormessages[] = 'Safe Mode enabled, therefore ImageMagick is unavailable. (disable Safe Mode if possible)'; |
||||||
3930 | } elseif (!$this->ImageMagickVersion()) { |
||||||
3931 | $errormessages[] = 'ImageMagick is not installed (it is highly recommended that you install it).'; |
||||||
3932 | } |
||||||
3933 | if ($this->SourceImageIsTooLarge($this->getimagesizeinfo[0], $this->getimagesizeinfo[1])) { |
||||||
3934 | $memory_get_usage = (function_exists('memory_get_usage') ? memory_get_usage() : 0); |
||||||
3935 | $errormessages[] = 'Source image is too large ('.$this->getimagesizeinfo[0].'x'.$this->getimagesizeinfo[1].' = '.number_format($this->getimagesizeinfo[0] * $this->getimagesizeinfo[1] / 1000000, 1).'Mpx, max='.number_format($this->config_max_source_pixels / 1000000, 1).'Mpx) for GD creation (either install ImageMagick or increase PHP memory_limit to at least '.ceil(($memory_get_usage + (5 * $this->getimagesizeinfo[0] * $this->getimagesizeinfo[1])) / 1048576).'M).'; |
||||||
3936 | } elseif (!$GDreadSupport) { |
||||||
3937 | $errormessages[] = 'GD does not have read support for "'.$imageHeader.'".'; |
||||||
3938 | } else { |
||||||
3939 | $errormessages[] = 'Source image probably corrupt.'; |
||||||
3940 | } |
||||||
3941 | $this->ErrorImage(implode("\n", $errormessages)); |
||||||
3942 | |||||||
3943 | } else { |
||||||
3944 | $this->DebugMessage('All attempts to create GD image source failed ('.(ini_get('safe_mode') ? 'Safe Mode enabled, ImageMagick unavailable and source image probably too large for GD': ($GDreadSupport ? 'source image probably corrupt' : 'GD does not have read support for "'.$imageHeader.'"')).'), cannot generate thumbnail'); |
||||||
3945 | //$this->DebugMessage('All attempts to create GD image source failed ('.($GDreadSupport ? 'source image probably corrupt' : 'GD does not have read support for "'.$imageHeader.'"').'), outputing raw image', __FILE__, __LINE__); |
||||||
3946 | //if (!$this->phpThumbDebug) { |
||||||
3947 | // header($imageHeader); |
||||||
3948 | // echo $this->rawImageData; |
||||||
3949 | // exit; |
||||||
3950 | //} |
||||||
3951 | return false; |
||||||
3952 | } |
||||||
3953 | } |
||||||
3954 | |||||||
3955 | //switch (substr($this->rawImageData, 0, 2)) { |
||||||
3956 | // case 'BM': |
||||||
3957 | switch (@$this->getimagesizeinfo[2]) { |
||||||
3958 | case 6: |
||||||
3959 | ob_start(); |
||||||
3960 | if (!@include_once __DIR__ .'/phpthumb.bmp.php' ) { |
||||||
3961 | ob_end_clean(); |
||||||
3962 | return $this->ErrorImage('include_once('. __DIR__ .'/phpthumb.bmp.php) failed'); |
||||||
3963 | } |
||||||
3964 | ob_end_clean(); |
||||||
3965 | if ($fp = @fopen($this->sourceFilename, 'rb')) { |
||||||
3966 | $this->rawImageData = ''; |
||||||
3967 | while (!feof($fp)) { |
||||||
3968 | $this->rawImageData .= fread($fp, 32768); |
||||||
3969 | } |
||||||
3970 | fclose($fp); |
||||||
3971 | } |
||||||
3972 | $phpthumb_bmp = new phpthumb_bmp(); |
||||||
3973 | $this->gdimg_source = $phpthumb_bmp->phpthumb_bmp2gd($this->rawImageData, phpthumb_functions::gd_version() >= 2.0); |
||||||
3974 | unset($phpthumb_bmp); |
||||||
3975 | if ($this->gdimg_source) { |
||||||
3976 | $this->DebugMessage('$phpthumb_bmp->phpthumb_bmp2gd() succeeded', __FILE__, __LINE__); |
||||||
3977 | } else { |
||||||
3978 | return $this->ErrorImage($this->ImageMagickVersion() ? 'ImageMagick failed on BMP source conversion' : 'phpthumb_bmp2gd() failed'); |
||||||
3979 | } |
||||||
3980 | break; |
||||||
3981 | //} |
||||||
3982 | //switch (substr($this->rawImageData, 0, 4)) { |
||||||
3983 | // case 'II'."\x2A\x00": |
||||||
3984 | // case 'MM'."\x00\x2A": |
||||||
3985 | case 7: |
||||||
3986 | case 8: |
||||||
3987 | return $this->ErrorImage($this->ImageMagickVersion() ? 'ImageMagick failed on TIFF source conversion' : 'ImageMagick is unavailable and phpThumb() does not support TIFF source images without it'); |
||||||
3988 | break; |
||||||
3989 | |||||||
3990 | //case "\xD7\xCD\xC6\x9A": |
||||||
3991 | // return $this->ErrorImage($this->ImageMagickVersion() ? 'ImageMagick failed on WMF source conversion' : 'ImageMagick is unavailable and phpThumb() does not support WMF source images without it'); |
||||||
3992 | // break; |
||||||
3993 | } |
||||||
3994 | |||||||
3995 | if (!$this->gdimg_source) { |
||||||
3996 | if ($this->rawImageData) { |
||||||
3997 | $HeaderFourBytes = substr($this->rawImageData, 0, 4); |
||||||
3998 | } elseif ($this->sourceFilename) { |
||||||
3999 | if ($fp = @fopen($this->sourceFilename, 'rb')) { |
||||||
4000 | $HeaderFourBytes = fread($fp, 4); |
||||||
4001 | fclose($fp); |
||||||
4002 | } else { |
||||||
4003 | return $this->ErrorImage('failed to open "'.$this->sourceFilename.'" SourceImageToGD() ['.__LINE__.']'); |
||||||
4004 | } |
||||||
4005 | } else { |
||||||
4006 | return $this->ErrorImage('Unable to create image, neither filename nor image data suppplied in SourceImageToGD() ['.__LINE__.']'); |
||||||
4007 | } |
||||||
4008 | if (!$this->ImageMagickVersion() && !phpthumb_functions::gd_version()) { |
||||||
4009 | return $this->ErrorImage('Neither GD nor ImageMagick seem to be installed on this server. At least one (preferably GD), or better both, MUST be installed for phpThumb to work.'); |
||||||
4010 | } elseif ($HeaderFourBytes == "\xD7\xCD\xC6\x9A") { // WMF |
||||||
4011 | return $this->ErrorImage($this->ImageMagickVersion() ? 'ImageMagick failed on WMF source conversion' : 'ImageMagick is unavailable and phpThumb() does not support WMF source images without it'); |
||||||
4012 | } elseif ($HeaderFourBytes == '%PDF') { // "%PDF" |
||||||
4013 | return $this->ErrorImage($this->ImageMagickVersion() ? 'ImageMagick and GhostScript are both required for PDF source images; GhostScript may not be properly configured' : 'ImageMagick and/or GhostScript are unavailable and phpThumb() does not support PDF source images without them'); |
||||||
4014 | } elseif (substr($HeaderFourBytes, 0, 3) == "\xFF\xD8\xFF") { // JPEG |
||||||
4015 | return $this->ErrorImage('Image (JPEG) is too large for PHP-GD memory_limit, please install ImageMagick or increase php.ini memory_limit setting'); |
||||||
4016 | } elseif ($HeaderFourBytes == '%PNG') { // "%PNG" |
||||||
4017 | return $this->ErrorImage('Image (PNG) is too large for PHP-GD memory_limit, please install ImageMagick or increase php.ini memory_limit setting'); |
||||||
4018 | } elseif (substr($HeaderFourBytes, 0, 3) == 'GIF') { // GIF |
||||||
4019 | return $this->ErrorImage('Image (GIF) is too large for PHP-GD memory_limit, please install ImageMagick or increase php.ini memory_limit setting'); |
||||||
4020 | } |
||||||
4021 | return $this->ErrorImage('Unknown image type identified by "'.$HeaderFourBytes.'" ('.phpthumb_functions::HexCharDisplay($HeaderFourBytes).') in SourceImageToGD() ['.__LINE__.']'); |
||||||
4022 | } |
||||||
4023 | } |
||||||
4024 | |||||||
4025 | if (!$this->gdimg_source) { |
||||||
4026 | if ($gdimg_exif_temp = $this->ImageCreateFromStringReplacement($this->exif_thumbnail_data, false)) { |
||||||
4027 | $this->DebugMessage('All other attempts failed, but successfully using EXIF thumbnail as source image', __FILE__, __LINE__); |
||||||
4028 | $this->gdimg_source = $gdimg_exif_temp; |
||||||
4029 | // override allow-enlarging setting if EXIF thumbnail is the only source available |
||||||
4030 | // otherwise thumbnails larger than the EXIF thumbnail will be created at EXIF size |
||||||
4031 | $this->aoe = true; |
||||||
4032 | return true; |
||||||
4033 | } |
||||||
4034 | return false; |
||||||
4035 | } |
||||||
4036 | |||||||
4037 | $this->source_width = imagesx($this->gdimg_source); |
||||||
4038 | $this->source_height = imagesy($this->gdimg_source); |
||||||
4039 | return true; |
||||||
4040 | } |
||||||
4041 | |||||||
4042 | private function ImageCropAuto() { |
||||||
4043 | // ImageCropAuto |
||||||
4044 | if (!is_null($this->ica)) { |
||||||
4045 | $this->DebugMessage('ImageCropAuto('.$this->ica.') starting', __FILE__, __LINE__); |
||||||
4046 | if (function_exists('imagecropauto')) { // (PHP 5 >= 5.5.0, PHP 7) |
||||||
4047 | // https://www.php.net/manual/en/function.imagecropauto.php |
||||||
4048 | // 0 = IMG_CROP_DEFAULT |
||||||
4049 | // 1 = IMG_CROP_TRANSPARENT |
||||||
4050 | // 2 = IMG_CROP_BLACK |
||||||
4051 | // 3 = IMG_CROP_WHITE |
||||||
4052 | // 4 = IMG_CROP_SIDES |
||||||
4053 | // 5 = IMG_CROP_THRESHOLD |
||||||
4054 | if (preg_match('#^(([0-4])|(5)\\|(0?\\.?[0-9]+)\\|([0-9A-F]{6}))$#i', $this->ica, $matches)) { |
||||||
4055 | @list($dummy, $dummy, $ica_mode1, $ica_mode2, $ica_threshold, $ica_color) = $matches; |
||||||
4056 | if ($ica_mode2) { |
||||||
4057 | $param_color = hexdec($ica_color); |
||||||
4058 | if (!imageistruecolor($this->gdimg_source)) { |
||||||
4059 | $param_color = imagecolorclosest($this->gdimg_source, hexdec(substr($ica_color, 0, 2)), hexdec(substr($ica_color, 2, 2)), hexdec(substr($ica_color, 4, 2))); |
||||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
It seems like
hexdec(substr($ica_color, 2, 2)) can also be of type double ; however, parameter $green of imagecolorclosest() does only seem to accept integer , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
It seems like
hexdec(substr($ica_color, 4, 2)) can also be of type double ; however, parameter $blue of imagecolorclosest() does only seem to accept integer , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
4060 | } |
||||||
4061 | $cropped = imagecropauto($this->gdimg_source, intval($ica_mode2), floatval($ica_threshold), $param_color); |
||||||
0 ignored issues
–
show
It seems like
$param_color can also be of type double ; however, parameter $color of imagecropauto() does only seem to accept integer , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
4062 | } else { |
||||||
4063 | $cropped = imagecropauto($this->gdimg_source, intval($ica_mode1)); |
||||||
4064 | } |
||||||
4065 | if ($cropped !== false) { // in case a new image resource was returned |
||||||
4066 | $this->DebugMessage('ImageCropAuto changing source image size from '.imagesx($this->gdimg_source).'x'.imagesy($this->gdimg_source).' to '.imagesx($cropped).'x'.imagesy($cropped), __FILE__, __LINE__); |
||||||
0 ignored issues
–
show
It seems like
$cropped can also be of type true ; however, parameter $image of imagesy() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
It seems like
$cropped can also be of type true ; however, parameter $image of imagesx() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
4067 | imagedestroy($this->gdimg_source); // we destroy the original image |
||||||
4068 | $this->gdimg_source = $cropped; // and assign the cropped image to $im |
||||||
4069 | $this->source_width = imagesx($this->gdimg_source); |
||||||
4070 | $this->source_height = imagesy($this->gdimg_source); |
||||||
4071 | } else { |
||||||
4072 | $this->DebugMessage('imagecropauto failed', __FILE__, __LINE__); |
||||||
4073 | } |
||||||
4074 | } else { |
||||||
4075 | $this->DebugMessage('invalid "ica" parameter syntax, ignoring', __FILE__, __LINE__); |
||||||
4076 | } |
||||||
4077 | } else { |
||||||
4078 | $this->DebugMessage('!function_exists(imagecropauto), ignoring "ica" parameter', __FILE__, __LINE__); |
||||||
4079 | } |
||||||
4080 | } |
||||||
4081 | return true; |
||||||
4082 | } |
||||||
4083 | |||||||
4084 | public function phpThumbDebugVarDump($var) { |
||||||
4085 | if (null === $var) { |
||||||
4086 | return 'NULL'; |
||||||
4087 | } elseif (is_bool($var)) { |
||||||
4088 | return ($var ? 'TRUE' : 'FALSE'); |
||||||
4089 | } elseif (is_string($var)) { |
||||||
4090 | return 'string('.strlen($var).')'.str_repeat(' ', max(0, 3 - strlen(strlen($var)))).' "'.$var.'"'; |
||||||
4091 | } elseif (is_int($var)) { |
||||||
4092 | return 'integer '.$var; |
||||||
4093 | } elseif (is_float($var)) { |
||||||
4094 | return 'float '.$var; |
||||||
4095 | } elseif (is_array($var)) { |
||||||
4096 | ob_start(); |
||||||
4097 | var_dump($var); |
||||||
4098 | $vardumpoutput = ob_get_contents(); |
||||||
4099 | ob_end_clean(); |
||||||
4100 | return strtr($vardumpoutput, "\n\r\t", ' '); |
||||||
4101 | } |
||||||
4102 | return gettype($var); |
||||||
4103 | } |
||||||
4104 | |||||||
4105 | public function phpThumbDebug($level='') { |
||||||
4106 | if ($level && ($this->phpThumbDebug !== $level)) { |
||||||
4107 | return true; |
||||||
4108 | } |
||||||
4109 | if ($this->config_disable_debug) { |
||||||
4110 | return $this->ErrorImage('phpThumbDebug disabled'); |
||||||
4111 | } |
||||||
4112 | |||||||
4113 | $FunctionsExistance = array('exif_thumbnail', 'gd_info', 'image_type_to_mime_type', 'getimagesize', 'imagecopyresampled', 'imagecopyresized', 'imagecreate', 'imagecreatefromstring', 'imagecreatetruecolor', 'imageistruecolor', 'imagerotate', 'imagetypes', 'version_compare', 'imagecreatefromgif', 'imagecreatefromjpeg', 'imagecreatefrompng', 'imagecreatefromwbmp', 'imagecreatefromxbm', 'imagecreatefromxpm', 'imagecreatefromstring', 'imagecreatefromgd', 'imagecreatefromgd2', 'imagecreatefromgd2part', 'imagejpeg', 'imagegif', 'imagepng', 'imagewbmp'); |
||||||
4114 | $ParameterNames = array('src', 'new', 'w', 'h', 'f', 'q', 'sx', 'sy', 'sw', 'sh', 'far', 'bg', 'bc', 'zc', 'ica', 'file', 'goto', 'err', 'xto', 'ra', 'ar', 'aoe', 'iar', 'maxb'); |
||||||
4115 | $ConfigVariableNames = array('document_root', 'temp_directory', 'output_format', 'output_maxwidth', 'output_maxheight', 'error_message_image_default', 'error_bgcolor', 'error_textcolor', 'error_fontsize', 'error_die_on_error', 'error_silent_die_on_error', 'error_die_on_source_failure', 'nohotlink_enabled', 'nohotlink_valid_domains', 'nohotlink_erase_image', 'nohotlink_text_message', 'nooffsitelink_enabled', 'nooffsitelink_valid_domains', 'nooffsitelink_require_refer', 'nooffsitelink_erase_image', 'nooffsitelink_text_message', 'high_security_enabled', 'allow_src_above_docroot', 'allow_src_above_phpthumb', 'max_source_pixels', 'use_exif_thumbnail_for_speed', 'border_hexcolor', 'background_hexcolor', 'ttf_directory', 'disable_pathinfo_parsing', 'disable_imagecopyresampled'); |
||||||
4116 | $OtherVariableNames = array('phpThumbDebug', 'thumbnailQuality', 'thumbnailFormat', 'gdimg_output', 'gdimg_source', 'sourceFilename', 'source_width', 'source_height', 'thumbnailCropX', 'thumbnailCropY', 'thumbnailCropW', 'thumbnailCropH', 'exif_thumbnail_width', 'exif_thumbnail_height', 'exif_thumbnail_type', 'thumbnail_width', 'thumbnail_height', 'thumbnail_image_width', 'thumbnail_image_height'); |
||||||
4117 | |||||||
4118 | $DebugOutput = array(); |
||||||
4119 | $DebugOutput[] = 'phpThumb() version = '.$this->phpthumb_version; |
||||||
4120 | $DebugOutput[] = 'phpversion() = '.@PHP_VERSION; |
||||||
4121 | $DebugOutput[] = 'PHP_OS = '.PHP_OS; |
||||||
4122 | $DebugOutput[] = '$_SERVER[SERVER_SOFTWARE] = '.@$_SERVER['SERVER_SOFTWARE']; |
||||||
4123 | $DebugOutput[] = '__FILE__ = '.__FILE__; |
||||||
4124 | $DebugOutput[] = 'realpath(.) = '.@realpath('.'); |
||||||
4125 | $DebugOutput[] = '$_SERVER[PHP_SELF] = '.@$_SERVER['PHP_SELF']; |
||||||
4126 | $DebugOutput[] = '$_SERVER[HOST_NAME] = '.@$_SERVER['HOST_NAME']; |
||||||
4127 | $DebugOutput[] = '$_SERVER[HTTP_REFERER] = '.@$_SERVER['HTTP_REFERER']; |
||||||
4128 | $DebugOutput[] = '$_SERVER[QUERY_STRING] = '.@$_SERVER['QUERY_STRING']; |
||||||
4129 | $DebugOutput[] = '$_SERVER[PATH_INFO] = '.@$_SERVER['PATH_INFO']; |
||||||
4130 | $DebugOutput[] = '$_SERVER[DOCUMENT_ROOT] = '.@$_SERVER['DOCUMENT_ROOT']; |
||||||
4131 | $DebugOutput[] = 'getenv(DOCUMENT_ROOT) = '.@getenv('DOCUMENT_ROOT'); |
||||||
4132 | $DebugOutput[] = ''; |
||||||
4133 | |||||||
4134 | $DebugOutput[] = 'get_magic_quotes_gpc() = '.$this->phpThumbDebugVarDump(@get_magic_quotes_gpc()); |
||||||
4135 | $DebugOutput[] = 'get_magic_quotes_runtime() = '.$this->phpThumbDebugVarDump(@get_magic_quotes_runtime()); |
||||||
4136 | $DebugOutput[] = 'error_reporting() = '.$this->phpThumbDebugVarDump(error_reporting()); |
||||||
4137 | $DebugOutput[] = 'ini_get(error_reporting) = '.$this->phpThumbDebugVarDump(@ini_get('error_reporting')); |
||||||
4138 | $DebugOutput[] = 'ini_get(display_errors) = '.$this->phpThumbDebugVarDump(@ini_get('display_errors')); |
||||||
4139 | $DebugOutput[] = 'ini_get(allow_url_fopen) = '.$this->phpThumbDebugVarDump(@ini_get('allow_url_fopen')); |
||||||
4140 | $DebugOutput[] = 'ini_get(disable_functions) = '.$this->phpThumbDebugVarDump(@ini_get('disable_functions')); |
||||||
4141 | $DebugOutput[] = 'get_cfg_var(disable_functions) = '.$this->phpThumbDebugVarDump(@get_cfg_var('disable_functions')); |
||||||
4142 | $DebugOutput[] = 'ini_get(safe_mode) = '.$this->phpThumbDebugVarDump(@ini_get('safe_mode')); |
||||||
4143 | $DebugOutput[] = 'ini_get(open_basedir) = '.$this->phpThumbDebugVarDump(@ini_get('open_basedir')); |
||||||
4144 | $DebugOutput[] = 'ini_get(max_execution_time) = '.$this->phpThumbDebugVarDump(@ini_get('max_execution_time')); |
||||||
4145 | $DebugOutput[] = 'ini_get(memory_limit) = '.$this->phpThumbDebugVarDump(@ini_get('memory_limit')); |
||||||
4146 | $DebugOutput[] = 'get_cfg_var(memory_limit) = '.$this->phpThumbDebugVarDump(@get_cfg_var('memory_limit')); |
||||||
4147 | $DebugOutput[] = 'memory_get_usage() = '.(function_exists('memory_get_usage') ? $this->phpThumbDebugVarDump(@memory_get_usage()) : 'n/a'); |
||||||
4148 | $DebugOutput[] = ''; |
||||||
4149 | |||||||
4150 | $DebugOutput[] = '$this->config_prefer_imagemagick = '.$this->phpThumbDebugVarDump($this->config_prefer_imagemagick); |
||||||
4151 | $DebugOutput[] = '$this->config_imagemagick_path = '.$this->phpThumbDebugVarDump($this->config_imagemagick_path); |
||||||
4152 | $DebugOutput[] = '$this->ImageMagickWhichConvert() = '.$this->ImageMagickWhichConvert(); |
||||||
4153 | $IMpathUsed = ($this->config_imagemagick_path ? $this->config_imagemagick_path : $this->ImageMagickWhichConvert()); |
||||||
4154 | $DebugOutput[] = '[actual ImageMagick path used] = '.$this->phpThumbDebugVarDump($IMpathUsed); |
||||||
4155 | $DebugOutput[] = 'file_exists([actual ImageMagick path used]) = '.$this->phpThumbDebugVarDump(@file_exists($IMpathUsed)); |
||||||
4156 | $DebugOutput[] = 'ImageMagickVersion(false) = '.$this->ImageMagickVersion(false); |
||||||
4157 | $DebugOutput[] = 'ImageMagickVersion(true) = '.$this->ImageMagickVersion(true); |
||||||
4158 | $DebugOutput[] = ''; |
||||||
4159 | |||||||
4160 | $DebugOutput[] = '$this->config_cache_directory = '.$this->phpThumbDebugVarDump($this->config_cache_directory); |
||||||
4161 | $DebugOutput[] = '$this->config_cache_directory_depth = '.$this->phpThumbDebugVarDump($this->config_cache_directory_depth); |
||||||
4162 | $DebugOutput[] = '$this->config_cache_disable_warning = '.$this->phpThumbDebugVarDump($this->config_cache_disable_warning); |
||||||
4163 | $DebugOutput[] = '$this->config_cache_maxage = '.$this->phpThumbDebugVarDump($this->config_cache_maxage); |
||||||
4164 | $DebugOutput[] = '$this->config_cache_maxsize = '.$this->phpThumbDebugVarDump($this->config_cache_maxsize); |
||||||
4165 | $DebugOutput[] = '$this->config_cache_maxfiles = '.$this->phpThumbDebugVarDump($this->config_cache_maxfiles); |
||||||
4166 | $DebugOutput[] = '$this->config_cache_force_passthru = '.$this->phpThumbDebugVarDump($this->config_cache_force_passthru); |
||||||
4167 | $DebugOutput[] = '$this->cache_filename = '.$this->phpThumbDebugVarDump($this->cache_filename); |
||||||
4168 | $DebugOutput[] = 'is_readable($this->config_cache_directory) = '.$this->phpThumbDebugVarDump(@is_readable($this->config_cache_directory)); |
||||||
4169 | $DebugOutput[] = 'is_writable($this->config_cache_directory) = '.$this->phpThumbDebugVarDump(@is_writable($this->config_cache_directory)); |
||||||
4170 | $DebugOutput[] = 'is_readable($this->cache_filename) = '.$this->phpThumbDebugVarDump(@is_readable($this->cache_filename)); |
||||||
4171 | $DebugOutput[] = 'is_writable($this->cache_filename) = '.(@file_exists($this->cache_filename) ? $this->phpThumbDebugVarDump(@is_writable($this->cache_filename)) : 'n/a'); |
||||||
4172 | $DebugOutput[] = ''; |
||||||
4173 | |||||||
4174 | foreach ($ConfigVariableNames as $varname) { |
||||||
4175 | $varname = 'config_'.$varname; |
||||||
4176 | $value = $this->$varname; |
||||||
4177 | $DebugOutput[] = '$this->'.str_pad($varname, 37, ' ', STR_PAD_RIGHT).' = '.$this->phpThumbDebugVarDump($value); |
||||||
4178 | } |
||||||
4179 | $DebugOutput[] = ''; |
||||||
4180 | foreach ($OtherVariableNames as $varname) { |
||||||
4181 | $value = $this->$varname; |
||||||
4182 | $DebugOutput[] = '$this->'.str_pad($varname, 27, ' ', STR_PAD_RIGHT).' = '.$this->phpThumbDebugVarDump($value); |
||||||
4183 | } |
||||||
4184 | $DebugOutput[] = 'strlen($this->rawImageData) = '.strlen(@$this->rawImageData); |
||||||
4185 | $DebugOutput[] = 'strlen($this->exif_thumbnail_data) = '.strlen(@$this->exif_thumbnail_data); |
||||||
4186 | $DebugOutput[] = ''; |
||||||
4187 | |||||||
4188 | foreach ($ParameterNames as $varname) { |
||||||
4189 | $value = $this->$varname; |
||||||
4190 | $DebugOutput[] = '$this->'.str_pad($varname, 4, ' ', STR_PAD_RIGHT).' = '.$this->phpThumbDebugVarDump($value); |
||||||
4191 | } |
||||||
4192 | $DebugOutput[] = ''; |
||||||
4193 | |||||||
4194 | foreach ($FunctionsExistance as $functionname) { |
||||||
4195 | $DebugOutput[] = 'builtin_function_exists('.$functionname.')'.str_repeat(' ', 23 - strlen($functionname)).' = '.$this->phpThumbDebugVarDump(phpthumb_functions::builtin_function_exists($functionname)); |
||||||
4196 | } |
||||||
4197 | $DebugOutput[] = ''; |
||||||
4198 | |||||||
4199 | $gd_info = gd_info(); |
||||||
4200 | foreach ($gd_info as $key => $value) { |
||||||
4201 | $DebugOutput[] = 'gd_info.'.str_pad($key, 34, ' ', STR_PAD_RIGHT).' = '.$this->phpThumbDebugVarDump($value); |
||||||
4202 | } |
||||||
4203 | $DebugOutput[] = ''; |
||||||
4204 | |||||||
4205 | $exif_info = phpthumb_functions::exif_info(); |
||||||
4206 | foreach ($exif_info as $key => $value) { |
||||||
4207 | $DebugOutput[] = 'exif_info.'.str_pad($key, 26, ' ', STR_PAD_RIGHT).' = '.$this->phpThumbDebugVarDump($value); |
||||||
4208 | } |
||||||
4209 | $DebugOutput[] = ''; |
||||||
4210 | |||||||
4211 | if ($ApacheLookupURIarray = phpthumb_functions::ApacheLookupURIarray(dirname(@$_SERVER['PHP_SELF']))) { |
||||||
4212 | foreach ($ApacheLookupURIarray as $key => $value) { |
||||||
4213 | $DebugOutput[] = 'ApacheLookupURIarray.'.str_pad($key, 15, ' ', STR_PAD_RIGHT).' = '.$this->phpThumbDebugVarDump($value); |
||||||
4214 | } |
||||||
4215 | } else { |
||||||
4216 | $DebugOutput[] = 'ApacheLookupURIarray() -- FAILED'; |
||||||
4217 | } |
||||||
4218 | $DebugOutput[] = ''; |
||||||
4219 | |||||||
4220 | if (isset($_GET) && is_array($_GET)) { |
||||||
4221 | foreach ($_GET as $key => $value) { |
||||||
4222 | $DebugOutput[] = '$_GET['.$key.']'.str_repeat(' ', 30 - strlen($key)).'= '.$this->phpThumbDebugVarDump($value); |
||||||
4223 | } |
||||||
4224 | } |
||||||
4225 | if (isset($_POST) && is_array($_POST)) { |
||||||
4226 | foreach ($_POST as $key => $value) { |
||||||
4227 | $DebugOutput[] = '$_POST['.$key.']'.str_repeat(' ', 29 - strlen($key)).'= '.$this->phpThumbDebugVarDump($value); |
||||||
4228 | } |
||||||
4229 | } |
||||||
4230 | $DebugOutput[] = ''; |
||||||
4231 | |||||||
4232 | $DebugOutput[] = '$this->debugmessages:'; |
||||||
4233 | foreach ($this->debugmessages as $errorstring) { |
||||||
4234 | $DebugOutput[] = ' * '.$errorstring; |
||||||
4235 | } |
||||||
4236 | $DebugOutput[] = ''; |
||||||
4237 | |||||||
4238 | $DebugOutput[] = '$this->debugtiming:'; |
||||||
4239 | foreach ($this->debugtiming as $timestamp => $timingstring) { |
||||||
4240 | $DebugOutput[] = ' * '.$timestamp.' '.$timingstring; |
||||||
4241 | } |
||||||
4242 | $DebugOutput[] = ' * Total processing time: '.number_format(max(array_keys($this->debugtiming)) - min(array_keys($this->debugtiming)), 6); |
||||||
4243 | |||||||
4244 | $this->f = (isset($_GET['f']) ? $_GET['f'] : $this->f); // debug modes 0-2 don't recognize text mode otherwise |
||||||
4245 | return $this->ErrorImage(implode("\n", $DebugOutput), 700, 500, true); |
||||||
4246 | } |
||||||
4247 | |||||||
4248 | public function FatalError($text) { |
||||||
4249 | if (null === $this->fatalerror) { |
||||||
4250 | $this->fatalerror = $text; |
||||||
4251 | } |
||||||
4252 | return true; |
||||||
4253 | } |
||||||
4254 | |||||||
4255 | public function ErrorImage($text, $width=0, $height=0, $forcedisplay=false) { |
||||||
4256 | $width = ($width ? $width : $this->config_error_image_width); |
||||||
4257 | $height = ($height ? $height : $this->config_error_image_height); |
||||||
4258 | |||||||
4259 | $text = 'phpThumb() v'.$this->phpthumb_version."\n".'http://phpthumb.sourceforge.net'."\n\n".($this->config_disable_debug ? 'Error messages disabled.'."\n\n".'edit phpThumb.config.php and (temporarily) set'."\n".'$PHPTHUMB_CONFIG[\'disable_debug\'] = false;'."\n".'to view the details of this error' : $text); |
||||||
4260 | |||||||
4261 | $this->FatalError($text); |
||||||
4262 | $this->DebugMessage($text, __FILE__, __LINE__); |
||||||
4263 | $this->purgeTempFiles(); |
||||||
4264 | if ($this->config_error_silent_die_on_error) { |
||||||
4265 | exit; |
||||||
4266 | } |
||||||
4267 | if ($this->phpThumbDebug && !$forcedisplay) { |
||||||
4268 | return false; |
||||||
4269 | } |
||||||
4270 | if (!$this->config_error_die_on_error && !$forcedisplay) { |
||||||
4271 | return false; |
||||||
4272 | } |
||||||
4273 | if ($this->err || $this->config_error_message_image_default) { |
||||||
4274 | // Show generic custom error image instead of error message |
||||||
4275 | // for use on production sites where you don't want debug messages |
||||||
4276 | if (($this->err == 'showerror') || $this->phpThumbDebug) { |
||||||
4277 | // fall through and actually show error message even if default error image is set |
||||||
4278 | } else { |
||||||
4279 | header('Location: '.($this->err ? $this->err : $this->config_error_message_image_default)); |
||||||
4280 | exit; |
||||||
4281 | } |
||||||
4282 | } |
||||||
4283 | $this->setOutputFormat(); |
||||||
4284 | if (!$this->thumbnailFormat || !$this->config_disable_debug || (phpthumb_functions::gd_version() < 1)) { |
||||||
4285 | $this->thumbnailFormat = 'text'; |
||||||
4286 | } |
||||||
4287 | if (@$this->thumbnailFormat == 'text') { |
||||||
4288 | // bypass all GD functions and output text error message |
||||||
4289 | if (!headers_sent()) { |
||||||
4290 | header('Content-type: text/plain'); |
||||||
4291 | echo $text; |
||||||
4292 | } else { |
||||||
4293 | echo '<pre>'.htmlspecialchars($text).'</pre>'; |
||||||
4294 | } |
||||||
4295 | exit; |
||||||
4296 | } |
||||||
4297 | |||||||
4298 | $FontWidth = imagefontwidth($this->config_error_fontsize); |
||||||
4299 | $FontHeight = imagefontheight($this->config_error_fontsize); |
||||||
4300 | |||||||
4301 | $LinesOfText = explode("\n", @wordwrap($text, floor($width / $FontWidth), "\n", true)); |
||||||
4302 | $height = max($height, count($LinesOfText) * $FontHeight); |
||||||
4303 | |||||||
4304 | $headers_file = ''; |
||||||
4305 | $headers_line = ''; |
||||||
4306 | if (phpthumb_functions::version_compare_replacement(PHP_VERSION, '4.3.0', '>=') && headers_sent($headers_file, $headers_line)) { |
||||||
4307 | |||||||
4308 | echo "\n".'**Headers already sent in file "'.$headers_file.'" on line "'.$headers_line.'", dumping error message as text:**<br><pre>'."\n\n".$text."\n".'</pre>'; |
||||||
4309 | |||||||
4310 | } elseif (headers_sent()) { |
||||||
4311 | |||||||
4312 | echo "\n".'**Headers already sent, dumping error message as text:**<br><pre>'."\n\n".$text."\n".'</pre>'; |
||||||
4313 | |||||||
4314 | } elseif ($gdimg_error = imagecreate($width, $height)) { |
||||||
4315 | |||||||
4316 | $background_color = phpthumb_functions::ImageHexColorAllocate($gdimg_error, $this->config_error_bgcolor, true); |
||||||
4317 | $text_color = phpthumb_functions::ImageHexColorAllocate($gdimg_error, $this->config_error_textcolor, true); |
||||||
4318 | imagefilledrectangle($gdimg_error, 0, 0, $width, $height, $background_color); |
||||||
4319 | $lineYoffset = 0; |
||||||
4320 | foreach ($LinesOfText as $line) { |
||||||
4321 | imagestring($gdimg_error, $this->config_error_fontsize, 2, $lineYoffset, $line, $text_color); |
||||||
4322 | $lineYoffset += $FontHeight; |
||||||
4323 | } |
||||||
4324 | if (function_exists('imagetypes')) { |
||||||
4325 | $imagetypes = imagetypes(); |
||||||
4326 | if ($imagetypes & IMG_PNG) { |
||||||
4327 | header('Content-Type: image/png'); |
||||||
4328 | imagepng($gdimg_error); |
||||||
4329 | } elseif ($imagetypes & IMG_GIF) { |
||||||
4330 | header('Content-Type: image/gif'); |
||||||
4331 | imagegif($gdimg_error); |
||||||
4332 | } elseif ($imagetypes & IMG_JPG) { |
||||||
4333 | header('Content-Type: image/jpeg'); |
||||||
4334 | imagejpeg($gdimg_error); |
||||||
4335 | } elseif ($imagetypes & IMG_WBMP) { |
||||||
4336 | header('Content-Type: image/vnd.wap.wbmp'); |
||||||
4337 | imagewbmp($gdimg_error); |
||||||
4338 | } |
||||||
4339 | } |
||||||
4340 | imagedestroy($gdimg_error); |
||||||
4341 | |||||||
4342 | } |
||||||
4343 | if (!headers_sent()) { |
||||||
4344 | echo "\n".'**Failed to send graphical error image, dumping error message as text:**<br>'."\n\n".$text; |
||||||
4345 | } |
||||||
4346 | exit; |
||||||
4347 | } |
||||||
4348 | |||||||
4349 | public function ImageCreateFromStringReplacement(&$RawImageData, $DieOnErrors=false) { |
||||||
4350 | // there are serious bugs in the non-bundled versions of GD which may cause |
||||||
4351 | // PHP to segfault when calling imagecreatefromstring() - avoid if at all possible |
||||||
4352 | // when not using a bundled version of GD2 |
||||||
4353 | if (!phpthumb_functions::gd_version()) { |
||||||
4354 | if ($DieOnErrors) { |
||||||
4355 | if (!headers_sent()) { |
||||||
4356 | // base64-encoded error image in GIF format |
||||||
4357 | $ERROR_NOGD = 'R0lGODlhIAAgALMAAAAAABQUFCQkJDY2NkZGRldXV2ZmZnJycoaGhpSUlKWlpbe3t8XFxdXV1eTk5P7+/iwAAAAAIAAgAAAE/vDJSau9WILtTAACUinDNijZtAHfCojS4W5H+qxD8xibIDE9h0OwWaRWDIljJSkUJYsN4bihMB8th3IToAKs1VtYM75cyV8sZ8vygtOE5yMKmGbO4jRdICQCjHdlZzwzNW4qZSQmKDaNjhUMBX4BBAlmMywFSRWEmAI6b5gAlhNxokGhooAIK5o/pi9vEw4Lfj4OLTAUpj6IabMtCwlSFw0DCKBoFqwAB04AjI54PyZ+yY3TD0ss2YcVmN/gvpcu4TOyFivWqYJlbAHPpOntvxNAACcmGHjZzAZqzSzcq5fNjxFmAFw9iFRunD1epU6tsIPmFCAJnWYE0FURk7wJDA0MTKpEzoWAAskiAAA7'; |
||||||
4358 | header('Content-Type: image/gif'); |
||||||
4359 | echo base64_decode($ERROR_NOGD); |
||||||
4360 | } else { |
||||||
4361 | echo '*** ERROR: No PHP-GD support available ***'; |
||||||
4362 | } |
||||||
4363 | exit; |
||||||
4364 | } else { |
||||||
4365 | $this->DebugMessage('ImageCreateFromStringReplacement() failed: gd_version says "'.phpthumb_functions::gd_version().'"', __FILE__, __LINE__); |
||||||
4366 | return false; |
||||||
4367 | } |
||||||
4368 | } |
||||||
4369 | if (phpthumb_functions::gd_is_bundled()) { |
||||||
4370 | $this->DebugMessage('ImageCreateFromStringReplacement() calling built-in imagecreatefromstring()', __FILE__, __LINE__); |
||||||
4371 | return @imagecreatefromstring($RawImageData); |
||||||
4372 | } |
||||||
4373 | if ($this->issafemode) { |
||||||
4374 | $this->DebugMessage('ImageCreateFromStringReplacement() failed: cannot create temp file in SAFE_MODE', __FILE__, __LINE__); |
||||||
4375 | return false; |
||||||
4376 | } |
||||||
4377 | |||||||
4378 | switch (substr($RawImageData, 0, 3)) { |
||||||
4379 | case 'GIF': |
||||||
4380 | $ICFSreplacementFunctionName = 'imagecreatefromgif'; |
||||||
4381 | break; |
||||||
4382 | case "\xFF\xD8\xFF": |
||||||
4383 | $ICFSreplacementFunctionName = 'imagecreatefromjpeg'; |
||||||
4384 | break; |
||||||
4385 | case "\x89".'PN': |
||||||
4386 | $ICFSreplacementFunctionName = 'imagecreatefrompng'; |
||||||
4387 | break; |
||||||
4388 | default: |
||||||
4389 | $this->DebugMessage('ImageCreateFromStringReplacement() failed: unknown fileformat signature "'.phpthumb_functions::HexCharDisplay(substr($RawImageData, 0, 3)).'"', __FILE__, __LINE__); |
||||||
4390 | return false; |
||||||
4391 | break; |
||||||
4392 | } |
||||||
4393 | $ErrorMessage = ''; |
||||||
4394 | if ($tempnam = $this->phpThumb_tempnam()) { |
||||||
4395 | if ($fp_tempnam = @fopen($tempnam, 'wb')) { |
||||||
4396 | fwrite($fp_tempnam, $RawImageData); |
||||||
4397 | fclose($fp_tempnam); |
||||||
4398 | @chmod($tempnam, $this->getParameter('config_file_create_mask')); |
||||||
4399 | if (($ICFSreplacementFunctionName == 'imagecreatefromgif') && !function_exists($ICFSreplacementFunctionName)) { |
||||||
4400 | |||||||
4401 | // Need to create from GIF file, but imagecreatefromgif does not exist |
||||||
4402 | ob_start(); |
||||||
4403 | if (!@include_once __DIR__ .'/phpthumb.gif.php' ) { |
||||||
4404 | $ErrorMessage = 'Failed to include required file "'. __DIR__ .'/phpthumb.gif.php" in '.__FILE__.' on line '.__LINE__; |
||||||
4405 | $this->DebugMessage($ErrorMessage, __FILE__, __LINE__); |
||||||
4406 | } |
||||||
4407 | ob_end_clean(); |
||||||
4408 | // gif_loadFileToGDimageResource() cannot read from raw data, write to file first |
||||||
4409 | if ($tempfilename = $this->phpThumb_tempnam()) { |
||||||
4410 | if ($fp_tempfile = @fopen($tempfilename, 'wb')) { |
||||||
4411 | fwrite($fp_tempfile, $RawImageData); |
||||||
4412 | fclose($fp_tempfile); |
||||||
4413 | $gdimg_source = gif_loadFileToGDimageResource($tempfilename); |
||||||
4414 | $this->DebugMessage('gif_loadFileToGDimageResource('.$tempfilename.') completed', __FILE__, __LINE__); |
||||||
4415 | $this->DebugMessage('deleting "'.$tempfilename.'"', __FILE__, __LINE__); |
||||||
4416 | unlink($tempfilename); |
||||||
4417 | return $gdimg_source; |
||||||
4418 | } else { |
||||||
4419 | $ErrorMessage = 'Failed to open tempfile in '.__FILE__.' on line '.__LINE__; |
||||||
4420 | $this->DebugMessage($ErrorMessage, __FILE__, __LINE__); |
||||||
4421 | } |
||||||
4422 | } else { |
||||||
4423 | $ErrorMessage = 'Failed to open generate tempfile name in '.__FILE__.' on line '.__LINE__; |
||||||
4424 | $this->DebugMessage($ErrorMessage, __FILE__, __LINE__); |
||||||
4425 | } |
||||||
4426 | |||||||
4427 | } elseif (function_exists($ICFSreplacementFunctionName) && ($gdimg_source = @$ICFSreplacementFunctionName($tempnam))) { |
||||||
4428 | |||||||
4429 | // great |
||||||
4430 | $this->DebugMessage($ICFSreplacementFunctionName.'('.$tempnam.') succeeded', __FILE__, __LINE__); |
||||||
4431 | $this->DebugMessage('deleting "'.$tempnam.'"', __FILE__, __LINE__); |
||||||
4432 | unlink($tempnam); |
||||||
4433 | return $gdimg_source; |
||||||
4434 | |||||||
4435 | } else { |
||||||
4436 | |||||||
4437 | // GD functions not available, or failed to create image |
||||||
4438 | $this->DebugMessage($ICFSreplacementFunctionName.'('.$tempnam.') '.(function_exists($ICFSreplacementFunctionName) ? 'failed' : 'does not exist'), __FILE__, __LINE__); |
||||||
4439 | if (isset($_GET['phpThumbDebug'])) { |
||||||
4440 | $this->phpThumbDebug(); |
||||||
4441 | } |
||||||
4442 | |||||||
4443 | } |
||||||
4444 | } else { |
||||||
4445 | $ErrorMessage = 'Failed to fopen('.$tempnam.', "wb") in '.__FILE__.' on line '.__LINE__."\n".'You may need to set $PHPTHUMB_CONFIG[temp_directory] in phpThumb.config.php'; |
||||||
4446 | if ($this->issafemode) { |
||||||
4447 | $ErrorMessage = 'ImageCreateFromStringReplacement() failed in '.__FILE__.' on line '.__LINE__.': cannot create temp file in SAFE_MODE'; |
||||||
4448 | } |
||||||
4449 | $this->DebugMessage($ErrorMessage, __FILE__, __LINE__); |
||||||
4450 | } |
||||||
4451 | $this->DebugMessage('deleting "'.$tempnam.'"', __FILE__, __LINE__); |
||||||
4452 | @unlink($tempnam); |
||||||
4453 | } else { |
||||||
4454 | $ErrorMessage = 'Failed to generate phpThumb_tempnam() in '.__FILE__.' on line '.__LINE__."\n".'You may need to set $PHPTHUMB_CONFIG[temp_directory] in phpThumb.config.php'; |
||||||
4455 | if ($this->issafemode) { |
||||||
4456 | $ErrorMessage = 'ImageCreateFromStringReplacement() failed in '.__FILE__.' on line '.__LINE__.': cannot create temp file in SAFE_MODE'; |
||||||
4457 | } |
||||||
4458 | } |
||||||
4459 | if ($DieOnErrors && $ErrorMessage) { |
||||||
4460 | return $this->ErrorImage($ErrorMessage); |
||||||
4461 | } |
||||||
4462 | return false; |
||||||
4463 | } |
||||||
4464 | |||||||
4465 | public function ImageResizeFunction(&$dst_im, &$src_im, $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH) { |
||||||
4466 | $this->DebugMessage('ImageResizeFunction($o, $s, '.$dstX.', '.$dstY.', '.$srcX.', '.$srcY.', '.$dstW.', '.$dstH.', '.$srcW.', '.$srcH.')', __FILE__, __LINE__); |
||||||
4467 | if (($dstW == $srcW) && ($dstH == $srcH)) { |
||||||
4468 | return imagecopy($dst_im, $src_im, $dstX, $dstY, $srcX, $srcY, $srcW, $srcH); |
||||||
4469 | } |
||||||
4470 | if (phpthumb_functions::gd_version() >= 2.0) { |
||||||
4471 | if ($this->config_disable_imagecopyresampled) { |
||||||
4472 | return phpthumb_functions::ImageCopyResampleBicubic($dst_im, $src_im, $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH); |
||||||
4473 | } |
||||||
4474 | return imagecopyresampled($dst_im, $src_im, $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH); |
||||||
4475 | } |
||||||
4476 | return imagecopyresized($dst_im, $src_im, $dstX, $dstY, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH); |
||||||
4477 | } |
||||||
4478 | |||||||
4479 | public function InitializeTempDirSetting() { |
||||||
4480 | $this->config_temp_directory = ($this->config_temp_directory ? $this->config_temp_directory : $this->realPathSafe((function_exists('sys_get_temp_dir') ? sys_get_temp_dir() : ''))); // sys_get_temp_dir added in PHP v5.2.1 |
||||||
4481 | $this->config_temp_directory = ($this->config_temp_directory ? $this->config_temp_directory : $this->realPathSafe(ini_get('upload_tmp_dir'))); |
||||||
4482 | $this->config_temp_directory = ($this->config_temp_directory ? $this->config_temp_directory : $this->realPathSafe(getenv('TMPDIR'))); |
||||||
4483 | $this->config_temp_directory = ($this->config_temp_directory ? $this->config_temp_directory : $this->realPathSafe(getenv('TMP'))); |
||||||
4484 | return true; |
||||||
4485 | } |
||||||
4486 | |||||||
4487 | public function phpThumb_tempnam() { |
||||||
4488 | $this->InitializeTempDirSetting(); |
||||||
4489 | $tempnam = $this->realPathSafe(tempnam($this->config_temp_directory, 'pThumb')); |
||||||
4490 | $this->tempFilesToDelete[$tempnam] = $tempnam; |
||||||
4491 | $this->DebugMessage('phpThumb_tempnam() returning "'.$tempnam.'"', __FILE__, __LINE__); |
||||||
4492 | return $tempnam; |
||||||
4493 | } |
||||||
4494 | |||||||
4495 | public function DebugMessage($message, $file='', $line='') { |
||||||
4496 | $this->debugmessages[] = $message.($file ? ' in file "'.(basename($file) ? basename($file) : $file).'"' : '').($line ? ' on line '.$line : ''); |
||||||
4497 | return true; |
||||||
4498 | } |
||||||
4499 | |||||||
4500 | public function DebugTimingMessage($message, $file='', $line='', $timestamp=0) { |
||||||
4501 | if (!$timestamp) { |
||||||
4502 | $timestamp = array_sum(explode(' ', microtime())); |
||||||
4503 | } |
||||||
4504 | $this->debugtiming[number_format($timestamp, 6, '.', '')] = ': '.$message.($file ? ' in file "'.(basename($file) ? basename($file) : $file).'"' : '').($line ? ' on line '.$line : ''); |
||||||
4505 | return true; |
||||||
4506 | } |
||||||
4507 | |||||||
4508 | } |
||||||
4509 |