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
imagescreatetruecolor with a white background
提问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:
$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