quick hack to protect css/js subpages of userpages. Should be replaced by some more...
[mediawiki.git] / includes / Image.php
blob27ad21e32638271dddae082559b668ef45f9a830
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 if ( (! file_exists( $thumbPath ) )
108 || ( filemtime($thumbPath) < filemtime($this->imagePath) ) ) {
109 # Squid purging
110 if ( $wgUseSquid ) {
111 $urlArr = Array(
112 $wgInternalServer.$thumbUrl
114 wfPurgeSquidServers($urlArr);
117 if ( $wgUseImageMagick ) {
118 # use ImageMagick
119 $cmd = $wgImageMagickConvertCommand .
120 " -quality 85 -geometry {$width} ".
121 escapeshellarg($this->imagePath) . " " .
122 escapeshellarg($thumbPath);
123 $conv = shell_exec( $cmd );
124 } else {
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 ) {
131 case 1: # GIF
132 $src_image = imagecreatefromgif( $this->imagePath );
133 break;
134 case 2: # JPG
135 $src_image = imagecreatefromjpeg( $this->imagePath );
136 break;
137 case 3: # PNG
138 $src_image = imagecreatefrompng( $this->imagePath );
139 break;
140 case 15: # WBMP for WML
141 $src_image = imagecreatefromwbmp( $this->imagePath );
142 break;
143 case 16: # XBM
144 $src_image = imagecreatefromxbm( $this->imagePath );
145 break;
146 default:
147 return "Image type not supported";
148 break;
150 $height = floor( $this->height * ( $width/$this->width ) );
151 $dst_image = imagecreatetruecolor( $width, $height );
152 imagecopyresampled( $dst_image, $src_image,
153 0,0,0,0,
154 $width, $height, $this->width, $this->height );
155 switch( $this->type ) {
156 case 1: # GIF
157 case 3: # PNG
158 case 15: # WBMP
159 case 16: # XBM
160 #$thumbUrl .= ".png";
161 #$thumbPath .= ".png";
162 imagepng( $dst_image, $thumbPath );
163 break;
164 case 2: # JPEG
165 #$thumbUrl .= ".jpg";
166 #$thumbPath .= ".jpg";
167 imageinterlace( $dst_image );
168 imagejpeg( $dst_image, $thumbPath, 95 );
169 break;
170 default:
171 break;
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 );
189 return $thumbUrl;
190 } //function createThumb
192 } //class