Jump to content


Photo

Image resize


  • Please log in to reply
10 replies to this topic

#1 jameschambers

jameschambers
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 18 June 2006 - 06:27 PM

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);

?>


#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 18 June 2006 - 06:33 PM

Try using imagecopyresampled() instead of imagecopyresized() and see how it goes.

Oh, and imagecreatetruecolor() instead of imagecreate()

#3 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 18 June 2006 - 06:39 PM

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]

#4 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 18 June 2006 - 06:46 PM

[!--quoteo(post=385374:date=Jun 18 2006, 07:39 PM:name=michaellunsford)--][div class=\'quotetop\']QUOTE(michaellunsford @ Jun 18 2006, 07:39 PM) View Post[/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 served from a Mac though right? I just viewed your link on both my XP Pro machine, and on my Mac and results were identical.

#5 jameschambers

jameschambers
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 18 June 2006 - 07:51 PM

Thanks, still a bit faded but much better. Cheers!

#6 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 18 June 2006 - 10:01 PM

[!--quoteo(post=385377:date=Jun 18 2006, 01:46 PM:name=SemiApocalyptic)--][div class=\'quotetop\']QUOTE(SemiApocalyptic @ Jun 18 2006, 01:46 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Surely that only matters if the code is being served 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?

#7 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 19 June 2006 - 07:14 AM

[!--quoteo(post=385439:date=Jun 18 2006, 11:01 PM:name=michaellunsford)--][div class=\'quotetop\']QUOTE(michaellunsford @ Jun 18 2006, 11:01 PM) View Post[/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 :)

#8 jameschambers

jameschambers
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 19 June 2006 - 12:11 PM

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

#9 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 19 June 2006 - 12:19 PM

Not as straight forward as you may think, you'll have to evaluate the type of file and the run the correct functions...

$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;
}
Thats just a basic example, you'd make it a bit more efficient that that though ;)

#10 jameschambers

jameschambers
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 19 June 2006 - 12:24 PM

Thanks very much, thats done the trick.

#11 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 20 June 2006 - 05:54 PM

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) View Post[/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\" /]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users