bad colors w/ imagecopyresampled

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jojo46
Forum Newbie
Posts: 7
Joined: Fri Feb 27, 2004 7:01 pm

bad colors w/ imagecopyresampled

Post by jojo46 »

the following is the code i use to make a thumbnail:

//will resize a jpeg to make it the width of widthS
//it will keep the ratio of height to width
//it will upload this file to the same directory, but
//will add a '_S' to the end of the file name
//will return true if successful, false if not
//file is relative addressing
function thumbnail( $ftp, $rootDir, $file, $widthS )
{
//get file size and proper ratio for new jpg
$tmp = getimagesize( "$file" );
$width = $tmp[0];
$height = $tmp[1];
$ratio = $width/$widthS;
$widthR = $width/$ratio;
$heightR = $height/$ratio;

//create jpeg image holder
if ( !($image = imagecreatefromjpeg( "$file" ) ) )
return false;
//create blank jpeg image holder
if ( !($imageCopy = imagecreate( $widthR, $heightR ) ) )
return false;

for($i=0; $i<256; $i++)
imagecolorallocate($imageCopy, $i, $i, $i);

//copy and resize image
if ( imagecopyresampled( $imageCopy, $image, 0, 0, 0, 0, $widthR, $heightR, $width, $height ) )
;
else
return false;

//create jpeg from image holder
imagejpeg( $imageCopy, "./dummy.txt" );

$fp = fopen( "./dummy.txt", "r" );

$f = $rootDir.$file."_S"; //absolute address
//move from temporary file (dummy.txt) to final resting place
if( !( ftp_fput( $ftp, str_replace( "./", "/", $f), $fp, FTP_BINARY ) ) )
return false;

fclose( $fp );

return true;
}//end of function thumbnail

basically it just resizes and moves it but i get these horrible colors on the new image. it's clear as far as graininess is concerned, but the colors are all washed, and on some of them it's extremely dark and hard to make out the image. i know it has something to do with how the function samles colors, but i don't know how to get around it to create good image quality. any help would be appreciated.
User avatar
Slippy
Forum Contributor
Posts: 113
Joined: Sat Jul 12, 2003 11:31 pm
Location: Vancouver eh!

Post by Slippy »

I think that the problems that you are experiencing are due to the limitations of the GD Library. I have had the same problem in the past and I seem to recall that the GD Lib only supports 256 colours... although this may not be true anymore.

One suggestion would be to set the

imagejpeg( $imageCopy, "./dummy.txt" );

to

imagejpeg( $imageCopy, "./dummy.txt", 100);

Where 100 represents the quality from 1-100 (I think)... it's been a while and I don't have a reference near by... hope that helps...
jojo46
Forum Newbie
Posts: 7
Joined: Fri Feb 27, 2004 7:01 pm

i appreciate the reply...

Post by jojo46 »

that actually helped get rid of the little bit of graininess that was there but the color's the same. i actually replaced
imagecreate( $widthR, $heightR )
with imagecreatetruecolor( $widthR, $heightR )
and that seems to have worked. it's a little too bright now if anything, which is still not a good thing, so still looking for any suggestions to get the color the same as the original jpeg.
User avatar
Slippy
Forum Contributor
Posts: 113
Joined: Sat Jul 12, 2003 11:31 pm
Location: Vancouver eh!

Post by Slippy »

Not sure if I can help there -- the fastest solution would be to turn down the brightness on your monitor ;)
jojo46
Forum Newbie
Posts: 7
Joined: Fri Feb 27, 2004 7:01 pm

lol

Post by jojo46 »

lol, thanks will do :) hehe
Post Reply