¿Por qué todos mis miniaturas generadas automáticamente con GD en PHP tienen fondos negros?

StackOverflow https://stackoverflow.com/questions/879669

  •  22-08-2019
  •  | 
  •  

Pregunta

Bueno, yo estoy usando el siguiente código de tomar cualquier imagen antigua en una miniatura de 160x120, el problema es si hay desbordamiento el fondo es siempre de color negro. He estado husmeando en la documentación de PHP, pero ninguna de estas funciones parecen tener ningún tipo de parámetros de color. Cualquier idea o punteros sería grande!

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

Además, si usted está claro lo que quiero decir, tomar esta imagen y considerar que era originalmente sólo de texto de color rojo sobre un fondo blanco

¿Fue útil?

Solución

El función imagecreatetruecolor crea un lienzo negro.

Utilice la función imagefill pintarlo blanco ...

Otros consejos

Añadir este antes de copiar el original en el nuevo:

$white = ImageColorAllocate($image_p, 255, 255, 255); 
ImageFillToBorder($image_p, 0, 0, $white, $white);

EDIT:

En realidad, no sabía nada de imagefill. . .

$white = imagecolorallocate($image_p, 255, 255, 255); 
imagefill($image_p, 0, 0, $white);

no uso imagecreatetruecolor lugar imagecreate, creo que eso resolvería

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top