php 在php中合并两个图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8858489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:38:55  来源:igfitidea点击:

Merge two images in php

phpimageimage-processinggd

提问by john tully

I have two images I would like to merge then save to a new location.
I would like the second image to be place directly below the first image.
I have the following so for but the image doesn't even save.

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

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

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

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

Both images have a width or 316px X 100px
From the above code the $destimg should now be 316x200 but that doesn't happen. Also like it to be a new image and save to another folder.

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

Thanks for any help.

谢谢你的帮助。

回答by Chris Hutchinson

The best approach for this situation may be to create a new image in memory with the combined dimensions you desire, then copy or resample the existing images to the new image, and then save the new image to disk.

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

For example:

例如:

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

}

Example:

例子:

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

回答by Avinash singh

i would like to add one more thing here if you are using PHP GD Library,then you should include imagesavealpha()and alphablending()also.

我想在这里补充一点,如果你使用的是PHP GD库,那么你应该包括imagesavealpha()alphablending()也。

回答by GT.

I suggest you to use Image Magick instead (pecl-imagick module or running it as command via shell). I have several reasons:

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

Imagick is:

意象是:

  • faster
  • knows more format
  • makes better quality images
  • have more ability (for example text rotation)
  • and more...
  • 快点
  • 了解更多格式
  • 制作更优质的图像
  • 有更多的能力(例如文本旋转)
  • 和更多...

Your method is Imagick::compositeImage if you use the php module. Manual: http://php.net/manual/en/function.imagick-compositeimage.php

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

回答by sepehr2121

I found the answer, use GD:

我找到了答案,使用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);

}

like this:

像这样:

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