d4b8a9a9936b709dcba7b01ba5e80115d4f2cb14
[atutor.git] / mods / wiki / plugins / feature / imgresize_magick.php
1 <?php
2
3 # This plugin rescales uploaded images using ImageMagick(1), if an uploaded
4 # image file is larger than allowed in EWIKI_IMAGE_MAXSIZE.
5 #
6 # NOTE: ImageMagick can usually be found on UNIX sytems only, but you could
7 # of course utilize another commandline instead if your system provides a
8 # similar one.
9
10
11
12 $ewiki_plugins["image_resize"][] = "ewiki_binary_resize_image_magick";
13
14
15 function ewiki_binary_resize_image_magick(&$filename, &$type, $return=0) {
16
17    if (!filesize($filename)) {
18       return(false);
19    }
20
21         if(filesize($filename) < EWIKI_IMAGE_MAXSIZE){
22                 return(true);
23         }
24
25    #-- temporary image file
26    $tmp_rescale = tempnam(EWIKI_TMP, "ewiki.img_resize_magick.tmp.");
27    $tmp_size = filesize($filename);
28
29    #-- initial rescale factor
30    $scale = sqrt(EWIKI_IMAGE_MAXSIZE / ($tmp_size + 1));
31
32    #-- try to rescale image
33    $loop=7;
34    while ($loop && ($tmp_size > EWIKI_IMAGE_MAXSIZE)) {
35
36       @unlink($tmp_rescale);
37       copy($filename, $tmp_rescale);
38
39       $n = round($scale * 100);
40       exec("mogrify -scale $n%x$n% $tmp_rescale");
41
42       clearstatcache();
43       $scale = $scale * 0.95;
44       $tmp_size = filesize($tmp_rescale);
45    }
46
47
48    #-- return result
49    if ((filesize($tmp_rescale)) &&
50        (filesize($tmp_rescale) < filesize($filename)) &&
51        (filesize($tmp_rescale) < EWIKI_IMAGE_MAXSIZE))
52    {
53       @unlink($filename);
54       $filename = $tmp_rescale;
55       return($true);
56    }
57    else {
58       @unlink($tmp_rescale);
59       return($false);
60    }
61
62 }
63
64
65
66 ?>