PHP: how to resize image for multiple size -
i have class resize image, worked create 1 size. need create multiple size each image.
$resizeobj -> resizeimage(150, 100, 'crop');
line create image 150*100
. need create image 150*100
, 200*250
how fix this?
class:
<?php class resize { // *** class variables private $image; private $width; private $height; private $imageresized; function __construct($filename) { // *** open file $this->image = $this->openimage($filename); // *** width , height $this->width = imagesx($this->image); $this->height = imagesy($this->image); } ## -------------------------------------------------------- private function openimage($file) { // *** extension $extension = strtolower(strrchr($file, '.')); switch($extension) { case '.jpg': case '.jpeg': $img = @imagecreatefromjpeg($file); break; case '.gif': $img = @imagecreatefromgif($file); break; case '.png': $img = @imagecreatefrompng($file); break; default: $img = false; break; } return $img; } ## -------------------------------------------------------- public function resizeimage($newwidth, $newheight, $option="auto") { // *** optimal width , height - based on $option $optionarray = $this->getdimensions($newwidth, $newheight, $option); $optimalwidth = $optionarray['optimalwidth']; $optimalheight = $optionarray['optimalheight']; // *** resample - create image canvas of x, y size $this->imageresized = imagecreatetruecolor($optimalwidth, $optimalheight); imagecopyresampled($this->imageresized, $this->image, 0, 0, 0, 0, $optimalwidth, $optimalheight, $this->width, $this->height); // *** if option 'crop', crop if ($option == 'crop') { $this->crop($optimalwidth, $optimalheight, $newwidth, $newheight); } } ## -------------------------------------------------------- private function getdimensions($newwidth, $newheight, $option) { switch ($option) { case 'exact': $optimalwidth = $newwidth; $optimalheight= $newheight; break; case 'portrait': $optimalwidth = $this->getsizebyfixedheight($newheight); $optimalheight= $newheight; break; case 'landscape': $optimalwidth = $newwidth; $optimalheight= $this->getsizebyfixedwidth($newwidth); break; case 'auto': $optionarray = $this->getsizebyauto($newwidth, $newheight); $optimalwidth = $optionarray['optimalwidth']; $optimalheight = $optionarray['optimalheight']; break; case 'crop': $optionarray = $this->getoptimalcrop($newwidth, $newheight); $optimalwidth = $optionarray['optimalwidth']; $optimalheight = $optionarray['optimalheight']; break; } return array('optimalwidth' => $optimalwidth, 'optimalheight' => $optimalheight); } ## -------------------------------------------------------- private function getsizebyfixedheight($newheight) { $ratio = $this->width / $this->height; $newwidth = $newheight * $ratio; return $newwidth; } private function getsizebyfixedwidth($newwidth) { $ratio = $this->height / $this->width; $newheight = $newwidth * $ratio; return $newheight; } private function getsizebyauto($newwidth, $newheight) { if ($this->height < $this->width) // *** image resized wider (landscape) { $optimalwidth = $newwidth; $optimalheight= $this->getsizebyfixedwidth($newwidth); } elseif ($this->height > $this->width) // *** image resized taller (portrait) { $optimalwidth = $this->getsizebyfixedheight($newheight); $optimalheight= $newheight; } else // *** image resizerd square { if ($newheight < $newwidth) { $optimalwidth = $newwidth; $optimalheight= $this->getsizebyfixedwidth($newwidth); } else if ($newheight > $newwidth) { $optimalwidth = $this->getsizebyfixedheight($newheight); $optimalheight= $newheight; } else { // *** sqaure being resized square $optimalwidth = $newwidth; $optimalheight= $newheight; } } return array('optimalwidth' => $optimalwidth, 'optimalheight' => $optimalheight); } ## -------------------------------------------------------- private function getoptimalcrop($newwidth, $newheight) { $heightratio = $this->height / $newheight; $widthratio = $this->width / $newwidth; if ($heightratio < $widthratio) { $optimalratio = $heightratio; } else { $optimalratio = $widthratio; } $optimalheight = $this->height / $optimalratio; $optimalwidth = $this->width / $optimalratio; return array('optimalwidth' => $optimalwidth, 'optimalheight' => $optimalheight); } ## -------------------------------------------------------- private function crop($optimalwidth, $optimalheight, $newwidth, $newheight) { // *** find center - used crop $cropstartx = ( $optimalwidth / 2) - ( $newwidth /2 ); $cropstarty = ( $optimalheight/ 2) - ( $newheight/2 ); $crop = $this->imageresized; //imagedestroy($this->imageresized); // *** crop center exact requested size $this->imageresized = imagecreatetruecolor($newwidth , $newheight); imagecopyresampled($this->imageresized, $crop , 0, 0, $cropstartx, $cropstarty, $newwidth, $newheight , $newwidth, $newheight); } ## -------------------------------------------------------- public function saveimage($savepath, $imagequality="100") { // *** extension $extension = strrchr($savepath, '.'); $extension = strtolower($extension); switch($extension) { case '.jpg': case '.jpeg': if (imagetypes() & img_jpg) { imagejpeg($this->imageresized, $savepath, $imagequality); } break; case '.gif': if (imagetypes() & img_gif) { imagegif($this->imageresized, $savepath); } break; case '.png': // *** scale quality 0-100 0-9 $scalequality = round(($imagequality/100) * 9); // *** invert quality setting 0 best, not 9 $invertscalequality = 9 - $scalequality; if (imagetypes() & img_png) { imagepng($this->imageresized, $savepath, $invertscalequality); } break; // ... etc default: // *** no extension - no save. break; } imagedestroy($this->imageresized); } ## -------------------------------------------------------- } ?>
create:
include("resize-class.php"); // *** 1) initialize / load image $resizeobj = new resize('sample.jpg'); // *** 2) resize image (options: exact, portrait, landscape, auto, crop) $resizeobj -> resizeimage(150, 100, 'crop'); // *** 3) save image $resizeobj -> saveimage('sample-resized.gif', 100);
just call methods more once?
include("resize-class.php"); $resizeobj = new resize('sample.jpg'); // first image $resizeobj->resizeimage(150, 100, 'crop'); $resizeobj->saveimage('sample-resized-150x100.gif', 100); // second image $resizeobj->resizeimage(200, 250, 'crop'); $resizeobj->saveimage('sample-resized-200x250.gif', 100);
i'm not familiar particular image resizing library, seems it'd work.
Comments
Post a Comment