简单写了一个php的图像处理类库,虽然功能比较少,但是目前也没用到太高级的,以后用到了再填吧,或者哪位给点建议加上什么功能,或者有什么需求可以跟我说,我有时间加上,如果哪位对这个类库进行了扩展的话,还麻烦拿出来大家分享一下,代码现在是能用就行,考虑的东西不是很多,有什么更好的建议请告诉我,谢谢
img.php
<?php
/**
* created by phpstorm.
* user: mction
* date: 2015/5/14 0014
* time: 15:36
* 简单的图像类库,本类中所有相对路径均基于网站根目录,如需修改,则修改常量__webroot__即可
* 功能:指定文字内容创建图片(不支持中文)、创建验证码图片、创建缩略图、其他功能待续
* 方法:
* style(array $options) 设置图片样式,每次设定前将重置为默认样式
* create_img_png() 创建png图像,相关属性由style指定
* create_img_jpeg() 创建jpeg图像,相关属性由style指定
* create_verify() 创建验证码图像,相关属性由style指定
* get_verify() 获得创建的验证码值,md5加密
* load_img(string $filepath) 加载图像,创建图像源,供其他方法调用源,filepath为图像相对路径
* create_thumb(string $filename,string $filepath) 创建由load_img()加载的图像的缩略图,filename为保存后的图像前缀,filepath为保存图像的相对路径,不包含文件名(例:/uploads/images/thumb/)
*/
if(!defined("__webroot__")) define("__webroot__",$_server['document_root']);
class img {
protected $_img; //图片源
protected $_fileimg; //加载的图片源
protected $_fileinfo; //加载的图片的信息数组
protected $_picinfo; //加载的图片的宽高等信息数组
protected $_rand = 'abcdefghijkmnopqrstuvwxyzabcdefghjklmnopqrstuvwxyz1234567890'; //随机因子
protected $_code = ''; //验证码
public $width = 300;//图片默认宽
public $height = 80; //图片默认高
public $backgroundcolor = "000000";
public $font = "/phps/public/font/arialnb.ttf"; //默认字体
public $fontsize = 16; //默认字体字号
public $fontcolor = "ffffff"; //默认字体颜色
public $content = "test word";
public $align = "left";
public $codes = 4; //验证码个数
public $line = 6; //干扰线条的个数
public $loaderr = ''; //错误信息
//public function __construct(){}
/** 设置图片属性
* @param array $options 属性数组
* @return $this 返回对象
*/
public function style($options){
$this -> _re_set();
foreach($options as $k=>$v){
if(in_array($k,array('width','height','backgroundcolor','font','fontsize','fontcolor','content','align','codes','line','snow'))){
if($k == "backgroundcolor" || $k == "fontcolor"){
if(preg_match("#([a-za-z0-9]{6})#",$v)) $this -> $k = $v;
}else{
$this -> $k = $v;
}
}
}
return $this;
}
/**
* 重置属性,不提供外部访问
*/
protected function _re_set(){
$this -> width = 100;
$this -> height = 30;
$this -> backgroundcolor = "000000";
$this -> font = "/phps/public/font/arialnb.ttf";
$this -> fontsize = 16;
$this -> fontcolor = "ffffff";
$this -> align = "left";
$this -> codes =4;
$this -> line = 6;
}
/**
* 创建图像源、添加背景、创建图像
* @param bool $bgc 指定是否创建背景色及矩形块
*/
protected function _create_img_gb($bgc = true){
$this -> _img = imagecreatetruecolor($this -> width,$this -> height); //创建背景源
if($bgc){
preg_match("#([a-za-z0-9]{2})([a-za-z0-9]{2})([a-za-z0-9]{2})#",$this -> backgroundcolor,$colorarr); //将颜色值分隔成三组16位进制数
$color = imagecolorallocate($this -> _img,hexdec($colorarr[1]),hexdec($colorarr[2]),hexdec($colorarr[3])); //给img图像源添加背景色
imagefilledrectangle($this -> _img,0,$this -> height,$this -> width,0,$color); //创建图像
}
}
/**
* 创建随机验证码
*/
protected function _create_code(){
$len = strlen($this -> _rand) - 1;
for($i = 0;$i < $this -> codes;$i++){
$this -> _code .= $this -> _rand[mt_rand(0,$len)];
}
}
/**
* 向图像中写入字符串,暂不支持中文
*/
protected function _write_text(){
$fontwidth = imagefontwidth($this -> fontsize); //获取字号的一个字符的宽度
preg_match_all('/(.)/us', $this -> content, $textarr); //将内容分隔成数组一遍统计个数
$fontheight = imagefontheight($this -> fontsize); //获取字号的高度
$x = ceil(($this -> width - ($fontwidth * count($textarr[0]))) / 2); //设置x轴距左边距的距离
$y = ceil(($this -> height + $fontheight) / 2); //设置y轴距上边距的距离
preg_match("#([a-za-z0-9]{2})([a-za-z0-9]{2})([a-za-z0-9]{2})#",$this -> fontcolor,$colorarr);
$color = imagecolorallocate($this -> _img,hexdec($colorarr[1]),hexdec($colorarr[2]),hexdec($colorarr[3])); //设置文字颜色
imagettftext($this -> _img,$this -> fontsize,0,$x,$y,$color,__webroot__.$this -> font,$this -> content); //写入内容
}
/**
* 向图像中写入验证码
*/
protected function _write_code(){
$_x = $this -> width / $this -> codes; //设置宽高比
for($i = 0;$i < $this -> codes;$i++){ //循环codes次,每次生成一位验证码值
$color = imagecolorallocate($this -> _img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156)); //随机生成验证码值的颜色
imagettftext($this -> _img,$this -> fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this -> height / 1.3,$color,__webroot__.$this -> font,$this -> _code[$i]); //生成一位验证码值
}
}
/**
* 向图像中写入干扰线条
*/
protected function _write_line() { //生成干扰线条
for ($i=0;$i < $this -> line;$i++) {
$color = imagecolorallocate($this -> _img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imageline($this -> _img,mt_rand(0,$this -> width),mt_rand(0,$this -> height),mt_rand(0,$this -> width),mt_rand(0,$this -> height),$color);
}
}
/**
* 设置图像类型为jpeg
*/
protected function _img_jpeg(){
header('content-type:image/jpeg');
imagejpeg($this -> _img);
imagedestroy($this -> _img);
}
/**
* 设置图像类型为png
*/
protected function _img_png(){
header('content-type:image/png');
imagepng($this -> _img);
imagedestroy($this -> _img);
}
/**
* 创建jpeg的字符串图像
*/
public function create_img_jpg(){
$this -> _create_img_gb(true);
$this -> _write_text();
$this -> _img_jpeg();
}
/**
* 创建png的字符串图像
*/
public function create_img_png(){
$this -> _create_img_gb(true);
$this -> _write_text();
$this -> _img_png();
}
/**
* 创建验证码的png图像
*/
public function create_verify(){
$this -> backgroundcolor = '';
for($i = 0;$i < 3;$i++){
$this -> backgroundcolor .= dechex(mt_rand(20,155));
}
$this -> _create_img_gb(true);
$this -> _create_code();
$this -> _write_line();
$this -> _write_code();
$this -> _img_png();
}
/**
* 外部获取md5加密后的验证码
* @return string
*/
public function get_verify(){
return md5($this -> _code);
}
/**
* 加载一个图像文件,并获取图像相关信息
* @param string $filepath 图像相对路径地址
* @return $this|bool 成功返回对象,否则返回false
*/
public function load_img($filepath){
$filepath = __webroot__.$filepath;
if(!is_file($filepath)){
$this -> loaderr = "路径错误,文件不存在";
return false;
}
$this -> _picinfo = getimagesize($filepath);
$this -> _fileinfo = pathinfo($filepath);
switch($this -> _picinfo[2]){
case 1:$this ->_fileimg = imagecreatefromgif($filepath);break;
case 2:$this ->_fileimg = imagecreatefromjpeg($filepath);break;
case 3:$this ->_fileimg = imagecreatefrompng($filepath);break;
default:$this -> loaderr = "类型错误,不支持的图片类型";return false;
}
return true;
}
/**
* 创建缩略图
* @param string $filename 保存的图片名称前缀
* @param string $filepath 保存图片的相对路径
* @return mixed 返回生成的图片的信息数组
*/
public function create_thumb($filename,$filepath){
$savepath = __webroot__.$filepath;
if(!file_exists($savepath)){
mkdir($savepath,0777,true);
}
$filename = $filename.date("ymdhis").rand(100,999).'.'.$this -> _fileinfo['extension'];
$filepath = $filepath.$filename;
$savepath = $savepath.$filename;
$this -> _create_img_gb(false);
imagecopyresampled($this -> _img,$this -> _fileimg,0,0,0,0,$this -> width,$this -> height,$this -> _picinfo[0],$this -> _picinfo[1]);
switch($this -> _picinfo[2]){
case 1:imagegif($this -> _img,$savepath);break;
case 2:imagejpeg($this -> _img,$savepath);break;
case 3:imagepng($this -> _img,$savepath);break;
}
$fileinfo['filename'] = $filename;
$fileinfo['filepath'] = $filepath;
return $fileinfo;
}
}
使用示例
$img = new img();
$options['width'] = 300;
$options['height'] = 100;
$options['content'] = "test create img";
$options['fontcolor'] = "ff0000";
$options['backgroundcolor'] = "aaaaaa";
$img -> style($options) -> create_img_jpg();
if($img -> load_img("/public/images/ad1.png")){
$fileinfo = $img -> style(array('width'=>30,'height'=>30)) -> create_thumb("thumb","/uploads/images/");
var_dump($fileinfo);
}else{
die("加载图像失败,".$img -> loaderr);
}
以上所述就是本文的全部内容了,希望大家能够喜欢。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!