首页 / PHP / php缩略图类(附调用示例)
php缩略图类(附调用示例)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php缩略图类(附调用示例),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3422字,纯文字阅读大概需要5分钟。
内容图文
![php缩略图类(附调用示例)](/upload/InfoBanner/zyjiaocheng/164/b0227944a0014e5a9f6e3b1480291851.jpg)
本文介绍下,php实现的一个缩略图类,支持加载图片文件与加载图片字符串,按比例拉伸等,代码后面有调用示例供参考。
分享个php缩略图类,可以实现如下的功能: 1,支持加载图片文件和加载图片字符串 2,可以将缩略图输出到浏览器和保持缩略图文件 3,支持gif,png,jpeg类型的缩略 4,可以设定是否按比例来拉伸代码如下: maxWidth = $maxWidth; $this->maxHeight = $maxHeight; $this->scale = $scale; $this->inflate = $inflate; $this->types = array( 'image/jpeg', 'image/png', 'image/gif' ); //加载MIME类型图像的函数名称 $this->imgLoaders = array( 'image/jpeg' => 'imagecreatefromjpeg', 'image/png' => 'imagecreatefrompng', 'image/gif' => 'imagecreatefromgif' ); //储存创建MIME类型图片的函数名称 $this->imgCreators = array( 'image/jpeg' => 'imagejpeg', 'image/png' => 'imagepng', 'image/gif' => 'imagegif' ); } /** * 文件方式加载图片 * @param string $image 源图片 * @return bool */ public function loadFile($image){ if(!$dims = @getimagesize($image)){ trigger_error("源图片不存在"); } if(in_array($dims['mime'], $this->types)){ $loader = $this->imgLoaders[$dims['mime']]; $this->source = $loader($image); $this->sourceWidth = $dims[0]; $this->sourceHeight = $dims[1]; $this->sourceMime = $dims['mime']; $this->initThumb(); return TRUE; }else{ trigger_error('不支持'.$dims['mime']."图片类型"); } } /** * 字符串方式加载图片 * @param string $image 字符串 * @param string $mime 图片类型 * @return type */ public function loadData($image,$mime){ if(in_array($mime, $this->types)){ if($this->source = @imagecreatefromstring($image)){ $this->sourceWidth = imagesx($this->source); $this->sourceHeight = imagesy($this->source); $this->sourceMime = $mime; $this->initThumb(); return TRUE; }else{ trigger_error("不能从字符串加载图片"); } }else{ trigger_error("不支持".$mime."图片格式"); } } /** * 生成缩略图 * @param string $file 文件名。如果不为空则储存为文件,否则直接输出到浏览器 */ public function buildThumb($file = null){ $creator = $this->imgCreators[$this->sourceMime]; if(isset($file)){ return $creator($this->thumb,$file); }else{ return $creator($this->thumb); } } /** * 处理缩放 */ public function initThumb(){ if($this->scale){ if($this->sourceWidth > $this->sourceHeight){ $this->thumbWidth = $this->maxWidth; $this->thumbHeight = floor($this->sourceHeight*($this->maxWidth/$this->sourceWidth)); }elseif($this->sourceWidth < $this->sourceHeight){ $this->thumbHeight = $this->maxHeight; $this->thumbWidth = floor($this->sourceWidth*($this->maxHeight/$this->sourceHeight)); }else{ $this->thumbWidth = $this->maxWidth; $this->thumbHeight = $this->maxHeight; } } $this->thumb = imagecreatetruecolor($this->thumbWidth, $this->thumbHeight); if($this->sourceWidth <= $this->maxWidth && $this->sourceHeight <= $this->maxHeight && $this->inflate == FALSE){ $this->thumb = $this->source; }else{ imagecopyresampled($this->thumb, $this->source, 0, 0, 0, 0, $this->thumbWidth, $this->thumbHeight, $this->sourceWidth, $this->sourceHeight); } } public function getMine(){ return $this->sourceMime; } public function getThumbWidth(){ return $this->thumbWidth; } public function getThumbHeight(){ return $this->thumbHeight; } } /** * 缩略图类调用示例(文件) */ $thumb = new Thumbnail(200, 200); $thumb->loadFile('wap.gif'); header('Content-Type:'.$thumb->getMine()); $thumb->buildThumb(); /** * 缩略图类调用示例(字符串) */ $thumb = new Thumbnail(200, 200); $image = file_get_contents('wap.gif'); $thumb->loadData($image, 'image/jpeg'); $thumb->buildThumb('wap_thumb.gif'); ?> |
内容总结
以上是互联网集市为您收集整理的php缩略图类(附调用示例)全部内容,希望文章能够帮你解决php缩略图类(附调用示例)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。