By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,053 Members | 954 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,053 IT Pros & Developers. It's quick & easy.

[Request] I am looking for a high-quality JPG resize/optimize function

P: n/a
Hi, I am looking for a complete PHP function that will resize &
optimize any JPG image with very good end results. My server is
running PHP 4.3.8.

So, for instance a 1600x1200 JPG at ../somewhere/picture.jpg

could be read by the function and save where I specify like
.../somewhere/pictureResized.jpg

.....what is most important for me besides a good end result is the
function should calculate which side of the image is larger, and based
on a maximum value I preset (like 800), adjust the image proportionally
to 800x600 (for example).

Thanks so much for anyone's assistance with this!

BEST REGARDS -

ALEX

Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
In comp.lang.php Ra*********@hotmail.com wrote:
So, for instance a 1600x1200 JPG at ../somewhere/picture.jpg

could be read by the function and save where I specify like
../somewhere/pictureResized.jpg
imagejpeg($newimage,$pathtoresizedpicture)
....what is most important for me besides a good end result is the
function should calculate which side of the image is larger, and based
on a maximum value I preset (like 800), adjust the image proportionally
to 800x600 (for example).


Almost trivial, see the function resize() in
http://tmp.tryba.nl/resize/img.phps

used to serve reduced images from raw jpegs created by a camera
(http://jamie.tryba.nl/slideshow.php)

Jul 17 '05 #2

P: n/a
Could you pls help me with actual implementation of this, so if I have
a 1600x1200 picture, how to tell it where it is, and where to save a
resized version on the server:

<?php

function resize($file,$nx,$ny)
{
$image=imagecreatefromjpeg($file);

$x=imagesx($image);
$y=imagesy($image);
$r=$x/$y;

$mr=$nx/$ny;

if($r>$mr)
{
$f=$x/$nx;
}
else
{
$f=$y/$ny;
}

$nx=round($x/$f);
$ny=round($y/$f);

$newimage=imagecreatetruecolor($nx,$ny);
imagecopyresampled($newimage,$image,0,0,0,0,$nx,$n y,$x,$y);

return $newimage;
}

?>

Jul 17 '05 #3

P: n/a
http://pear.php.net/package/Image_Transform

Regards,

Peter

<Ra*********@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi, I am looking for a complete PHP function that will resize &
optimize any JPG image with very good end results. My server is
running PHP 4.3.8.

So, for instance a 1600x1200 JPG at ../somewhere/picture.jpg

could be read by the function and save where I specify like
../somewhere/pictureResized.jpg

....what is most important for me besides a good end result is the
function should calculate which side of the image is larger, and based
on a maximum value I preset (like 800), adjust the image proportionally
to 800x600 (for example).

Thanks so much for anyone's assistance with this!

BEST REGARDS -

ALEX

Jul 17 '05 #4

P: n/a
ALEX,

There are many different ways of scaling an image.

depending on the amount of detail and amount of colors in the original, you
should use different methods. It is sometimes better not to scale an image
at once, but in several steps instead, where each step isn't bigger than
49% scaleing, to preserve quality.

one simple php-script should be like this:
<?php
// The file
$filename = 'test.jpg';
$outputfilename = "/somewhere/picture.jpg";

// Set a maximum height and width
$width = 200;
$height = 200;

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

if ($width && ($width_orig < $height_orig)) {
$width = ($height / $height_orig) * $width_orig;
} else {
$height = ($width / $width_orig) * $height_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height,
$width_orig, $height_orig);

// Output
imagejpeg($image_p, $outputfilename, 100);
?>

play with it..

more you can find on:
http://nl.php.net/manual/en/function...yresampled.php

greetings Caspar.


Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.