热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

php方便水印和缩略图的图形类

这是个方便做水印和缩略图的类,简化一些操作,按照一些CSS的习惯写参数
代码如下:



/*
*@author 夜无眠 27262681@qq.com
*copyright http://www.gowake.cn
*/

class img {
function __construct($arg = null) {
$args = func_get_args();
if($arg == null) {
return null;
}
$this->im = call_user_func_array(array($this,'create'),$args);
}

function __call($func,$arg) {
if(function_exists('image'.$func)) {
$funcstr = 'image'.$func;
}elseif (function_exists($func)){
$funcstr = $func;
}else {
error("没有这个类方法或函数");
}
return call_user_func_array($funcstr,$arg);
}

/*
*创建图像
*@param string/int 图片文件路径或者宽度
*@param int 高度,可省略
*@param string 6位16进制整数
*/
function create($arg = null) {
$args = func_get_args();
if(is_file($args[0])) {
$this->file = $args[0];
$size = getimagesize($this->file) or error('图片类型错误');
$this->size = $this->size ? $this->size : $size;
$type = image_type_to_extension($size[2],false);
$this->type = $this->type ? $this->type : $type;
$createfunc = 'imagecreatefrom'.$type;
$im = $createfunc($this->file);
}elseif((int)$args[0]>0 and (int)$args[1]>0) {
$im = imagecreatetruecolor((int)$args[0],(int)$args[1]) or error("对不起,参数错误!");
if(!$args[2]) {
$color = hexdec('7fffffff');
imagecolortransparent($im,$color);
}else {
$color = hexdec(str_replace('#','',$args[2]));
}
$this->size = $this->size ? $this->size : array((int)$args[0] ,(int)$args[1]);
imagefill($im, 1, 1, $color);
}else {
error("对不起,参数错误!");
}
//imagealphablending($im,false);//这两行用来记录透明通道
imagesavealpha($im,true);
imageinterlace($im,true);//开启隔行扫描
return $im;
}

/*
*生成缩略图
*@param int $w 新图片的宽度
*@param int $h 新图片的宽度
*@param string/bool $color 可选,新图片的背景色,false或空为透明
*@param bool $lashen 可选,是否拉伸,默认不拉伸
*/
function suolue($w = null,$h = null,$color = false,$lashen = false) {
$w_o = imagesx($this->im);
$h_o = imagesy($this->im);
if($w == null and $h != null) {
$w = $h * $w_o/$h_o;
}elseif ($w != null and $h == null){
$h = $w * $h_o/$w_o;
}
$this->size = null;
$im = $this->create($w,$h,$color);
$w_n = $w;
$h_n = $h;
if($w_o/$h_o > $w/$h) {
$h_n = $w*$h_o/$w_o;
$y = ($h-$h_n)/2;
}elseif ($w_o/$h_o <$w/$h){
$w_n = $h*$w_o/$h_o;
$x = ($w-$w_n)/2;
}
if($lashen) {
$w_n = $w;
$h_n = $h;
$x = 0;
$y = 0;
}
imagecopyresampled($im,$this->im,$x,$y,0,0,$w_n,$h_n,$w_o,$h_o);
//imagedestroy($this->im);
$this->im = $im;
return $im;
}

/*
*在图片上写字
*@param string $str 要写的字符串
*@param array $arg 字符串相关的参数,为一个关联数组,left 为距左边距离,right为距右边距离,left优先,top为距顶部距离,bottom为距底部距离,top优先;angle为角度,color为6位数16进制颜色,touming为文字透明度,font为字体文件
*/
function write($str = '' , $arg = array()) {
$size = $arg['size'] ? $arg['size'] : 20;
$angle = $arg['angle'] ? $arg['angle'] : 0 ;
$color = $arg['color'] ? $arg['color'] : '000000';
$touming = $arg['touming'] ? $arg['touming'] : 100;
$touming = dechex((100-$touming)*127/100);
$color = hexdec($touming.str_replace("#","",$color));
$fOnt= $arg['font'] ? $arg['font'] : 'arial.ttf';
$boxarr = imagettfbbox($size,$angle,$font,$str);
$w = imagesx($this->im);
$h = imagesy($this->im);

$x_l = $x_r = $boxarr[0];
$y_t = $y_b = $boxarr[1];
for($i=0;$i<7;$i = $i+2) {
$x_l = $boxarr[$i] <$x_l ? $boxarr[$i] : $x_l;
$x_r = $boxarr[$i] > $x_r ? $boxarr[$i] : $x_r;
$y_t = $boxarr[$i+1] <$y_t ? $boxarr[$i+1] : $y_t;
$y_b = $boxarr[$i+1] > $y_b ? $boxarr[$i+1] : $y_b;
}
$width = $x_r - $x_l;
$height = $y_b - $y_t;

/*获取精确偏移量*/
$im = $this->create($width*4,$height*4);
$tm = hexdec('7fffffff');
imagettftext($im,$size,$angle,$width*2,$height*2,$color,$font,$str);
for($i=0;$i<$width*4;$i++) {
for($ii=0;$ii<$height*4;$ii++) {
if(imagecolorat($im,$i,$ii) != $tm) {
$x_l = $i;
break(2);
}
}
}
for($i=0;$i<$height*4;$i++) {
for($ii=$x_l;$ii<$width*4;$ii++) {
if(imagecolorat($im,$ii,$i) != $tm) {
$y_t = $i;
break(2);
}
}
}
for($i=$width*4-1;$i>0;$i--) {
for($ii=$y_t;$ii<$height*4;$ii++) {
if(imagecolorat($im,$i,$ii) != $tm) {
$x_r = $i;
break(2);
}
}
}
for($i=$height*4-1;$i>0;$i--) {
for($ii=$x_l;$ii<=$x_r;$ii++) {
if(imagecolorat($im,$ii,$i) != $tm) {
$y_b = $i;
break(2);
}
}
}
$x_off = $x_l - $width*2;
$y_off = $y_b - $height*2;
$width = $x_r - $x_l; //精确宽度
$height = $y_b - $y_t; //精确高度
imagedestroy($im);

if(isset($arg['left'])) {
$x = (int)$arg['left'] - $x_off;
}elseif (isset($arg['right'])){
$x = $w - (int)$arg['right'] - $width - $x_off;
}else {
$x = ($w - $width)/2 - $x_off;
}
if(isset($arg['top'])) {
$y = (int)$arg['top'] - $y_off + $height;
}elseif (isset($arg['bottom'])){
$y = $h - (int)$arg['bottom'] - $y_off;
}else {
$y = ($h + $height)/2 - $y_off;
}

imagettftext($this->im,$size,$angle,$x,$y,$color,$font,$str);
return $this->im;
}

/*
*合并图片(如图片水影)
*@param string/resource $file 图片文件路径或这图片标识符
*@param array $arg 字符串相关的参数,为一个关联数组,left 为距左边距离,right为距右边距离,left优先,top为距顶部距离,bottom为距底部距离,top优先;touming为文字透明度
*/
function merge($file,$arg = array()) {
if(is_file($file)) {
$imc = $this->create($file);
}elseif(gettype($file)=='resource') {
$imc = $file;
}else {
error("没有图片");
}
$touming = $arg['touming'] ? (int)$arg['touming'] : 100 ;
$w = imagesx($this->im);
$h = imagesy($this->im);
$width = imagesx($imc);
$height = imagesy($imc);
if(isset($arg['left'])) {
$x = (int)$arg['left'];
}elseif (isset($arg['right'])){
$x = $w - (int)$arg['right'] - $width;
}else {
$x = ($w - $width)/2;
}
if(isset($arg['top'])) {
$y = (int)$arg['top'];
}elseif (isset($arg['bottom'])){
$y = $h - $height - $arg['bottom'];
}else {
$y = ($h - $height)/2;
}
imagecopymergegray($this->im,$imc,$x,$y,0,0,$width,$height,$touming);
}

/*
*输出图片
*@param string $type
*@param string $filename 要转存的文件路径
*@param int $zhiliang jpeg图片特有的,图像清晰度
*/
function display($type = null,$filename = null,$zhiliang = null) {
if($type == null) {
$type = $this->type ? $this->type : 'jpg';
}
if(($type == 'jpeg' or $type == 'jpg') and $zhiliang == null) {
$type = 'jpeg';
$zhiliang = 100;
}
if($filename == null) {
header('Content-type: image/'.$type);
}
$displayfunc = 'image'.$type;
$displayfunc($this->im,$filename,$zhiliang);
imagedestroy($this->im);
}

function randcolor($a,$b) {
$a = $a>255 ? 255 : (int)$a;
$a = $a<0 ? 0 : (int)$a;
$b = $b>255 ? 255 : (int)$b;
$b = $b<0 ? 0 : (int)$b;
for($i=0;$i<3;$i++) {
$color .= str_pad(dechex(mt_rand($a,$b)), 2, "0", STR_PAD_LEFT);
}
return $color;
}
}

