代码之家  ›  专栏  ›  技术社区  ›  significance

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

  •  2
  • significance  · 技术社区  · 14 年前

    我需要创建一个透明的图像,然后结合透明PNG到它,同时保持图像质量。

    我该怎么做?

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

    outputs a black background.

    谢谢)

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

            $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);
    
    2 回复  |  直到 14 年前
        1
  •  1
  •   Wrikken    14 年前
     $img = imagecreatetruecolor(...);
     imagealphablending($img,false);
     //rest of code.
    
        2
  •  0
  •   significance    14 年前

    最终工作代码:

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