open edit help in separate window to work around IE data loss bug
[mediawiki.git] / includes / Image.php
blobc183d46735442494b4ac3211af6217f40697c068
1 <?php
2 # Class to represent an image
3 # Provides methods to retrieve paths (physical, logical, URL),
4 # to generate thumbnails or for uploading.
6 class Image
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 $width, # \
15 $height, # --- returned by getimagesize, see http://de3.php.net/manual/en/function.getimagesize.php
16 $type, # |
17 $attr; # /
21 function Image( $name )
23 $this->name = $name;
24 $this->title = Title::makeTitle( Namespace::getImage(), $this->name );
25 $this->imagePath = wfImagePath( $name );
26 $this->url = $this->wfImageUrl( $name );
28 if ( $this->fileExists = file_exists( $this->imagePath ) ) // Sic!, "=" is intended
30 list($this->width, $this->height, $this->type, $this->attr) = getimagesize( $this->imagePath );
34 function newFromTitle( $nt )
36 $img = new Image( $nt->getDBKey() );
37 $img->title = $nt;
38 return $img;
41 function getName()
43 return $this->name;
46 function getURL()
48 return $this->url;
51 function getImagePath()
53 return $this->imagePath;
56 function getWidth()
58 return $this->width;
61 function getHeight()
63 return $this->height;
66 function getType()
68 return $this->type;
71 function getEscapeLocalURL()
73 return $this->title->escapeLocalURL();
76 function wfImageUrl( $name )
78 global $wgUploadPath;
79 $hash = md5( $name );
81 $url = "{$wgUploadPath}/" . $hash{0} . "/" .
82 substr( $hash, 0, 2 ) . "/{$name}";
83 return wfUrlencode( $url );
87 function exists()
89 return $this->fileExists;
92 function thumbUrl( $width, $subdir="thumb" ) {
93 global $wgUploadPath;
95 $name = $this->thumbName( $width );
96 $hash = md5( $name );
97 $url = "{$wgUploadPath}/{$subdir}/" . $hash{0} . "/" . substr( $hash, 0, 2 ) . "/{$name}";
99 return wfUrlencode($url);
102 function thumbName( $width ) {
103 return $width."px-".$this->name;
106 function createThumb( $width ) {
107 global $wgUploadDirectory;
108 global $wgImageMagickConvertCommand;
109 global $wgUseImageMagick;
110 global $wgUseSquid, $wgInternalServer;
111 $thumbName = $this->thumbName( $width );
112 $thumbPath = wfImageThumbDir( $thumbName )."/".$thumbName;
113 $thumbUrl = $this->thumbUrl( $width );
115 if ( ! $this->exists() )
117 # If there is no image, there will be no thumbnail
118 return "";
121 # Sanity check $width
122 $width = IntVal( $width );
123 if( $width <= 0 ) {
124 # BZZZT
125 return "";
127 if( $width > $this->width ) {
128 # Don't make an image bigger than the source
129 return $this->getURL();
132 if ( (! file_exists( $thumbPath ) )
133 || ( filemtime($thumbPath) < filemtime($this->imagePath) ) ) {
134 # Squid purging
135 if ( $wgUseSquid ) {
136 $urlArr = Array(
137 $wgInternalServer.$thumbUrl
139 wfPurgeSquidServers($urlArr);
142 if ( $wgUseImageMagick ) {
143 # use ImageMagick
144 $cmd = $wgImageMagickConvertCommand .
145 " -quality 85 -geometry {$width} ".
146 escapeshellarg($this->imagePath) . " " .
147 escapeshellarg($thumbPath);
148 $conv = shell_exec( $cmd );
149 } else {
150 # Use PHP's builtin GD library functions.
152 # First find out what kind of file this is, and select the correct
153 # input routine for this.
155 switch( $this->type ) {
156 case 1: # GIF
157 $src_image = imagecreatefromgif( $this->imagePath );
158 break;
159 case 2: # JPG
160 $src_image = imagecreatefromjpeg( $this->imagePath );
161 break;
162 case 3: # PNG
163 $src_image = imagecreatefrompng( $this->imagePath );
164 break;
165 case 15: # WBMP for WML
166 $src_image = imagecreatefromwbmp( $this->imagePath );
167 break;
168 case 16: # XBM
169 $src_image = imagecreatefromxbm( $this->imagePath );
170 break;
171 default:
172 return "Image type not supported";
173 break;
175 $height = floor( $this->height * ( $width/$this->width ) );
176 $dst_image = imagecreatetruecolor( $width, $height );
177 imagecopyresampled( $dst_image, $src_image,
178 0,0,0,0,
179 $width, $height, $this->width, $this->height );
180 switch( $this->type ) {
181 case 1: # GIF
182 case 3: # PNG
183 case 15: # WBMP
184 case 16: # XBM
185 #$thumbUrl .= ".png";
186 #$thumbPath .= ".png";
187 imagepng( $dst_image, $thumbPath );
188 break;
189 case 2: # JPEG
190 #$thumbUrl .= ".jpg";
191 #$thumbPath .= ".jpg";
192 imageinterlace( $dst_image );
193 imagejpeg( $dst_image, $thumbPath, 95 );
194 break;
195 default:
196 break;
198 imagedestroy( $dst_image );
199 imagedestroy( $src_image );
204 # Check for zero-sized thumbnails. Those can be generated when
205 # no disk space is available or some other error occurs
207 $thumbstat = stat( $thumbPath );
208 if( $thumbstat["size"] == 0 )
210 unlink( $thumbPath );
214 return $thumbUrl;
215 } //function createThumb
217 } //class
219 // return path name of an image
220 // canonicalize name.
221 function wfImagePath( $imgname )
223 global $wgUploadDirectory;
225 $nt = Title::newFromText( $imgname );
226 if( !$nt ) return "";
228 $name = $nt->getDBkey();
229 $hash = md5( $name );
231 $path = "{$wgUploadDirectory}/" . $hash{0} . "/" .
232 substr( $hash, 0, 2 ) . "/{$name}";
233 return $path;