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
Compress image and create thumbnail script
Moderator: General Moderators
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Compress image and create thumbnail script
Look into the GD or ImageMagick extensions. If they are not installed you may be about to exec() them.
(#10850)
Re: Compress image and create thumbnail script
This is a piece of "thumbnail code" I use...
Hope it makes sense to you....
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
Take a look at http://www.phpgd.com - there's a thumbnailer class there.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.