php - Resize Images (supports transparency) -


i need preview of image after upload.according me , better solution store thumbnails on server cache.however function using doesn't seems support transparency.it fill background black

here function using

<?php /**  * resize image class allow resize image  *  * can resize exact size  * max width size while keep aspect ratio  * max height size while keep aspect ratio  * automatic while keep aspect ratio  */ class resizeimage {     private $ext;     private $image;     private $newimage;     private $origwidth;     private $origheight;     private $resizewidth;     private $resizeheight;     /**      * class constructor requires send through image filename      *      * @param string $filename - filename of image want resize      */     public function __construct( $filename )     {         if(file_exists($filename))         {             $this->setimage( $filename );         } else {             throw new exception('image ' . $filename . ' can not found, try image.');         }     }     /**      * set image variable using image create      *      * @param string $filename - image filename      */     private function setimage( $filename )     {         $size = getimagesize($filename);         $this->ext = $size['mime'];         switch($this->ext)         {             // image jpg             case 'image/jpg':             case 'image/jpeg':                 // create jpeg extension                 $this->image = imagecreatefromjpeg($filename);                 break;             // image gif             case 'image/gif':                 $this->image = @imagecreatefromgif($filename);                 break;             // image png             case 'image/png':                 $this->image = @imagecreatefrompng($filename);                 break;             // mime type not found             default:                 throw new exception("file not image, please use file type.", 1);         }         $this->origwidth = imagesx($this->image);         $this->origheight = imagesy($this->image);     }     /**      * save image image type original image      *      * @param  string[type] $savepath     - path store new image      * @param  string $imagequality       - qulaity level of image create      *      * @return saves image      */     public function saveimage($savepath, $imagequality="100", $download = false)     {         switch($this->ext)         {             case 'image/jpg':             case 'image/jpeg':                 // check php supports file type                 if (imagetypes() & img_jpg) {                     imagejpeg($this->newimage, $savepath, $imagequality);                 }                 break;             case 'image/gif':                 // check php supports file type                 if (imagetypes() & img_gif) {                     imagegif($this->newimage, $savepath);                 }                 break;             case 'image/png':                 $invertscalequality = 9 - round(($imagequality/100) * 9);                 // check php supports file type                 if (imagetypes() & img_png) {                     imagepng($this->newimage, $savepath, $invertscalequality);                 }                 break;         }         if($download)         {             header('content-description: file transfer');             header("content-type: application/octet-stream");             header("content-disposition: attachment; filename= ".$savepath."");             readfile($savepath);         }         imagedestroy($this->newimage);     }     /**      * resize image these set dimensions      *      * @param  int $width           - max width of image      * @param  int $height          - max height of image      * @param  string $resizeoption - scale option image      *      * @return save new image      */     public function resizeto( $width, $height, $resizeoption = 'default' )     {         switch(strtolower($resizeoption))         {             case 'exact':                 $this->resizewidth = $width;                 $this->resizeheight = $height;             break;             case 'maxwidth':                 $this->resizewidth  = $width;                 $this->resizeheight = $this->resizeheightbywidth($width);             break;             case 'maxheight':                 $this->resizewidth  = $this->resizewidthbyheight($height);                 $this->resizeheight = $height;             break;             default:                 if($this->origwidth > $width || $this->origheight > $height)                 {                     if ( $this->origwidth > $this->origheight ) {                          $this->resizeheight = $this->resizeheightbywidth($width);                          $this->resizewidth  = $width;                     } else if( $this->origwidth < $this->origheight ) {                         $this->resizewidth  = $this->resizewidthbyheight($height);                         $this->resizeheight = $height;                     }                 } else {                     $this->resizewidth = $width;                     $this->resizeheight = $height;                 }             break;         }         $this->newimage = imagecreatetruecolor($this->resizewidth, $this->resizeheight);         imagecopyresampled($this->newimage, $this->image, 0, 0, 0, 0, $this->resizewidth, $this->resizeheight, $this->origwidth, $this->origheight);     }     /**      * resized height width keeping aspect ratio      *      * @param  int $width - max image width      *      * @return height keeping aspect ratio      */     private function resizeheightbywidth($width)     {         return floor(($this->origheight/$this->origwidth)*$width);     }     /**      * resized width height keeping aspect ratio      *      * @param  int $height - max image height      *      * @return width keeping aspect ratio      */     private function resizewidthbyheight($height)     {         return floor(($this->origwidth/$this->origheight)*$height);     } } ?> 

it awesome if modify make handle images transparent background. in case have better function please share

kind regards

your class handles jpeg , png files need check type , make changes if png:

relace this

$this->newimage = imagecreatetruecolor($this->resizewidth, $this->resizeheight); 

with this

$this->newimage = imagecreatetruecolor($this->resizewidth, $this->resizeheight); imagesavealpha($this->newimage, true);  $transparent_color = imagecolorallocatealpha($this->newimage, 0, 0, 0, 127); imagefill($this->newimage, 0, 0, $transparent_color); 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -