2 require_once 'File/Convert/Solution.php';
4 class File_Convert_Solution_scaleimage extends File_Convert_Solution
15 function targetName($fn, $x, $y)
18 return $fn . '.'.$x.'x'.$y.'.' . $this->ext;
23 function convert($fn,$x,$y,$pg)
27 if (!file_exists($fn) || (empty($x) && empty($y))) {
32 $target = $fn . '.'.$x.'x'.$y.'.' . $ext;
34 $this->debug("COVERT: FE:" . (file_exists($target) ? 1: 0) );
35 $this->debug("COVERT: FS:" . (file_exists($target) ? (filemtime($target) . '>' . @filemtime($fn)) : 'n/a'));
37 if ($this->debug < 2 && file_exists($target) && filesize($target) && filemtime($target) > filemtime($fn)) {
38 $this->debug("SCALEIMAGE - image exists $target");
41 $targetName = $target;
43 if ($this->to == 'image/x-ms-bmp') {
44 $targetName = "bmp3:$target";
48 //echo "GOT TARGET" . $target;
50 list($width, $height) = @getimagesize($fn);
53 switch (true) { // what about fit/pad etc...
55 // added to allow fix to 'x' without padding.. (empty string in x or y)
56 case (empty($x) && !strlen($x)) : // y only
62 case (empty($y) && !strlen($y)) : // x only
64 //print_R(array($x,$width));
70 if ($y == $height) { // no need to change
78 if ($x == $width) { // no need to change
86 list($width, $height) = @getimagesize($fn);
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) {
96 $extent ="-extent '{$x}x{$y}>' -gravity center " .
98 (!empty(self::$options['transparent_background'])) ?
99 "-transparent white" :
106 require_once 'System.php';
107 $CONVERT = System::which("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';
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);
125 // 100x0 --<< 100 SQUARE? = pad..
126 // 100x << 100 width proportion..
127 // 100x200 << fit and pad.
131 list($width, $height) = @getimagesize($fn);
134 $pad = is_numeric($x) && is_numeric($y);
139 $newheight = ($x/$width ) * $height;
141 $newwidth = ($y/$height) * $width;
147 $scaley = $newheight;
155 if ( (empty($y) && $x > $width && $x > $height)
156 || (!empty($y) && $x > $width && $y > $height)) {
158 // larger with padding..
162 $newheight = empty($y) ? $x : $y;
164 $padx = floor(($newwidth - $width) /2);
165 $pady = floor(($newheight - $height) /2);
172 // smaller or without padding..
175 $percent = $x/$width;
177 $newheight = empty($y) ? $x : $y;
180 $percent = min($percent, $y/$height);
183 $scalex = $width * $percent;
184 $scaley = $height * $percent;
186 $padx = floor(($newwidth - $scalex) /2);
187 $pady = floor(($newheight - $scaley) /2);
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 ));
197 $thumb = imagecreatetruecolor($newwidth, $newheight);
198 $white = imagecolorallocate ( $thumb , 255, 255, 255);
200 imagefill($thumb, 0,0, $white);
202 switch(exif_imagetype($fn)) {
204 $source = imagecreatefrompng($fn);
207 $source = imagecreatefromjpeg($fn);
210 die("invalid image type");
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);
218 imagejpeg($thumb,$target);
225 $fe = file_exists($target) && filesize($target) ? $target : false;
230 @chmod($target,fileperms($fn));