changed git call from https to git readonly
[atutor.git] / mods / photo_album / classes / phpThumb_1.7.2 / phpthumb.functions.php
1 <?php
2 //////////////////////////////////////////////////////////////
3 ///  phpThumb() by James Heinrich <info@silisoftware.com>   //
4 //        available at http://phpthumb.sourceforge.net     ///
5 //////////////////////////////////////////////////////////////
6 ///                                                         //
7 // phpthumb.functions.php - general support functions       //
8 //                                                         ///
9 //////////////////////////////////////////////////////////////
10
11 class phpthumb_functions {
12
13         function user_function_exists($functionname) {
14                 if (function_exists('get_defined_functions')) {
15                         static $get_defined_functions = array();
16                         if (empty($get_defined_functions)) {
17                                 $get_defined_functions = get_defined_functions();
18                         }
19                         return in_array(strtolower($functionname), $get_defined_functions['user']);
20                 }
21                 return function_exists($functionname);
22         }
23
24
25         function builtin_function_exists($functionname) {
26                 if (function_exists('get_defined_functions')) {
27                         static $get_defined_functions = array();
28                         if (empty($get_defined_functions)) {
29                                 $get_defined_functions = get_defined_functions();
30                         }
31                         return in_array(strtolower($functionname), $get_defined_functions['internal']);
32                 }
33                 return function_exists($functionname);
34         }
35
36
37         function version_compare_replacement_sub($version1, $version2, $operator='') {
38                 // If you specify the third optional operator argument, you can test for a particular relationship.
39                 // The possible operators are: <, lt, <=, le, >, gt, >=, ge, ==, =, eq, !=, <>, ne respectively.
40                 // Using this argument, the function will return 1 if the relationship is the one specified by the operator, 0 otherwise.
41
42                 // If a part contains special version strings these are handled in the following order: dev < (alpha = a) < (beta = b) < RC < pl
43                 static $versiontype_lookup = array();
44                 if (empty($versiontype_lookup)) {
45                         $versiontype_lookup['dev']   = 10001;
46                         $versiontype_lookup['a']     = 10002;
47                         $versiontype_lookup['alpha'] = 10002;
48                         $versiontype_lookup['b']     = 10003;
49                         $versiontype_lookup['beta']  = 10003;
50                         $versiontype_lookup['RC']    = 10004;
51                         $versiontype_lookup['pl']    = 10005;
52                 }
53                 if (isset($versiontype_lookup[$version1])) {
54                         $version1 = $versiontype_lookup[$version1];
55                 }
56                 if (isset($versiontype_lookup[$version2])) {
57                         $version2 = $versiontype_lookup[$version2];
58                 }
59
60                 switch ($operator) {
61                         case '<':
62                         case 'lt':
63                                 return intval($version1 < $version2);
64                                 break;
65                         case '<=':
66                         case 'le':
67                                 return intval($version1 <= $version2);
68                                 break;
69                         case '>':
70                         case 'gt':
71                                 return intval($version1 > $version2);
72                                 break;
73                         case '>=':
74                         case 'ge':
75                                 return intval($version1 >= $version2);
76                                 break;
77                         case '==':
78                         case '=':
79                         case 'eq':
80                                 return intval($version1 == $version2);
81                                 break;
82                         case '!=':
83                         case '<>':
84                         case 'ne':
85                                 return intval($version1 != $version2);
86                                 break;
87                 }
88                 if ($version1 == $version2) {
89                         return 0;
90                 } elseif ($version1 < $version2) {
91                         return -1;
92                 }
93                 return 1;
94         }
95
96
97         function version_compare_replacement($version1, $version2, $operator='') {
98                 if (function_exists('version_compare')) {
99                         // built into PHP v4.1.0+
100                         return version_compare($version1, $version2, $operator);
101                 }
102
103                 // The function first replaces _, - and + with a dot . in the version strings
104                 $version1 = strtr($version1, '_-+', '...');
105                 $version2 = strtr($version2, '_-+', '...');
106
107                 // and also inserts dots . before and after any non number so that for example '4.3.2RC1' becomes '4.3.2.RC.1'.
108                 // Then it splits the results like if you were using explode('.',$ver). Then it compares the parts starting from left to right.
109                 $version1 = eregi_replace('([0-9]+)([A-Z]+)([0-9]+)', '\\1.\\2.\\3', $version1);
110                 $version2 = eregi_replace('([0-9]+)([A-Z]+)([0-9]+)', '\\1.\\2.\\3', $version2);
111
112                 $parts1 = explode('.', $version1);
113                 $parts2 = explode('.', $version1);
114                 $parts_count = max(count($parts1), count($parts2));
115                 for ($i = 0; $i < $parts_count; $i++) {
116                         $comparison = phpthumb_functions::version_compare_replacement_sub($version1, $version2, $operator);
117                         if ($comparison != 0) {
118                                 return $comparison;
119                         }
120                 }
121                 return 0;
122         }
123
124
125         function phpinfo_array() {
126                 static $phpinfo_array = array();
127                 if (empty($phpinfo_array)) {
128                         ob_start();
129                         phpinfo();
130                         $phpinfo = ob_get_contents();
131                         ob_end_clean();
132                         $phpinfo_array = explode("\n", $phpinfo);
133                 }
134                 return $phpinfo_array;
135         }
136
137
138         function exif_info() {
139                 static $exif_info = array();
140                 if (empty($exif_info)) {
141                         // based on code by johnschaefer at gmx dot de
142                         // from PHP help on gd_info()
143                         $exif_info = array(
144                                 'EXIF Support'           => '',
145                                 'EXIF Version'           => '',
146                                 'Supported EXIF Version' => '',
147                                 'Supported filetypes'    => ''
148                         );
149                         $phpinfo_array = phpthumb_functions::phpinfo_array();
150                         foreach ($phpinfo_array as $dummy => $line) {
151                                 $line = trim(strip_tags($line));
152                                 foreach ($exif_info as $key => $value) {
153                                         if (strpos($line, $key) === 0) {
154                                                 $newvalue = trim(str_replace($key, '', $line));
155                                                 $exif_info[$key] = $newvalue;
156                                         }
157                                 }
158                         }
159                 }
160                 return $exif_info;
161         }
162
163
164         function ImageTypeToMIMEtype($imagetype) {
165                 if (function_exists('image_type_to_mime_type') && ($imagetype >= 1) && ($imagetype <= 16)) {
166                         // PHP v4.3.0+
167                         return image_type_to_mime_type($imagetype);
168                 }
169                 static $image_type_to_mime_type = array(
170                         1  => 'image/gif',                     // IMAGETYPE_GIF
171                         2  => 'image/jpeg',                    // IMAGETYPE_JPEG
172                         3  => 'image/png',                     // IMAGETYPE_PNG
173                         4  => 'application/x-shockwave-flash', // IMAGETYPE_SWF
174                         5  => 'image/psd',                     // IMAGETYPE_PSD
175                         6  => 'image/bmp',                     // IMAGETYPE_BMP
176                         7  => 'image/tiff',                    // IMAGETYPE_TIFF_II (intel byte order)
177                         8  => 'image/tiff',                    // IMAGETYPE_TIFF_MM (motorola byte order)
178                         9  => 'application/octet-stream',      // IMAGETYPE_JPC
179                         10 => 'image/jp2',                     // IMAGETYPE_JP2
180                         11 => 'application/octet-stream',      // IMAGETYPE_JPX
181                         12 => 'application/octet-stream',      // IMAGETYPE_JB2
182                         13 => 'application/x-shockwave-flash', // IMAGETYPE_SWC
183                         14 => 'image/iff',                     // IMAGETYPE_IFF
184                         15 => 'image/vnd.wap.wbmp',            // IMAGETYPE_WBMP
185                         16 => 'image/xbm',                     // IMAGETYPE_XBM
186
187                         'gif'  => 'image/gif',                 // IMAGETYPE_GIF
188                         'jpg'  => 'image/jpeg',                // IMAGETYPE_JPEG
189                         'jpeg' => 'image/jpeg',                // IMAGETYPE_JPEG
190                         'png'  => 'image/png',                 // IMAGETYPE_PNG
191                         'bmp'  => 'image/bmp',                 // IMAGETYPE_BMP
192                         'ico'  => 'image/x-icon',
193                 );
194
195                 return (isset($image_type_to_mime_type[$imagetype]) ? $image_type_to_mime_type[$imagetype] : false);
196         }
197
198
199         function HexCharDisplay($string) {
200                 $len = strlen($string);
201                 $output = '';
202                 for ($i = 0; $i < $len; $i++) {
203                         $output .= ' 0x'.str_pad(dechex(ord($string{$i})), 2, '0', STR_PAD_LEFT);
204                 }
205                 return $output;
206         }
207
208
209         function IsHexColor($HexColorString) {
210                 return eregi('^[0-9A-F]{6}$', $HexColorString);
211         }
212
213
214         function ImageColorAllocateAlphaSafe(&$gdimg_hexcolorallocate, $R, $G, $B, $alpha=false) {
215                 if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.2', '>=') && ($alpha !== false)) {
216                         return ImageColorAllocateAlpha($gdimg_hexcolorallocate, $R, $G, $B, intval($alpha));
217                 } else {
218                         return ImageColorAllocate($gdimg_hexcolorallocate, $R, $G, $B);
219                 }
220         }
221
222         function ImageHexColorAllocate(&$gdimg_hexcolorallocate, $HexColorString, $dieOnInvalid=false, $alpha=false) {
223                 if (!is_resource($gdimg_hexcolorallocate)) {
224                         die('$gdimg_hexcolorallocate is not a GD resource in ImageHexColorAllocate()');
225                 }
226                 if (phpthumb_functions::IsHexColor($HexColorString)) {
227                         $R = hexdec(substr($HexColorString, 0, 2));
228                         $G = hexdec(substr($HexColorString, 2, 2));
229                         $B = hexdec(substr($HexColorString, 4, 2));
230                         return phpthumb_functions::ImageColorAllocateAlphaSafe($gdimg_hexcolorallocate, $R, $G, $B, $alpha);
231                 }
232                 if ($dieOnInvalid) {
233                         die('Invalid hex color string: "'.$HexColorString.'"');
234                 }
235                 return ImageColorAllocate($gdimg_hexcolorallocate, 0x00, 0x00, 0x00);
236         }
237
238
239         function HexColorXOR($hexcolor) {
240                 return strtoupper(str_pad(dechex(~hexdec($hexcolor) & 0xFFFFFF), 6, '0', STR_PAD_LEFT));
241         }
242
243
244         function GetPixelColor(&$img, $x, $y) {
245                 if (!is_resource($img)) {
246                         return false;
247                 }
248                 return @ImageColorsForIndex($img, @ImageColorAt($img, $x, $y));
249         }
250
251
252         function GrayscaleValue($r, $g, $b) {
253                 return round(($r * 0.30) + ($g * 0.59) + ($b * 0.11));
254         }
255
256
257         function GrayscalePixel($OriginalPixel) {
258                 $gray = phpthumb_functions::GrayscaleValue($OriginalPixel['red'], $OriginalPixel['green'], $OriginalPixel['blue']);
259                 return array('red'=>$gray, 'green'=>$gray, 'blue'=>$gray);
260         }
261
262
263         function GrayscalePixelRGB($rgb) {
264                 $r = ($rgb >> 16) & 0xFF;
265                 $g = ($rgb >>  8) & 0xFF;
266                 $b =  $rgb        & 0xFF;
267                 return ($r * 0.299) + ($g * 0.587) + ($b * 0.114);
268         }
269
270
271         function ImageCopyResampleBicubic($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) {
272                 // ron at korving dot demon dot nl
273                 // http://www.php.net/imagecopyresampled
274
275                 $scaleX = ($src_w - 1) / $dst_w;
276                 $scaleY = ($src_h - 1) / $dst_h;
277
278                 $scaleX2 = $scaleX / 2.0;
279                 $scaleY2 = $scaleY / 2.0;
280
281                 $isTrueColor = ImageIsTrueColor($src_img);
282
283                 for ($y = $src_y; $y < $src_y + $dst_h; $y++) {
284                         $sY   = $y * $scaleY;
285                         $siY  = (int) $sY;
286                         $siY2 = (int) $sY + $scaleY2;
287
288                         for ($x = $src_x; $x < $src_x + $dst_w; $x++) {
289                                 $sX   = $x * $scaleX;
290                                 $siX  = (int) $sX;
291                                 $siX2 = (int) $sX + $scaleX2;
292
293                                 if ($isTrueColor) {
294
295                                         $c1 = ImageColorAt($src_img, $siX, $siY2);
296                                         $c2 = ImageColorAt($src_img, $siX, $siY);
297                                         $c3 = ImageColorAt($src_img, $siX2, $siY2);
298                                         $c4 = ImageColorAt($src_img, $siX2, $siY);
299
300                                         $r = (( $c1             +  $c2             +  $c3             +  $c4            ) >> 2) & 0xFF0000;
301                                         $g = ((($c1 & 0x00FF00) + ($c2 & 0x00FF00) + ($c3 & 0x00FF00) + ($c4 & 0x00FF00)) >> 2) & 0x00FF00;
302                                         $b = ((($c1 & 0x0000FF) + ($c2 & 0x0000FF) + ($c3 & 0x0000FF) + ($c4 & 0x0000FF)) >> 2);
303
304                                 } else {
305
306                                         $c1 = ImageColorsForIndex($src_img, ImageColorAt($src_img, $siX, $siY2));
307                                         $c2 = ImageColorsForIndex($src_img, ImageColorAt($src_img, $siX, $siY));
308                                         $c3 = ImageColorsForIndex($src_img, ImageColorAt($src_img, $siX2, $siY2));
309                                         $c4 = ImageColorsForIndex($src_img, ImageColorAt($src_img, $siX2, $siY));
310
311                                         $r = ($c1['red']   + $c2['red']   + $c3['red']   + $c4['red'] )  << 14;
312                                         $g = ($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) <<  6;
313                                         $b = ($c1['blue']  + $c2['blue']  + $c3['blue']  + $c4['blue'] ) >>  2;
314
315                                 }
316                                 ImageSetPixel($dst_img, $dst_x + $x - $src_x, $dst_y + $y - $src_y, $r+$g+$b);
317                         }
318                 }
319                 return true;
320         }
321
322
323         function ImageCreateFunction($x_size, $y_size) {
324                 $ImageCreateFunction = 'ImageCreate';
325                 if (phpthumb_functions::gd_version() >= 2.0) {
326                         $ImageCreateFunction = 'ImageCreateTrueColor';
327                 }
328                 if (!function_exists($ImageCreateFunction)) {
329                         return phpthumb::ErrorImage($ImageCreateFunction.'() does not exist - no GD support?');
330                 }
331                 if (($x_size <= 0) || ($y_size <= 0)) {
332                         return phpthumb::ErrorImage('Invalid image dimensions: '.$ImageCreateFunction.'('.$x_size.', '.$y_size.')');
333                 }
334                 return $ImageCreateFunction($x_size, $y_size);
335         }
336
337
338         function ImageCopyRespectAlpha(&$dst_im, &$src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct=100) {
339                 for ($x = $src_x; $x < $src_w; $x++) {
340                         for ($y = $src_y; $y < $src_h; $y++) {
341                                 $RealPixel    = phpthumb_functions::GetPixelColor($dst_im, $dst_x + $x, $dst_y + $y);
342                                 $OverlayPixel = phpthumb_functions::GetPixelColor($src_im, $x, $y);
343                                 $alphapct = $OverlayPixel['alpha'] / 127;
344                                 $opacipct = $pct / 100;
345                                 $overlaypct = (1 - $alphapct) * $opacipct;
346
347                                 $newcolor = phpthumb_functions::ImageColorAllocateAlphaSafe(
348                                         $dst_im,
349                                         round($RealPixel['red']   * (1 - $overlaypct)) + ($OverlayPixel['red']   * $overlaypct),
350                                         round($RealPixel['green'] * (1 - $overlaypct)) + ($OverlayPixel['green'] * $overlaypct),
351                                         round($RealPixel['blue']  * (1 - $overlaypct)) + ($OverlayPixel['blue']  * $overlaypct),
352                                         //$RealPixel['alpha']);
353                                         0);
354
355                                 ImageSetPixel($dst_im, $dst_x + $x, $dst_y + $y, $newcolor);
356                         }
357                 }
358                 return true;
359         }
360
361
362         function ProportionalResize($old_width, $old_height, $new_width=false, $new_height=false) {
363                 $old_aspect_ratio = $old_width / $old_height;
364                 if (($new_width === false) && ($new_height === false)) {
365                         return false;
366                 } elseif ($new_width === false) {
367                         $new_width = $new_height * $old_aspect_ratio;
368                 } elseif ($new_height === false) {
369                         $new_height = $new_width / $old_aspect_ratio;
370                 }
371                 $new_aspect_ratio = $new_width / $new_height;
372                 if ($new_aspect_ratio == $old_aspect_ratio) {
373                         // great, done
374                 } elseif ($new_aspect_ratio < $old_aspect_ratio) {
375                         // limited by width
376                         $new_height = $new_width / $old_aspect_ratio;
377                 } elseif ($new_aspect_ratio > $old_aspect_ratio) {
378                         // limited by height
379                         $new_width = $new_height * $old_aspect_ratio;
380                 }
381                 return array(round($new_width), round($new_height));
382         }
383
384
385         function SafeExec($command) {
386                 static $AllowedExecFunctions = array();
387                 if (empty($AllowedExecFunctions)) {
388                         $AllowedExecFunctions = array('shell_exec'=>true, 'passthru'=>true, 'system'=>true, 'exec'=>true);
389                         if (@ini_get('safe_mode')) {
390                                 $AllowedExecFunctions['shell_exec'] = false;
391                         }
392                         $disable_functions = explode(',', @ini_get('disable_functions'));
393                         foreach ($AllowedExecFunctions as $key => $value) {
394                                 if (in_array($key, $disable_functions)) {
395                                         $AllowedExecFunctions[$key] = false;
396                                 }
397                         }
398                 }
399                 foreach ($AllowedExecFunctions as $execfunction => $is_allowed) {
400                         if (!$is_allowed) {
401                                 continue;
402                         }
403                         switch ($execfunction) {
404                                 case 'passthru':
405                                         ob_start();
406                                         $execfunction($command);
407                                         $returnvalue = ob_get_contents();
408                                         ob_end_clean();
409                                         break;
410
411                                 case 'shell_exec':
412                                 case 'system':
413                                 case 'exec':
414                                 default:
415                                         $returnvalue = @$execfunction($command);
416                                         break;
417                         }
418                         return $returnvalue;
419                 }
420                 return false;
421         }
422
423
424         function ApacheLookupURIarray($filename) {
425                 // apache_lookup_uri() only works when PHP is installed as an Apache module.
426                 if (php_sapi_name() == 'apache') {
427                         $keys = array('status', 'the_request', 'status_line', 'method', 'content_type', 'handler', 'uri', 'filename', 'path_info', 'args', 'boundary', 'no_cache', 'no_local_copy', 'allowed', 'send_bodyct', 'bytes_sent', 'byterange', 'clength', 'unparsed_uri', 'mtime', 'request_time');
428                         if ($apacheLookupURIobject = @apache_lookup_uri($filename)) {
429                                 $apacheLookupURIarray = array();
430                                 foreach ($keys as $dummy => $key) {
431                                         $apacheLookupURIarray[$key] = @$apacheLookupURIobject->$key;
432                                 }
433                                 return $apacheLookupURIarray;
434                         }
435                 }
436                 return false;
437         }
438
439
440         function gd_is_bundled() {
441                 static $isbundled = null;
442                 if (is_null($isbundled)) {
443                         $gd_info = gd_info();
444                         $isbundled = (strpos($gd_info['GD Version'], 'bundled') !== false);
445                 }
446                 return $isbundled;
447         }
448
449
450         function gd_version($fullstring=false) {
451                 static $cache_gd_version = array();
452                 if (empty($cache_gd_version)) {
453                         $gd_info = gd_info();
454                         if (eregi('bundled \((.+)\)$', $gd_info['GD Version'], $matches)) {
455                                 $cache_gd_version[1] = $gd_info['GD Version'];  // e.g. "bundled (2.0.15 compatible)"
456                                 $cache_gd_version[0] = (float) $matches[1];     // e.g. "2.0" (not "bundled (2.0.15 compatible)")
457                         } else {
458                                 $cache_gd_version[1] = $gd_info['GD Version'];                       // e.g. "1.6.2 or higher"
459                                 $cache_gd_version[0] = (float) substr($gd_info['GD Version'], 0, 3); // e.g. "1.6" (not "1.6.2 or higher")
460                         }
461                 }
462                 return $cache_gd_version[intval($fullstring)];
463         }
464
465
466         function filesize_remote($remotefile, $timeout=10) {
467                 $size = false;
468                 $url = parse_url($remotefile);
469                 if ($fp = @fsockopen($url['host'], ($url['port'] ? $url['port'] : 80), $errno, $errstr, $timeout)) {
470                         fwrite($fp, 'HEAD '.@$url['path'].@$url['query'].' HTTP/1.0'."\r\n".'Host: '.@$url['host']."\r\n\r\n");
471                         if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.0', '>=')) {
472                                 stream_set_timeout($fp, $timeout);
473                         }
474                         while (!feof($fp)) {
475                                 $headerline = fgets($fp, 4096);
476                                 if (eregi('^Content-Length: (.*)', $headerline, $matches)) {
477                                         $size = intval($matches[1]);
478                                         break;
479                                 }
480                         }
481                         fclose ($fp);
482                 }
483                 return $size;
484         }
485
486
487         function filedate_remote($remotefile, $timeout=10) {
488                 $date = false;
489                 $url = parse_url($remotefile);
490                 if ($fp = @fsockopen($url['host'], ($url['port'] ? $url['port'] : 80), $errno, $errstr, $timeout)) {
491                         fwrite($fp, 'HEAD '.@$url['path'].@$url['query'].' HTTP/1.0'."\r\n".'Host: '.@$url['host']."\r\n\r\n");
492                         if (phpthumb_functions::version_compare_replacement(phpversion(), '4.3.0', '>=')) {
493                                 stream_set_timeout($fp, $timeout);
494                         }
495                         while (!feof($fp)) {
496                                 $headerline = fgets($fp, 4096);
497                                 if (eregi('^Last-Modified: (.*)', $headerline, $matches)) {
498                                         $date = strtotime($matches[1]) - date('Z');
499                                         break;
500                                 }
501                         }
502                         fclose ($fp);
503                 }
504                 return $date;
505         }
506
507
508         function md5_file_safe($filename) {
509                 // md5_file() doesn't exist in PHP < 4.2.0
510                 if (function_exists('md5_file')) {
511                         return md5_file($filename);
512                 }
513                 if ($fp = @fopen($filename, 'rb')) {
514                         $rawData = '';
515                         do {
516                                 $buffer = fread($fp, 8192);
517                                 $rawData .= $buffer;
518                         } while (strlen($buffer) > 0);
519                         fclose($fp);
520                         return md5($rawData);
521                 }
522                 return false;
523         }
524
525
526         function nonempty_min() {
527                 $arg_list = func_get_args();
528                 $acceptable = array();
529                 foreach ($arg_list as $dummy => $arg) {
530                         if ($arg) {
531                                 $acceptable[] = $arg;
532                         }
533                 }
534                 return min($acceptable);
535         }
536
537
538         function LittleEndian2String($number, $minbytes=1) {
539                 $intstring = '';
540                 while ($number > 0) {
541                         $intstring = $intstring.chr($number & 255);
542                         $number >>= 8;
543                 }
544                 return str_pad($intstring, $minbytes, "\x00", STR_PAD_RIGHT);
545         }
546
547         function OneOfThese() {
548                 // return the first useful (non-empty/non-zero/non-false) value from those passed
549                 $arg_list = func_get_args();
550                 foreach ($arg_list as $key => $value) {
551                         if ($value) {
552                                 return $value;
553                         }
554                 }
555                 return false;
556         }
557
558         function CaseInsensitiveInArray($needle, $haystack) {
559                 $needle = strtolower($needle);
560                 foreach ($haystack as $key => $value) {
561                         if (is_array($value)) {
562                                 // skip?
563                         } elseif ($needle == strtolower($value)) {
564                                 return true;
565                         }
566                 }
567                 return false;
568         }
569
570         function SafeURLread($url, &$error) {
571                 if (function_exists('curl_version')) {
572                         $ch = curl_init();
573                         curl_setopt($ch, CURLOPT_URL, $url);
574                         curl_setopt($ch, CURLOPT_HEADER, false);
575                         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
576                         curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
577                         $rawData = curl_exec($ch);
578                         curl_close($ch);
579                         if (strlen($rawData) > 0) {
580                                 return $rawData;
581                         }
582                         $error .= 'CURL available but returned no data; ';
583                 } else {
584                         $error .= 'CURL unavailable; ';
585                 }
586                 if (@ini_get('allow_url_fopen')) {
587                         $rawData = '';
588                         ob_start();
589                         if ($fp = fopen($url, 'rb')) {
590                                 do {
591                                         $buffer = fread($fp, 8192);
592                                         $rawData .= $buffer;
593                                 } while (strlen($buffer) > 0);
594                                 fclose($fp);
595                         } else {
596                                 $error = trim(strip_tags(ob_get_contents()));
597                         }
598                         ob_end_clean();
599                         if (!$error) {
600                                 return $rawData;
601                         }
602                         $error .= '; "allow_url_fopen" enabled but returned no data; ';
603                 } else {
604                         $error .= '"allow_url_fopen" disabled; ';
605                 }
606                 return false;
607         }
608
609 }
610
611
612 if (!function_exists('gd_info')) {
613         // built into PHP v4.3.0+ (with bundled GD2 library)
614         function gd_info() {
615                 static $gd_info = array();
616                 if (empty($gd_info)) {
617                         // based on code by johnschaefer at gmx dot de
618                         // from PHP help on gd_info()
619                         $gd_info = array(
620                                 'GD Version'         => '',
621                                 'FreeType Support'   => false,
622                                 'FreeType Linkage'   => '',
623                                 'T1Lib Support'      => false,
624                                 'GIF Read Support'   => false,
625                                 'GIF Create Support' => false,
626                                 'JPG Support'        => false,
627                                 'PNG Support'        => false,
628                                 'WBMP Support'       => false,
629                                 'XBM Support'        => false
630                         );
631                         $phpinfo_array = phpthumb_functions::phpinfo_array();
632                         foreach ($phpinfo_array as $dummy => $line) {
633                                 $line = trim(strip_tags($line));
634                                 foreach ($gd_info as $key => $value) {
635                                         //if (strpos($line, $key) !== false) {
636                                         if (strpos($line, $key) === 0) {
637                                                 $newvalue = trim(str_replace($key, '', $line));
638                                                 $gd_info[$key] = $newvalue;
639                                         }
640                                 }
641                         }
642                         if (empty($gd_info['GD Version'])) {
643                                 // probable cause: "phpinfo() disabled for security reasons"
644                                 if (function_exists('ImageTypes')) {
645                                         $imagetypes = ImageTypes();
646                                         if ($imagetypes & IMG_PNG) {
647                                                 $gd_info['PNG Support'] = true;
648                                         }
649                                         if ($imagetypes & IMG_GIF) {
650                                                 $gd_info['GIF Create Support'] = true;
651                                         }
652                                         if ($imagetypes & IMG_JPG) {
653                                                 $gd_info['JPG Support'] = true;
654                                         }
655                                         if ($imagetypes & IMG_WBMP) {
656                                                 $gd_info['WBMP Support'] = true;
657                                         }
658                                 }
659                                 // to determine capability of GIF creation, try to use ImageCreateFromGIF on a 1px GIF
660                                 if (function_exists('ImageCreateFromGIF')) {
661                                         if ($tempfilename = phpthumb::phpThumb_tempnam()) {
662                                                 if ($fp_tempfile = @fopen($tempfilename, 'wb')) {
663                                                         fwrite($fp_tempfile, base64_decode('R0lGODlhAQABAIAAAH//AP///ywAAAAAAQABAAACAUQAOw==')); // very simple 1px GIF file base64-encoded as string
664                                                         fclose($fp_tempfile);
665
666                                                         // if we can convert the GIF file to a GD image then GIF create support must be enabled, otherwise it's not
667                                                         $gd_info['GIF Read Support'] = (bool) @ImageCreateFromGIF($tempfilename);
668                                                 }
669                                                 unlink($tempfilename);
670                                         }
671                                 }
672                                 if (function_exists('ImageCreateTrueColor') && @ImageCreateTrueColor(1, 1)) {
673                                         $gd_info['GD Version'] = '2.0.1 or higher (assumed)';
674                                 } elseif (function_exists('ImageCreate') && @ImageCreate(1, 1)) {
675                                         $gd_info['GD Version'] = '1.6.0 or higher (assumed)';
676                                 }
677                         }
678                 }
679                 return $gd_info;
680         }
681 }
682
683
684 if (!function_exists('is_executable')) {
685         // in PHP v3+, but v5.0+ for Windows
686         function is_executable($filename) {
687                 // poor substitute, but better than nothing
688                 return file_exists($filename);
689         }
690 }
691
692
693 if (!function_exists('preg_quote')) {
694         // included in PHP v3.0.9+, but may be unavailable if not compiled in
695         function preg_quote($string, $delimiter='\\') {
696                 static $preg_quote_array = array();
697                 if (empty($preg_quote_array)) {
698                         $escapeables = '.\\+*?[^]$(){}=!<>|:';
699                         for ($i = 0; $i < strlen($escapeables); $i++) {
700                                 $strtr_preg_quote[$escapeables{$i}] = $delimiter.$escapeables{$i};
701                         }
702                 }
703                 return strtr($string, $strtr_preg_quote);
704         }
705 }
706
707 if (!function_exists('file_get_contents')) {
708         // included in PHP v4.3.0+
709         function file_get_contents($filename) {
710                 if (eregi('^(f|ht)tp\://', $filename)) {
711                         return SafeURLread($filename);
712                 }
713                 if ($fp = @fopen($filename, 'rb')) {
714                         $rawData = '';
715                         do {
716                                 $buffer = fread($fp, 8192);
717                                 $rawData .= $buffer;
718                         } while (strlen($buffer) > 0);
719                         fclose($fp);
720                         return $rawData;
721                 }
722                 return false;
723         }
724 }
725
726
727 if (!function_exists('file_put_contents')) {
728         // included in PHP v5.0.0+
729         function file_put_contents($filename, $filedata) {
730                 if ($fp = @fopen($filename, 'wb')) {
731                         fwrite($fp, $filedata);
732                         fclose($fp);
733                         return true;
734                 }
735                 return false;
736         }
737 }
738
739 if (!function_exists('imagealphablending')) {
740         // built-in function requires PHP v4.0.6+ *and* GD v2.0.1+
741         function imagealphablending(&$img, $blendmode=true) {
742                 // do nothing, this function is declared here just to
743                 // prevent runtime errors if GD2 is not available
744                 return true;
745         }
746 }
747
748 if (!function_exists('imagesavealpha')) {
749         // built-in function requires PHP v4.3.2+ *and* GD v2.0.1+
750         function imagesavealpha(&$img, $blendmode=true) {
751                 // do nothing, this function is declared here just to
752                 // prevent runtime errors if GD2 is not available
753                 return true;
754         }
755 }
756
757 ?>