如何使用PHP将中心边缘的图像1与图像2合并?

时间:2013-08-30 03:37:44

标签: php

我有一个示例代码:

$image_1 = "image_1.jpg";

image 1

$image_2 = "image_2.jpg";

image 2

代码php合并两个图像:

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 = imagecreatefromjpeg($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);
}

$image_1 = 'image_1.jpg';
$image_2 = 'image_2.jpg';
merge($image_1, $image_2, 'merged.jpg');

结果是:

merged

如何使用image_2

修复保证金中心的image_1

true

1 个答案:

答案 0 :(得分:0)

尝试将imagecopy()的第三个参数(图像位置的x值)更改为较大图像宽度的1/2 - 较小图像宽度的1/2。