PHP GD 库,将 2 张图像并排转换为 1 张

标签 php image gd

<分区>

我想拍摄两张图片并将它们并排合并(不重叠)。我想做的一个例子是前后图片(两张图片的高度相同但宽度不同)。任何关于如何使用 PHP GD 图像库完成的示例代码将不胜感激。谢谢!

最佳答案

<?php

$img1_path = 'images_1.png';
$img2_path = 'images_2.png';

list($img1_width, $img1_height) = getimagesize($img1_path);
list($img2_width, $img2_height) = getimagesize($img2_path);

$merged_width  = $img1_width + $img2_width;
//get highest
$merged_height = $img1_height > $img2_height ? $img1_height : $img2_height;

$merged_image = imagecreatetruecolor($merged_width, $merged_height);

imagealphablending($merged_image, false);
imagesavealpha($merged_image, true);

$img1 = imagecreatefrompng($img1_path);
$img2 = imagecreatefrompng($img2_path);

imagecopy($merged_image, $img1, 0, 0, 0, 0, $img1_width, $img1_height);
//place at right side of $img1
imagecopy($merged_image, $img2, $img1_width, 0, 0, 0, $img2_width, $img2_height);

//save file or output to broswer
$SAVE_AS_FILE = TRUE;
if( $SAVE_AS_FILE ){
    $save_path = "your target path";
    imagepng($merged_image,$save_path);
}else{
    header('Content-Type: image/png');
    imagepng($merged_image);
}

//release memory
imagedestroy($merged_image);

?>

尝试一下

关于PHP GD 库,将 2 张图像并排转换为 1 张,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25636066/

相关文章:

php - 验证此图像验证码。 Jquery PHP 和 JavaScript

java - JFrame 中的图像图标

php - 使用 PHP GD 库将 webp 转换为 jpeg

php - 我在哪里可以找到 GD 兼容字体?

jpg文件上的php gd图像png水印产生奇怪的结果

php - 如何在 php 中获取上传图片的元标记信息?

php - 使用 PDO 作为驱动程序时,CodeIgniter 未捕获数据库错误

php - 无法通过引用传递参数 2 - uuid PDO

php - 如何检测字体中定义的字符?

jquery - 等待使用 JQuery 加载图像