【问题标题】:Merging two images with PHP用 PHP 合并两个图像
【发布时间】:2011-04-22 00:52:13
【问题描述】:

我正在尝试用 PHP 将两个图像合并在一起。

例如...我将如何使用基本的 PHP 将图像 1 放在图像 2 的顶部或合并? 我尝试了诸如水印之类的方法,但它似乎不起作用。

图片一

图片二

...让它变成这个? 最终结果:

【问题讨论】:

  • 只是显示或yu试图生成图像
  • 如果水印有效,但没有给出你想要的结果,我的歪脑正在考虑组合 3 张图像。第一个图像是一个空白的白色图像,你合并左边的第一个图像和右边的第二个图像。我知道编写代码不像发布 cmets 那样容易。只要评论一下我的想法
  • 你确定你需要php吗?你可以简单的css它
  • 如果我允许用​​户下载图像,我需要 PHP。
  • @Homework 您在下面得到了答案。但仅供参考:您可以通过 JavaScript 和 <canvas> 元素组合图像(也可以做很多其他事情)。渲染的图像也可以像你说的那样被用户下载。

标签: php image merge resize overlay


【解决方案1】:

使用 GD 库或 ImageMagick。我搜索了“PHP GD 合并图像”并获得了几篇关于此操作的文章。过去我所做的是创建一个大的空白图像,然后使用 imagecopymerge() 将这些图像粘贴到我原来的空白图像中。查看 google 上的文章,您会发现一些可以立即开始使用的源代码。

