imagefun php,ImageWatermark.php

该代码段展示了如何使用PHP实现不同计划(健身、读书、存钱、美容和其他)的图片合成,并添加日期、计划名称和描述文字作为水印。通过获取背景和目标图片信息,创建真彩色画布,将文字写入图片,然后合并到背景图上,最后保存为PNG格式。
摘要由CSDN通过智能技术生成

/**

* Created by PhpStorm.

* User: rdluck

* Date: 2016/10/22

* Time: 下午7:56

*/

function KeepingWatermark($backgroundImgPath,$keepingImgPath,$data){

//根据背景图片路径获取背景图片信息

$bgImgInfo=getimagesize($backgroundImgPath);

if (!$bgImgInfo){

echo "获取背景图片信息失败!";

return;

}

//获取背景图片类型

$bgImgType=image_type_to_extension($bgImgInfo[2], false);

//创建背景图片

$bgImgFun="imagecreatefrom{$bgImgType}";

//创建背景图片内存信息

$bgImgData=$bgImgFun($backgroundImgPath);

//获取健身图片

$keepingImgInfo=getimagesize($keepingImgPath);

if (!$keepingImgInfo){

echo "获取背景图片信息失败!";

return;

}

//获取健身图片类型

$keepingImgType=image_type_to_extension($keepingImgInfo[2],false);

$keepingImgFun="imagecreatefrom{$keepingImgType}";

$keepingImgData=$keepingImgFun($keepingImgPath);

$fontPath="font/msyh.ttc";

//写文字

//日

$dayColor = imagecolorallocatealpha($keepingImgData, 74, 206, 32, 50);

imagettftext($keepingImgData, 64, 1, 496, 289, $dayColor, $fontPath, "02");

//月

$monthColor=imagecolorallocatealpha($keepingImgData, 0, 0, 0, 50);

imagettftext($keepingImgData, 42, 0, 604, 300, $monthColor, $fontPath, "07月");

//年

$yearColor=imagecolorallocatealpha($keepingImgData, 0, 0, 0, 50);

imagettftext($keepingImgData, 42, 0, 569, 374, $yearColor, $fontPath, "2016年");

//第几天

$whatdayColor=imagecolorallocatealpha($keepingImgData, 74, 206, 32, 50);

imagettftext($keepingImgData, 36, 0, 571, 435, $whatdayColor, $fontPath, "第一天");

//我的健身计划

$keepPlanColor=imagecolorallocatealpha($keepingImgData, 255, 255, 255, 50);

imagettftext($keepingImgData, 25, 0, 523, 484, $keepPlanColor, $fontPath, "《我的健身计划》");

//描述信息

$descColor=imagecolorallocatealpha($keepingImgData, 255, 255, 255, 50);

imagettftext($keepingImgData, 28, 0, 120, 670, $descColor, $fontPath, "没\n有\n借\n口\n!");

$desc1Color=imagecolorallocatealpha($keepingImgData, 255, 255, 255, 50);

imagettftext($keepingImgData, 28, 0, 70, 670, $desc1Color, $fontPath, "再\n难\n也\n要\n坚\n持\n!");

//创建真彩色画布(ps:只有这样才能保证后面copy图片的时候不会失真)

//根据图片,创建画布大小

$imgCanver = imageCreatetruecolor(760,1000);

//为真彩色画布创建白色背景,再设置为透明

$color = imagecolorallocate($imgCanver, 255, 255, 255);

imagefill($imgCanver, 0, 0, $color);

imageColorTransparent($imgCanver, $color);

//复制健身图片

imagecopy($imgCanver, $keepingImgData, 0, 0, 0, 0, 760, 1334);

//复制背景图

imagecopy($imgCanver, $bgImgData, 0, 0, 0, 0, $bgImgInfo[0], $bgImgInfo[1]);

$saveFunc = "imagepng";

$savePath = time(). ".png";

if (file_exists($savePath)) {

if (!unlink($savePath)) {

echo "删除文件失败!";

return;

}

}

$saveFunc($imgCanver, $savePath);

imagedestroy($imgCanver);

return $savePath;

}

