bad colors w/ imagecopyresampled
Posted: Sat Apr 03, 2004 12:45 am
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.
//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.