]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - ImageTile.php
minor fixes
[SourceForge/phpwiki.git] / ImageTile.php
1 <?php
2
3 require_once('lib/stdlib.php');
4
5 $remove = 0;
6 if (preg_match('/^(http|ftp|https):\/\//i',$_REQUEST['url'])) {
7     
8     $data_path = '';
9     list($usec, $sec) = explode(" ", microtime());
10     
11     $fp = fopen('config/config.ini','r');
12     while($config = fgetcsv($fp,1024,';')) {
13         if (preg_match('/DATA_PATH/',$config[0])) {
14             list($key,$value) = split('=',$config[0]);
15             $data_path = trim($value).'/';
16         }
17     }
18     fclose($fp);
19     @mkdir($data_path."uploads/thumbs");
20     $file = $data_path."uploads/thumbs/image_" . ((float)$usec + (float)$sec);
21     $source = url_get_contents($_REQUEST['url']);
22
23     @$fp = fopen($file,'w+');
24     if (!$fp) {
25         header ("Content-type: text/html");
26         echo "<html><head></head><body>ERROR : unable to open $file in write mode</body></html>";
27     }
28     fwrite($fp,$source);
29     $remove = 1;
30     
31 } else {
32     @$fp = fopen($_REQUEST['url'],r);
33     
34     if (!$fp) {
35     
36         header ("Content-type: text/html");
37         echo "<html><head></head><body>Not an image</body></html>";
38         exit();
39
40     } else {
41         $file = $_REQUEST['url'];
42         fclose($fp);
43     }
44 }
45 list ($a, $b, $type, $attr) = @getimagesize ($file);
46
47 if ($type == 0) {
48
49     $type = basename ($_REQUEST['url']);
50     $type = preg_split ('/\./',$type);
51     $type = array_pop ($type);
52
53 }
54
55
56 switch ($type) {
57     case '2':
58         if (function_exists("imagecreatefromjpeg"))
59             $img = @imagecreatefromjpeg ($file);
60         else
61             show_plain ($file);
62         break;
63     case '3':
64         if (function_exists("imagecreatefrompng"))
65             $img = @imagecreatefrompng ($file);
66         else
67             show_plain ($file);
68         break;
69     case '1':
70         if (function_exists("imagecreatefromgif"))
71             $img = @imagecreatefromgif ($file);
72         else
73             show_plain ($file);
74         break;
75     case '15':
76         if (function_exists("imagecreatefromwbmp"))
77             $img = @imagecreatefromwbmp ($file);
78         else
79             show_plain ($file);
80         break;
81     case '16':
82         if (function_exists("imagecreatefromxbm"))
83             $img = @imagecreatefromxbm ($file);
84         else
85             show_plain ($file);
86         break;
87     case 'xpm':
88         if (function_exists("imagecreatefromxpm"))
89             $img = @imagecreatefromxpm ($file);
90         else
91             show_plain ($file);
92         break;
93     case 'gd':
94         if (function_exists("imagecreatefromgd"))
95             $img = @imagecreatefromgd ($file);
96         else
97             show_plain ($file);
98         break;
99     case 'gd2':
100         if (function_exists("imagecreatefromgd2"))
101             $img = @imagecreatefromgd2 ($file);
102         else
103             show_plain ($file);
104         break;
105     default:
106         //we are not stupid...
107         header ("Content-type: text/html");
108         echo "<html><head></head><body>Not an image</body></html>";
109         exit();
110         break;
111 }    
112
113 $width  = @imagesx($img);
114 $height = @imagesy($img);
115
116 $newwidth = $_REQUEST['width'];
117 if (empty($newidth)) $newidth = 50;
118     
119 $newheight = $_REQUEST['height'];
120 if (empty($newheight)) $newheight = round($newwidth * ($height / $width)) ;
121
122 $thumb = imagecreate($newwidth, $newheight);
123 $img = imagecopyresampled($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
124
125 if ($remove == 1) unlink ($file);
126
127 header ("Content-type: image/png");
128 imagepng($thumb);
129
130 function show_plain () {
131     $mime = mime_content_type ($_REQUEST['url']);
132     header ("Content-type: $mime");
133     readfile($_REQUEST['url']);
134     exit();
135 }
136
137 ?>