/*
function error($msg,$debug = false) {
$err = new Exception($msg);
$str = "

\n错误:\n".print_r($err->getTrace(),1)."\n
";
if($debug == true) {
file_put_contents(date('Y-m-d').".log",$str);
return $str;
}else{
die($str);
}
}
*/
?>


这是简单的用法实例

代码如下:


$img = new img('a.png');
$m = $img->im;
$im = $img->suolue(100);
$img->im = $m;
$img->suolue(300);
$img->merge($m,array('left'=>0,'top'=>0,'touming'=>60));
$img->merge($im,array('right'=>0,'top'=>0,'touming'=>60));
$img->merge($im,array('left'=>0,'bottom'=>0,'touming'=>60));
$img->merge($im,array('right'=>0,'bottom'=>0,'touming'=>60));

$img->write("春天来了",array('left'=>0,'top'=>0,'size'=>30,'color'=>$img->randcolor(0,180),'angle'=>-45,'font'=>'simfang.ttf','touming'=>80));
$img->write("春天来了",array('left'=>0,'bottom'=>0,'size'=>30,'color'=>$img->randcolor(0,180),'angle'=>45,'font'=>'simfang.ttf','touming'=>80));
$img->write("春天来了",array('right'=>0,'bottom'=>0,'size'=>30,'color'=>$img->randcolor(0,180),'angle'=>-45,'font'=>'simfang.ttf','touming'=>80));
$img->write("春天来了",array('right'=>0,'top'=>0,'size'=>30,'color'=>$img->randcolor(0,180),'angle'=>45,'font'=>'simfang.ttf','touming'=>80));
$img->display("gif");

