【问题标题】:Combine 2-3 transparent PNG images on top of each other with PHP使用 PHP 将 2-3 个透明 PNG 图像相互叠加
【发布时间】:2010-11-26 17:03:51
【问题描述】:

我正在为一个项目开发自定义头像系统,但我从未真正在 PHP 的图像方面做过很多事情。我认为我需要以某种方式使用 GD,但我什至不知道从哪里开始。

基本上,有一堆预制的透明PNG图像。用户可以选择其中的 2-3 个来自定义他们的头像,我希望能够拍摄这些图像并从中制作一个图像以存储在一个文件夹中。

【问题讨论】:

    标签: php image png image-manipulation gd


    【解决方案1】:
    $image_1 = imagecreatefrompng('image_1.png');
    $image_2 = imagecreatefrompng('image_2.png');
    imagealphablending($image_1, true);
    imagesavealpha($image_1, true);
    imagecopy($image_1, $image_2, 0, 0, 0, 0, 100, 100);
    imagepng($image_1, 'image_3.png');
    

    【讨论】:

    • 顺便说一下,这是使用GD。
    • 好吧,我需要最终的图像也是透明的 PNG。
    • 我用两行代码编辑了上面的内容,使背景和最终图像也透明。
    • 令人惊讶的是,在 stackoverflow 上的众多类似主题中,建议 imagealphablendingimagesavealphaimagecopy 之前和之后的各种组合,这是有效的。 :)
    • 这是一个很好的解决方案。它对我来说非常好。
    【解决方案2】:

    这帮助我从其他 3 个 PNG 文件创建了一个 PNG 图像,以创建带背景的水印图像。希望它可以帮助别人。


    $bgFile = __DIR__ . "/background-layer-1.png"; // 93 x 93

    $imageFile = __DIR__ . "/icon-layer-2.png"; // 76 x 76

    $watermarkFile = __DIR__ . "/stars-layer-3.png"; // 133 x 133(是白星)

    <?php
    // Download the image files if we don't have them
    function get_file($file, $from) {
        if (!file_exists(__DIR__ . "/" . $file)) { file_put_contents(__DIR__ . "/" . $file, file_get_contents($from)); }
    }
    get_file("background-layer-1.png", "http://i.imgur.com/6pgf3WK.png");
    get_file("icon-layer-2.png", "http://i.imgur.com/0sJt52z.png");
    get_file("stars-layer-3.png", "http://i.imgur.com/1Tvlokk.png");
    
    $bgFile = __DIR__ . "/background-layer-1.png"; // 93 x 93
    $imageFile = __DIR__ . "/icon-layer-2.png"; // 76 x 76
    $watermarkFile = __DIR__ . "/stars-layer-3.png"; // 133 x 133
    
    // We want our final image to be 76x76 size
    $x = $y = 76;
    
    // dimensions of the final image
    $final_img = imagecreatetruecolor($x, $y);
    
    // Create our image resources from the files
    $image_1 = imagecreatefrompng($bgFile);
    $image_2 = imagecreatefrompng($imageFile);
    $image_3 = imagecreatefrompng($watermarkFile);
    
    // Enable blend mode and save full alpha channel
    imagealphablending($final_img, true);
    imagesavealpha($final_img, true);
    
    // Copy our image onto our $final_img
    imagecopy($final_img, $image_1, 0, 0, 0, 0, $x, $y);
    imagecopy($final_img, $image_2, 0, 0, 0, 0, $x, $y);
    imagecopy($final_img, $image_3, 0, 0, 0, 0, $x, $y);
    
    ob_start();
    imagepng($final_img);
    $watermarkedImg = ob_get_contents(); // Capture the output
    ob_end_clean(); // Clear the output buffer
    
    header('Content-Type: image/png');
    echo $watermarkedImg; // outputs: `http://i.imgur.com/f7UWKA8.png`
    

    输出:

    【讨论】:

      【解决方案3】:

      也可以采用这种方式。希望这对未来的访客有用。

      $base = imagecreatefrompng('your base image path');
      //logo is transparent: in this case stackoverflow logo
      $logo = imagecreatefrompng("path for image with transparent background");
      
      //Adjust paramerters according to your image
      imagecopymerge_alpha($base, $logo, 60, 60, 0, 0, 300, 200, 100);
      
      header('Content-Type: image/png');
      imagepng($base);
      
      //@see: http://php.net/manual/en/function.imagecopymerge.php for below function in first comment
      function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){ 
              // creating a cut resource 
              $cut = imagecreatetruecolor($src_w, $src_h); 
      
              // copying relevant section from background to the cut resource 
              imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h); 
      
              // copying relevant section from watermark to the cut resource 
              imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h); 
      
              // insert cut resource to destination image 
              imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct); 
          } 
      

      工作示例: 这是背景图片
      这是 stackoverflow 的标志。

      这是组合结果。

      【讨论】:

        【解决方案4】:

        绝对使用 GD 库。

        <?php
        
        $final_img = imagecreate($x, $y); // where x and y are the dimensions of the final image
        
        $image_1 = imagecreatefrompng('image_1.png');
        $image_2 = imagecreatefrompng('image_2.png');
        $image_3 = imagecreatefrompng('image_3.png');
        imagecopy($image_1, $final_img, 0, 0, 0, 0, $x, $y);
        imagecopy($image_2, $final_img, 0, 0, 0, 0, $x, $y);
        imagecopy($image_3, $final_img, 0, 0, 0, 0, $x, $y);
        
        imagealphablending($final_img, false);
        imagesavealpha($final_img, true);
        if($output_to_browser){
        
        header('Content-Type: image/png');
        imagepng($final_img);
        
        }else{
        // output to file
        
        imagepng($final_img, 'final_img.png');
        
        }
        
        ?>
        

        【讨论】:

        • 这个对我不起作用。 imagecopy() 变量的顺序是否正确?我认为 $final_img 应该是第一位的,就像下面@Anil 的回答一样。
        【解决方案5】:

        您要使用的是PHP ImageMagick 实用程序。

        具体来说,CombineImages 命令。

        【讨论】:

        • 有没有 combineImages 的例子?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-05-21
        • 2012-03-06
        • 2013-04-11
        • 1970-01-01
        • 2011-06-02
        • 1970-01-01
        • 2018-10-09
        相关资源
        最近更新 更多