<?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. UE 的使用

    1.查找,只匹配整个词语:匹配词语Andy,而不匹配包含Andy的词语,Andy前后有特殊字符才能匹配成功,前后的特殊字符表明Andy是一个词语. 2.正则表达式匹配,如下: 符号 功能 % 匹配行首 ...

  2. jQuery Mobile 手动显示ajax加载器,提示加载中...

    在使用jQuery Mobile开发时,有时候我们需要在请求ajax期间,显示加载提示框(例如:一个旋转图片+一个提示:加载中...).这个时候,我们可以手动显示jQuery Mobile的加载器,大 ...

  3. Codeforces Gym 100015B Ball Painting 找规律

    Ball Painting 题目连接: http://codeforces.com/gym/100015/attachments Description There are 2N white ball ...

  4. Yeoman+Express+Angular在Linux上开发配置方法

    $mkdir ExpressWithAngularTest $cd ExpressWithAngularTest choose needed components you'd like to add ...

  5. 今天弱爆了,svn创建项目

    今天弱爆了 1.再svnRoot下新建你要建的项目名如:hqdj  文件夹,然后选中它点击右键选中create repository here... ,选择文件系统类型 2.进入conf文件夹进行配置 ...

  6. Qt Quick实现的涂鸦程序

    之前一直以为 Qt Quick 里 Canvas 才干够自绘.后来发觉不是,原来还有好几种方式都能够画图! 能够使用原始的 OpenGL(Qt Quick 使用 OpenGL 渲染).能够构造QSGN ...

  7. KMP算法具体解释(转)

    作者:July. 出处:http://blog.csdn.net/v_JULY_v/. 引记 此前一天,一位MS的朋友邀我一起去与他讨论高速排序,红黑树,字典树,B树.后缀树,包含KMP算法,只有在解 ...

  8. 基于jQuery的网站首页宽屏焦点图幻灯片

    今天给大家分享一款基于jQuery的网站首页宽屏焦点图幻灯片.这款焦点图适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗.效果图如下: 在线预览 ...

  9. jQuery刷新div内容,并对刷新后元素绑定事件。$(document).on()

    给id=zt的元素绑定点击事件 点击刷新id=ps_list中类容(内容中含有id=zt元素) 把zt的点击事件委托到了document上,这样就不用考虑事件是否能绑定到新加元素上 代码如下: $(d ...

  10. 使用solr的函数查询,并获取tf*idf值

    1. 使用函数df(field,keyword) 和idf(field,keyword). http://118.85.207.11:11100/solr/mobile/select?q={!func ...