PHP - 使用imagecopy函数垂直合并两个图像 [英] PHP - Merge two images vertically using imagecopy function
问题描述
我必须垂直合并两个PNG图像。
I have to merge two PNG images vertically.
//place at right side of $img1
imagecopy($merged_image, $img2, $img1_width, 0, 0, 0, $img2_width, $img2_height);
但我需要在底部之后合并图像。(垂直)
But I need to merge the images one after bottom one.(vertically)
如何在第一张图片底部后合并第二张图片?
How to merge that second image after bottom of first image?
请提供意见!
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 = "images/Sample_Output.png";
imagepng($merged_image,$save_path);
}else{
header('Content-Type: image/png');
imagepng($merged_image);
}
//release memory
imagedestroy($merged_image);
输出:
推荐答案
当我考虑这个功能时
imagecopy(资源$ dst_im,资源$ src_im,int $ dst_x,int $ dst_y,int $ src_x,int $ src_y,int $ src_w,int $ src_h)
imagecopy ( resource $dst_im , resource$src_im , int $dst_x , int $dst_y , int $src_x , int$src_y , int $src_w , int $src_h )
它表示 dst_x
和 dst_y
应分别是目标点的x坐标和目标点的y坐标。
It says that the dst_x
and dst_y
should be the x-coordinate of destination point and y-coordinate of destination point respectively.
所以要垂直合并两个图像,它应该是这样的。
So to merge two images vertically, it should be something like this.
imagecopy($ merged_image,$ img2,0,$ img1_height,0,0,$ img2_width,$ img2_height);
你还应该改变根据最终结果的需要, $ merged_width
和 $ merged_height
变量值。更改以下两行,
You should also change the $merged_width
and $merged_height
variable values as needed for the final result. Change the following two lines,
$merged_width = $img1_width + $img2_width; //get highest
$merged_height = $img1_height > $img2_height ? $img1_height : $img2_height;
如下所示,
$merged_width = $img1_width > $img2_width ? $img1_width : $img2_width; //get highest width as result image width
$merged_height = $img1_height + $img2_height;
最终结果:
$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 ? $img1_width : $img2_width; //get highest width as result image width
$merged_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, 0,$img1_height , 0, 0, $img2_width, $img2_height);
//save file or output to broswer
$SAVE_AS_FILE = TRUE;
if( $SAVE_AS_FILE ){
$save_path = "images/Sample_Output.png";
imagepng($merged_image,$save_path);
}else{
header('Content-Type: image/png');
imagepng($merged_image);
}
//release memory
imagedestroy($merged_image);
这篇关于PHP - 使用imagecopy函数垂直合并两个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!