/*$backgroundImg="images/keeping/keepbg.png";

$keepingImg="images/keeping/keeping.jpg";

//健身合成图

$result=KeepingWatermark($backgroundImg,$keepingImg);

echo $result;*/

/**

* 读书计划图片合成

* @param $backgroundImgPath 背景图片路径

* @param $readImgPath 读书图片

* @param $data 相关数据

* @return string|void

*/

function ReadWatermark($backgroundImgPath,$readImgPath,$data){

//根据背景图片路径获取背景图片信息

$bgImgInfo=getimagesize($backgroundImgPath);

if (!$bgImgInfo){

echo "获取背景图片信息失败!";

return;

}

//获取背景图片类型

$bgImgType=image_type_to_extension($bgImgInfo[2], false);

//创建背景图片

$bgImgFun="imagecreatefrom{$bgImgType}";

//创建背景图片内存信息

$bgImgData=$bgImgFun($backgroundImgPath);

//获取读书图片信息

$readImgInfo=getimagesize($readImgPath);

if (!$readImgInfo){

echo "获取读书图片信息失败!";

return;

}

//获取健身图片类型

$readImgType=image_type_to_extension($readImgInfo[2],false);

$readImgFun="imagecreatefrom{$readImgType}";

$readImgData=$readImgFun($readImgPath);

$fontPath="font/msyh.ttc";

//写文字

//日

$dayColor = imagecolorallocatealpha($readImgData, 255, 255, 255, 50);

imagettftext($readImgData, 64, 0, 496, 289, $dayColor, $fontPath, "02");

//月

$monthColor=imagecolorallocatealpha($readImgData, 0, 0, 0, 43);

imagettftext($readImgData, 42, 0, 604, 300, $monthColor, $fontPath, "07月");

//年

$yearColor=imagecolorallocatealpha($readImgData, 0, 0, 0, 50);

imagettftext($readImgData, 42, 0, 569, 374, $yearColor, $fontPath, "2016年");

//第几天

$whatdayColor=imagecolorallocatealpha($readImgData, 255, 255, 255, 50);

imagettftext($readImgData, 36, 0, 571, 435, $whatdayColor, $fontPath, "第一天");

//读槽边往事计划

$readPlanColor=imagecolorallocatealpha($readImgData, 255, 255, 255, 50);

imagettftext($readImgData, 25, 0, 499, 484, $readPlanColor, $fontPath, "《我的健身计划》");

//描述信息

$descColor=imagecolorallocatealpha($readImgData, 123, 80, 93, 50);

imagettftext($readImgData, 28, 0, 80, 820, $descColor, $fontPath, "再难也要坚持\n没有借口ß!");

//创建真彩色画布(ps:只有这样才能保证后面copy图片的时候不会失真)

//根据图片,创建画布大小

$imgCanver = imageCreatetruecolor(760,1000);

//为真彩色画布创建白色背景,再设置为透明

$color = imagecolorallocate($imgCanver, 255, 255, 255);

imagefill($imgCanver, 0, 0, $color);

imageColorTransparent($imgCanver, $color);

//复制读书图片

imagecopy($imgCanver, $readImgData, 0, 0, 0, 0, 760, 1334);

//复制背景图

imagecopy($imgCanver, $bgImgData, 0, 0, 0, 0, $bgImgInfo[0], $bgImgInfo[1]);

$saveFunc = "imagepng";

$savePath = time(). ".png";

if (file_exists($savePath)) {

if (!unlink($savePath)) {

echo "删除文件失败!";

return;

}

}

$saveFunc($imgCanver, $savePath);

imagedestroy($imgCanver);

return $savePath;

}

