注:此功能依赖GD2图形库

最近要用php生成缩略图,在网上找了一下,发现了这篇文章:PHP生成图片缩略图

试用了一下后,发现有这样几个问题:

1、png图片生成的缩略图是jpg格式的

2、png图片生成的缩略图没有了透明(半透明)效果(填充了黑色背景)

3、代码语法比较老

因此,在这个版本的基础上简单修改优化了一下。

PHP生成缩略图类

<?php/** desc: Resize Image(png, jpg, gif)* author: 十年后的卢哥哥(http://www.cnblogs.com/lurenjiashuo/)* date: 2014.11.13* base from: http://www.oschina.net/code/snippet_5189_2491*/class ResizeImage {//图片类型private $type;//实际宽度private $width;//实际高度private $height;//改变后的宽度private $resize_width;//改变后的高度private $resize_height;//是否裁图private $cut;//源图象private $srcimg;//目标图象地址private $dstimg;//临时创建的图象private $im;function __construct($imgPath, $width, $height, $isCut, $savePath) {$this->srcimg = $imgPath;$this->resize_width = $width;$this->resize_height = $height;$this->cut = $isCut;//图片的类型$this->type = strtolower(substr(strrchr($this->srcimg,"."),1));//初始化图象$this->initi_img();//目标图象地址$this -> dst_img($savePath);//--$this->width = imagesx($this->im);$this->height = imagesy($this->im);//生成图象$this->newimg();ImageDestroy ($this->im);}private function newimg() {//改变后的图象的比例$resize_ratio = ($this->resize_width)/($this->resize_height);//实际图象的比例$ratio = ($this->width)/($this->height);if($this->cut) {//裁图$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);if($this->type=="png") {imagefill($newimg, 0, 0, imagecolorallocatealpha($newimg, 0, 0, 0, 127));}if($ratio>=$resize_ratio) {//高度优先imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);} else {//宽度优先imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));}} else {//不裁图if($ratio>=$resize_ratio) {$newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);if($this->type=="png") {imagefill($newimg, 0, 0, imagecolorallocatealpha($newimg, 0, 0, 0, 127));}imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);} else {$newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);if($this->type=="png") {imagefill($newimg, 0, 0, imagecolorallocatealpha($newimg, 0, 0, 0, 127));}imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);}}if($this->type=="png") {imagesavealpha($newimg, true);imagepng ($newimg,$this->dstimg);} else {imagejpeg ($newimg,$this->dstimg);}}//初始化图象private function initi_img() {if($this->type=="jpg") {$this->im = imagecreatefromjpeg($this->srcimg);}if($this->type=="gif") {$this->im = imagecreatefromgif($this->srcimg);}if($this->type=="png") {$this->im = imagecreatefrompng($this->srcimg);}}//图象目标地址private function dst_img($dstpath) {$full_length  = strlen($this->srcimg);$type_length  = strlen($this->type);$name_length  = $full_length-$type_length;$name         = substr($this->srcimg,0,$name_length-1);$this->dstimg = $dstpath;}}
?>

 使用

使用时,直接调用类的构造函数即可,构造函数如下:

$resizeimage = new resizeimage($imgPath, $width, $height, $isCut, $savePath); 

参数

$imgPath:原图片地址

$width:缩略图宽

$height:缩略图高

$isCut:是否裁剪,bool值

$savePath:缩略图地址(可以跟原图片地址相同)

示例

<?phpinclude "ResizeImage.php";//jpg$jpgResize = new ResizeImage("img/test_1920_1200.jpg", 320, 240, false, "img/test_320_240.jpg");//png$pngResize = new ResizeImage("img/test_1024_746.png", 320, 240, false, "img/test_320_240.png");?>

  

 

转载于:https://www.cnblogs.com/sdzbxfcy/p/5641878.html

(转)PHP生成图片缩略图相关推荐

  1. java 图片处理 图片缩略图,java怎么生成图片缩略图,缩小图片,高清图片缩小

    java如何生成图片缩略图,缩小图片,高清图片缩小 可以把图片缩小到理想的倍数,也可以根据自己的需要来具体规定图片转化后的大小 对于类型为jpg的图片来说,只需要三个参数就能转化得到自己想要的图片 参 ...

  2. php图片生成缩略图_PHP生成图片缩略图类示例

    本文实例讲述了PHP生成图片缩略图类.分享给大家供大家参考,具体如下: class App_image_helper { protected $imgFileName; protected $imgW ...

  3. php图片生成缩略图_PHP实现生成图片缩略图函数

    本文主要和大家介绍了PHP基于GD库实现的生成图片缩略图函数,涉及php针对图片属性相关操作技巧,需要的朋友可以参考下,希望能帮助到大家. /** * 生成缩略图函数(支持图片格式:gif.jpeg. ...

  4. php图片缩略图的方法,php生成图片缩略图的方法

    本文实例讲述了php生成图片缩略图的方法.分享给大家供大家参考.具体如下: 这里需要用到GD2 library function make_thumb($src,$dest,$desired_widt ...

  5. php 生成缩略图保存,PHP批量生成图片缩略图的方法

    //用PHP批量生成图片缩略图 function mkdirs($dirname,$mode=0777) //创建目录(目录, [模式]) { if(!is_dir($dirname)) { mkdi ...

  6. IOS平台生成图片缩略图的两种方案

    2013-04-16 15:48 2574人阅读 评论(0) 收藏 举报 IOS平台生成图片缩略图的两种方案 只写出关键部分,其它部分自己添加既可 1.自动缩放到指定大小 + (UIImage *)t ...

  7. JAVA生成图片缩略图、JAVA截取图片局部内容

    目前,google已经有了更好的处理JAVA图片的工具,请搜索:Thumbnailator package com.ares.image.test;import java.awt.Color; imp ...

  8. 业界对生成图片缩略图的做法归纳

    网站如果有很多用户上传图片(相册,商品图片),一般的做法是将用户图片保存在磁盘上面(数据库中记录图片的地址).用户上传的时候按照原图.中图.小图等各个尺寸都生成一份保存在磁盘上.比如php的网店系统e ...

  9. php 缩略图生成类,php生成图片缩略图类

    /** * @abstract 生成图片的缩略图,可以指定任意尺寸,生成的图片为png格式 * @example * $file = 'test.png'; * $th =new Thumbnail( ...

最新文章

  1. 二维树状数组 ----2021广东省赛 ----- K - Kera‘s line segment[区间转二维平面+树状数组维护前缀最小最大值]
  2. 18个堪称神器的命令行工具,高效运维必备
  3. Mysql 忘记管理员密码更改
  4. Azure 命令行工具大混战,都是什么,该选哪个?
  5. NOIP模拟测试21「折纸·不等式」
  6. wordpress仿uehtml设计作品分享主题模板
  7. javascript Array类型 方法大全
  8. Python中如何设置函数的默认值
  9. 这8款浏览器兼容性测试工具,用了以后测试效率可以“起飞”~~
  10. PMBOK - 项目管理知识体系 - 学习系列(1) -
  11. 如何在IIS添加MIME扩展类型
  12. 数学逻辑习题集(2)
  13. 怎么修改游戏ip地址
  14. Microsoft Excel 教程:如何在 Excel 中隐藏或显示行或列?
  15. 字符数组动态开辟空间和静态开辟空间
  16. 博客美化总结(持续更新)
  17. 老师的好帮手——课堂考勤记录整合
  18. 五大企业面试真题(含腾讯)
  19. 全国计算机等级三级网络技术试卷详解(一)
  20. js删除数组内多个指定下标的元素

热门文章

  1. linux中程序包管理方式出现的原由(转载)
  2. bootstrap 图标 由Glyphicons提供
  3. [转]vim 如何编辑 GB2312 编码的文件?
  4. 在C++Builder2010中配置OpenCV2.2
  5. java spark es_JavaSpark写RDD到ES时指定文档ID的
  6. L1-056 猜数字-PAT团体程序设计天梯赛GPLT
  7. 蓝桥杯 ADV-78 算法提高 最长单词
  8. LeetCode 77. Combinations
  9. L2-012. 关于堆的判断-PAT团体程序设计天梯赛GPLT
  10. java实现for文件删除_Java 添加、删除、替换、格式化Word中的文本的步骤详解(基于Spire.Cloud.SDK for Java)...