Michdd Posted May 11, 2009 Share Posted May 11, 2009 What is the best way to create thumbnails of an image and possibly change the image type to a smaller format. For example: bitmap > gif or jpg, whichever would be best. I'd appreciate if you could point me in the direct of some specific functions. Link to comment https://forums.phpfreaks.com/topic/157734-thumbnails-converting-image-types/ Share on other sites More sharing options...
The Little Guy Posted May 11, 2009 Share Posted May 11, 2009 I would have to say the best way would be to use imagemagick. Link to comment https://forums.phpfreaks.com/topic/157734-thumbnails-converting-image-types/#findComment-831967 Share on other sites More sharing options...
dreamwest Posted May 11, 2009 Share Posted May 11, 2009 class SimpleImage { var $image; var $image_type; function load($filename) { $image_info = getimagesize($filename); $this->image_type = $image_info[2]; if( $this->image_type == IMAGETYPE_JPEG ) { $this->image = imagecreatefromjpeg($filename); } elseif( $this->image_type == IMAGETYPE_GIF ) { $this->image = imagecreatefromgif($filename); } elseif( $this->image_type == IMAGETYPE_PNG ) { $this->image = imagecreatefrompng($filename); } } function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) { if( $image_type == IMAGETYPE_JPEG ) { imagejpeg($this->image,$filename,$compression); } elseif( $image_type == IMAGETYPE_GIF ) { imagegif($this->image,$filename); } elseif( $image_type == IMAGETYPE_PNG ) { imagepng($this->image,$filename); } if( $permissions != null) { chmod($filename,$permissions); } } function output($image_type=IMAGETYPE_JPEG) { if( $image_type == IMAGETYPE_JPEG ) { imagejpeg($this->image); } elseif( $image_type == IMAGETYPE_GIF ) { imagegif($this->image); } elseif( $image_type == IMAGETYPE_PNG ) { imagepng($this->image); } } function getWidth() { return imagesx($this->image); } function getHeight() { return imagesy($this->image); } function resizeToHeight($height) { $ratio = $height / $this->getHeight(); $width = $this->getWidth() * $ratio; $this->resize($width,$height); } function resizeToWidth($width) { $ratio = $width / $this->getWidth(); $height = $this->getheight() * $ratio; $this->resize($width,$height); } function scale($scale) { $width = $this->getWidth() * $scale/100; $height = $this->getheight() * $scale/100; $this->resize($width,$height); } function resize($width,$height) { $new_image = imagecreatetruecolor($width, $height); imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); $this->image = $new_image; } } //start code settings $thumbnail_width = 210; $thumbnail_height = 260; $thumbnail_jpeg_quality = 95; $our_file = //image location // resize $image = new SimpleImage(); $image->load("tmp-" . $our_file); // get the size, figure out the proper thumbnail size and create the thumbnail list($original_width, $original_height) = getimagesize("tmp-" . $our_file); if ($original_width > $original_height || $original_width == $original_height) { $image->resizeToWidth($thumbnail_width); } else { $image->resizeToHeight($thumbnail_height); } $image->save("thm-" . $our_file); Link to comment https://forums.phpfreaks.com/topic/157734-thumbnails-converting-image-types/#findComment-831970 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.