992b22d03f254379da8e95f8003f62675b31acad
[atutor.git] / mods / wiki / plugins / feature / imgresize_gd.php
1 <?php
2
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
7
8 #  NOTE: It is currently disabled for Win32, because nobody knows, if
9 #  this will crash the PHP interpreter on those systems.
10
11
12 define("EWIKI_IMGRESIZE_WIN", 0);
13
14
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");
17 }
18 if (function_exists("imagecreate")) {
19    $ewiki_plugins["image_resize"][] = "ewiki_binary_resize_image_gd";
20 }
21
22
23 function ewiki_binary_resize_image_gd(&$filename, &$mime, $return=0) {
24
25            /*** this disallows Win32 ***/
26    if (    (DIRECTORY_SEPARATOR!="/") && !EWIKI_IMAGERESIZE_WIN
27        || (strpos($mime, "image/")!==0) )
28    { 
29       return(false);
30    }
31
32         if(filesize($filename) < EWIKI_IMAGE_MAXSIZE){
33                 return(true);
34         }
35
36    $tmp_rescale = $filename;
37
38    #-- initial rescale
39    $r = EWIKI_IMAGE_MAXSIZE / filesize($tmp_rescale);
40    $r = ($r) + ($r - 1) * ($r - 1);
41
42    #-- read orig image
43    strtok($mime, "/");
44    $type = strtok("/");
45    if (function_exists($pf = "imagecreatefrom$type")) {
46       $orig_image = $pf($filename);
47    }
48    else {
49       return(false);
50    }
51    $orig_x = imagesx($orig_image);
52    $orig_y = imagesy($orig_image);
53
54    #-- change mime from .gif to .png
55    if (($type == "gif") && (false || function_exists("imagepng") && !function_exists("imagegif"))) {
56       $type = "png";
57    }
58
59    #-- retry resizing
60    $loop = 20;
61    while (($loop--) && (filesize($tmp_rescale) > EWIKI_IMAGE_MAXSIZE)) {
62
63       if ($filename == $tmp_rescale) {
64          $tmp_rescale = tempnam(EWIKI_TMP, "ewiki.img_resize_gd.tmp.");
65       }
66
67       #-- sizes
68       $new_x = (int) ($orig_x * $r);
69       $new_y = (int) ($orig_y * $r);
70
71       #-- new gd image
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);
76       }
77       else {
78          $new_image = imagecreatetruecolor($new_x, $new_y);
79       }
80
81       #-- resize action
82       imagecopyresized($new_image, $orig_image, 0,0, 0,0, $new_x,$new_y, $orig_x,$orig_y);
83
84       #-- special things
85       if ( ($type == "png") && function_exists("imagesavealpha") ) {
86          imagesavealpha($new_image, 1);
87       }
88
89       #-- save
90       if (function_exists($pf = "image$type")) {
91          $pf($new_image, $tmp_rescale);
92       }
93       else {
94          return(false);   # cannot save in orig format (.gif)
95       }
96
97       #-- prepare next run
98       imagedestroy($new_image);
99       clearstatcache();
100       $r *= 0.95;
101    }
102
103    #-- stop
104    imagedestroy($orig_image);
105
106    #-- security check filesizes, abort
107    if (!filesize($filename) || !filesize($tmp_rescale) || (filesize($tmp_rescale) > EWIKI_IMAGE_MAXSIZE)) {
108       unlink($tmp_rescale);
109       return($false);
110    }
111
112    #-- set $mime, as it may have changed (.gif)
113    $mime = strtok($mime, "/") . "/" . $type;
114    if (!strstr($filename, ".$type")) {
115       unlink($filename);
116       $filename .= ".$type";
117    }
118
119    #-- move tmp file to old name
120    copy($tmp_rescale, $filename);
121    unlink($tmp_rescale);
122    return(true);
123
124 }
125
126 ?>