Fix bad path to magnify clip
[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 if ( (! file_exists( $thumbPath ) )
108 || ( filemtime($thumbPath) < filemtime($this->imagePath) ) ) {
109 # Squid purging
110 if ( $wgUseSquid ) {
111 $urlArr = Array(
112 $wgInternalServer.$thumbUrl
113 );
114 wfPurgeSquidServers($urlArr);
115 }
116
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.
126 #
127 # First find out what kind of file this is, and select the correct
128 # input routine for this.
129
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;
149 }
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;
172 }
173 imagedestroy( $dst_image );
174 imagedestroy( $src_image );
175
176
177 }
178 #
179 # Check for zero-sized thumbnails. Those can be generated when
180 # no disk space is available or some other error occurs
181 #
182 $thumbstat = stat( $thumbPath );
183 if( $thumbstat["size"] == 0 )
184 {
185 unlink( $thumbPath );
186 }
187
188 }
189 return $thumbUrl;
190 } //function createThumb
191
192 } //class