Page 1 of 1

bad colors w/ imagecopyresampled

Posted: Sat Apr 03, 2004 12:45 am
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.

Posted: Sat Apr 03, 2004 12:57 am
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...

i appreciate the reply...

Posted: Sat Apr 03, 2004 1:05 am
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.

Posted: Sat Apr 03, 2004 1:16 am
by Slippy
Not sure if I can help there -- the fastest solution would be to turn down the brightness on your monitor ;)

lol

Posted: Sat Apr 03, 2004 12:35 pm
by jojo46
lol, thanks will do :) hehe