首页 / PHP / php 图片调整大小 封装类【转载】
php 图片调整大小 封装类【转载】
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php 图片调整大小 封装类【转载】,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4232字,纯文字阅读大概需要7分钟。
内容图文
1 <?php 2class ImageResize 3{ 4private$image; 5private$img_des; 6private$image_type; 7private$permissions; 8private$compression; 9 10/** 11 * 构造函数 12 * @param string $img_src 源图片 13 * @param string $img_des 保存图片对象 14 * @param int $compression 压缩比率,范围0-100(0:压缩比最高,100:压缩比最低) 15 * @param null $permissions 改变文件模式 16 * mode 参数由 4 个数字组成: 17 * 第一个数字永远是 0 18 * 第二个数字规定所有者的权限 19 * 第二个数字规定所有者所属的用户组的权限 20 * 第四个数字规定其他所有人的权限 21 * 1 - 执行权限,2 - 写权限,4 - 读权限 22*/ 23function __construct($img_src, $img_des = NULL, $compression = 75, $permissions = null) 24 { 25$image_info = getimagesize($img_src); 26 27$this->img_des = $img_des; 28$this->image_type = $image_info[2]; 29$this->compression = $compression; 30$this->permissions = $permissions; 31 32if ($this->image_type == IMAGETYPE_JPEG) { 33$this->image = ImageCreateFromJPEG($img_src); 34 } elseif ($this->image_type == IMAGETYPE_GIF) { 35$this->image = ImageCreateFromGIF($img_src); 36 } elseif ($this->image_type == IMAGETYPE_PNG) { 37$this->image = ImageCreateFromPNG($img_src); 38 } 39 } 40 41/** 42 * 保存图片 43*/ 44function save() 45 { 46if ($this->image_type == IMAGETYPE_JPEG) { 47 imagejpeg($this->image, $this->img_des, $this->compression); 48 } elseif ($this->image_type == IMAGETYPE_GIF) { 49 imagegif($this->image, $this->img_des); 50 } elseif ($this->image_type == IMAGETYPE_PNG) { 51 imagepng($this->image, $this->img_des); 52 } 53 54if ($this->permissions != null) { 55chmod($this->image, $this->compression); 56 } 57 } 58 59/** 60 * 做为图片流直接输出 61*/ 62function output() 63 { 64if ($this->image_type == IMAGETYPE_JPEG) { 65header(‘Content-Type: image/jpg‘); 66 imagejpeg($this->image); 67 } elseif ($this->image_type == IMAGETYPE_GIF) { 68header(‘Content-Type: image/gif‘); 69 imagegif($this->image); 70 } elseif ($this->image_type == IMAGETYPE_PNG) { 71header(‘Content-Type: image/png‘); 72 imagepng($this->image); 73 } 74 } 75 76/** 77 * 获取图片宽度 78 * @return int 图片宽度 79*/ 80function getWidth() 81 { 82return imagesx($this->image); 83 } 84 85/* 86 * 获取图片高度 87 * @return int 图片高度 88*/ 89function getHeight() 90 { 91return imagesy($this->image); 92 } 93 94/** 95 * 按照固定高度缩放图片 96 * @param $height 需要改变大小的高度 97*/ 98function resizeToHeight($height) 99 { 100$ratio = $height / $this->getHeight(); 101$width = $this->getWidth() * $ratio; 102$this->resize($width, $height); 103 } 104105/** 106 * 按照固定宽度缩放图片 107 * @param $width 指定宽度 108*/109function resizeToWidth($width) 110 { 111$ratio = $width / $this->getWidth(); 112$height = $this->getheight() * $ratio; 113$this->resize($width, $height); 114 } 115116/** 117 * 等比缩放图片 118 * @param int $scale 缩放比例 119*/120function scale($scale) 121 { 122$width = $this->getWidth() * $scale / 100; 123$height = $this->getheight() * $scale / 100; 124$this->resize($width, $height); 125 } 126127128/** 129 * 指定宽度和高度缩放图片 130 * @param int $width 缩放宽度 131 * @param int $height 缩放高度 132 * @return 缩放后图片对象 133*/134function resize($width, $height) 135 { 136$new_image = imagecreatetruecolor($width, $height); 137138if ($this->image_type == IMAGETYPE_GIF || $this->image_type == IMAGETYPE_PNG) { 139$current_transparent = imagecolortransparent($this->image); 140if ($current_transparent != -1) { 141$transparent_color = imagecolorsforindex($this->image, $current_transparent); 142$current_transparent = imagecolorallocate($new_image, $transparent_color[‘red‘], $transparent_color[‘green‘], $transparent_color[‘blue‘]); 143 imagefill($new_image, 0, 0, $current_transparent); 144 imagecolortransparent($new_image, $current_transparent); 145 } elseif ($this->image_type == IMAGETYPE_PNG) { 146 imagealphablending($new_image, false); 147$color = imagecolorallocatealpha($new_image, 0, 0, 0, 127); 148 imagefill($new_image, 0, 0, $color); 149 imagesavealpha($new_image, true); 150 } 151 } 152153 imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); 154$this->image = $new_image; 155 } 156} 157158 ?>
使用方法:
1 $image = new ImageResize($tmp_image_name, NULL, 100); 2$image->output();
原文:http://www.cnblogs.com/itafter/p/4120729.html
内容总结
以上是互联网集市为您收集整理的php 图片调整大小 封装类【转载】全部内容,希望文章能够帮你解决php 图片调整大小 封装类【转载】所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。