]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - ImageTile.php
trailing_spaces
[SourceForge/phpwiki.git] / ImageTile.php
1 <?php // -*-php-*- $Id$
2 // FIXME! This is a mess. Everything.
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) = explode('=', $config[0]);
15             $data_path = trim($value).'/';
16         break;
17     }
18     }
19     fclose($fp);
20     @mkdir($data_path."uploads/thumbs",0775);
21     $file = $data_path."uploads/thumbs/image_" . ((float)$usec + (float)$sec);
22     $source = url_get_contents($_REQUEST['url']);
23
24     @$fp = fopen($file,'w+');
25     if (!$fp) {
26         header ("Content-type: text/html");
27         echo "<html><head></head><body>ERROR : unable to open $file in write mode</body></html>";
28     }
29     fwrite($fp,$source);
30     $remove = 1;
31
32 } else {
33     @$fp = fopen($_REQUEST['url'],"r");
34
35     if (!$fp) {
36
37         header ("Content-type: text/html");
38         echo "<html><head></head><body>Not an image</body></html>";
39         exit();
40
41     } else {
42         $file = $_REQUEST['url'];
43         fclose($fp);
44     }
45 }
46 list ($a, $b, $type, $attr) = @getimagesize ($file);
47
48 if (!$type) {
49     $type = basename ($_REQUEST['url']);
50     $type = preg_split ('/\./',$type);
51     $type = array_pop ($type);
52 }
53
54 switch ($type) {
55     case '2':
56         if (function_exists("imagecreatefromjpeg"))
57             $img = @imagecreatefromjpeg ($file);
58         else
59             show_plain ($file);
60         break;
61     case '3':
62         if (function_exists("imagecreatefrompng"))
63             $img = @imagecreatefrompng ($file);
64         else
65             show_plain ($file);
66         break;
67     case '1':
68         if (function_exists("imagecreatefromgif"))
69             $img = @imagecreatefromgif ($file);
70         else
71             show_plain ($file);
72         break;
73     case '15':
74         if (function_exists("imagecreatefromwbmp"))
75             $img = @imagecreatefromwbmp ($file);
76         else
77             show_plain ($file);
78         break;
79     case '16':
80         if (function_exists("imagecreatefromxbm"))
81             $img = @imagecreatefromxbm ($file);
82         else
83             show_plain ($file);
84         break;
85     case 'xpm':
86         if (function_exists("imagecreatefromxpm"))
87             $img = @imagecreatefromxpm ($file);
88         else
89             show_plain ($file);
90         break;
91     case 'gd':
92         if (function_exists("imagecreatefromgd"))
93             $img = @imagecreatefromgd ($file);
94         else
95             show_plain ($file);
96         break;
97     case 'gd2':
98         if (function_exists("imagecreatefromgd2"))
99             $img = @imagecreatefromgd2 ($file);
100         else
101             show_plain ($file);
102         break;
103     default:
104         //we are not stupid...
105         header ("Content-type: text/html");
106         echo "<html><head></head><body>Not an image</body></html>";
107         exit();
108         break;
109 }
110
111 $width  = @imagesx($img);
112 $height = @imagesy($img);
113
114 $newwidth = $_REQUEST['width'];
115 if (empty($newidth)) $newidth = 50;
116
117 $newheight = $_REQUEST['height'];
118 if (empty($newheight)) $newheight = round($newwidth * ($height / $width)) ;
119
120 // php-4.2.x is stupid enough to define on gd only a stub for imagecopyresampled.
121 // So function_exists('imagecopyresampled') will fail.
122 if (!extension_loaded('gd2') and (substr(PHP_OS,0,3) != 'WIN'))
123     loadPhpExtension('gd2');
124 if (extension_loaded('gd2')) {
125     $thumb = imagecreatetruecolor($newwidth, $newheight);
126     $img = imagecopyresampled($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
127 } else {
128     $thumb = imagecreate($newwidth, $newheight);
129     $img = imagecopyresized($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
130 }
131
132 if ($remove == 1) unlink ($file);
133
134 header ("Content-type: image/png");
135 imagepng($thumb);
136
137 function show_plain () {
138     $mime = mime_content_type ($_REQUEST['url']);
139     header ("Content-type: $mime");
140     readfile($_REQUEST['url']);
141     exit();
142 }
143
144 // Local Variables:
145 // mode: php
146 // tab-width: 8
147 // c-basic-offset: 4
148 // c-hanging-comment-ender-p: nil
149 // indent-tabs-mode: nil
150 // End:
151 ?>