PHP에서 GD를 가진 내 자동 생성 썸네일이 왜 검은 색 배경을 가지고 있습니까?

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

  •  22-08-2019
  •  | 
  •  

문제

글쎄, 나는 다음 코드를 사용하여 오래된 이미지를 160x120 썸네일로 가져 가고 있습니다. 문제는 배경이 항상 검은 색이 오버플로되는 경우 문제입니다. 나는 PHP 문서를 둘러싼 스누핑을 해왔지만 이러한 기능 중 어느 것도 어떤 종류의 색상 매개 변수가없는 것 같습니다. 모든 아이디어 나 포인터가 좋을 것입니다!

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

또한 내가 무슨 뜻인지 불분명하다면 가져 가라 이 사진 그리고 원래 흰색 배경에 빨간색 텍스트라고 생각합니다.

도움이 되었습니까?

해결책

그만큼 imageCreateTruecolor 기능 검은 색 캔버스를 만듭니다.

ImageFill 기능을 사용하여 흰색으로 칠하십시오 ...

다른 팁

원본을 새로 복사하기 전에 다음을 추가하십시오.

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

편집하다:

사실, 나는 ImageFill에 대해 몰랐습니다. . .

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

imagecreatecolor를 대신 사용하지 마십시오.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top