【问题标题】:Create a transparent png file using PHP使用 PHP 创建一个透明的 png 文件
【发布时间】:2013-06-20 17:44:52
【问题描述】:

目前我想创建一个质量最低的透明png。

代码:

<?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');
?>

但是,也有一些问题:

  1. 在创建任何新文件之前我需要指定一个 png 文件吗?或者我可以在没有任何现有 png 文件的情况下创建吗?

    警告:imagecreatefrompng(test.png):打开流失败:

    中没有这样的文件或目录

    C:\DSPadmin\DEV\ajax_optipng1.5\create.php 在第 4 行

  2. 虽然有错误信息,但还是生成了一个png文件,但是,我发现文件是黑色图像,是否需要指定任何参数使其透明?

谢谢。

【问题讨论】:

    标签: php image png imagecreatefrompng


    【解决方案1】:

    到 1) imagecreatefrompng('test.png') 尝试打开文件 test.png,然后可以使用 GD 函数对其进行编辑。

    到 2) 启用保存 alpha 通道 imagesavealpha($img, true); 使用。 以下代码通过启用 Alpha 保存并用透明度填充它来创建一个 200x200 像素大小的透明图像。

    <?php
    $img = imagecreatetruecolor(200, 200);
    imagesavealpha($img, true);
    $color = imagecolorallocatealpha($img, 0, 0, 0, 127);
    imagefill($img, 0, 0, $color);
    imagepng($img, 'test.png');
    

    【讨论】:

    • 感谢您的帮助!你介意教我如何最小化 png 文件的大小吗?在 imagepng 功能中设置“9”质量级别是我唯一能做的吗?谢谢
    • imagepngs 默认的“质量”设置(应该命名为压缩,因为pngs 压缩是无损的)是 9(afaik,我测试没有设置质量(234 Bytes) ,质量 0(几百个KB)和设置为 9(234 字节))。所以我想这是GD能做的最好的。
    • 这让我的黑线消失了
    【解决方案2】:

    看看:

    一个示例函数复制透明的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");
        ?>
    

    【讨论】:

      【解决方案3】:

      1) 您可以在没有任何现有文件的情况下创建一个新的 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 );
      ?>
      

      您可以在这篇文章中阅读更多内容:How to Create an Image Using PHP

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-31
        • 1970-01-01
        • 2012-11-02
        • 1970-01-01
        • 2023-04-01
        • 1970-01-01
        • 2011-11-29
        • 2012-07-22
        相关资源
        最近更新 更多