注:此功能依赖GD2图形库

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:缩略图地址(可以跟原图片地址相同)

示例

 
<?php
include "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"); ?>
 

效果

  

php生成图片缩略图,支持png透明的更多相关文章

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

    package com.ares.image.test; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; ...

  2. 使用DD_belatedPNG让IE6支持PNG透明图片

    使用DD_belatedPNG让IE6支持PNG透明图片 众所周知IE6不支持透明的PNG图片,而PNG图片在Web设计方面表现力上,具有其它图形格式所达不到的效果,IE6这一致命缺陷极大地限制了We ...

  3. PHP原生写的生成图片缩略图类

    PHP原生写的生成图片缩略图类,本文以京东商品图片为例,分别生成三种不同尺寸的图片.调用方法很简单只要传参数高度和宽度,及新图片的名称. 引入缩略图类 include_once 'ImageResiz ...

  4. IEPNGFix 解决IE6支持PNG透明问题

    IE6本身是支持索引色透明度(PNG8)格式,但不支持真彩色透明度(PNG24)格式. 使用IE PNG Fix 2.0可以完美解决IE6支持PNG透明问题,而且支持背景定位和重复属性. IE PNG ...

  5. 解析IE, FireFox, Opera 浏览器支持Alpha透明的方法

    先请看如下代码: filter:alpha(opacity=50);       /* IE */  -moz-opacity:0.5;              /* Moz + FF */  op ...

  6. 通过IE私有滤镜让IE6 7 8支持背景透明,内容不透明效果。

    CSS3已经支持背景rgba的rgba透明度,这一方法可以避免元素内容也随背景一起变透明(详情请阅http://www.cssha.com/css3-new-knowledge-student).但是 ...

  7. C#生成图片缩略图(2种思路)

    前言:在日常图片浏览中,如果图片过多,只有一张张的打开图片才能知道图片的内容,显然这样浏览起来非常不便.Windows系统在浏览图片时提供了缩略图的功能,这样大大的方便了浏览者了解每张图片的内容,本实 ...

  8. PHP 生成图片缩略图函数

    <?php /** * 生成缩略图函数(支持图片格式:gif.jpeg.png和bmp) * @author ruxing.li * @param string $src 源图片路径 * @pa ...

  9. java生成生成图片缩略图

    /** * */ package com.fkhwl.fkhserver.core.utils; import java.awt.Image; import java.awt.image.Buffer ...

随机推荐

  1. strom的使用02

    1.grouping分组策略 stream grouping就是用来定义一个stream应该如果分配给Bolts上面的多个Tasks. storm里面有6种类型的stream grouping: 1. ...

  2. web前端之html5开发中常用的开发工具

    正所谓“工欲善其事,必先利其器”,对Web开发人员来说,好工具的使用总会给人带来事半功倍的效果.正准备学习HTML5或者已经进行了一段时间的HTML5开发的童鞋,都有必要了解下,HTML5都有哪些开发 ...

  3. python非递归全排列

    刚刚开始学习python,按照廖雪峰的网站看的,当前看到了函数这一节.结合数组操作,写了个非递归的全排列生成.原理是插入法,也就是在一个有n个元素的已有排列中,后加入的元素,依次在前,中,后的每一个位 ...

  4. java内存泄漏的经典案例

    这篇文章主要介绍了Java中典型的内存泄露问题和解决方法,典型的内存泄露例子是一个没有实现hasCode和 equals方法的Key类在HashMap中保存的情况,可以通过实现Key类的equals和 ...

  5. select元素中设置padding效果

    更新2016-9-17:这个bug是在mac os上的发生,在window系统中不存在这个问题! 给select元素增加:padding-left:20px;但是未生效,如下图所示: 在select上 ...

  6. treap 模版

    struct Treap { struct node { node *son[]; int key,siz,wei,cnt; node(int _key,node *f) { son[]=son[]= ...

  7. QAbstractItemView::setRootIndex(const QModelIndex & index) 失效

    问题: 在逻辑中使用了, QAbstractItemView::setRootIndex(const QModelIndex & index), 第一次设置生效, view 进入了model ...

  8. iOS10 拍照崩溃问题

    根据相对应得功能添加相关权限即可,没必要全部添加,后面的描述可以官方点,因为会以弹出框的形式访问的,比如相机权限后面的描述可以为:这个应用需要访问相机:后续如果发现其他iOS10上面的问题会及时更新的

  9. 个人博客作业Week1

    个人博客作业Week1 一.问题 通读<构建之法>我有一下几个问题 PM没有参与代码编如何进行管理. 软件工程师的职业资格考试对我们来说很有必要吗. 当我们为用户开发软件时我们需要了解用户 ...

  10. (原创)RS232串口信号定义

    好久没用动硬件了,串口更是好久没用用了. 曾经接口信号记得很清楚,久了,忘了. 今天,重新回顾,笔记记下. DB9接口分公头和母头,公头即插针头,电脑机箱上多少公头.母头即插孔座. 合理的硬件设计均以 ...