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 if ( (! file_exists( $thumbPath ) )
108 ||
( filemtime($thumbPath) < filemtime($this->imagePath
) ) ) {
112 $wgInternalServer.$thumbUrl
114 wfPurgeSquidServers($urlArr);
117 if ( $wgUseImageMagick ) {
119 $cmd = $wgImageMagickConvertCommand .
120 " -quality 85 -geometry {$width} ".
121 escapeshellarg($this->imagePath
) . " " .
122 escapeshellarg($thumbPath);
123 $conv = shell_exec( $cmd );
125 # Use PHP's builtin GD library functions.
127 # First find out what kind of file this is, and select the correct
128 # input routine for this.
130 switch( $this->type
) {
132 $src_image = imagecreatefromgif( $this->imagePath
);
135 $src_image = imagecreatefromjpeg( $this->imagePath
);
138 $src_image = imagecreatefrompng( $this->imagePath
);
140 case 15: # WBMP for WML
141 $src_image = imagecreatefromwbmp( $this->imagePath
);
144 $src_image = imagecreatefromxbm( $this->imagePath
);
147 return "Image type not supported";
150 $height = floor( $this->height
* ( $width/$this->width
) );
151 $dst_image = imagecreatetruecolor( $width, $height );
152 imagecopyresampled( $dst_image, $src_image,
154 $width, $height, $this->width
, $this->height
);
155 switch( $this->type
) {
160 #$thumbUrl .= ".png";
161 #$thumbPath .= ".png";
162 imagepng( $dst_image, $thumbPath );
165 #$thumbUrl .= ".jpg";
166 #$thumbPath .= ".jpg";
167 imageinterlace( $dst_image );
168 imagejpeg( $dst_image, $thumbPath, 95 );
173 imagedestroy( $dst_image );
174 imagedestroy( $src_image );
179 # Check for zero-sized thumbnails. Those can be generated when
180 # no disk space is available or some other error occurs
182 $thumbstat = stat( $thumbPath );
183 if( $thumbstat["size"] == 0 )
185 unlink( $thumbPath );
190 } //function createThumb