侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

php常用图片处理类

php  /  管理员 发布于 7年前   239

本文为大家分享的php常用图片处理类,供大家参考学习,具体内容如下

types=$types;$this->image=$this->imagesources($imageaddress);$this->width=$this->imagesizex();$this->height=$this->imagesizey();$this->value1=$value1;$this->value2=$value2;$this->endaddress=$endaddress;}function outimage(){ //根据传入type值的不同,输出不同的功能switch($this->types){case 1:$this->scaling();break;case 2:$this->clipping();break;case 3:$this->imagewater();break;default:return false;}}private function imagewater(){ //http://www.hzhuti.com 加图片水印功能//用函数获取水印文件的长和宽$imagearrs=$this->getimagearr($this->value1);//调用函数计算出水印加载的位置$positionarr=$this->position($this->value2, $imagearrs[0], $imagearrs[1]);//加水印imagecopy($this->image, $this->imagesources($this->value1), $positionarr[0], $positionarr[1], 0, 0, $imagearrs[0], $imagearrs[1]);//调用输出方法保存$this->output($this->image);}private function clipping(){ //图片裁剪功能//将传进来的值分别赋给变量list($src_x, $src_y)=explode(",", $this->value1);list($dst_w, $dst_h)=explode(",", $this->value2);if($this->width < $src_x+$dst_w || $this->height < $src_y+$dst_h){ //这个判断就是限制不能截取到图片外面去return false;}//创建新的画布资源$newimg=imagecreatetruecolor($dst_w, $dst_h);//进行裁剪imagecopyresampled($newimg, $this->image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $dst_w, $dst_h);//调用输出方法保存$this->output($newimg);}private function scaling(){ //图片缩放功能//获取等比缩放的宽和高$this-> proimagesize();//根据参数进行缩放,并调用输出函数保存处理后的文件$this->output($this->imagescaling());}private function imagesources($imgad){ //获取图片类型并打开图像资源$imagearray=$this->getimagearr($imgad);switch($imagearray[2]){case 1://gif$this->imgtype=1;$img=imagecreatefromgif($imgad);break;case 2://jpeg$this->imgtype=2;$img=imagecreatefromjpeg($imgad);break;case 3://png$this->imgtype=3;$img=imagecreatefrompng($imgad);break;default:return false;}return $img;}private function imagesizex(){ //获得图片宽度return imagesx($this->image);}private function imagesizey(){ //获取图片高度return imagesy($this->image);}private function proimagesize(){ //计算等比缩放的图片的宽和高if($this->value1 && ($this->width < $this->height)) { //等比缩放算法$this->value1=round(($this->value2/ $this->height)*$this->width);}else{$this->value2=round(($this->value1/ $this->width) * $this->height);}}private function imagescaling(){//图像缩放功能,返回处理后的图像资源$newimg=imagecreatetruecolor($this->value1, $this->value2);$tran=imagecolortransparent($this->image);//处理透明算法if($tran >= 0 && $tran < imagecolorstotal($this->image)){$tranarr=imagecolorsforindex($this->image, $tran);$newcolor=imagecolorallocate($newimg, $tranarr['red'], $tranarr['green'], $tranarr['blue']);imagefill($newimg, 0, 0, $newcolor);imagecolortransparent($newimg, $newcolor);}imagecopyresampled($newimg, $this->image, 0, 0, 0, 0, $this->value1, $this->value2, $this->width, $this->height);return $newimg;}private function output($image){//输出图像switch($this->imgtype){case 1:imagegif($image, $this->endaddress);break;case 2:imagejpeg($image, $this->endaddress);break;case 3:imagepng($image, $this->endaddress);break;default:return false;}}private function getimagearr($imagesou){//返回图像属性数组方法return getimagesize($imagesou);}private function position($num, $width, $height){//根据传入的数字返回一个位置的坐标,$width和$height分别代表插入图像的宽和高switch($num){case 1:$positionarr[0]=0;$positionarr[1]=0;break;case 2:$positionarr[0]=($this->width-$width)/2;$positionarr[1]=0;break;case 3:$positionarr[0]=$this->width-$width;$positionarr[1]=0;break;case 4:$positionarr[0]=0;$positionarr[1]=($this->height-$height)/2;break;case 5:$positionarr[0]=($this->width-$width)/2;$positionarr[1]=($this->height-$height)/2;break;case 6:$positionarr[0]=$this->width-$width;$positionarr[1]=($this->height-$height)/2;break;case 7:$positionarr[0]=0;$positionarr[1]=$this->height-$height;break;case 8:$positionarr[0]=($this->width-$width)/2;$positionarr[1]=$this->height-$height;break;case 9:$positionarr[0]=$this->width-$width;$positionarr[1]=$this->height-$height;break;case 0:$positionarr[0]=rand(0, $this->width-$width);$positionarr[1]=rand(0, $this->height-$height);break;}return $positionarr;}function __destruct(){imagedestroy($this->image);}}?>

以上就是本文的全部内容,希望对大家学习php程序设计有所帮助。

您可能感兴趣的文章:

  • Ajax+PHP边学边练 之五 图片处理
  • php图片处理:加水印、缩略图的实现(自定义函数:watermark、thumbnail)
  • PHP图片处理类 phpThumb参数用法介绍
  • php多功能图片处理类分享(php图片缩放类)
  • PHPThumb图片处理实例
  • PHP图片处理之图片旋转和图片翻转实例
  • PHP图片处理之使用imagecopy函数添加图片水印实例
  • PHP图片处理之使用imagecopyresampled函数裁剪图片例子
  • PHP图片处理之使用imagecopyresampled函数实现图片缩放例子
  • PHP图片处理之图片背景、画布操作


  • 上一条:
    php文件缓存方法总结
    下一条:
    PHP匿名函数和use子句用法实例
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(95个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客