php 带有白色背景的 imagescreatetruecolor

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8135271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:02:22  来源:igfitidea点击:

imagescreatetruecolor with a white background

phpimage

提问by Jean-Pierre Bazinet

I'm pretty sure I need to use imagefilledrectangle in order to get a white background instead of black on this... just not sure how. I've tried a few ways.

我很确定我需要使用 imagefilledrectangle 以获得白色背景而不是黑色......只是不确定如何。我尝试了几种方法。

$targetImage = imagecreatetruecolor($thumbw,$thumbh);
imagecopyresized($targetImage,$sourceImage,0,0,0,0,$thumbWidth,$thumbHeight,imagesx($sourceImage),imagesy($sourceImage));

回答by Gustav Bertram

From the PHP manual entry for imagefill:

来自imagefillPHP 手册条目

$image = imagecreatetruecolor(100, 100);

// set background to white
$white = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $white);

回答by KIKO Software

imagefill() uses flood fill, which is quite slow compared to just painting a color in a rectangle without regard for the content of the image. So imagefilledrectangle() will be a lot quicker.

imagefill() 使用泛光填充,与仅在矩形中绘制颜色而不考虑图像内容相比,速度非常慢。所以 imagefilledrectangle() 会快很多。

// get size of target image
$width  = imagesx($targetImage);
$height = imagesy($targetImage);

// get the color white
$white  = imagecolorallocate($targetImage,255,255,255);

// fill entire image (quickly)
imagefilledrectangle($targetImage,0,0,$width-1,$height-1,$white);

Speed is often a consideration when writing code.

在编写代码时,速度通常是一个考虑因素。

回答by Tim S.

$targetImage = imagecreatetruecolor($thumbw,$thumbh);

// get the color white
$color = imagecolorallocate($targetImage, 255, 255, 255);

// fill entire image
imagefill($targetImage, 0, 0, $color);

imagecolorallocate: http://www.php.net/manual/en/function.imagecolorallocate.php

图像颜色分配:http://www.php.net/manual/en/function.imagecolorallocate.php

imagefill: http://php.net/manual/en/function.imagefill.php

图像填充:http://php.net/manual/en/function.imagefill.php