Page 1 of 1

Compress image and create thumbnail script

Posted: Sun Mar 09, 2008 8:28 am
by dalton565
Hey guys im making a site for my project, and basically it has a facility to upload images of new products. At present it simple uploads the original image and displays it throughout the site. Which is inefficient as where there alot of images loading u may have a few mb loading. Also when i show a thumbnail of the image its still the original size and loads a smaller version of the original image. What im looking to achieve is

1. When the image is uploaded, create a thumbnail of the image reduced in filesize and dimensions
eg i upload an image of 1mb 1024x768 the following happens
- create a thumbnail 100x100px 50kb in size

Is there a simple way to do this in php the only methods ive found via google just reduce the dimensions of the image and not the filesize also.

Ay help thoroughly appreciated

Thanks
R

Re: Compress image and create thumbnail script

Posted: Sun Mar 09, 2008 1:40 pm
by Christopher
Look into the GD or ImageMagick extensions. If they are not installed you may be about to exec() them.

Re: Compress image and create thumbnail script

Posted: Sun Mar 09, 2008 6:14 pm
by bertfour
This is a piece of "thumbnail code" I use...

Hope it makes sense to you....

Code: Select all

 
 
<?php 
 
function createthumb($name,$filename,$new_w,$new_h){
    $src_img=imagecreatefromjpeg($name);
    $old_x=imageSX($src_img);
    $old_y=imageSY($src_img);
    if ($old_x > $old_y) {
        $thumb_w=$new_w;
        $thumb_h=$old_y*($new_h/$old_x);
    }
    if ($old_x < $old_y) {
        $thumb_w=$old_x*($new_w/$old_y);
        $thumb_h=$new_h;
    }
    if ($old_x == $old_y) {
        $thumb_w=$new_w;
        $thumb_h=$new_h;
    }
        $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
        imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
    imagejpeg($dst_img,$filename); 
    imagedestroy($dst_img); 
    imagedestroy($src_img); 
}
?>
 

Re: Compress image and create thumbnail script

Posted: Mon Mar 10, 2008 10:41 am
by pickle
Take a look at http://www.phpgd.com - there's a thumbnailer class there.