2482718c2b0905ed10d02713584fe40862622d1d
[lhc/web/wiklou.git] / includes / Image.php
1 <?php
2 # Class to represent an image
3 # Provides methods to retrieve paths (physical, logical, URL),
4 # to generate thumbnails or for uploading.
5
6 class Image
7 {
8 /* private */
9 var $name, # name of the image
10 $imagePath, # Path of the image
11 $url, # Image URL
12 $title, # Title object for this image. Initialized when needed.
13 $fileExists, # does the image file exist on disk?
14 $historyLine, # Number of line to return by nextHistoryLine()
15 $historyRes, # result of the query for the image's history
16 $width, # \
17 $height, # |
18 $bits, # --- returned by getimagesize, see http://de3.php.net/manual/en/function.getimagesize.php
19 $type, # |
20 $attr; # /
21
22
23
24 function Image( $name )
25 {
26 global $wgUploadDirectory;
27
28 $this->name = $name;
29 $this->title = Title::makeTitle( Namespace::getImage(), $this->name );
30 //$this->imagePath = wfImagePath( $name );
31 $hash = md5( $this->title->getDBkey() );
32 $this->imagePath = $wgUploadDirectory . "/" . $hash{0} . "/" .substr( $hash, 0, 2 ) . "/{$name}";
33
34 $this->url = $this->wfImageUrl( $name );
35
36 if ( $this->fileExists = file_exists( $this->imagePath ) ) // Sic!, "=" is intended
37 {
38 list($this->width, $this->height, $this->type, $this->attr) = getimagesize( $this->imagePath );
39 $gid = getimagesize( $this->imagePath );
40 $this->width = $gid["width"];
41 $this->height = $gid["height"];
42 $this->type = $gid["type"];
43 $this->attr = $gid["attr"];
44 if ( defined( $gid["bits"] ) )
45 {
46 $this->bits = $gid["bits"];
47 } else {
48 $this->bits = 0;
49 }
50 }
51 $this->historyLine = 0;
52 }
53
54 function newFromTitle( $nt )
55 {
56 $img = new Image( $nt->getDBKey() );
57 $img->title = $nt;
58 return $img;
59 }
60
61 function getName()
62 {
63 return $this->name;
64 }
65
66 function getURL()
67 {
68 return $this->url;
69 }
70
71 function getImagePath()
72 {
73 return $this->imagePath;
74 }
75
76 function getWidth()
77 {
78 return $this->width;
79 }
80
81 function getHeight()
82 {
83 return $this->height;
84 }
85
86 function getType()
87 {
88 return $this->type;
89 }
90
91 function getEscapeLocalURL()
92 {
93 return $this->title->escapeLocalURL();
94 }
95
96 function wfImageUrl( $name )
97 {
98 global $wgUploadPath;
99 $hash = md5( $name );
100
101 $url = "{$wgUploadPath}/" . $hash{0} . "/" .
102 substr( $hash, 0, 2 ) . "/{$name}";
103 return wfUrlencode( $url );
104 }
105
106
107 function exists()
108 {
109 return $this->fileExists;
110 }
111
112 function thumbUrl( $width, $subdir="thumb" ) {
113 global $wgUploadPath;
114
115 $name = $this->thumbName( $width );
116 $hash = md5( $name );
117 $url = "{$wgUploadPath}/{$subdir}/" . $hash{0} . "/" . substr( $hash, 0, 2 ) . "/{$name}";
118
119 return wfUrlencode($url);
120 }
121
122 function thumbName( $width ) {
123 return $width."px-".$this->name;
124 }
125
126 //**********************************************************************
127 // Create a thumbnail of the image having the specified width.
128 // The thumbnail will not be created if the width is larger than the
129 // image's width. Let the browser do the scaling in this case.
130 // The thumbnail is stored on disk and is only computed if the thumbnail
131 // file does not exist OR if it is older than the image.
132 function createThumb( $width ) {
133 global $wgUploadDirectory;
134 global $wgImageMagickConvertCommand;
135 global $wgUseImageMagick;
136 global $wgUseSquid, $wgInternalServer;
137
138 $width = IntVal( $width );
139
140 $thumbName = $this->thumbName( $width );
141 $thumbPath = wfImageThumbDir( $thumbName )."/".$thumbName;
142 $thumbUrl = $this->thumbUrl( $width );
143
144 if ( ! $this->exists() )
145 {
146 # If there is no image, there will be no thumbnail
147 return "";
148 }
149
150 # Sanity check $width
151 if( $width <= 0 ) {
152 # BZZZT
153 return "";
154 }
155
156 if( $width > $this->width ) {
157 # Don't make an image bigger than the source
158 return $this->getURL();
159 }
160
161 if ( (! file_exists( $thumbPath ) )
162 || ( filemtime($thumbPath) < filemtime($this->imagePath) ) ) {
163 # Squid purging
164 if ( $wgUseSquid ) {
165 $urlArr = Array(
166 $wgInternalServer.$thumbUrl
167 );
168 wfPurgeSquidServers($urlArr);
169 }
170
171 if ( $wgUseImageMagick ) {
172 # use ImageMagick
173 $cmd = $wgImageMagickConvertCommand .
174 " -quality 85 -geometry {$width} ".
175 escapeshellarg($this->imagePath) . " " .
176 escapeshellarg($thumbPath);
177 $conv = shell_exec( $cmd );
178 } else {
179 # Use PHP's builtin GD library functions.
180 #
181 # First find out what kind of file this is, and select the correct
182 # input routine for this.
183
184 $truecolor = false;
185
186 switch( $this->type ) {
187 case 1: # GIF
188 $src_image = imagecreatefromgif( $this->imagePath );
189 break;
190 case 2: # JPG
191 $src_image = imagecreatefromjpeg( $this->imagePath );
192 $truecolor = true;
193 break;
194 case 3: # PNG
195 $src_image = imagecreatefrompng( $this->imagePath );
196 $truecolor = ( $this->bits > 8 );
197 break;
198 case 15: # WBMP for WML
199 $src_image = imagecreatefromwbmp( $this->imagePath );
200 break;
201 case 16: # XBM
202 $src_image = imagecreatefromxbm( $this->imagePath );
203 break;
204 default:
205 return "Image type not supported";
206 break;
207 }
208 $height = floor( $this->height * ( $width/$this->width ) );
209 if ( $truecolor ) {
210 $dst_image = imagecreatetruecolor( $width, $height );
211 } else {
212 $dst_image = imagecreate( $width, $height );
213 }
214 imagecopyresampled( $dst_image, $src_image,
215 0,0,0,0,
216 $width, $height, $this->width, $this->height );
217 switch( $this->type ) {
218 case 1: # GIF
219 case 3: # PNG
220 case 15: # WBMP
221 case 16: # XBM
222 #$thumbUrl .= ".png";
223 #$thumbPath .= ".png";
224 imagepng( $dst_image, $thumbPath );
225 break;
226 case 2: # JPEG
227 #$thumbUrl .= ".jpg";
228 #$thumbPath .= ".jpg";
229 imageinterlace( $dst_image );
230 imagejpeg( $dst_image, $thumbPath, 95 );
231 break;
232 default:
233 break;
234 }
235 imagedestroy( $dst_image );
236 imagedestroy( $src_image );
237
238
239 }
240 #
241 # Check for zero-sized thumbnails. Those can be generated when
242 # no disk space is available or some other error occurs
243 #
244 $thumbstat = stat( $thumbPath );
245 if( $thumbstat["size"] == 0 )
246 {
247 unlink( $thumbPath );
248 }
249
250 }
251 return $thumbUrl;
252 } // END OF function createThumb
253
254 //**********************************************************************
255 // Return the image history of this image, line by line.
256 // start with current version, than old versions.
257 // use $this->historyLine to check which line to return:
258 // 0 return line for current version
259 // 1 query for old versions, return first one
260 // 2, ... return next old version from above query
261 function nextHistoryLine()
262 {
263 $fname = "Image::nextHistoryLine()";
264
265 if ( $this->historyLine == 0 ) // called for the first time, return line from cur
266 {
267 $sql = "SELECT img_size,img_description,img_user," .
268 "img_user_text,img_timestamp, '' AS oi_archive_name FROM image WHERE " .
269 "img_name='" . wfStrencode( $this->title->getDBkey() ) . "'";
270 $this->historyRes = wfQuery( $sql, DB_READ, $fname );
271
272 if ( 0 == wfNumRows( $this->historyRes ) ) { return FALSE; }
273
274 } else if ( $this->historyLine == 1 )
275 {
276 $sql = "SELECT oi_size AS img_size, oi_description AS img_description," .
277 "oi_user AS img_user," .
278 "oi_user_text AS img_user_text, oi_timestamp AS img_timestamp , oi_archive_name FROM oldimage WHERE " .
279 "oi_name='" . wfStrencode( $this->title->getDBkey() ) . "' " .
280 "ORDER BY oi_timestamp DESC";
281 $this->historyRes = wfQuery( $sql, DB_READ, $fname );
282 }
283 $this->historyLine ++;
284
285 return wfFetchObject( $this->historyRes );
286 }
287
288 function resetHistory()
289 {
290 $this->historyLine = 0;
291 }
292
293
294 } //class
295