1.创建一个适应大小的底层图片
2.读取需要拼接的图片
$dst_data=file_get_contents($dst_src);
$dst_img = imagecreatefromstring($dst_data);
3.把读取的图放到指定的位置,这里需要慢慢调试,使用下面的函数
imagecopymerge
4.出拼接后的图
header("Content-Type:image/jpeg");
imagejpeg($dst_img);
可能会用到的函数
imagecreatetruecolor //生成底图
file_get_contents //获取对应的图片
imagecreatefromstring //字符串转换成图片
imagecopyresampled //拷贝图片调整大小
imagecopymerge //合成图片
header("Content-type: image/jpeg");
//原始图像
$dst = "images/flower_1.jpg";
//得到原始图片信息
$dst_im = imagecreatefromjpeg($dst);
$dst_info = getimagesize($dst);
//水印图像
$src = "images/logo.gif";
$src_im = imagecreatefromgif($src);
$src_info = getimagesize($src);
//水印透明度
$alpha = 30;
//合并水印图片
imagecopymerge($dst_im,$src_im,$dst_info[0]-$src_info[0],$dst_info[1]-$src_info[1],0,0,$src_info[0],
$src_info[1],$alpha);
//输出合并后水印图片
imagejpeg($dst_im);
imagedestroy($dst_im);
imagedestroy($src_im);
下面是个简单的水印demo
<?php
function combine_image($image1,$image2,$c=0,$d=0)//$image1底层图片 $image2水印图片 $c合并X坐标 $d合并Y坐标
{
$filename=time().".jpg";
if(!$image1 || !$image2) return false;
$aa=getimagesize($image1);
$width=$aa["0"];
$height=$aa["1"];
$kind=$aa['mime'];
$aa1=getimagesize($image2);
$width1=$aa1["0"];
$height1=$aa1["1"];
$kind1=$aa1['mime'];
//指定缩放出来的最大的宽度(也有可能是高度)
$max=1000;
//根据最大值为300,算出另一个边的长度,得到缩放后的图片宽度和高度
if($width > $height){
$width=$max;
$height=$height*($max/$aa['0']);
}else{
$height=$max;
$width=$width*($max/$aa['1']);
}
$im = imagecreatetruecolor($width,$height);
if($kind=='image/jpeg'){
$im1 = imagecreatefromjpeg($image1);
}elseif($kind=='image/png'){
$im1 = imagecreatefrompng($image1);
}else{
$im1 = imagecreatefromgif($image1);
}
if($kind1=='image/jpeg'){
$im2 = imagecreatefromjpeg($image2);
}elseif($kind1=='image/png'){
$im2 = imagecreatefrompng($image2);
}else{
$im2 = imagecreatefromgif($image2);
}
imagecopyresampled($im, $im1, 0, 0, 0, 0,$width,$height,$aa['0'],$aa['1']);
imagecopyresampled($im, $im2, $c, $d,0, 0,$width,$height,$aa1['0'],$aa1['1']);
header('Content-Type: image/jpg');
//imagejpeg($im,$filename);
imagejpeg($im);
imagedestroy($im);
imagedestroy($im1);
imagedestroy($im2);
return $filename;
}
combine_image('1.jpg','2.png');