Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / wiki / ewiki / plugins / feature / imgresize_gd.php
blobe88c998306cb4278ad240c93bfdf26f2dd99febb
1 <?php
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) )
29 return(false);
32 $tmp_rescale = $filename;
34 #-- initial rescale
35 $r = EWIKI_IMAGE_MAXSIZE / filesize($tmp_rescale);
36 $r = ($r) + ($r - 1) * ($r - 1);
38 #-- read orig image
39 strtok($mime, "/");
40 $type = strtok("/");
41 if (function_exists($pf = "imagecreatefrom$type")) {
42 $orig_image = $pf($filename);
44 else {
45 return(false);
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"))) {
52 $type = "png";
55 #-- retry resizing
56 $loop = 20;
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.");
63 #-- sizes
64 $new_x = (int) ($orig_x * $r);
65 $new_y = (int) ($orig_y * $r);
67 #-- new gd image
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);
73 else {
74 $new_image = imagecreatetruecolor($new_x, $new_y);
77 #-- resize action
78 imagecopyresized($new_image, $orig_image, 0,0, 0,0, $new_x,$new_y, $orig_x,$orig_y);
80 #-- special things
81 if ( ($type == "png") && function_exists("imagesavealpha") ) {
82 imagesavealpha($new_image, 1);
85 #-- save
86 if (function_exists($pf = "image$type")) {
87 $pf($new_image, $tmp_rescale);
89 else {
90 return(false); # cannot save in orig format (.gif)
93 #-- prepare next run
94 imagedestroy($new_image);
95 clearstatcache();
96 $r *= 0.95;
99 #-- stop
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);
105 return($false);
108 #-- set $mime, as it may have changed (.gif)
109 $mime = strtok($mime, "/") . "/" . $type;
110 if (!strstr($filename, ".$type")) {
111 unlink($filename);
112 $filename .= ".$type";
115 #-- move tmp file to old name
116 copy($tmp_rescale, $filename);
117 unlink($tmp_rescale);
118 return(true);