代码之家  ›  专栏  ›  技术社区  ›  Bartłomiej Semańczyk

如何使用PHP创建png的背景透明颜色?

  •  0
  • Bartłomiej Semańczyk  · 技术社区  · 3 年前

    目前我想创建一个透明的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: 第4行的\DPSadmin\DEV\ajax_optipng1.5\create.php

    2. 虽然有错误消息,但它仍然生成了一个png文件,然而,我发现这个文件是一个黑色的图像,我需要指定任何参数来使它透明吗?

    谢谢

    0 回复  |  直到 13 年前
        1
  •  53
  •   max-m    13 年前

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

    至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');
    
        2
  •  8
  •   neuraminidase7    13 年前

    看看:

    示例函数复制透明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
  •  2
  •   user3051471    12 年前

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

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

    推荐文章