【讨论】:

    【解决方案2】:

    您可以使用ImageMagick 扩展来做到这一点。我猜 combineImages() 方法会做你想做的事。

    【讨论】:

      【解决方案3】:

      PHP 中的 GD 图像处理库可能是在 PHP 中处理图像的最佳选择。尝试其中一种 imagecopy 功能(imagecopy、imagecopymerge,...)。他们每个人都以不同的方式组合了 2 张图像。请参阅php documentation on imagecopy 了解更多信息。

      【讨论】:

        【解决方案4】:

        我用我做的一个来让它工作。

        <?php
        $dest = imagecreatefrompng('vinyl.png');
        $src = imagecreatefromjpeg('cover2.jpg');
        
        imagealphablending($dest, false);
        imagesavealpha($dest, true);
        
        imagecopymerge($dest, $src, 10, 9, 0, 0, 181, 180, 100); //have to play with these numbers for it to work for you, etc.
        
        header('Content-Type: image/png');
        imagepng($dest);
        
        imagedestroy($dest);
        imagedestroy($src);
        ?>
        

        【讨论】:

        • 适用于具有透明度的图像。
        【解决方案5】:

        问题是关于合并两个图像,但是在这种特定情况下,您不应该这样做。您应该将Content Image(即封面)放入&lt;img /&gt; 标签,将Style Image放入CSS,为什么?

        1. 正如我所说,封面属于文档的内容,而黑胶唱片和阴影只是页面样式的一部分。
        2. 这种分离使用起来更方便。用户可以轻松复制该图像。通过网络蜘蛛索引更容易。
        3. 最后,它更容易维护。

        所以使用一个非常简单的代码:

        <div class="cover">
           <img src="/content/images/covers/movin-mountains.png" alt="Moving mountains by Pneuma" width="100" height="100" />
        </div>
        
        .cover {
            padding: 10px;
            padding-right: 100px;
        
            background: url(/style/images/cover-background.png) no-repeat;
        }
        

        【讨论】:

        • 感谢您,但我要求使用 PHP。还是要保存这个。
        • 是的。服务器更容易,因为它不必处理所有图像。而且它对用户来说也更快,因为它没有始终使用乙烯基的图像部分。
        • 所选答案很有趣,因为我们可以混合两张图片并在 facebook 上分享。
        • CSS 仅在图像用于网站时才有效。无法使用 CSS 创建要在社交媒体上共享、用户下载等的图像。
        • 可以帮我合并后保存图像。这可以通过编写html来实现吗?
        【解决方案6】:

        ImageArtist 是我编写的一个纯 gd 包装器,这使您能够非常容易地进行复杂的图像处理,因为您可以使用这个强大的库只用很少的步骤来解决您的问题。

        这是一个示例代码。

        $img1 = new Image("./cover.jpg");
        $img2 = new Image("./box.png");
        $img2->merge($img1,9,9);
        $img2->save("./merged.png",IMAGETYPE_PNG);
        

        这就是我的结果。

        【讨论】:

        • 太棒了!谢谢。
        • 太棒了!你节省了我的时间!
        • 我的完美+10000:D
        【解决方案7】:

        你可以试试我的功能,在不改变图像比例的情况下水平或垂直合并图像。只需复制粘贴即可。

        function merge($filename_x, $filename_y, $filename_result, $mergeType = 0) {
        
            //$mergeType 0 for horizandal merge 1 for vertical merge
        
         // Get dimensions for specified images
         list($width_x, $height_x) = getimagesize($filename_x);
         list($width_y, $height_y) = getimagesize($filename_y);
        
        
        $lowerFileName = strtolower($filename_x); 
        if(substr_count($lowerFileName, '.jpg')>0 || substr_count($lowerFileName, '.jpeg')>0){
            $image_x = imagecreatefromjpeg($filename_x);    
        }else if(substr_count($lowerFileName, '.png')>0){
            $image_x = imagecreatefrompng($filename_x); 
        }else if(substr_count($lowerFileName, '.gif')>0){
            $image_x = imagecreatefromgif($filename_x); 
        }
        
        
        $lowerFileName = strtolower($filename_y); 
        if(substr_count($lowerFileName, '.jpg')>0 || substr_count($lowerFileName, '.jpeg')>0){
            $image_y = imagecreatefromjpeg($filename_y);    
        }else if(substr_count($lowerFileName, '.png')>0){
            $image_y = imagecreatefrompng($filename_y); 
        }else if(substr_count($lowerFileName, '.gif')>0){
            $image_y = imagecreatefromgif($filename_y); 
        }
        
        
        if($mergeType==0){
            //for horizandal merge
             if($height_y<$height_x){
                $new_height = $height_y;
        
                $new_x_height = $new_height;
                $precentageReduced = ($height_x - $new_height)/($height_x/100);
                $new_x_width = ceil($width_x - (($width_x/100) * $precentageReduced));
        
                 $tmp = imagecreatetruecolor($new_x_width, $new_x_height);
                imagecopyresampled($tmp, $image_x, 0, 0, 0, 0, $new_x_width, $new_x_height, $width_x, $height_x);
                $image_x = $tmp;
        
                $height_x = $new_x_height;
                $width_x = $new_x_width;
        
             }else{
                $new_height = $height_x;
        
                $new_y_height = $new_height;
                $precentageReduced = ($height_y - $new_height)/($height_y/100);
                $new_y_width = ceil($width_y - (($width_y/100) * $precentageReduced));
        
                 $tmp = imagecreatetruecolor($new_y_width, $new_y_height);
                imagecopyresampled($tmp, $image_y, 0, 0, 0, 0, $new_y_width, $new_y_height, $width_y, $height_y);
                $image_y = $tmp;
        
                $height_y = $new_y_height;
                $width_y = $new_y_width;
        
             }
        
             $new_width = $width_x + $width_y;
        
             $image = imagecreatetruecolor($new_width, $new_height);
        
            imagecopy($image, $image_x, 0, 0, 0, 0, $width_x, $height_x);
            imagecopy($image, $image_y, $width_x, 0, 0, 0, $width_y, $height_y);
        
        }else{
        
        
            //for verical merge
            if($width_y<$width_x){
                $new_width = $width_y;
        
                $new_x_width = $new_width;
                $precentageReduced = ($width_x - $new_width)/($width_x/100);
                $new_x_height = ceil($height_x - (($height_x/100) * $precentageReduced));
        
                $tmp = imagecreatetruecolor($new_x_width, $new_x_height);
                imagecopyresampled($tmp, $image_x, 0, 0, 0, 0, $new_x_width, $new_x_height, $width_x, $height_x);
                $image_x = $tmp;
        
                $width_x = $new_x_width;
                $height_x = $new_x_height;
        
             }else{
                $new_width = $width_x;
        
                $new_y_width = $new_width;
                $precentageReduced = ($width_y - $new_width)/($width_y/100);
                $new_y_height = ceil($height_y - (($height_y/100) * $precentageReduced));
        
                 $tmp = imagecreatetruecolor($new_y_width, $new_y_height);
                imagecopyresampled($tmp, $image_y, 0, 0, 0, 0, $new_y_width, $new_y_height, $width_y, $height_y);
                $image_y = $tmp;
        
                $width_y = $new_y_width;
                $height_y = $new_y_height;
        
             }
        
             $new_height = $height_x + $height_y;
        
             $image = imagecreatetruecolor($new_width, $new_height);
        
            imagecopy($image, $image_x, 0, 0, 0, 0, $width_x, $height_x);
            imagecopy($image, $image_y, 0, $height_x, 0, 0, $width_y, $height_y);
        
        }
        
        
        
        
        
        $lowerFileName = strtolower($filename_result); 
        if(substr_count($lowerFileName, '.jpg')>0 || substr_count($lowerFileName, '.jpeg')>0){
            imagejpeg($image, $filename_result);
        }else if(substr_count($lowerFileName, '.png')>0){
            imagepng($image, $filename_result);
        }else if(substr_count($lowerFileName, '.gif')>0){
            imagegif($image, $filename_result); 
        }
        
        
         // Clean up
         imagedestroy($image);
         imagedestroy($image_x);
         imagedestroy($image_y);
        
        }
        
        
        merge('images/h_large.jpg', 'images/v_large.jpg', 'images/merged_har.jpg',0); //merge horizontally
        merge('images/h_large.jpg', 'images/v_large.jpg', 'images/merged.jpg',1); //merge vertically
        

        【讨论】:

          【解决方案8】:

          合并两张图片 png 和 jpg/png [Image Masking]

          //URL or Local path
          $src_url = '1.png';
          $dest_url = '2.jpg';
          $src = imagecreatefrompng($src_url);
          $dest1 = imagecreatefromjpeg($dest_url);
          
          //if you want to make same size
          list($width, $height) = getimagesize($dest_url);
          list($newWidth, $newHeight) = getimagesize($src_url);
          $dest = imagecreatetruecolor($newWidth, $newHeight);
          
          imagecopyresampled($dest, $dest1, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
          
          list($src_w, $src_h) = getimagesize($src_url);
          
          //merger with same size
          $this->imagecopymerge_alpha($dest, $src, 0, 0, 0, 0, $src_w, $src_h, 100);
          
          //show output on browser
          header('Content-Type: image/png');
          imagejpeg($dest);
          

          imagecopymerge_alpha

          function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct)
              {
                  $cut = imagecreatetruecolor($src_w, $src_h);
                  imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);
                  imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
                  imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct);
              }
          

          【讨论】:

            猜你喜欢
            • 2011-05-21
            • 1970-01-01
            • 2012-10-10
            • 2012-02-10
            • 1970-01-01
            • 2012-03-11
            • 1970-01-01
            • 2011-01-20
            相关资源
            最近更新 更多