]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - ImageTile.php
fix "r"
[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) {
48     $type = basename ($_REQUEST['url']);
49     $type = preg_split ('/\./',$type);
50     $type = array_pop ($type);
51 }
52
53 switch ($type) {
54     case '2':
55         if (function_exists("imagecreatefromjpeg"))
56             $img = @imagecreatefromjpeg ($file);
57         else
58             show_plain ($file);
59         break;
60     case '3':
61         if (function_exists("imagecreatefrompng"))
62             $img = @imagecreatefrompng ($file);
63         else
64             show_plain ($file);
65         break;
66     case '1':
67         if (function_exists("imagecreatefromgif"))
68             $img = @imagecreatefromgif ($file);
69         else
70             show_plain ($file);
71         break;
72     case '15':
73         if (function_exists("imagecreatefromwbmp"))
74             $img = @imagecreatefromwbmp ($file);
75         else
76             show_plain ($file);
77         break;
78     case '16':
79         if (function_exists("imagecreatefromxbm"))
80             $img = @imagecreatefromxbm ($file);
81         else
82             show_plain ($file);
83         break;
84     case 'xpm':
85         if (function_exists("imagecreatefromxpm"))
86             $img = @imagecreatefromxpm ($file);
87         else
88             show_plain ($file);
89         break;
90     case 'gd':
91         if (function_exists("imagecreatefromgd"))
92             $img = @imagecreatefromgd ($file);
93         else
94             show_plain ($file);
95         break;
96     case 'gd2':
97         if (function_exists("imagecreatefromgd2"))
98             $img = @imagecreatefromgd2 ($file);
99         else
100             show_plain ($file);
101         break;
102     default:
103         //we are not stupid...
104         header ("Content-type: text/html");
105         echo "<html><head></head><body>Not an image</body></html>";
106         exit();
107         break;
108 }    
109
110 $width  = @imagesx($img);
111 $height = @imagesy($img);
112
113 $newwidth = $_REQUEST['width'];
114 if (empty($newidth)) $newidth = 50;
115     
116 $newheight = $_REQUEST['height'];
117 if (empty($newheight)) $newheight = round($newwidth * ($height / $width)) ;
118
119 $thumb = imagecreate($newwidth, $newheight);
120 $img = imagecopyresampled($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
121
122 if ($remove == 1) unlink ($file);
123
124 header ("Content-type: image/png");
125 imagepng($thumb);
126
127 function show_plain () {
128     $mime = mime_content_type ($_REQUEST['url']);
129     header ("Content-type: $mime");
130     readfile($_REQUEST['url']);
131     exit();
132 }
133
134
135 /*
136  $Log: not supported by cvs2svn $
137 */
138
139 // Local Variables:
140 // mode: php
141 // tab-width: 8
142 // c-basic-offset: 4
143 // c-hanging-comment-ender-p: nil
144 // indent-tabs-mode: nil
145 // End:
146 ?>