注:此功能依赖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. [转]DataURL与File,Blob,canvas对象之间的互相转换的Javascript

    来源 http://blog.csdn.net/cuixiping/article/details/45932793 canvas转换为dataURL (从canvas获取dataURL) var d ...

  2. ATS

    一) 起因:由于苹果规定2017年1月1日以后,所有APP都要使用HTTPS进行网络请求. 之前,可以在Info.plist中禁用ATS功能,按如下设置即可. <key>NSAppTran ...

  3. html5本地存储(localStorage)使用介绍

    1.html5几种存储形式 本地存储(localStorage && sessionStorage) 离线缓存(application cache) indexedDB 和 webSQ ...

  4. 微信小程序-页面链接

    navigator 页面链接. 注:navigator-hover默认为{background-color: rgba(0, 0, 0, 0.1); opacity: 0.7;}, <navig ...

  5. SQL-Server使用点滴(一)

    前言 SQL的语法比较简单,学起来相比界面UI控制要简单得多,但是SQL在企业级应用中又是如此的重要,以至于很多开发人员都把重点放在SQL上. SQL并没有面向对象的概念,最复杂的设计也不过是表值函数 ...

  6. Sprint第三阶段(第四天12.12)

  7. Linux就该这么学

    第三章:Vim编辑器与Shell脚本 Vim文本编辑器 在Linux系统中配置应用服务,实际上就是在修改它的配置文件. 在热门的Linux操作系统中都会默认安装一款超好用的文本编辑器--"v ...

  8. Making my own Autonomous Robot in ROS / Gazebo, Day 1: Building the static model

    Day 1: Setting up ROS: Indigo OS: Ubuntu 14.04 OS: Gazebo 7.0.0 Initialize the workspace To create t ...

  9. jq pagination分页 全选、单选的思考

    $().pagination(总条数,配置项); 后端分页的跨页选择: 思路:把浏览过的页整体保存为,整体拥有 curPage(当前页码).allChoice(当前页是否全选).selected当前页 ...

  10. LoadLibrary失败,GetLastError MOD_NOT_FOUND

    即使传入的.dll文件存在,也可能返回这个错误.因为加载的DLL库可能以来其他库,尤其是编译器的dll. 以腾讯的debug版libtim.dll为例:如果没有msvcr100d.dll和msvcp1 ...