Why do all my auto-generated thumbnails from GD to PHP have a black background?
Well, I am using the following code to take any old image into a 160x120 thumbnail, the problem is that the background overflow is always black. I followed the PHP docs but none of these functions have any color options. Any ideas or pointers would be great!
$original = 'original_image.jpg';
$thumbnail = 'output_thumbnail.jpg';
list($width,$height) = getimagesize($original);
$width_ratio = 160 / $width;
if ($height * $width_ratio <= 120)
{
$adjusted_width = 160;
$adjusted_height = $height * $width_ratio;
}
else
{
$height_ratio = 120 / $height;
$adjusted_width = $width * $height_ratio;
$adjusted_height = 120;
}
$image_p = imagecreatetruecolor(160,120);
$image = imagecreatefromjpeg($original);
imagecopyresampled($image_p,$image,ceil((160 - $adjusted_width) / 2),ceil((120 - $adjusted_height) / 2),0,0,ceil($adjusted_width),ceil($adjusted_height),$width,$height);
imagejpeg($image_p,$thumbnail,100);
Also if you are unclear what I mean, take this image and think that it was originally just red text on a white background
0
a source to share
3 answers
the imagecreatetruecolor function creates a black canvas.
Use the imagefill function to paint it with white.
+2
a source to share
Add this before copying the original to the new one:
$white = ImageColorAllocate($image_p, 255, 255, 255);
ImageFillToBorder($image_p, 0, 0, $white, $white);
EDIT:
Actually, I was not aware of the image ...
$white = imagecolorallocate($image_p, 255, 255, 255);
imagefill($image_p, 0, 0, $white);
+1
a source to share