moved squid purge down, untested
[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 @$gis = getimagesize( $this->imagePath );
39 if( $gis !== false ) {
40 $this->width = $gis[0];
41 $this->height = $gis[1];
42 $this->type = $gis[2];
43 $this->attr = $gis[3];
44 if ( isset( $gis["bits"] ) ) {
45 $this->bits = $gis["bits"];
46 } else {
47 $this->bits = 0;
48 }
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 // Returns the URL.
133 function createThumb( $width ) {
134 global $wgUploadDirectory;
135 global $wgImageMagickConvertCommand;
136 global $wgUseImageMagick;
137 global $wgUseSquid, $wgInternalServer;
138
139 $width = IntVal( $width );
140
141 $thumbName = $this->thumbName( $width );
142 $thumbPath = wfImageThumbDir( $thumbName )."/".$thumbName;
143 $thumbUrl = $this->thumbUrl( $width );
144
145 if ( ! $this->exists() )
146 {
147 # If there is no image, there will be no thumbnail
148 return "";
149 }
150
151 # Sanity check $width
152 if( $width <= 0 ) {
153 # BZZZT
154 return "";
155 }
156
157 if( $width > $this->width ) {
158 # Don't make an image bigger than the source
159 return $this->getURL();
160 }
161
162 if ( (! file_exists( $thumbPath ) ) || ( filemtime($thumbPath) < filemtime($this->imagePath) ) ) {
163 if ( $wgUseImageMagick ) {
164 # use ImageMagick
165 $cmd = $wgImageMagickConvertCommand .
166 " -quality 85 -geometry {$width} ".
167 escapeshellarg($this->imagePath) . " " .
168 escapeshellarg($thumbPath);
169 $conv = shell_exec( $cmd );
170 } else {
171 # Use PHP's builtin GD library functions.
172 #
173 # First find out what kind of file this is, and select the correct
174 # input routine for this.
175
176 $truecolor = false;
177
178 switch( $this->type ) {
179 case 1: # GIF
180 $src_image = imagecreatefromgif( $this->imagePath );
181 break;
182 case 2: # JPG
183 $src_image = imagecreatefromjpeg( $this->imagePath );
184 $truecolor = true;
185 break;
186 case 3: # PNG
187 $src_image = imagecreatefrompng( $this->imagePath );
188 $truecolor = ( $this->bits > 8 );
189 break;
190 case 15: # WBMP for WML
191 $src_image = imagecreatefromwbmp( $this->imagePath );
192 break;
193 case 16: # XBM
194 $src_image = imagecreatefromxbm( $this->imagePath );
195 break;
196 default:
197 return "Image type not supported";
198 break;
199 }
200 $height = floor( $this->height * ( $width/$this->width ) );
201 if ( $truecolor ) {
202 $dst_image = imagecreatetruecolor( $width, $height );
203 } else {
204 $dst_image = imagecreate( $width, $height );
205 }
206 imagecopyresampled( $dst_image, $src_image,
207 0,0,0,0,
208 $width, $height, $this->width, $this->height );
209 switch( $this->type ) {
210 case 1: # GIF
211 case 3: # PNG
212 case 15: # WBMP
213 case 16: # XBM
214 #$thumbUrl .= ".png";
215 #$thumbPath .= ".png";
216 imagepng( $dst_image, $thumbPath );
217 break;
218 case 2: # JPEG
219 #$thumbUrl .= ".jpg";
220 #$thumbPath .= ".jpg";
221 imageinterlace( $dst_image );
222 imagejpeg( $dst_image, $thumbPath, 95 );
223 break;
224 default:
225 break;
226 }
227 imagedestroy( $dst_image );
228 imagedestroy( $src_image );
229
230
231 }
232 #
233 # Check for zero-sized thumbnails. Those can be generated when
234 # no disk space is available or some other error occurs
235 #
236 $thumbstat = stat( $thumbPath );
237 if( $thumbstat["size"] == 0 )
238 {
239 unlink( $thumbPath );
240 }
241
242 # Purge squid
243 # This has to be done after the image is updated and present for all machines on NFS,
244 # or else the old version might be stored into the squid again
245 if ( $wgUseSquid ) {
246 $urlArr = Array(
247 $wgInternalServer.$thumbUrl
248 );
249 wfPurgeSquidServers($urlArr);
250 }
251 }
252 return $thumbUrl;
253 } // END OF function createThumb
254
255 //**********************************************************************
256 // Return the image history of this image, line by line.
257 // starts with current version, then old versions.
258 // uses $this->historyLine to check which line to return:
259 // 0 return line for current version
260 // 1 query for old versions, return first one
261 // 2, ... return next old version from above query
262 function nextHistoryLine()
263 {
264 $fname = "Image::nextHistoryLine()";
265 $dbr =& wfGetDB( DB_SLAVE );
266 if ( $this->historyLine == 0 ) {// called for the first time, return line from cur
267 $this->historyRes = $dbr->select( 'image',
268 array( 'img_size','img_description','img_user','img_user_text','img_timestamp', "'' AS oi_archive_name" ),
269 array( 'img_name' => $this->title->getDBkey() ),
270 $fname
271 );
272 if ( 0 == wfNumRows( $this->historyRes ) ) {
273 return FALSE;
274 }
275 } else if ( $this->historyLine == 1 ) {
276 $this->historyRes = $dbr->select( 'oldimage',
277 array( 'oi_size AS img_size', 'oi_description AS img_description', 'oi_user AS img_user',
278 'oi_user_text AS img_user_text', 'oi_timestamp AS img_timestamp', 'oi_archive_name'
279 ), array( 'oi_name' => $this->title->getDBkey() ), $fname, array( 'ORDER BY' => 'oi_timestamp DESC' )
280 );
281 }
282 $this->historyLine ++;
283
284 return $dbr->fetchObject( $this->historyRes );
285 }
286
287 function resetHistory()
288 {
289 $this->historyLine = 0;
290 }
291
292
293 } //class
294