2 # Class to represent an image
3 # Provides methods to retrieve paths (physical, logical, URL),
4 # to generate thumbnails or for uploading.
9 var $name, # name of the image
10 $imagePath, # Path of the image
12 $title, # Title object for this image. Initialized when needed.
13 $fileExists, # does the image file exist on disk?
15 $height, # --- returned by getimagesize, see http://de3.php.net/manual/en/function.getimagesize.php
21 function Image( $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() );
51 function getImagePath()
53 return $this->imagePath
;
71 function getEscapeLocalURL()
73 return $this->title
->escapeLocalURL();
76 function wfImageUrl( $name )
81 $url = "{$wgUploadPath}/" . $hash{0} . "/" .
82 substr( $hash, 0, 2 ) . "/{$name}";
83 return wfUrlencode( $url );
89 return $this->fileExists
;
92 function thumbUrl( $width, $subdir="thumb" ) {
95 $name = $this->thumbName( $width );
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
121 # Sanity check $width
122 $width = IntVal( $width );
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
) ) ) {
137 $wgInternalServer.$thumbUrl
139 wfPurgeSquidServers($urlArr);
142 if ( $wgUseImageMagick ) {
144 $cmd = $wgImageMagickConvertCommand .
145 " -quality 85 -geometry {$width} ".
146 escapeshellarg($this->imagePath
) . " " .
147 escapeshellarg($thumbPath);
148 $conv = shell_exec( $cmd );
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
) {
157 $src_image = imagecreatefromgif( $this->imagePath
);
160 $src_image = imagecreatefromjpeg( $this->imagePath
);
163 $src_image = imagecreatefrompng( $this->imagePath
);
165 case 15: # WBMP for WML
166 $src_image = imagecreatefromwbmp( $this->imagePath
);
169 $src_image = imagecreatefromxbm( $this->imagePath
);
172 return "Image type not supported";
175 $height = floor( $this->height
* ( $width/$this->width
) );
176 $dst_image = imagecreatetruecolor( $width, $height );
177 imagecopyresampled( $dst_image, $src_image,
179 $width, $height, $this->width
, $this->height
);
180 switch( $this->type
) {
185 #$thumbUrl .= ".png";
186 #$thumbPath .= ".png";
187 imagepng( $dst_image, $thumbPath );
190 #$thumbUrl .= ".jpg";
191 #$thumbPath .= ".jpg";
192 imageinterlace( $dst_image );
193 imagejpeg( $dst_image, $thumbPath, 95 );
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 );
215 } //function createThumb
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}";