php 合成图片保存,php 图片合成

本文介绍了一个PHP函数,hecheng(),用于合成图片,将二维码与背景图合并,并提供图片缩放功能。它展示了如何使用imagecopymerge操作图片,以及根据不同图片类型进行输出。关键点包括文件读取、图像处理和内容类型判断。
摘要由CSDN通过智能技术生成

hecheng('back.png','123.png','newaaa.png');

// 合成图片

function hecheng($backpc,$qr,$newpc)

{

$qrcode = thumb($qr,150,150,'new_');

$bigImg = imagecreatefromstring(file_get_contents($backpc));

$qrcodeimg = imagecreatefromstring(file_get_contents($qrcode));

list($qCodeWidth, $qCodeHight, $qCodeType) = getimagesize($qrcode);

// imagecopymerge使用注解

imagecopymerge($bigImg, $qrcodeimg, 133, 550, 0, 0, $qCodeWidth, $qCodeHight, 100);

list($bigWidth, $bigHight, $bigType) = getimagesize($backpc);

switch ($bigType) {

case 1: //gif

header('Content-Type:image/gif');

imagegif($bigImg);

break;

case 2: //jpg

header('Content-Type:image/jpg');

imagejpeg($bigImg);

break;

case 3: //png

// header('Content-Type:image/png');

imagepng($bigImg,$newpc);

break;

default:

# code...

break;

}

imagedestroy($bigImg);

imagedestroy($qrcodeimg);

return true;

}

// 缩放图片

function thumb($file,$dw,$dh,$pre)

{

$brr=pathinfo($file);

$dir=$brr['dirname']; //图片保存路径

$basename=$brr['basename'];//图片名字

$dstname=$pre.$basename; //目标图片名字

$path=$dir.'/'.$dstname;//目标图片保存轮径

$arr=getimagesize($file); //获得图片信息

$sw=$arr[0]; //原图宽

$sh=$arr[1]; //原图的高

$type=$arr[2]; //图片格式 1 = GIF,2 = JPG,3 = PNG

$mime=$arr['mime']; //MIME 类型

switch ($type) {

case 1:

$imgcreate='imagecreatefromgif';

$imgout='imagegif';

break;

case 2:

$imgcreate='imagecreatefromjpeg';

$imgout='imagejpeg';

break;

case 3:

$imgcreate='imagecreatefrompng';

$imgout='imagepng';

break;

}

$src=$imgcreate($file);

$b=$sw/$dw>$sh/$dh?$sw/$dw:$sh/$dh;

$dw=floor($sw/$b);

$dh=floor($sh/$b);

$dst=imagecreatetruecolor($dw,$dh);

$bool=imagecopyresampled($dst, $src, 0, 0 , 0 , 0, $dw , $dh , $sw, $sh);

$imgout($dst,$path);

return $dstname;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值