-
php图片处理类,生成缩略图,增加水印,获取图片信息
本文章提供这款图片处理类,他可以做的事情是把图片生成缩略图,可以给图片增加水印以及获取图片信息,算是比较实用代码又简洁的函数,实例代码如下:
- class image
- {
- public $info=array();
- function __construct()
- {
- !extension_loaded('gd') && exit("www.111cn.net提示:服务器环境不支持gd库");
- return true;
- }
- function image()
- {
- $this->__construct();
- }
- function thumb($image,$thumb_width=300,$thumb_height=225)
- {
- $info=$this->info($image);
- $scale=min(1,min($thumb_width/$info['width'],$thumb_height/$info['height'])); //按比例缩放
- $thumb_width=intval($info['width']*$scale);
- $thumb_height=intval($info['height']*$scale);
- $createfunc='imagecreatefrom'.($info['type']=='jpg'?'jpeg':$info['type']);
- $im=$createfunc($image);
- $thumb_im=$info['type']!='gif' && function_exists('imagecreatetruecolor')?imagecreatetruecolor($thumb_width,$thumb_height):imagecreate($thumb_width,$thumb_height);
- imagecopyresampled($thumb_im,$im,0,0,0,0,$thumb_width,$thumb_height,$info['width'],$info['height']);
- if($info['type']=='gif' || $info['type']=='png')
- {
- $bgcolor=imagecolorallocate($thumb_im,0,255,0);
- imagecolortransparent($thumb_im,$bgcolor);
- }
- $imagefunc='image'.($info['type']=='jpg'?'jpeg':$info['type']);
- $thumbname='thumb_'.$info['name'].'.'.$info['type'];
- $imagefunc($thumb_im,$info['path'].$thumbname);
- imagedestroy($im);
- imagedestroy($thumb_im);
- return $info['path'].$thumbname;
- }
- function watermark($image,$pos=9,$watermarkimg='images/watermark.gif',$pct=65,$text='',$w_font=5,$w_color='#ff0000')
- {
- $imageinfo=$this->info($image);
- $source_w=$imageinfo['width'];
- $source_h=$imageinfo['height'];
- $imagecreatefunc='imagecreatefrom'.($imageinfo['type']=='jpg'?'jpeg':$imageinfo['type']);
- $im=$imagecreatefunc($image);
- if(!emptyempty($watermarkimg) && file_exists($watermarkimg)) //添加图片水印
- {
- $iswaterimage=true;
- $watermarkinfo=$this->info($watermarkimg);
- $width=$watermarkinfo['width'];
- $height=$watermarkinfo['height'];
- $watermarkcreatefunc='imagecreatefrom'.($watermarkinfo['type']=='jpg'?'jpeg':$watermarkinfo['type']);
- $watermark_im=$watermarkcreatefunc($watermarkimg);
- }
- else //添加文字水印
- {
- $iswaterimage=false;
- if(!emptyempty($w_color) && strlen($w_color)==7)
- {
- $r=hexdec(substr($w_color,1,2));
- $g=hexdec(substr($w_color,3,2));
- $b=hexdec(substr($w_color,5,2));
- }
- $temp = imagettfbbox(ceil($w_font*2.5), 0, 'fonts/alger.ttf', $text);
- $width = $temp[2] - $temp[6];
- $height = $temp[3] - $temp[7];
- unset($temp);
- }
- switch($pos)
- {
- case 0:
- $wx = mt_rand(0,($source_w - $width));
- $wy = mt_rand(0,($source_h - $height));
- break;
- case 1:
- $wx = 5;
- $wy = 5;
- break;
- case 2:
- $wx = ($source_w - $width) / 2;
- $wy = 5;
- break;
- case 3:
- $wx = $source_w - $width-5;
- $wy = 5;
- break;
- case 4:
- $wx = 5;
- $wy = ($source_h - $height) / 2;
- break;
- case 5:
- $wx = ($source_w - $width) / 2;
- $wy = ($source_h - $height) / 2;
- break;
- case 6:
- $wx = $source_w - $width-5;
- $wy = ($source_h - $height) / 2;
- break;
- case 7:
- $wx = 5;
- $wy = $source_h - $height-5;
- break;
- case 8:
- $wx = ($source_w - $width) / 2;
- $wy = $source_h - $height-5;
- break;
- default:
- $wx = $source_w - $width-5;
- $wy = $source_h - $height-5;
- break;
- }
- if($iswaterimage)
- {
- if($imageinfo['type'] == 'png') {
- imagecopy($im, $watermark_im, $wx, $wy, 0, 0, $width, $height);
- } else {
- imagecopymerge($im, $watermark_im, $wx, $wy, 0, 0, $width, $height, $pct);
- }
- }
- else
- {
- imagestring($im,$w_font,$wx,$wy,$text,imagecolorallocate($im,$r,$g,$b));
- }
- $imagefunc='image'.($imageinfo['type']=='jpg'?'jpeg':$imageinfo['type']);
- $imagefunc($im,$image);
- imagedestroy($im);
- return true;
- }
- function info($image)
- {//开源代码phpfensi.com
- $info=array();
- $info['size']=filesize($image);
- $imageinfo=getimagesize($image);
- $info['width']=$imageinfo[0];
- $info['height']=$imageinfo[1];
- $info['width_height']=$imageinfo[3];
- $info['mime']=$imageinfo['mime'];
- unset($imageinfo);
- $imageinfo=pathinfo($image);
- $info['path']=$imageinfo['dirname'].'/';
- $info['type']=strtolower($imageinfo['extension']); //图片类型,不含'.'
- $info['name']=$imageinfo['filename'];
- unset($imageinfo,$name);
- $this->info=$info;
- return $info;
- }
- }
原文链接:http://www.phpfensi.com/php/20140819/4454.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式