fix image issue
[pear] / File / Convert / Solution / scaleimage.php
1 <?php
2 require_once 'File/Convert/Solution.php';
3
4 class File_Convert_Solution_scaleimage extends File_Convert_Solution
5 {
6     
7    var $cmd;
8     static $rules = array(
9          
10         
11          
12     );
13       
14     
15     function targetName($fn, $x, $y)
16     {
17           
18         return  $fn . '.'.$x.'x'.$y.'.' . $this->ext;
19         
20          
21     }
22      
23     function convert($fn,$x,$y,$pg) 
24     {
25         
26           clearstatcache();
27         if (!file_exists($fn) || (empty($x) && empty($y))) {
28             
29             return false;
30         }
31         $ext = $this->ext;
32         $target = $fn . '.'.$x.'x'.$y.'.' . $ext;
33         
34         $this->debug("COVERT: FE:" . (file_exists($target) ? 1: 0) );
35         $this->debug("COVERT: FS:" . (file_exists($target) ?  (filemtime($target) . '>' .  @filemtime($fn)) : 'n/a'));
36  
37         if ($this->debug < 2 && file_exists($target)  && filesize($target) && filemtime($target) > filemtime($fn)) {
38             $this->debug("SCALEIMAGE - image exists $target");
39             return $target;
40         }
41         $targetName = $target;
42         $strip = '-strip';
43         if ($this->to == 'image/x-ms-bmp') {
44             $targetName = "bmp3:$target";
45             $strip = '';
46         }
47         
48         //echo "GOT TARGET"  . $target;
49         
50         list($width, $height) = @getimagesize($fn);
51        
52         $extent = '';
53         switch (true) { // what about fit/pad etc...
54             
55             // added to allow fix to 'x' without padding.. (empty string in x or y)
56             case (empty($x) && !strlen($x)) :  // y only
57                 $scale = "x{$y}";
58                
59                 break;
60             
61             
62             case (empty($y) && !strlen($y)) : // x only
63                 $scale = "{$x}x";
64                 //print_R(array($x,$width));
65                 
66                 break;
67             
68             case (empty($x)) :
69                 $scale = "x{$y}>";
70                  if ($y == $height) { // no need to change
71                     return $fn;
72                 }
73                 
74                 break;
75             case (empty($y)) :
76                 $scale = "{$x}x>";
77               
78                 if ($x == $width) {  // no need to change
79                     return $fn;
80                 }
81                 
82                 
83                 break;
84             default:
85                 
86                 list($width, $height) = @getimagesize($fn);
87                  
88                 $scale = "{$x}x{$y}>";
89                 $define = "-define jpeg:size={$x}x{$y}";
90                 // if image required is bigger than the original - then we will just pad it..
91                 if ($width < $x && $height < $y) {
92                     $scale = '';
93                     $define  = '';
94                 }
95                 
96                 $extent ="-extent '{$x}x{$y}>' -gravity center " . 
97                     (
98                         (!empty(self::$options['transparent_background'])) ? 
99                         "-transparent white" :
100                         "-background white"
101                     ) . 
102                     " {$define}";
103                 break;
104         }
105         
106         require_once 'System.php';
107         $CONVERT = System::which("convert");
108         
109           if ($CONVERT) {
110             // note extend has to go after the resize.. so it does that first...
111             // changed to using 'sample' rather than resize
112             //-- it's alot faster? - not sure about quality though?
113             // 5Mb is the cut off to use the faster version.
114             $resize_method = @filesize($fn) > 50000000 ? '-sample' : '-scale';
115             
116             $cmd = "{$CONVERT} " . $strip . " -colorspace sRGB -interlace none -density 800 -quality 90 ". 
117                  (strlen($scale) ?  " {$resize_method} '{$scale}' " : '' ).
118                  $extent  . " '{$fn}' '{$targetName}'";
119              $cmdres  = $this->exec($cmd);
120             $this->exec($cmd);
121             
122             
123             
124         } else {
125             // 100x0 --<< 100 SQUARE? = pad..
126             // 100x   << 100 width proportion..
127             // 100x200 << fit and pad.
128             
129              
130             
131             list($width, $height) = @getimagesize($fn);
132             
133             
134             $pad = is_numeric($x) && is_numeric($y);
135            
136             if (!$pad) {
137                 if ($x) {
138                     $newwidth = $x;
139                     $newheight = ($x/$width ) * $height;
140                 } else {
141                     $newwidth = ($y/$height) * $width;
142                     $newheight = $y;
143                 }
144                 $padx= 0;
145                 $pady = 0;
146                 $scalex = $newwidth;
147                 $scaley = $newheight;
148                 
149             } else {
150                 
151                  
152             
153             
154             
155                 if ( (empty($y)  && $x > $width && $x >  $height)
156                     || (!empty($y)  && $x > $width && $y > $height)) {
157                     
158                     // larger with padding..
159                     
160                     
161                     $newwidth =  $x;
162                     $newheight = empty($y) ? $x : $y;
163                     // pad..
164                     $padx = floor(($newwidth - $width) /2);
165                     $pady = floor(($newheight - $height) /2);
166                     
167                     $scalex = $width;
168                     $scaley = $height;
169                     
170                 } else {
171                     
172                     // smaller or without padding..
173                     
174                     
175                     $percent = $x/$width;
176                     $newwidth =  $x;
177                     $newheight = empty($y) ? $x : $y;
178                     
179                     if (!empty($y)) {
180                         $percent = min($percent,   $y/$height);
181                     }
182                     
183                     $scalex = $width * $percent;
184                     $scaley = $height * $percent;
185                     
186                     $padx = floor(($newwidth - $scalex) /2);
187                     $pady = floor(($newheight - $scaley) /2);
188                     
189                     
190                 }
191             }
192             
193             
194             //echo '<PRE>';print_r(array(  'x' => $x, 'y' => $y,  'newwidth' => $newwidth , 'newheight' => $newheight , 'width' => $width , 'height' => $height ,
195             //    'scalex' => $scalex , 'scaley' => $scaley ,  'padx' => $padx,  'pady' => $pady ));
196             //exit;
197             $thumb = imagecreatetruecolor($newwidth, $newheight);
198             $white = imagecolorallocate ( $thumb , 255, 255, 255);
199
200             imagefill($thumb, 0,0,  $white);
201             
202             switch(exif_imagetype($fn)) {
203                 case IMAGETYPE_PNG:
204                     $source = imagecreatefrompng($fn);
205                     break;
206                 case IMAGETYPE_JPEG:
207                     $source = imagecreatefromjpeg($fn);
208                     break;
209                 default:
210                     die("invalid image type");
211                     
212             }
213             // Resize
214             //resource $dst_image , resource $src_image , 
215                 // int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h 
216             imagecopyresampled($thumb, $source, $padx, $pady, 0, 0, $scalex, $scaley, $width, $height);
217
218             imagejpeg($thumb,$target);
219         }
220         
221         
222          // echo $cmd;          exit;
223        
224         clearstatcache();
225         $fe =   file_exists($target)  && filesize($target) ? $target : false;
226         
227         if (!$fe) {
228             return false;
229         }
230         @chmod($target,fileperms($fn));
231         return $fe;
232         
233     }
234 }