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