使用 PHP 创建一个透明的 png 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17271742/
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
Create a transparent png file using PHP
提问by user782104
Currently I would like to create a transparent png with the lowest quality .
目前我想创建一个质量最低的透明 png。
The code:
编码:
<?php
function createImg ($src, $dst, $width, $height, $quality) {
$newImage = imagecreatetruecolor($width,$height);
$source = imagecreatefrompng($src); //imagecreatefrompng() returns an image identifier representing the image obtained from the given filename.
imagecopyresampled($newImage,$source,0,0,0,0,$width,$height,$width,$height);
imagepng($newImage,$dst,$quality); //imagepng() creates a PNG file from the given image.
return $dst;
}
createImg ('test.png','test.png','1920','1080','1');
?>
However, there are some problems:
但是,存在一些问题:
Do I need to specific a png file before creating any new file? Or can I create without any existing png file?
Warning: imagecreatefrompng(test.png): failed to open stream: No such file or directory in
C:\DSPadmin\DEV\ajax_optipng1.5\create.php on line 4
Although there are error message , it still generate a png file , however, what I found that is the file is a black color image , do I need to specific any parameter to make it transparent?
在创建任何新文件之前,我是否需要指定一个 png 文件?或者我可以在没有任何现有 png 文件的情况下创建吗?
警告:imagecreatefrompng(test.png):无法打开流:中没有这样的文件或目录
C:\DSPadmin\DEV\ajax_optipng1.5\create.php 第 4 行
虽然有报错信息,但还是生成了一个png文件,但是,我发现文件是黑色图像,是否需要指定任何参数使其透明?
Thanks.
谢谢。
回答by max-m
To 1)
imagecreatefrompng('test.png')tries to open the file test.pngwhich then can be edited with GD functions.
To 1)
imagecreatefrompng('test.png')尝试打开文件test.png,然后可以使用 GD 功能进行编辑。
To 2)
To enable saving of the alpha channel imagesavealpha($img, true);is used.
The following code creates a 200x200px sized transparent image by enabling alpha saving and filling it with transparency.
到 2) 启用 alpha 通道的保存imagesavealpha($img, true);被使用。下面的代码通过启用 alpha 保存并用透明度填充它来创建一个 200x200px 大小的透明图像。
<?php
$img = imagecreatetruecolor(200, 200);
imagesavealpha($img, true);
$color = imagecolorallocatealpha($img, 0, 0, 0, 127);
imagefill($img, 0, 0, $color);
imagepng($img, 'test.png');
回答by neuraminidase7
Take a look at:
看一眼:
An example function copies transparent PNG files:
一个示例函数复制透明的 PNG 文件:
<?php
function copyTransparent($src, $output)
{
$dimensions = getimagesize($src);
$x = $dimensions[0];
$y = $dimensions[1];
$im = imagecreatetruecolor($x,$y);
$src_ = imagecreatefrompng($src);
// Prepare alpha channel for transparent background
$alpha_channel = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagecolortransparent($im, $alpha_channel);
// Fill image
imagefill($im, 0, 0, $alpha_channel);
// Copy from other
imagecopy($im,$src_, 0, 0, 0, 0, $x, $y);
// Save transparency
imagesavealpha($im,true);
// Save PNG
imagepng($im,$output,9);
imagedestroy($im);
}
$png = 'test.png';
copyTransparent($png,"png.png");
?>
回答by user3051471
1) You can create a new png file without any existing one.
2) You get a black color image because you use imagecreatetruecolor();. It creates a highest quality image with a black background. As you need a lowest quality image use imagecreate();
1) 您可以创建一个没有任何现有 png 文件的新 png 文件。2)你得到一个黑色图像,因为你使用imagecreatetruecolor();. 它创建具有黑色背景的最高质量图像。当您需要最低质量的图像时,请使用imagecreate();
<?php
$tt_image = imagecreate( 100, 50 ); /* width, height */
$background = imagecolorallocatealpha( $tt_image, 0, 0, 255, 127 ); /* In RGB colors- (Red, Green, Blue, Transparency ) */
header( "Content-type: image/png" );
imagepng( $tt_image );
imagecolordeallocate( $background );
imagedestroy( $tt_image );
?>
You can read more in this article: How to Create an Image Using PHP
您可以在本文中阅读更多内容:如何使用 PHP 创建图像

