0

0

php图片处理类(生成缩略图,增加水印,获取图片信息)_PHP教程

php中文网

php中文网

发布时间:2016-07-13 10:45:38

|

1042人浏览过

|

来源于php中文网

原创

php教程图片处理类(生成缩略图,增加水印,获取图片信息)
本文章提供这款图片处理类,他可以做的事情有把图片生成缩略图,可能给图片增加水印以及获取图片信息,算是比较实用代码又简洁的函数*/

class image {  public $info=array();  function __construct()  {   !extension_loaded('gd') && exit("www.bKjia.c0m提示:服务器环境不支持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(!empty($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(!empty($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)  {   $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;  } }

 

北方供求商业完整版
北方供求商业完整版

专业的供求、二手、分类信息发布系统,功能介绍:HTML生成功能:发布信息自动生成相关列表,不必全部生成多功能图文编辑器:让你的信息更灵活多变,可添加图片水印强大的管理员权限分配:可管理某一个分类,或地区的信息强大JS功能:方便远程调用个人开店,企业黄页一步到位:每个会员都拥有自己的店铺,企业会员可加入到企业黄页集成支付宝在线支付v3.5 Build 0717更新修正了会员编辑信息的一处BUG,以前

下载

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/633005.htmlTechArticlephp教程图片处理类(生成缩略图,增加水印,获取图片信息) 本文章提供这款图片处理类,他可以做的事情有把图片生成缩略图,可能给图片增...

相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

php

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
nginx部署php项目教程汇总
nginx部署php项目教程汇总

本专题整合了nginx部署php项目教程汇总,阅读专题下面的文章了解更多详细内容。

0

2026.01.13

PHP 表单处理与文件上传安全实战
PHP 表单处理与文件上传安全实战

本专题聚焦 PHP 在表单处理与文件上传场景中的实战与安全问题,系统讲解表单数据获取与校验、XSS 与 CSRF 防护、文件类型与大小限制、上传目录安全配置、恶意文件识别以及常见安全漏洞的防范策略。通过贴近真实业务的案例,帮助学习者掌握 安全、规范地处理用户输入与文件上传的完整开发流程。

5

2026.01.13

PPT交互图表教程大全
PPT交互图表教程大全

本专题整合了PPT交互图表相关教程汇总,阅读专题下面的文章了解更多详细内容。

55

2026.01.12

Java 项目构建与依赖管理(Maven / Gradle)
Java 项目构建与依赖管理(Maven / Gradle)

本专题系统讲解 Java 项目构建与依赖管理的完整体系,重点覆盖 Maven 与 Gradle 的核心概念、项目生命周期、依赖冲突解决、多模块项目管理、构建加速与版本发布规范。通过真实项目结构示例,帮助学习者掌握 从零搭建、维护到发布 Java 工程的标准化流程,提升在实际团队开发中的工程能力与协作效率。

21

2026.01.12

c++主流开发框架汇总
c++主流开发框架汇总

本专题整合了c++开发框架推荐,阅读专题下面的文章了解更多详细内容。

135

2026.01.09

c++框架学习教程汇总
c++框架学习教程汇总

本专题整合了c++框架学习教程汇总,阅读专题下面的文章了解更多详细内容。

66

2026.01.09

学python好用的网站推荐
学python好用的网站推荐

本专题整合了python学习教程汇总,阅读专题下面的文章了解更多详细内容。

140

2026.01.09

学python网站汇总
学python网站汇总

本专题整合了学python网站汇总,阅读专题下面的文章了解更多详细内容。

13

2026.01.09

python学习网站
python学习网站

本专题整合了python学习相关推荐汇总,阅读专题下面的文章了解更多详细内容。

19

2026.01.09

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PHP课程
PHP课程

共137课时 | 8.5万人学习

JavaScript ES5基础线上课程教学
JavaScript ES5基础线上课程教学

共6课时 | 6.9万人学习

PHP新手语法线上课程教学
PHP新手语法线上课程教学

共13课时 | 0.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号