注:此功能依赖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. 怎样处理“error C2220: warning treated as error - no object file generated”错误

    最近用VS2010 编译ceflib开源库是出现"怎样处理"error C2220: warning treated as error - no object file gener ...

  2. ReportService未指定 OverwriteDataSources

    报表服务器部署好之后,查看报表就显示ConnectionString 属性尚未初始化. 然后重启部署并查看部署时控制台的输出信息,发现之前的部署成功消息是假象,实际上部署的时候有一个警告: 不能将数据 ...

  3. c a header file realize of multiple .c file

    //I write a few variables declaration,then include this head-file in multiple realization-c-file,the ...

  4. fineUI ueditor(可能别的editor也有)配置注意事项

    1.把ueditor配置在弹窗里的一些小问题 这种弹窗的原理 如果把ueditor放进这个小窗口里 放进items里(以下截图还没有放) 那么配置的ueditor是不可用的 就好像enable=fal ...

  5. 在WebBrowser中截获弹出对话框内容并将其屏蔽

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  6. Javascript中的栈

    栈 是一种遵从 后进先出(LIFO)原则的有序集合.就像一摞盘子. push 添加一个元素到栈顶 pop 移除并返回栈顶的元素 peek 返回栈顶元素 isEmpty 如果栈里没有任何元素,返回tru ...

  7. 在ionic/cordova中使用Form模型验证(w5cValidator)

    在构建ionic项目过程中,当我们创建一个类似表单提交的页面时,可能会对用户的输入内容做某些规则验证,通过后再执行提交处理. 在验证的过程中,为了提供较好的用户体验,可能希望有类似于jquery Va ...

  8. 使用 Babel + React + Webpack 搭建 Web 应用

    话不说直接上正题. 环境搭建 Babel--目前浏览器对于ES6的语法解析支持度还不高,所以要通过转码在编译,所以在使用ES6之前要安装Babel,之前安装的时候遇到了一些问题但是没有全部记录下来,现 ...

  9. android线程登录

    主入口代码: package com.tp.soft.app; import java.io.IOException; import java.util.HashMap; import java.ut ...

  10. Linux json解析jq

    jq是一个linux下很方便的json解析器,平时看json文件都是一行混在一起的,非常不适合阅读. https://stedolan.github.io/jq/download/ 直接到官网上下载, ...