Don't create thumbnail images larger than the image; use the source image
[lhc/web/wiklou.git] / includes / Image.php
1 <?php
2 # Class to represent an image
3 # Provides methods to retrieve paths (physical, logical, URL),
4 # to generate thumbnails or for uploading.
5
6 class Image
7 {
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; # /
18
19
20 function Image( $name )
21 {
22 $this->name = $name;
23 $this->title = Title::makeTitle( Namespace::getImage(), $this->name );
24 $this->imagePath = wfImagePath( $name );
25 $this->url = $this->wfImageUrl( $name );
26
27 if ( $this->fileExists = file_exists( $this->imagePath ) ) // Sic!, "=" is intended
28 {
29 list($this->width, $this->height, $this->type, $this->attr) = getimagesize( $this->imagePath );
30 }
31 }
32
33 function newFromTitle( $nt )
34 {
35 $img = new Image( $nt->getDBKey() );
36 $img->title = $nt;
37 return $img;
38 }
39
40 function getName()
41 {
42 return $this->name;
43 }
44
45 function getURL()
46 {
47 return $this->url;
48 }
49
50 function getImagePath()
51 {
52 return $this->imagePath;
53 }
54
55 function getWidth()
56 {
57 return $this->width;
58 }
59
60 function getHeight()
61 {
62 return $this->height;
63 }
64
65 function getType()
66 {
67 return $this->type;
68 }
69
70 function getEscapeLocalURL()
71 {
72 return $this->title->escapeLocalURL();
73 }
74
75 function wfImageUrl( $name )
76 {
77 global $wgUploadPath;
78 $hash = md5( $name );
79
80 $url = "{$wgUploadPath}/" . $hash{0} . "/" .
81 substr( $hash, 0, 2 ) . "/{$name}";
82 return wfUrlencode( $url );
83 }
84
85
86 function exists()
87 {
88 return $this->fileExists;
89 }
90
91
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 );
100
101 if ( ! $this->exists() )
102 {
103 # If there is no image, there will be no thumbnail
104 return "";
105 }
106
107 # Sanity check $width
108 $width = IntVal( $width );
109 if( $width <= 0 ) {
110 # BZZZT
111 return "";
112 }
113 if( $width > $this->width ) {
114 # Don't make an image bigger than the source
115 return $this->getURL();
116 }
117
118 if ( (! file_exists( $thumbPath ) )
119 || ( filemtime($thumbPath) < filemtime($this->imagePath) ) ) {
120 # Squid purging
121 if ( $wgUseSquid ) {
122 $urlArr = Array(
123 $wgInternalServer.$thumbUrl
124 );
125 wfPurgeSquidServers($urlArr);
126 }
127
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.
137 #
138 # First find out what kind of file this is, and select the correct
139 # input routine for this.
140
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;
160 }
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;
183 }
184 imagedestroy( $dst_image );
185 imagedestroy( $src_image );
186
187
188 }
189 #
190 # Check for zero-sized thumbnails. Those can be generated when
191 # no disk space is available or some other error occurs
192 #
193 $thumbstat = stat( $thumbPath );
194 if( $thumbstat["size"] == 0 )
195 {
196 unlink( $thumbPath );
197 }
198
199 }
200 return $thumbUrl;
201 } //function createThumb
202
203 } //class