3 # if someone uploads an image, which is larger than the allowed
4 # image size (EWIKI_IMAGE_MAXSIZE), then this plugin tries to
5 # rescale that image until it fits; it utilizes the PHP libgd
6 # functions to accomplish this
8 # NOTE: It is currently disabled for Win32, because nobody knows, if
9 # this will crash the PHP interpreter on those systems.
12 define("EWIKI_IMGRESIZE_WIN", 0);
15 if (!strstr(PHP_VERSION, "-dev") && !(extension_loaded("php_gd2.dll") or extension_loaded("gd.so")) && !function_exists("imagecreate") && function_exists("dl")) { #-- try to load gd lib
16 @dl("php_gd2.dll") or @dl("gd.so");
18 if (function_exists("imagecreate")) {
19 $ewiki_plugins["image_resize"][] = "ewiki_binary_resize_image_gd";
23 function ewiki_binary_resize_image_gd(&$filename, &$mime, $return=0) {
25 /*** this disallows Win32 ***/
26 if ( (DIRECTORY_SEPARATOR!="/") && !EWIKI_IMAGERESIZE_WIN
27 || (strpos($mime, "image/")!==0) )
32 if(filesize($filename) < EWIKI_IMAGE_MAXSIZE){
36 $tmp_rescale = $filename;
39 $r = EWIKI_IMAGE_MAXSIZE / filesize($tmp_rescale);
40 $r = ($r) + ($r - 1) * ($r - 1);
45 if (function_exists($pf = "imagecreatefrom$type")) {
46 $orig_image = $pf($filename);
51 $orig_x = imagesx($orig_image);
52 $orig_y = imagesy($orig_image);
54 #-- change mime from .gif to .png
55 if (($type == "gif") && (false || function_exists("imagepng") && !function_exists("imagegif"))) {
61 while (($loop--) && (filesize($tmp_rescale) > EWIKI_IMAGE_MAXSIZE)) {
63 if ($filename == $tmp_rescale) {
64 $tmp_rescale = tempnam(EWIKI_TMP, "ewiki.img_resize_gd.tmp.");
68 $new_x = (int) ($orig_x * $r);
69 $new_y = (int) ($orig_y * $r);
72 $tc = function_exists("imageistruecolor") && imageistruecolor($orig_image);
73 if (!$tc || ($type == "gif")) {
74 $new_image = imagecreate($new_x, $new_y);
75 imagepalettecopy($new_image, $orig_image);
78 $new_image = imagecreatetruecolor($new_x, $new_y);
82 imagecopyresized($new_image, $orig_image, 0,0, 0,0, $new_x,$new_y, $orig_x,$orig_y);
85 if ( ($type == "png") && function_exists("imagesavealpha") ) {
86 imagesavealpha($new_image, 1);
90 if (function_exists($pf = "image$type")) {
91 $pf($new_image, $tmp_rescale);
94 return(false); # cannot save in orig format (.gif)
98 imagedestroy($new_image);
104 imagedestroy($orig_image);
106 #-- security check filesizes, abort
107 if (!filesize($filename) || !filesize($tmp_rescale) || (filesize($tmp_rescale) > EWIKI_IMAGE_MAXSIZE)) {
108 unlink($tmp_rescale);
112 #-- set $mime, as it may have changed (.gif)
113 $mime = strtok($mime, "/") . "/" . $type;
114 if (!strstr($filename, ".$type")) {
116 $filename .= ".$type";
119 #-- move tmp file to old name
120 copy($tmp_rescale, $filename);
121 unlink($tmp_rescale);