/*$backgroundImg="images/read/readbg.png";

$readImg="images/read/read.jpg";

//读书合成图

$result=ReadWatermark($backgroundImg,$readImg);

print_r($result);*/

/**

* 存钱计划图片合成

* @param $backgroundImgPath 背景图片路径

* @param $saveMoneyImgPath 读书图片

* @param $data 相关数据

* @return string|void

*/

function SaveMoneyWatermark($backgroundImgPath,$saveMoneyImgPath,$data){

//根据背景图片路径获取背景图片信息

$bgImgInfo=getimagesize($backgroundImgPath);

if (!$bgImgInfo){

echo "获取背景图片信息失败!";

return;

}

//获取背景图片类型

$bgImgType=image_type_to_extension($bgImgInfo[2], false);

//创建背景图片

$bgImgFun="imagecreatefrom{$bgImgType}";

//创建背景图片内存信息

$bgImgData=$bgImgFun($backgroundImgPath);

//获取存钱图片信息

$saveMoneyImgInfo=getimagesize($saveMoneyImgPath);

if (!$saveMoneyImgInfo){

echo "获取存钱图片信息失败!";

return;

}

//获取存钱图片类型

$saveMoneyImgType=image_type_to_extension($saveMoneyImgInfo[2],false);

$saveMoneyImgFun="imagecreatefrom{$saveMoneyImgType}";

$saveMoneyImgData=$saveMoneyImgFun($saveMoneyImgPath);

$fontPath="font/msyh.ttc";

//写文字

//日

$dayColor = imagecolorallocatealpha($saveMoneyImgData, 192, 168, 125, 50);

imagettftext($saveMoneyImgData, 64, 0, 496, 289, $dayColor, $fontPath, "02");

//月

$monthColor=imagecolorallocatealpha($saveMoneyImgData, 0, 0, 0, 43);

imagettftext($saveMoneyImgData, 43, 0, 604, 300, $monthColor, $fontPath, "07月");

//年

$yearColor=imagecolorallocatealpha($saveMoneyImgData, 0, 0, 0, 50);

imagettftext($saveMoneyImgData, 43, 0, 569, 374, $yearColor, $fontPath, "2016年");

//第几天

$whatdayColor=imagecolorallocatealpha($saveMoneyImgData, 192, 168, 125, 50);

imagettftext($saveMoneyImgData, 36, 0, 571, 435, $whatdayColor, $fontPath, "第一天");

//读槽边往事计划

$readPlanColor=imagecolorallocatealpha($saveMoneyImgData, 255, 255, 255, 50);

imagettftext($saveMoneyImgData, 25, 0, 523, 485, $readPlanColor, $fontPath, "《不要做月光族》");

//描述信息

$descColor=imagecolorallocatealpha($saveMoneyImgData, 115, 100, 72, 50);

imagettftext($saveMoneyImgData, 28, 0, 484, 539, $descColor, $fontPath, "再难也要坚持没\n有借口!");

//创建真彩色画布(ps:只有这样才能保证后面copy图片的时候不会失真)

//根据图片,创建画布大小

$imgCanver = imageCreatetruecolor(760,1000);

//为真彩色画布创建白色背景,再设置为透明

$color = imagecolorallocate($imgCanver, 255, 255, 255);

imagefill($imgCanver, 0, 0, $color);

imageColorTransparent($imgCanver, $color);

//复制读书图片

imagecopy($imgCanver, $saveMoneyImgData, 0, 0, 0, 0, 760, 1334);

//复制背景图

imagecopy($imgCanver, $bgImgData, 0, 0, 0, 0, $bgImgInfo[0], $bgImgInfo[1]);

$saveFunc = "imagepng";

$savePath = time(). ".png";

if (file_exists($savePath)) {

if (!unlink($savePath)) {

echo "删除文件失败!";

return;

}

}

$saveFunc($imgCanver, $savePath);

imagedestroy($imgCanver);

return $savePath;

}

