Click to See Complete Forum and Search --> : JPEG Compression
neil9999
04-30-2005, 06:49 AM
Hi,
I would like to be able to upload a picture (ideally in any format) and have it compressed to a JPEG of around 80kb.
Can this be done? I have the GD library.
Thanks,
Neil
clairec666
04-30-2005, 11:51 AM
Hi. Here is a little sample code from my website:
I can't remember exactly how it works. But at quick glance, it seems that it rejects all GIF files, and converts all PNG and JPEG files to a similar size. With a bit of experimentation, you can change the size limits of the resultant image so that it is close to 80kb.
You will also need to change the upload path, and some other stuff.
Hope it helps!
foreach($_FILES as $file_name => $file_array) {
if (is_uploaded_file($file_array['tmp_name'])) {
$name = (string) $file_array[name];
$name_array = explode(".", $name);
$last_array_item = sizeof($name_array) - 1;
$file_ending = $name_array[$last_array_item];
$temp_name = "Temp file!";
switch(strtolower($file_ending)) {
case 'png':
$image_type_for_resize = "PNG";
break;
case 'jpg':
case 'jpeg':
case 'jpe':
$image_type_for_resize = "JPEG";
break;
case 'gif':
$image_type_for_resize = "GIF";
break;
default:
$image_type_for_resize = false;
break;
}
if($image_type_for_resize && $image_type_for_resize != "GIF") {
move_uploaded_file($file_array['tmp_name'], "$file_dir/$upload_file_name") or die ("Couldn't copy");
$img_name = "../photos/$upload_file_name";
$new_name = "../thumbs/$upload_file_name";
$size=GetImageSize($img_name);
$width_ratio = ($size[0] / $max_width);
$height_ratio = ($size[1] / $max_height);
if($width_ratio >=$height_ratio) {
$ratio = $width_ratio;
}
else {
$ratio = $height_ratio;
}
$new_width = ($size[0] / $ratio);
$new_height = ($size[1] / $ratio);
switch($image_type_for_resize) {
case "PNG":
$src_img = ImageCreateFromPNG($img_name);
break;
case "JPEG":
$src_img = ImageCreateFromJPEG($img_name);
break;
}
$thumb = ImageCreateTrueColor($new_width,$new_height);
ImageCopyResampled($thumb, $src_img, 0,0,0,0,($new_width-1),($new_height-1),$size[0],$size[1]);
ImageJPEG($thumb,$new_name);
ImageDestroy($src_img);
ImageDestroy($thumb);
}
elseif($image_type_for_resize=="GIF") {
echo "Please change your image type to JPEG or PNG before uploading";
}
else {
echo "Your file isn't a recognised image type. Please try again";
}
}
else {
print "There was a problem with the file upload";
}
}
neil9999
05-01-2005, 04:03 AM
Thanks for your help, but I don't want something which resizes the image, I would like it compressed:
imagecreatefromjpeg("oldimage.jpg")
//image, output file, compression
imagejpeg ($im,"outputimage.jpg",$compression)
I would like to calculate $compression using the size of oldimage.jpg so it always returns an image of around 80kb, but I don't know how to calculate this!
Thanks,
Neil
SpectreReturns
05-01-2005, 04:17 AM
Actually, that last parameter is 'quality', which is close, but not quite compression. The only way I can think of doing this is to create 10 versions of the file, each with 10 more quality rate, then do a filesize check on it to see which is closest to 80, afterwards deleting the others.
Edit: oh wait, this might calculate it for you:
80 / filesize * 100
neil9999
05-01-2005, 04:32 AM
Thanks, I'll try that.
Neil
neil9999
05-01-2005, 04:44 AM
Thanks but that seems to output a filesize of around 38kb from my 1.55mb test file.
Neil
SpectreReturns
05-02-2005, 12:55 AM
Try a couple other tests :p
SpectreReturns
05-27-2005, 10:58 PM
I've been looking through this, and I suspect that if you use the filesize in KB it will work.
Sorry to bump this by the way.