推荐阅读
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 云原生边缘计算之KubeEdge简介及功能特点
    本文介绍了云原生边缘计算中的KubeEdge系统,该系统是一个开源系统,用于将容器化应用程序编排功能扩展到Edge的主机。它基于Kubernetes构建,并为网络应用程序提供基础架构支持。同时,KubeEdge具有离线模式、基于Kubernetes的节点、群集、应用程序和设备管理、资源优化等特点。此外,KubeEdge还支持跨平台工作,在私有、公共和混合云中都可以运行。同时,KubeEdge还提供数据管理和数据分析管道引擎的支持。最后,本文还介绍了KubeEdge系统生成证书的方法。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 本文介绍了数据库的存储结构及其重要性,强调了关系数据库范例中将逻辑存储与物理存储分开的必要性。通过逻辑结构和物理结构的分离,可以实现对物理存储的重新组织和数据库的迁移,而应用程序不会察觉到任何更改。文章还展示了Oracle数据库的逻辑结构和物理结构,并介绍了表空间的概念和作用。 ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • Python字典推导式及循环列表生成字典方法
    本文介绍了Python中使用字典推导式和循环列表生成字典的方法,包括通过循环列表生成相应的字典,并给出了执行结果。详细讲解了代码实现过程。 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • “你永远都不知道明天和‘公司的意外’哪个先来。”疫情期间,这是我们最战战兢兢的心情。但是显然,有些人体会不了。这份行业数据,让笔者“柠檬” ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 生成对抗式网络GAN及其衍生CGAN、DCGAN、WGAN、LSGAN、BEGAN介绍
    一、GAN原理介绍学习GAN的第一篇论文当然由是IanGoodfellow于2014年发表的GenerativeAdversarialNetworks(论文下载链接arxiv:[h ... [详细]
  • [译]技术公司十年经验的职场生涯回顾
    本文是一位在技术公司工作十年的职场人士对自己职业生涯的总结回顾。她的职业规划与众不同,令人深思又有趣。其中涉及到的内容有机器学习、创新创业以及引用了女性主义者在TED演讲中的部分讲义。文章表达了对职业生涯的愿望和希望,认为人类有能力不断改善自己。 ... [详细]
  • 本文介绍了在Win10上安装WinPythonHadoop的详细步骤,包括安装Python环境、安装JDK8、安装pyspark、安装Hadoop和Spark、设置环境变量、下载winutils.exe等。同时提醒注意Hadoop版本与pyspark版本的一致性,并建议重启电脑以确保安装成功。 ... [详细]
  • Android Studio Bumblebee | 2021.1.1(大黄蜂版本使用介绍)
    本文介绍了Android Studio Bumblebee | 2021.1.1(大黄蜂版本)的使用方法和相关知识,包括Gradle的介绍、设备管理器的配置、无线调试、新版本问题等内容。同时还提供了更新版本的下载地址和启动页面截图。 ... [详细]
author-avatar
拍友2502926823
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有