/*$backgroundImg="images/saveMoney/savemoneybg.png";

$saveMoneyImg="images/saveMoney/savemoeny.jpg";

$smresult=SaveMoneyWatermark($backgroundImg,$saveMoneyImg);

echo $smresult;*/

/**

* 美容计划图片合成

* @param $backgroundImgPath 背景图片路径

* @param $saveMoneyImgPath 美容图片

* @param $data 相关数据

* @return string|void

*/

function BeautyWatermark($backgroundImgPath,$beautyImgPath,$data){

//根据背景图片路径获取背景图片信息

$bgImgInfo=getimagesize($backgroundImgPath);

if (!$bgImgInfo){

echo "获取背景图片信息失败!";

return;

}

//获取背景图片类型

$bgImgType=image_type_to_extension($bgImgInfo[2], false);

//创建背景图片

$bgImgFun="imagecreatefrom{$bgImgType}";

//创建背景图片内存信息

$bgImgData=$bgImgFun($backgroundImgPath);

//获取存钱图片信息

$beautyImgInfo=getimagesize($beautyImgPath);

if (!$beautyImgInfo){

echo "获取美容图片信息失败!";

return;

}

//获取存钱图片类型

$beautyImgType=image_type_to_extension($beautyImgInfo[2],false);

$beautyImgFun="imagecreatefrom{$beautyImgType}";

$beautyImgData=$beautyImgFun($beautyImgPath);

$fontPath="font/msyh.ttc";

//写文字

//日

$dayColor = imagecolorallocatealpha($beautyImgData, 93, 94, 103, 50);

imagettftext($beautyImgData, 64, 0, 496, 289, $dayColor, $fontPath, "02");

//月

$monthColor=imagecolorallocatealpha($beautyImgData, 0, 0, 0, 43);

imagettftext($beautyImgData, 43, 0, 604, 300, $monthColor, $fontPath, "07月");

//年

$yearColor=imagecolorallocatealpha($beautyImgData, 0, 0, 0, 50);

imagettftext($beautyImgData, 43, 0, 569, 374, $yearColor, $fontPath, "2016年");

//第几天

$whatdayColor=imagecolorallocatealpha($beautyImgData, 93, 94, 103, 50);

imagettftext($beautyImgData, 36, 0, 571, 435, $whatdayColor, $fontPath, "第一天");

//读槽边往事计划

$readPlanColor=imagecolorallocatealpha($beautyImgData, 255, 255, 255, 50);

imagettftext($beautyImgData, 25, 0, 475, 484, $readPlanColor, $fontPath, "《计划让自己美起来》");

//描述信息

$descColor=imagecolorallocatealpha($beautyImgData, 255, 255, 255, 50);

imagettftext($beautyImgData, 28, 0, 69, 774, $descColor, $fontPath, "再难也要坚持没\n有借口!");

//创建真彩色画布(ps:只有这样才能保证后面copy图片的时候不会失真)

//根据图片,创建画布大小

$imgCanver = imageCreatetruecolor(760,1000);

//为真彩色画布创建白色背景,再设置为透明

$color = imagecolorallocate($imgCanver, 255, 255, 255);

imagefill($imgCanver, 0, 0, $color);

imageColorTransparent($imgCanver, $color);

//复制读书图片

imagecopy($imgCanver, $beautyImgData, 0, 0, 0, 0, 760, 1334);

//复制背景图

imagecopy($imgCanver, $bgImgData, 0, 0, 0, 0, $bgImgInfo[0], $bgImgInfo[1]);

$saveFunc = "imagepng";

$savePath = time(). ".png";

if (file_exists($savePath)) {

if (!unlink($savePath)) {

echo "删除文件失败!";

return;

}

}

$saveFunc($imgCanver, $savePath);

imagedestroy($imgCanver);

return $savePath;

}

