如何创建透明画布,然后向其中添加透明 png?

发布于 2024-09-06 17:53:19 字数 1133 浏览 4 评论 0原文

我需要创建一个透明图像,然后将透明 png 组合到其中,同时保持图像质量。

我该怎么做?

imagecreatetruecolor(...);
//processing using imagecopymerge(..);
imagepng(...);

输出黑色背景。

谢谢:)

这是我的实际代码供参考...

        $d = getimagesize(TMP.$this->files[0]);
    $source_height = $d[0];
    $source_width = $d[1];

    $this->canvas = imagecreatetruecolor($source_width*count($this->files),$source_height);

    imagealphablending($this->canvas, false );

    $i=0;
    foreach($this->files as $f){
        $dst_x = $source_width*$i;
        $im = imagecreatefrompng(TMP.$f);
        imagecopyresampled  (  $this->canvas  , $im  ,
               $dst_x  ,
               $dst_y = 0 , 
               $src_x = 0 ,
               $src_y = 0 ,
               $source_width  ,
               $source_height  ,
               $source_width  ,
               $source_height);

        $i++;
        imagepng($im,TMP.$i.".png");
        if($i>3)break;
    }
    $fn = TMP."stiched_up_$i*$source_width.png";
    imagesavealpha($this->canvas,TRUE);
    imagepng($this->canvas,$fn);

i need to create a transparent image, then combine transparent pngs to it whilst maintaining image quality.

how can i do this?

imagecreatetruecolor(...);
//processing using imagecopymerge(..);
imagepng(...);

outputs a black background.

thanks :)

here's my actual code for reference...

        $d = getimagesize(TMP.$this->files[0]);
    $source_height = $d[0];
    $source_width = $d[1];

    $this->canvas = imagecreatetruecolor($source_width*count($this->files),$source_height);

    imagealphablending($this->canvas, false );

    $i=0;
    foreach($this->files as $f){
        $dst_x = $source_width*$i;
        $im = imagecreatefrompng(TMP.$f);
        imagecopyresampled  (  $this->canvas  , $im  ,
               $dst_x  ,
               $dst_y = 0 , 
               $src_x = 0 ,
               $src_y = 0 ,
               $source_width  ,
               $source_height  ,
               $source_width  ,
               $source_height);

        $i++;
        imagepng($im,TMP.$i.".png");
        if($i>3)break;
    }
    $fn = TMP."stiched_up_$i*$source_width.png";
    imagesavealpha($this->canvas,TRUE);
    imagepng($this->canvas,$fn);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

用心笑 2024-09-13 17:53:19
 $img = imagecreatetruecolor(...);
 imagealphablending($img,false);
 //rest of code.
 $img = imagecreatetruecolor(...);
 imagealphablending($img,false);
 //rest of code.
葬シ愛 2024-09-13 17:53:19

最终工作代码:

        $d = getimagesize(TMP.$this->files[0]);
    $source_height = $d[0];
    $source_width = $d[1];

    $this->canvas = imagecreatetruecolor($source_width*count($this->files),$source_height);
    imagesavealpha($this->canvas,TRUE);
    imagealphablending($this->canvas, false );

    $i=0;
    foreach($this->files as $f){
        $dst_x = $source_width*$i;
        $im = imagecreatefrompng(TMP.$f);
        imagecopy (  $this->canvas  , $im  ,
               $dst_x  ,
               $dst_y = 0 , 
               $src_x = 0 ,
               $src_y = 0 ,
               $source_width  ,
               $source_height);

        $i++;
        imagepng($im,TMP.$i.".png");
        // if($i>3)break;
    }
    $fn = TMP."stiched_up_$i*$source_width.png";
    imagepng($this->canvas,$fn);
    // create canvas correct size i.e. count(images)*width
    // add each picture in with correct offset i.e. picture_i*width,0

    echo basename($fn);

final working code:

        $d = getimagesize(TMP.$this->files[0]);
    $source_height = $d[0];
    $source_width = $d[1];

    $this->canvas = imagecreatetruecolor($source_width*count($this->files),$source_height);
    imagesavealpha($this->canvas,TRUE);
    imagealphablending($this->canvas, false );

    $i=0;
    foreach($this->files as $f){
        $dst_x = $source_width*$i;
        $im = imagecreatefrompng(TMP.$f);
        imagecopy (  $this->canvas  , $im  ,
               $dst_x  ,
               $dst_y = 0 , 
               $src_x = 0 ,
               $src_y = 0 ,
               $source_width  ,
               $source_height);

        $i++;
        imagepng($im,TMP.$i.".png");
        // if($i>3)break;
    }
    $fn = TMP."stiched_up_$i*$source_width.png";
    imagepng($this->canvas,$fn);
    // create canvas correct size i.e. count(images)*width
    // add each picture in with correct offset i.e. picture_i*width,0

    echo basename($fn);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文