Jump to content

Archived

This topic is now archived and is closed to further replies.

jameschambers

Image resize

Recommended Posts

I am using a jpeg image resizing script which is destroying the colour of the images I am putting into it. The best way i can describe them is washed out, althogh sometimes they come out totally monochrome. Is this a bug? what can I do to stop it?

Any help gratefully received

<?php

$image = $_REQUEST['image'];
$max_width = $_REQUEST['max_width'];
$max_height = $_REQUEST['max_height'];

if (!$max_width)
$max_width = 80;
if (!$max_height)
$max_height = 60;

$size = GetImageSize($image);
$width = $size[0];
$height = $size[1];

$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;

if ( ($width <= $max_width) && ($height <= $max_height) ) {
$tn_width = $width;
$tn_height = $height;
}
else if (($x_ratio * $height) < $max_height) {
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
}
else {
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}

$src = ImageCreateFromJpeg($image);
$dst = ImageCreate($tn_width,$tn_height);
ImageCopyResized($dst, $src, 0, 0, 0, 0,
$tn_width,$tn_height,$width,$height);
header('Content-type: image');
ImageJpeg($dst, null, -1);
ImageDestroy($src);
ImageDestroy($dst);

?>

Share this post


Link to post
Share on other sites
Try using imagecopyresampled() instead of imagecopyresized() and see how it goes.

Oh, and imagecreatetruecolor() instead of imagecreate()

Share this post


Link to post
Share on other sites
Instead of imagecreate, you might try using imagecreatetruecolor.

Now, I run macintosh and am noticing that images are significantly desaturated once they've been run through PHP. So much so that you don't even have to see them side by side to see how dramatic the difference.

However, the same side by side comparison on a standard Windows PC reveals no difference at all. The original, beautifully saturated image is rendered on a Windows PC the same way php is rendering the other image.

Here is my side by side comparison. Side by side on the mac -- night and day. Side by side on my test PC, they are identical.
[a href=\"http://www.virtual-showcase.net/proofs/gulfsouth/test.html\" target=\"_blank\"]http://www.virtual-showcase.net/proofs/gulfsouth/test.html[/a]

Share this post


Link to post
Share on other sites
[!--quoteo(post=385374:date=Jun 18 2006, 07:39 PM:name=michaellunsford)--][div class=\'quotetop\']QUOTE(michaellunsford @ Jun 18 2006, 07:39 PM) [snapback]385374[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Instead of imagecreate, you might try using imagecreatetruecolor.

Now, I run macintosh and am noticing that images are significantly desaturated once they've been run through PHP. So much so that you don't even have to see them side by side to see how dramatic the difference.

However, the same side by side comparison on a standard Windows PC reveals no difference at all. The original, beautifully saturated image is rendered on a Windows PC the same way php is rendering the other image.

Here is my side by side comparison. Side by side on the mac -- night and day. Side by side on my test PC, they are identical.
[a href=\"http://www.virtual-showcase.net/proofs/gulfsouth/test.html\" target=\"_blank\"]http://www.virtual-showcase.net/proofs/gulfsouth/test.html[/a]
[/quote]
Surely that only matters if the code is being [i]served[/i] from a Mac though right? I just viewed your link on both my XP Pro machine, and on my Mac and results were identical.

Share this post


Link to post
Share on other sites
[!--quoteo(post=385377:date=Jun 18 2006, 01:46 PM:name=SemiApocalyptic)--][div class=\'quotetop\']QUOTE(SemiApocalyptic @ Jun 18 2006, 01:46 PM) [snapback]385377[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Surely that only matters if the code is being [i]served[/i] from a Mac though right? I just viewed your link on both my XP Pro machine, and on my Mac and results were identical.
[/quote]

Really? This opens a whole new can of worms. Few questions for you.

Which version of MacOS?
Does your display adapter support core image?
32 bit color depth?

Share this post


Link to post
Share on other sites
[!--quoteo(post=385439:date=Jun 18 2006, 11:01 PM:name=michaellunsford)--][div class=\'quotetop\']QUOTE(michaellunsford @ Jun 18 2006, 11:01 PM) [snapback]385439[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Really? This opens a whole new can of worms. Few questions for you.

Which version of MacOS?
Does your display adapter support core image?
32 bit color depth?
[/quote]
OS X (10.4.6)
No core image support (ATI Radeon 9200) - Running a non-Apple display, Iiyama 17" TFT.
32 bit colour depth.

Its not really high specced, its a PPC based MacMini (1.42GHz/1GB), my first Mac :)

Share this post


Link to post
Share on other sites
Hi, got a follow up question about the script, I would like it to be able to resize gifs and pngs as well as jpegs, i'm sure there is a very straight forward way to do this but being pretty new to php i can't seem to get it to work.

Cheers

Share this post


Link to post
Share on other sites
Not as straight forward as you may think, you'll have to evaluate the type of file and the run the correct functions...

[code]$pos = strrpos($image,".");
$ext = substr($image,$pos);
switch($ext) {
    case ".jpg":
         $src = ImageCreateFromJpeg($image);
         $dst = ImageCreate($tn_width,$tn_height);
         ImageCopyResized($dst, $src, 0, 0, 0, 0,
         $tn_width,$tn_height,$width,$height);
         header('Content-type: image');
         ImageJpeg($dst, null, -1);
         ImageDestroy($src);
         ImageDestroy($dst);
         break;
    case ".gif":
         $src = ImageCreateFromGif($image);
         $dst = ImageCreate($tn_width,$tn_height);
         ImageCopyResized($dst, $src, 0, 0, 0, 0,
         $tn_width,$tn_height,$width,$height);
         header('Content-type: image');
         ImageGif($dst, null, -1);
         ImageDestroy($src);
         ImageDestroy($dst);
         break;
}[/code]Thats just a basic example, you'd make it a bit more efficient that that though ;)

Share this post


Link to post
Share on other sites
You can also use imagecreatefromstring(file_get_contents($file)). That's the way I usually do it just incase someone uploads an odd image type I'm not expecting.

[!--quoteo(post=385545:date=Jun 19 2006, 02:14 AM:name=SemiApocalyptic)--][div class=\'quotetop\']QUOTE(SemiApocalyptic @ Jun 19 2006, 02:14 AM) [snapback]385545[/snapback][/div][div class=\'quotemain\'][!--quotec--]
OS X (10.4.6)
No core image support (ATI Radeon 9200) - Running a non-Apple display, Iiyama 17" TFT.
32 bit colour depth.

Its not really high specced, its a PPC based MacMini (1.42GHz/1GB), my first Mac :)
[/quote]

Ahh, a graphics guru friend of mine told me that safari will use embedded color profiles, while other browsers may not. I checked it in Firefox and, sure enough, both render the exact same.

Now, how to save the file so that it will be presented the same with and without the embedded color profile... [img src=\"style_emoticons/[#EMO_DIR#]/huh.gif\" style=\"vertical-align:middle\" emoid=\":huh:\" border=\"0\" alt=\"huh.gif\" /]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.