/*$backgroundImg="images/beauty/beautybg.png";

$beautyImg="images/beauty/beauty.jpg";

$smresult=BeautyWatermark($backgroundImg,$beautyImg);

echo $smresult;*/

/**

* 其他计划图片合成

* @param $backgroundImgPath 背景图片路径

* @param $otherImgPath 其他计划图片

* @param $data 相关数据

* @return string|void

*/

function OtherWatermark($backgroundImgPath,$otherImgPath,$data){

//根据背景图片路径获取背景图片信息

$bgImgInfo=getimagesize($backgroundImgPath);

if (!$bgImgInfo){

echo "获取背景图片信息失败!";

return;

}

//获取背景图片类型

$bgImgType=image_type_to_extension($bgImgInfo[2], false);

//创建背景图片

$bgImgFun="imagecreatefrom{$bgImgType}";

//创建背景图片内存信息

$bgImgData=$bgImgFun($backgroundImgPath);

//获取存钱图片信息

$otherImgInfo=getimagesize($otherImgPath);

if (!$otherImgInfo){

echo "获取美容图片信息失败!";

return;

}

//获取存钱图片类型

$otherImgType=image_type_to_extension($otherImgInfo[2],false);

$otherImgFun="imagecreatefrom{$otherImgType}";

$otherImgData=$otherImgFun($otherImgPath);

$fontPath="font/msyh.ttc";

//写文字

//日

$dayColor = imagecolorallocatealpha($otherImgData, 246, 175, 81, 50);

imagettftext($otherImgData, 64, 0, 496, 289, $dayColor, $fontPath, "02");

//月

$monthColor=imagecolorallocatealpha($otherImgData, 0, 0, 0, 50);

imagettftext($otherImgData, 43, 0, 604, 300, $monthColor, $fontPath, "07月");

//年

$yearColor=imagecolorallocatealpha($otherImgData, 0, 0, 0, 50);

imagettftext($otherImgData, 43, 0, 596, 374, $yearColor, $fontPath, "2016年");

//第几天

$whatdayColor=imagecolorallocatealpha($otherImgData, 246, 175, 81, 50);

imagettftext($otherImgData, 36, 0, 571, 435, $whatdayColor, $fontPath, "第一天");

//读槽边往事计划

$otherColor=imagecolorallocatealpha($otherImgData, 255, 255, 255, 50);

imagettftext($otherImgData, 25, 0, 528, 484, $otherColor, $fontPath, "《宝贝每一天照》");

//描述信息

$descColor=imagecolorallocatealpha($otherImgData, 255, 255, 255, 50);

imagettftext($otherImgData, 28, 0, 69, 774, $descColor, $fontPath, "再难也要坚持没\n有借口!");

//创建真彩色画布(ps:只有这样才能保证后面copy图片的时候不会失真)

//根据图片,创建画布大小

$imgCanver = imageCreatetruecolor(760,1000);

//为真彩色画布创建白色背景,再设置为透明

$color = imagecolorallocate($imgCanver, 255, 255, 255);

imagefill($imgCanver, 0, 0, $color);

imageColorTransparent($imgCanver, $color);

//复制读书图片

imagecopy($imgCanver, $otherImgData, 0, 0, 0, 0, 760, 1334);

//复制背景图

imagecopy($imgCanver, $bgImgData, 0, 0, 0, 0, $bgImgInfo[0], $bgImgInfo[1]);

$saveFunc = "imagepng";

$savePath = time(). ".png";

if (file_exists($savePath)) {

if (!unlink($savePath)) {

echo "删除文件失败!";

return;

}

}

$saveFunc($imgCanver, $savePath);

imagedestroy($imgCanver);

return $savePath;

}

$backgroundImg="images/other/otherbg.png";

$otherImg="images/other/other.jpg";

$smresult=OtherWatermark($backgroundImg,$otherImg);

echo $smresult;

一键复制

编辑

Web IDE

原始数据

按行查看

历史

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值