php添加背景图及设置格式,PHP添加PNG图片背景透明水印操作类定义与用法示例

更新时间:2023-06-24 09:53:44 阅读: 评论:0

php添加背景图及设置格式,PHP添加PNG图⽚背景透明⽔印操
作类定义与⽤法⽰例
本⽂实例讲述了PHP添加PNG图⽚背景透明⽔印操作类定义与⽤法。分享给⼤家供⼤家参考,具体如下:
图⽚相关操作类
class ImageTool
{
private $imagePath;//图⽚路径
private $outputDir;//输出⽂件夹
public $memoryImg;//内存图像
public $path;
public function __construct($imagePath, $outputDir = null)
{
排骨汤的做法$this->imagePath = $imagePath;
$this->outputDir = $outputDir;
$this->memoryImg = null;
$this->path = null;
}
/**
* 显⽰内存中的图⽚
* @param $image
*/
public function showImage()
{
if ($this->memoryImg != null) {
$info = getimagesize($this->imagePath);
$type = image_type_to_extension($info[2], fal);
header('Content-type:' . $info['mime']);
$funs = "image{$type}";
$funs($this->memoryImg);
imagedestroy($this->memoryImg);
1匹
$this->memoryImg = null;
}
/**
* 保存图⽚
* @param $image 图⽚路径
* @return string
*/
private function saveImage($image)
{
$info = getimagesize($this->imagePath);
$type = image_type_to_extension($info[2], fal);
$funs = "image{$type}";
if (empty($this->outputDir)) {
$funs($image, md5($this->imagePath) . '.' . $type);
return md5($this->imagePath) . '.' . $type;
} el {
$funs($image, $this->outputDir . md5($this->imagePath) . '.' . $type); return $this->outputDir . md5($this->imagePath) . '.' . $type;
}
}
/**
* 压缩图⽚
* @param $width 压缩后宽度
* @param $height 压缩后⾼度
* @param bool $output 是否输出⽂件
雌性激素药物
* @return resource
*/
public function compressImage($width, $height, $output = fal)
{
$image = null;
$info = getimagesize($this->imagePath);
$type = image_type_to_extension($info[2], fal);
$fun = "imagecreatefrom{$type}";
$image = $fun($this->imagePath);
imagesavealpha($image,true);//
imagealphablending($thumbnail,fal);//这⾥很重要,意思是不合并颜⾊,直接⽤$img图像颜⾊替换,包括透明⾊; imagesavealpha($thumbnail,true);//
imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $width, $height, $info[0], $info[1]);
imagedestroy($image);
if ($output) {
$path = $this->saveImage($thumbnail);
$this->path = $path;
}
$this->memoryImg = $thumbnail;
return $this;
}
/**
俗套* 为图像添加⽂字标记
*
* @param $content ⽂本内容
* @param $size 字体⼤⼩
* @param $font 字体样式
* @param bool $output 是否输出⽂件
* @return $this
*/
public function addTextmark($content, $size, $font, $output = fal)
{
$info = getimagesize($this->imagePath);
$type = image_type_to_extension($info[2], fal);
$fun = "imagecreatefrom{$type}";
$image = $fun($this->imagePath);
$color = imagecolorallocatealpha($image, 0, 0, 0, 80);
$posX = imagesx($image) - strlen($content) * $size / 2;
$posY = imagesy($image) - $size / 1.5;
imagettftext($image, $size, 0, $posX, $posY, $color, $font, $content);
if ($output) {
$this->saveImage($image);半茶碗
}
}
/**
* 为图⽚添加⽔印
马拉松训练冷若冰霜
*
* @param $watermark ⽔印图⽚路径
* @param $alpha ⽔印透明度(0-100)
* @param bool $output 是否输出⽂件
* @return $this
*/
public function addWatermark($watermark, $alpha, $output = fal)
{
$image_info = getimagesize($this->imagePath);
$image_type = image_type_to_extension($image_info[2], fal);
$image_fun = "imagecreatefrom{$image_type}";
$image = $image_fun($this->imagePath);
$mark_info = getimagesize($watermark);
$mark_type = image_type_to_extension($mark_info[2], fal);
$mark_fun = "imagecreatefrom{$mark_type}";
$mark = $mark_fun($watermark);
$posX = imagesx($image) - imagesx($mark);
$posY = imagesy($image) - imagesy($mark);
imagesavealpha($mark, true);
imagecopymerge($image, $mark, $posX, $posY, 0, 0, $mark_info[0], $mark_info[1], $alpha); imagesavealpha($mark, true);
if ($output) {
$path = $this->saveImage($image);
$this->path = $path;
}数学研究
$this->memoryImg = $image;
return $this;
}
//⽤给定⾓度旋转图像,以jpeg图像格式为例
* @param $degrees 旋转⾓度
* @param bool $output 是否保存图⽚
* @return $this
*/
function rotateImage($degrees, $output = fal)
{
$info = getimagesize($this->imagePath);
$type = image_type_to_extension($info[2], fal);
$fun = "imagecreatefrom{$type}";
$image = $fun($this->imagePath);
$block = imagecreatetruecolor(170,170);//建⽴⼀个画板
$bg = imagecolorallocatealpha($block , 0 , 0 , 0 , 127);//拾取⼀个完全透明的颜⾊
$image = imagerotate($image, $degrees, $bg ,0);
imagesavealpha($image, true);
header("Content-type: image/{$type}");
//旋转后的图⽚保存
if ($output) {
$path = $this->saveImage($image);
$this->path = $path;
}
$this->memoryImg = $image;
return $this;
}
/**
* 添加PNG透明图⽚
* $bigImgPath ⽬标图⽚路径
* $smallImgPath ⽔印图⽚路径
* $width 相对于⽬标图的x轴放置位置 左上⾓为 0
* $height 相对于⽬标图的y轴放置位置 左上⾓为0
* $bigImgPaths 合成后的图⽚路径 若路径名与第⼀张或第⼆张路径相同 直接覆盖原图
*/
public function mergerImg($bigImgPath, $smallImgPath, $width, $height, $bigImgPaths)

本文发布于:2023-06-24 09:53:44,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1052609.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:添加   图像   路径   透明   输出
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图