1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > php生成缩略图 加水印类

php生成缩略图 加水印类

时间:2020-10-22 01:37:28

相关推荐

php生成缩略图 加水印类

php教程|php手册

php,生成,缩,略图,加,水印,类,这个,一个,简单,库,操作,php,燕,十八,公益,PHP,讲堂,论,坛,ht

php教程-php手册

这个一个简单的GD库操作

小偷电影采集源码,vscode中c语言函数,ubuntu数学软件,tomcat的安全设置,爬虫采集男女,php 上层目录,短视频seo优化哪个好,开发者个人网站源码,wordpress中文博客模板下载lzw

[php]

手机网站源码下载,ubuntu怎么安装的,tomcat编译文件指向,爬虫占内存,PHP淘宝抢购脚本,seo信息流优化能挣多少钱lzw

<?php

个人博客php源码,vscode编译卡死,ubuntu设置开机启动内核,tomcat吃鸡,sqlite快速查找,织梦实时推送插件,前端模仿excel的框架,图论与爬虫论文,php content,seo信息发布,各种测试网站源码,手机端网页视频播放,简单的html模板贴吧,html订单详情页面源码,php题目录入管理系统源代码,人人商城小程序服务器配置要求lzw

/****

燕十八 公益PHP讲堂

论 坛: http://www.zixue.it

微 博: /Yshiba

YY频道: 88354001

****/

/***

想操作图片

先得把图片的大小,类型信息得到

水印:就是把指定的水印复制到目标上,并加透明效果

缩略图:就是把大图片复制到小尺寸画面上

***/

class ImageTool {

// imageInfo 分析图片的信息

// return array()

public static function imageInfo($image) {

// 判断图片是否存在

if (!file_exists($image)) {

return false;

}

$info = getimagesize($image);

if ($info == false) {

return false;

}

// 此时info分析出来,是一个数组

$img[‘width’] = $info[0];

$img[‘height’] = $info[1];

$img[‘ext’] = substr($info[‘mime’], strpos($info[‘mime’], ‘/’) + 1);

return $img;

}

/*

加水印功能

parm String $dst 等操作图片

parm String $water 水印小图

parm String $save,不填则默认替换原始图

*/

public static function water($dst, $water, $save = NULL, $pos = 2, $alpha = 50) {

// 先保证2个图片存在

if (!file_exists($dst) || !file_exists($water)) {

return false;

}

// 首先保证水印不能比待操作图片还大

$dinfo = self::imageInfo($dst);

$winfo = self::imageInfo($water);

if ($winfo[‘height’] > $dinfo[‘height’] || $winfo[‘width’] > $dinfo[‘width’]) {

return false;

}

// 两张图,读到画布上! 但是图片可能是png,可能是jpeg,用什么函数读?

$dfunc = ‘imagecreatefrom’ . $dinfo[‘ext’];

$wfunc = ‘imagecreatefrom’ . $winfo[‘ext’];

if (!function_exists($dfunc) || !function_exists($wfunc)) {

return false;

}

// 动态加载函数来创建画布

$dim = $dfunc($dst);

// 创建待操作的画布

$wim = $wfunc($water);

// 创建水印画布

// 根据水印的位置 计算粘贴的坐标

switch($pos) {

case 0 :

// 左上角

$posx = 0;

$posy = 0;

break;

case 1 :

// 右上角

$posx = $dinfo[‘width’] – $winfo[‘width’];

$posy = 0;

break;

case 3 :

// 左下角

$posx = 0;

$posy = $dinfo[‘height’] – $winfo[‘height’];

break;

default :

$posx = $dinfo[‘width’] – $winfo[‘width’];

$posy = $dinfo[‘height’] – $winfo[‘height’];

}

// 加水印

imagecopymerge($dim, $wim, $posx, $posy, 0, 0, $winfo[‘width’], $winfo[‘height’], $alpha);

// 保存

if (!$save) {

$save = $dst;

unlink($dst);

// 删除原图

}

$createfunc = ‘image’ . $dinfo[‘ext’];

$createfunc($dim, $save);

imagedestroy($dim);

imagedestroy($wim);

return true;

}

/**

thumb 生成缩略图

等比例缩放,两边留白

**/

public static function thumb($dst, $save = NULL, $width = 200, $height = 200) {

// 首先判断待处理的图片存不存在

$dinfo = self::imageInfo($dst);

if ($dinfo == false) {

return false;

}

// 计算缩放比例

$calc = min($width / $dinfo[‘width’], $height / $dinfo[‘height’]);

// 创建原始图的画布

$dfunc = ‘imagecreatefrom’ . $dinfo[‘ext’];

$dim = $dfunc($dst);

// 创建缩略画布

$tim = imagecreatetruecolor($width, $height);

// 创建白色填充缩略画布

$white = imagecolorallocate($tim, 255, 255, 255);

// 填充缩略画布

imagefill($tim, 0, 0, $white);

// 复制并缩略

$dwidth = (int)$dinfo[‘width’] * $calc;

$dheight = (int)$dinfo[‘height’] * $calc;

$paddingx = (int)($width – $dwidth) / 2;

$paddingy = (int)($height – $dheight) / 2;

imagecopyresampled($tim, $dim, $paddingx, $paddingy, 0, 0, $dwidth, $dheight, $dinfo[‘width’], $dinfo[‘height’]);

// 保存图片

if (!$save) {

$save = $dst;

unlink($dst);

}

$createfunc = ‘image’ . $dinfo[‘ext’];

$createfunc($tim, $save);

imagedestroy($dim);

imagedestroy($tim);

return true;

}

}

// print_r(ImageTool::imageInfo(‘./home.jpg’));

/*

echo ImageTool::water(‘./home.jpg’,’./smallfeng.jpg’,’home1.jpg’,0)?’OK’:’FAIL’;

echo ImageTool::water(‘./home.jpg’,’./smallfeng.jpg’,’home2.jpg’,1)?’OK’:’FAIL’;

echo ImageTool::water(‘./home.jpg’,’./smallfeng.jpg’,’home3.jpg’,2)?’OK’:’FAIL’;

echo ImageTool::water(‘./home.jpg’,’./smallfeng.jpg’,’home4.jpg’,3)?’OK’:’FAIL’;

*/

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。