【问题标题】:Merge two images in php在php中合并两个图像
【发布时间】:2012-02-10 02:58:22
【问题描述】:

我想要合并两张图片,然后保存到新位置。
我希望将第二张图片直接放在第一张图片的下方。
我有以下内容,但图像甚至没有保存。

$destimg = imagecreatefromjpeg('images/myimg.jpg');

$src = imagecreatefromgif('images/second.gif');  

// Copy and merge
imagecopymerge($destimg, $src, 316, 100, 0, 0, 316, 100, 100);

两张图片的宽度为 316 像素 X 100 像素
从上面的代码中,$destimg 现在应该是 316x200,但这不会发生。也喜欢它是一个新图像并保存到另一个文件夹。

感谢您的帮助。

【问题讨论】:

  • 你看到你的PHP版本和需要的版本了吗?
  • 感谢您的回复。我在描述中添加了 php 5,但我认为它已从 mod 中删除。

标签: php image image-processing gd


【解决方案1】:

这种情况的最佳方法可能是在内存中创建具有您想要的组合尺寸的新图像,然后将现有图像复制或重新采样到新图像,然后将新图像保存到磁盘。

例如:

function merge($filename_x, $filename_y, $filename_result) {

 // Get dimensions for specified images

 list($width_x, $height_x) = getimagesize($filename_x);
 list($width_y, $height_y) = getimagesize($filename_y);

 // Create new image with desired dimensions

 $image = imagecreatetruecolor($width_x + $width_y, $height_x);

 // Load images and then copy to destination image

 $image_x = imagecreatefromjpeg($filename_x);
 $image_y = imagecreatefromgif($filename_y);

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

 // Save the resulting image to disk (as JPEG)

 imagejpeg($image, $filename_result);

 // Clean up

 imagedestroy($image);
 imagedestroy($image_x);
 imagedestroy($image_y);

}

例子:

merge('images/myimg.jpg', 'images/second.gif', 'images/merged.jpg');

【讨论】:

  • 通过检查 imagecreatetruecolor 和 imagecreatefromjpeg 的结果来包含更好的错误处理是明智的。为简洁起见,我忽略了这一点。
【解决方案2】:

如果您使用的是 PHP GD 库,我想在这里再添加一件事,那么您还应该包括 imagesavealpha()alphablending()

【讨论】:

    【解决方案3】:

    我建议您改用 Image Magick(pecl-imagick 模块或通过 shell 作为命令运行它)。我有几个原因:

    Imagick 是:

    • 更快
    • 了解更多格式
    • 制作质量更高的图片
    • 拥有更多能力(例如文本旋转)
    • 还有更多...

    如果你使用 php 模块,你的方法是 Imagick::compositeImage。手动:http://php.net/manual/en/function.imagick-compositeimage.php

    【讨论】:

      【解决方案4】:

      我找到了答案,使用GD:

       function merge($filename_x, $filename_y, $filename_result) {
      
       // Get dimensions for specified images
      
       list($width_x, $height_x) = getimagesize($filename_x);
       list($width_y, $height_y) = getimagesize($filename_y);
      
       // Create new image with desired dimensions
      
       $image = imagecreatetruecolor($width_x, $height_x);
      
       // Load images and then copy to destination image
      
       $image_x = imagecreatefromjpeg($filename_x);
       $image_y = imagecreatefromgif($filename_y);
      
       imagecopy($image, $image_x, 0, 0, 0, 0, $width_x, $height_x);
                              //  top, left, border,border
       imagecopy($image, $image_y, 100, 3100, 0, 0, $width_y, $height_y);
      
       // Save the resulting image to disk (as JPEG)
      
       imagejpeg($image, $filename_result);
      
       // Clean up
      
       imagedestroy($image);
       imagedestroy($image_x);
       imagedestroy($image_y);
      
      }
      

      像这样:

      merge('images/myimage.jpg', 'images/second.gif', 'images/merged.jpg');
      

      【讨论】:

      • 答案在上面。
      猜你喜欢
      • 2012-10-10
      • 2011-04-22
      • 2016-09-02
      • 2011-05-21
      • 2012-01-22
      • 1970-01-01
      • 2018-01-19
      • 1970-01-01
      • 2021-01-16
      相关资源
      最近更新 更多