php 图片调整大小 封装类【转载】
<?php
class ImageResize
{
private $image;
private $img_des;
private $image_type;
private $permissions;
private $compression; /**
* 构造函数
* @param string $img_src 源图片
* @param string $img_des 保存图片对象
* @param int $compression 压缩比率,范围0-100(0:压缩比最高,100:压缩比最低)
* @param null $permissions 改变文件模式
* mode 参数由 4 个数字组成:
* 第一个数字永远是 0
* 第二个数字规定所有者的权限
* 第二个数字规定所有者所属的用户组的权限
* 第四个数字规定其他所有人的权限
* 1 - 执行权限,2 - 写权限,4 - 读权限
*/
function __construct($img_src, $img_des = NULL, $compression = 75, $permissions = null)
{
$image_info = getimagesize($img_src); $this->img_des = $img_des;
$this->image_type = $image_info[2];
$this->compression = $compression;
$this->permissions = $permissions; if ($this->image_type == IMAGETYPE_JPEG) {
$this->image = ImageCreateFromJPEG($img_src);
} elseif ($this->image_type == IMAGETYPE_GIF) {
$this->image = ImageCreateFromGIF($img_src);
} elseif ($this->image_type == IMAGETYPE_PNG) {
$this->image = ImageCreateFromPNG($img_src);
}
} /**
* 保存图片
*/
function save()
{
if ($this->image_type == IMAGETYPE_JPEG) {
imagejpeg($this->image, $this->img_des, $this->compression);
} elseif ($this->image_type == IMAGETYPE_GIF) {
imagegif($this->image, $this->img_des);
} elseif ($this->image_type == IMAGETYPE_PNG) {
imagepng($this->image, $this->img_des);
} if ($this->permissions != null) {
chmod($this->image, $this->compression);
}
} /**
* 做为图片流直接输出
*/
function output()
{
if ($this->image_type == IMAGETYPE_JPEG) {
header('Content-Type: image/jpg');
imagejpeg($this->image);
} elseif ($this->image_type == IMAGETYPE_GIF) {
header('Content-Type: image/gif');
imagegif($this->image);
} elseif ($this->image_type == IMAGETYPE_PNG) {
header('Content-Type: image/png');
imagepng($this->image);
}
} /**
* 获取图片宽度
* @return int 图片宽度
*/
function getWidth()
{
return imagesx($this->image);
} /*
* 获取图片高度
* @return int 图片高度
*/
function getHeight()
{
return imagesy($this->image);
} /**
* 按照固定高度缩放图片
* @param $height 需要改变大小的高度
*/
function resizeToHeight($height)
{
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width, $height);
} /**
* 按照固定宽度缩放图片
* @param $width 指定宽度
*/
function resizeToWidth($width)
{
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width, $height);
} /**
* 等比缩放图片
* @param int $scale 缩放比例
*/
function scale($scale)
{
$width = $this->getWidth() * $scale / 100;
$height = $this->getheight() * $scale / 100;
$this->resize($width, $height);
} /**
* 指定宽度和高度缩放图片
* @param int $width 缩放宽度
* @param int $height 缩放高度
* @return 缩放后图片对象
*/
function resize($width, $height)
{
$new_image = imagecreatetruecolor($width, $height); if ($this->image_type == IMAGETYPE_GIF || $this->image_type == IMAGETYPE_PNG) {
$current_transparent = imagecolortransparent($this->image);
if ($current_transparent != -1) {
$transparent_color = imagecolorsforindex($this->image, $current_transparent);
$current_transparent = imagecolorallocate($new_image, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
imagefill($new_image, 0, 0, $current_transparent);
imagecolortransparent($new_image, $current_transparent);
} elseif ($this->image_type == IMAGETYPE_PNG) {
imagealphablending($new_image, false);
$color = imagecolorallocatealpha($new_image, 0, 0, 0, 127);
imagefill($new_image, 0, 0, $color);
imagesavealpha($new_image, true);
}
} imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
} ?>
使用方法:
$image = new ImageResize($tmp_image_name, NULL, 100);
$image->output();
php 图片调整大小 封装类【转载】的更多相关文章
- Excel图片调整大小
Excel图片调整大小 Sub 图片调整合适大小() ' Debug.Print ActiveWorkbook.Name 图片显示比例 = 0.9 '1为顶满单元格 Dim wb As Workboo ...
- python图片添加水印(转载)
转载来自:http://blog.csdn.net/orangleliu/ # -*- encoding=utf-8 -*- ''''' author: orangleliu pil处理图片,验证,处 ...
- 黄聪:C#图片处理封装类(裁剪、缩放、清晰度、加水印、生成缩略图)有示例(转)
C#图片处理示例(裁剪,缩放,清晰度,水印) 吴剑 2011-02-20 原创文章,转载必需注明出处:http://www.cnblogs.com/wu-jian/ 前言 需求源自项目中的一些应用,比 ...
- placehold.it-在线图片生成器(转载)
做网站的时候 如果 有的产品等客户没有上传图片,可以用这个网站生成的图片 并配以文字进行图片的占位 以免造成页面的空挡或者页面错位等 原文地址:http://www.cnblogs.com/xumen ...
- php如何控制用户对图片的访问 PHP禁止图片盗链(转载)
把images目录设置成不充许http访问(把图片目录的:读取.目录浏览 两个权限去掉). 用一个PHP文件,直接用file函数读取这个图片.在这个PHP文件里进行权限控制. apache环境中,在你 ...
- ueditor 单独图片上传 转载
<body> <script type="text/javascript"> //这个是图片上传的,网上还有附件上传的 (function($) { var ...
- Android 高级UI设计笔记06:仿微信图片选择器(转载)
仿微信图片选择器: 一.项目整体分析: 1. Android加载图片的3个目标: (1)尽可能的去避免内存溢出. a. 根据图片的显示大小去压缩图片 b. 使用缓存对我们图片进行管理(LruCache ...
- javascript图片延迟加载(转载)
<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content=&q ...
- 【Thumbnailator】java 使用Thumbnailator实现等比例缩放图片,旋转图片等【转载】
Thumbnailator概述: Thumbnailator是与Java界面流畅的缩略图生成库.它简化了通过提供一个API允许精细的缩略图生成调整生产从现有的图像文件的缩略图和图像对象的过程, ...
随机推荐
- Android常见工具类封装
MD5加密 import android.annotation.SuppressLint; import java.security.MessageDigest; public class MD5 { ...
- Codeforces Gym 100187E E. Two Labyrinths bfs
E. Two Labyrinths Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/prob ...
- scu 4436: Easy Math 水题
4436: Easy Math Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.scu.edu.cn/soj/problem.actio ...
- 提供一个免费的CSDN下载账号
账号:windforce05password:w12345678请下载了资源后评价一下资源,以便赚回分数.
- android之多媒体篇(二)
管理音频焦点 情景:当你的app隐退到后台,而其他也有播放能力的app浮现在前台,这个时候,你可能要暂停你原有app的播放功能,和解除监听Media Button,把控制权交给前台的APP. 这就需要 ...
- WPF 之 未捕获异常的处理
首先,我们当然是要求应用程序开发人员,尽可能地在程序可能出现异常的地方都去捕捉异常,使用try…catch的方式.但是总是有一些意外的情况可能会发生,这就导致会出现所谓的“未捕获异常(Unhandle ...
- .Net Static 与单例
Static 关键字作为修饰符可以用于类.方法和成员变量上.其含义是对于整个应用程序生命周期内,访问该修饰符修饰的对象/方法/变量都引用到同一实例(内存地址).但正因如此在多线程下会出现线程安全问题: ...
- solr--搜索参数随笔
1.默认搜索域.查询域的关系 qf字段的配置:query fields,指定solr从哪些field中搜索 在solrj中设置如下: params.setParam("qf", & ...
- solr4.x配置IK2012FF智能分词+同义词配置
本文配置环境:solr4.6+ IK2012ff +tomcat7 在Solr4.0发布以后,官方取消了BaseTokenizerFactory接口,而直接使用Lucene Analyzer标准接口T ...
- IOS 图片转换二进制 二进制转换为图片
//类方法 图片 转换为二进制 +(NSData *)Image_TransForm_Data:(UIImage *)image { NSData *imageData = UIImageJPEGRe ...