<?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 图片调整大小 封装类【转载】的更多相关文章

  1. Excel图片调整大小

    Excel图片调整大小 Sub 图片调整合适大小() ' Debug.Print ActiveWorkbook.Name 图片显示比例 = 0.9 '1为顶满单元格 Dim wb As Workboo ...

  2. python图片添加水印(转载)

    转载来自:http://blog.csdn.net/orangleliu/ # -*- encoding=utf-8 -*- ''''' author: orangleliu pil处理图片,验证,处 ...

  3. 黄聪:C#图片处理封装类(裁剪、缩放、清晰度、加水印、生成缩略图)有示例(转)

    C#图片处理示例(裁剪,缩放,清晰度,水印) 吴剑 2011-02-20 原创文章,转载必需注明出处:http://www.cnblogs.com/wu-jian/ 前言 需求源自项目中的一些应用,比 ...

  4. placehold.it-在线图片生成器(转载)

    做网站的时候 如果 有的产品等客户没有上传图片,可以用这个网站生成的图片 并配以文字进行图片的占位 以免造成页面的空挡或者页面错位等 原文地址:http://www.cnblogs.com/xumen ...

  5. php如何控制用户对图片的访问 PHP禁止图片盗链(转载)

    把images目录设置成不充许http访问(把图片目录的:读取.目录浏览 两个权限去掉). 用一个PHP文件,直接用file函数读取这个图片.在这个PHP文件里进行权限控制. apache环境中,在你 ...

  6. ueditor 单独图片上传 转载

    <body> <script type="text/javascript"> //这个是图片上传的,网上还有附件上传的 (function($) { var ...

  7. Android 高级UI设计笔记06:仿微信图片选择器(转载)

    仿微信图片选择器: 一.项目整体分析: 1. Android加载图片的3个目标: (1)尽可能的去避免内存溢出. a. 根据图片的显示大小去压缩图片 b. 使用缓存对我们图片进行管理(LruCache ...

  8. javascript图片延迟加载(转载)

    <!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content=&q ...

  9. 【Thumbnailator】java 使用Thumbnailator实现等比例缩放图片,旋转图片等【转载】

    Thumbnailator概述:     Thumbnailator是与Java界面流畅的缩略图生成库.它简化了通过提供一个API允许精细的缩略图生成调整生产从现有的图像文件的缩略图和图像对象的过程, ...

随机推荐

  1. 四月二十五日,bugzilla for CentOS 安装

    Bugzilla for CentOS 5.4 制作人,陈浩 时间:2014.4.25 原创 文件夹 Bugzilla for CentOS 5.4 一. 装系统 1) 新建虚拟机  15G硬盘,51 ...

  2. 微信公共服务平台开发(.Net 的实现)7-------发送图文消息

    之前我们讲过让微信发送给我们普通的文本信息,下面我们来看看如何发送图文信息,需要注意的是这里说的是,让微信发给我们,而不是我们拍个图片发给微信处理,我们上传图片在以后的章节介绍.下面是发送图文消息的函 ...

  3. [MongoDB] Query, update, index and group

    /* 1. Query Operators */ db.posts.find({ viewsCount: {$get: 1000, $lte: 3000} }, {_id: 0, viewsCount ...

  4. ScrollView反弹效果 仿小米私密短信效果

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/28441197 如今非常多APP都给ScrollView加入了反弹效果.QQ.小米 ...

  5. IO 延迟与Queue Depth

     IO 延迟:存储设备的IO延迟 Queue Depth:磁盘控制器所发出的批量指令的最大条数 IOPS:磁盘设备每秒的IO 三者之间的关系:IOPS=(Queue Depth)/(IO latenc ...

  6. 《linux性能及调优指南》

    http://blog.chinaunix.net/uid-26000296-id-4065871.html

  7. IPC——信号量

    Linux进程间通信——使用信号量 这篇文章将讲述别一种进程间通信的机制——信号量.注意请不要把它与之前所说的信号混淆起来,信号与信号量是不同的两种事物.有关信号的更多内容,可以阅读我的另一篇文章:L ...

  8. 进程间通信和同步:pipe、FIFO、消息队列、信号量、共享内存、信号

    一.半双工管道(pipe) 关于管道详细介绍可参考http://www.cnblogs.com/nufangrensheng/p/3560130.html. 1.管道实现父子进程间通信实例: /* p ...

  9. Golang学习 - reflect 包

    ------------------------------------------------------------ 在 reflect 包中,主要通过两个函数 TypeOf() 和 ValueO ...

  10. 构建高性能服务(三)Java高性能缓冲设计 vs Disruptor vs LinkedBlockingQueue--转载

    原文地址:http://maoyidao.iteye.com/blog/1663193 一个仅仅部署在4台服务器上的服务,每秒向Database写入数据超过100万行数据,每分钟产生超过1G的数据.而 ...