/    Sign up×
Community /Pin to ProfileBookmark

JPEG Compression

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

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@clairec666Apr 30.2005 — 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";

}

}
Copy linkTweet thisAlerts:
@neil9999authorMay 01.2005 — Thanks for your help, but I don't want something which resizes the image, I would like it compressed:

[code=php]imagecreatefromjpeg("oldimage.jpg")
//image, output file, compression
imagejpeg ($im,"outputimage.jpg",$compression)[/code]


I would like to calculate [FONT=Lucida Console]$compression[/FONT] using the size of [FONT=Lucida Console]oldimage.jpg[/FONT] so it always returns an image of around 80kb, but I don't know how to calculate this!

Thanks,

Neil
Copy linkTweet thisAlerts:
@SpectreReturnsMay 01.2005 — 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:
<i>
</i>80 / filesize * 100
Copy linkTweet thisAlerts:
@neil9999authorMay 01.2005 — Thanks, I'll try that.

Neil
Copy linkTweet thisAlerts:
@neil9999authorMay 01.2005 — Thanks but that seems to output a filesize of around 38kb from my 1.55mb test file.

Neil
Copy linkTweet thisAlerts:
@SpectreReturnsMay 02.2005 — Try a couple other tests :p
Copy linkTweet thisAlerts:
@SpectreReturnsMay 28.2005 — 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.
×

Success!

Help @neil9999 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...