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
20 function Image( $name )
23 $this->title
= Title
::makeTitle( Namespace::getImage(), $this->name
);
24 $this->imagePath
= wfImagePath( $name );
25 $this->url
= $this->wfImageUrl( $name );
27 if ( $this->fileExists
= file_exists( $this->imagePath
) ) // Sic!, "=" is intended
29 list($this->width
, $this->height
, $this->type
, $this->attr
) = getimagesize( $this->imagePath
);
33 function newFromTitle( $nt )
35 $img = new Image( $nt->getDBKey() );
50 function getImagePath()
52 return $this->imagePath
;
70 function getEscapeLocalURL()
72 return $this->title
->escapeLocalURL();
75 function wfImageUrl( $name )
80 $url = "{$wgUploadPath}/" . $hash{0} . "/" .
81 substr( $hash, 0, 2 ) . "/{$name}";
82 return wfUrlencode( $url );
88 return $this->fileExists
;
92 function createThumb( $width ) {
93 global $wgUploadDirectory;
94 global $wgImageMagickConvertCommand;
95 global $wgUseImageMagick;
96 global $wgUseSquid, $wgInternalServer;
97 $thumbName = $width."px-".$this->name
;
98 $thumbPath = wfImageThumbDir( $thumbName )."/".$thumbName;
99 $thumbUrl = wfImageThumbUrl( $thumbName );
101 if ( ! $this->exists() )
103 # If there is no image, there will be no thumbnail
107 # Sanity check $width
108 $width = IntVal( $width );
113 if( $width > $this->width
) {
114 # Don't make an image bigger than the source
115 return $this->getURL();
118 if ( (! file_exists( $thumbPath ) )
119 ||
( filemtime($thumbPath) < filemtime($this->imagePath
) ) ) {
123 $wgInternalServer.$thumbUrl
125 wfPurgeSquidServers($urlArr);
128 if ( $wgUseImageMagick ) {
130 $cmd = $wgImageMagickConvertCommand .
131 " -quality 85 -geometry {$width} ".
132 escapeshellarg($this->imagePath
) . " " .
133 escapeshellarg($thumbPath);
134 $conv = shell_exec( $cmd );
136 # Use PHP's builtin GD library functions.
138 # First find out what kind of file this is, and select the correct
139 # input routine for this.
141 switch( $this->type
) {
143 $src_image = imagecreatefromgif( $this->imagePath
);
146 $src_image = imagecreatefromjpeg( $this->imagePath
);
149 $src_image = imagecreatefrompng( $this->imagePath
);
151 case 15: # WBMP for WML
152 $src_image = imagecreatefromwbmp( $this->imagePath
);
155 $src_image = imagecreatefromxbm( $this->imagePath
);
158 return "Image type not supported";
161 $height = floor( $this->height
* ( $width/$this->width
) );
162 $dst_image = imagecreatetruecolor( $width, $height );
163 imagecopyresampled( $dst_image, $src_image,
165 $width, $height, $this->width
, $this->height
);
166 switch( $this->type
) {
171 #$thumbUrl .= ".png";
172 #$thumbPath .= ".png";
173 imagepng( $dst_image, $thumbPath );
176 #$thumbUrl .= ".jpg";
177 #$thumbPath .= ".jpg";
178 imageinterlace( $dst_image );
179 imagejpeg( $dst_image, $thumbPath, 95 );
184 imagedestroy( $dst_image );
185 imagedestroy( $src_image );
190 # Check for zero-sized thumbnails. Those can be generated when
191 # no disk space is available or some other error occurs
193 $thumbstat = stat( $thumbPath );
194 if( $thumbstat["size"] == 0 )
196 unlink( $thumbPath );
201 } //function createThumb