full IE6 support now
[mediawiki.git] / includes / Image.php
blobed69912a617cbb4fc56f303aa2e424a764734a7d
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; # /
20 function Image( $name )
22 $this->name = $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() );
36 $img->title = $nt;
37 return $img;
40 function getName()
42 return $this->name;
45 function getURL()
47 return $this->url;
50 function getImagePath()
52 return $this->imagePath;
55 function getWidth()
57 return $this->width;
60 function getHeight()
62 return $this->height;
65 function getType()
67 return $this->type;
70 function getEscapeLocalURL()
72 return $this->title->escapeLocalURL();
75 function wfImageUrl( $name )
77 global $wgUploadPath;
78 $hash = md5( $name );
80 $url = "{$wgUploadPath}/" . $hash{0} . "/" .
81 substr( $hash, 0, 2 ) . "/{$name}";
82 return wfUrlencode( $url );
86 function exists()
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
104 return "";
107 # Sanity check $width
108 $width = IntVal( $width );
109 if( $width <= 0 ) {
110 # BZZZT
111 return "";
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) ) ) {
120 # Squid purging
121 if ( $wgUseSquid ) {
122 $urlArr = Array(
123 $wgInternalServer.$thumbUrl
125 wfPurgeSquidServers($urlArr);
128 if ( $wgUseImageMagick ) {
129 # use ImageMagick
130 $cmd = $wgImageMagickConvertCommand .
131 " -quality 85 -geometry {$width} ".
132 escapeshellarg($this->imagePath) . " " .
133 escapeshellarg($thumbPath);
134 $conv = shell_exec( $cmd );
135 } else {
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 ) {
142 case 1: # GIF
143 $src_image = imagecreatefromgif( $this->imagePath );
144 break;
145 case 2: # JPG
146 $src_image = imagecreatefromjpeg( $this->imagePath );
147 break;
148 case 3: # PNG
149 $src_image = imagecreatefrompng( $this->imagePath );
150 break;
151 case 15: # WBMP for WML
152 $src_image = imagecreatefromwbmp( $this->imagePath );
153 break;
154 case 16: # XBM
155 $src_image = imagecreatefromxbm( $this->imagePath );
156 break;
157 default:
158 return "Image type not supported";
159 break;
161 $height = floor( $this->height * ( $width/$this->width ) );
162 $dst_image = imagecreatetruecolor( $width, $height );
163 imagecopyresampled( $dst_image, $src_image,
164 0,0,0,0,
165 $width, $height, $this->width, $this->height );
166 switch( $this->type ) {
167 case 1: # GIF
168 case 3: # PNG
169 case 15: # WBMP
170 case 16: # XBM
171 #$thumbUrl .= ".png";
172 #$thumbPath .= ".png";
173 imagepng( $dst_image, $thumbPath );
174 break;
175 case 2: # JPEG
176 #$thumbUrl .= ".jpg";
177 #$thumbPath .= ".jpg";
178 imageinterlace( $dst_image );
179 imagejpeg( $dst_image, $thumbPath, 95 );
180 break;
181 default:
182 break;
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 );
200 return $thumbUrl;
201 } //function createThumb
203 } //class