使用PHP合并两张图片

83

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

例如...我应该如何使用基本的PHP将图像一放置在图像二的上方或合并两张图像呢? 我已经尝试过类似添加水印的方法,但似乎没有效果。

图像一

alt text

图像二

alt text

...最终效果如下:FINAL RESULT:

alt text


1
只需显示已有的文本内容,不需要生成图像。 - zod
1
如果水印正在工作,但结果不如您所期望的那样,我的扭曲思维正在考虑三个图像的组合。第一张图像是一个空白的白色图像,您将第一张图像与左侧的第二张图像合并。我知道编写代码不会像发布评论那样容易。只需在评论中提及我的想法即可。 - zod
1
你确定需要使用 PHP 吗?你可以轻松使用 CSS。 - warfish
如果我允许用户下载图像,我需要使用PHP。 - homework
@作业 你在下面得到了答案。但是只是为了提供信息:你可以通过JavaScript和<canvas>元素组合图像(还可以做很多其他事情)。渲染的图像也可以像你说的那样被用户下载。 - StanE
@StanE,感谢您提供的信息。在过去的几年中,我学到了很多东西。 - homework
8个回答

133

我从我自己创建的一个代码中使它工作了。

<?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
适用于带有透明度的图像。 - homework

26

这个问题是关于合并两张图片,但在这种情况下,您不应该这样做。您应该将内容图像(即封面)放入<img />标签中,将样式图像放入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;
}

4
感谢您提供的内容,但我需要的是关于 PHP 的信息。不过我仍然会保存这个信息。 - homework
1
正确。这样对服务器来说更容易,因为它不必处理所有图像。对用户来说也更快,因为它不会一直拥有带有乙烯基的图像部分。 - Nicky Smits
2
所选答案很有趣,因为我们可以混合两张图片并在Facebook上分享。 - Fabio Montefuscolo
4
仅当图片用于网站时,CSS才起作用。要在社交媒体上分享、供用户下载等目的,不能使用CSS创建图片。 - Sherwin Flight
有人能帮我吗?我需要在合并图像后保存它。这是否可以通过编写HTML实现? - Rahul Vats
@Crozin 如何将包含CSS背景图和原始图像的图片下载为一个文件。 - NomanJaved

20

ImageArtist 是由我编写的纯gd封装程序,它使您可以轻松地进行复杂的图像操作。对于您的问题,使用这个强大的库只需要很少的步骤即可解决。

以下是一个示例代码:

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

这就是我的结果的样子。

输入图片描述


1
太棒了!谢谢。 - homework
太棒了!你节省了我好几个小时的时间! - Atomico
完美 +10000 分,来自我 :D - Vipertecpro
太棒了,我正在查看这个包并注意到CircularShape类,想知道是否可以向圆形添加边框,或者有没有任何文档可以参考? - youngdero

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

感谢分享,非常感激。 - Mustafa Erdogan

2

使用GD库或ImageMagick。我在谷歌上搜索了“PHP GD合并图片”,找到了几篇相关文章。以前我做过的方法是创建一个大的空白图片,然后使用imagecopymerge()将这些图像粘贴到原始的空白图片中。查看谷歌上的这些文章,你会发现一些可立即开始使用的源代码。


2
你可以使用ImageMagick扩展来完成此操作。我猜测combineImages()方法将会满足你的需求。

2
合并两个图片(png和jpg/png) [图像遮罩]
//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);
    }

1

PHP中的GD图像处理库可能是处理图像最好的选择。尝试使用其中一个imagecopy函数(imagecopy,imagecopymerge等)。它们中的每一个都以不同的方式组合两个图像。有关更多信息,请参见php文档中关于imagecopy的说明


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接