您好,欢迎访问一九零五行业门户网

PHP图像处理类库及演示分享,php图像处理类库_PHP教程

php图像处理类库及演示分享,php图像处理类库简单写了一个php的图像处理类库,虽然功能比较少,但是目前也没用到太高级的,以后用到了再填吧,或者哪位给点建议加上什么功能,或者有什么需求可以跟我说,我有时间加上,如果哪位对这个类库进行了扩展的话,还麻烦拿出来大家分享一下,代码现在是能用就行,考虑的东西不是很多,有什么更好的建议请告诉我,谢谢
img.php
_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 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 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 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 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);}
转载自:http://www.aspnetjia.com
http://www.bkjia.com/phpjc/1094588.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/1094588.htmltecharticlephp图像处理类库及演示分享,php图像处理类库 简单写了一个php的图像处理类库,虽然功能比较少,但是目前也没用到太高级的,以后用到了...
其它类似信息

推荐信息