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") && !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 $tmp_rescale = $filename;
35 $r = EWIKI_IMAGE_MAXSIZE
/ filesize($tmp_rescale);
36 $r = ($r) +
($r - 1) * ($r - 1);
41 if (function_exists($pf = "imagecreatefrom$type")) {
42 $orig_image = $pf($filename);
47 $orig_x = imagesx($orig_image);
48 $orig_y = imagesy($orig_image);
50 #-- change mime from .gif to .png
51 if (($type == "gif") && (false ||
function_exists("imagepng") && !function_exists("imagegif"))) {
57 while (($loop--) && (filesize($tmp_rescale) > EWIKI_IMAGE_MAXSIZE
)) {
59 if ($filename == $tmp_rescale) {
60 $tmp_rescale = tempnam(EWIKI_TMP
, "ewiki.img_resize_gd.tmp.");
64 $new_x = (int) ($orig_x * $r);
65 $new_y = (int) ($orig_y * $r);
68 $tc = function_exists("imageistruecolor") && imageistruecolor($orig_image);
69 if (!$tc ||
($type == "gif")) {
70 $new_image = imagecreate($new_x, $new_y);
71 imagepalettecopy($new_image, $orig_image);
74 $new_image = imagecreatetruecolor($new_x, $new_y);
78 imagecopyresized($new_image, $orig_image, 0,0, 0,0, $new_x,$new_y, $orig_x,$orig_y);
81 if ( ($type == "png") && function_exists("imagesavealpha") ) {
82 imagesavealpha($new_image, 1);
86 if (function_exists($pf = "image$type")) {
87 $pf($new_image, $tmp_rescale);
90 return(false); # cannot save in orig format (.gif)
94 imagedestroy($new_image);
100 imagedestroy($orig_image);
102 #-- security check filesizes, abort
103 if (!filesize($filename) ||
!filesize($tmp_rescale) ||
(filesize($tmp_rescale) > EWIKI_IMAGE_MAXSIZE
)) {
104 unlink($tmp_rescale);
108 #-- set $mime, as it may have changed (.gif)
109 $mime = strtok($mime, "/") . "/" . $type;
110 if (!strstr($filename, ".$type")) {
112 $filename .= ".$type";
115 #-- move tmp file to old name
116 copy($tmp_rescale, $filename);
117 unlink($tmp_rescale);