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允许精细的缩略图生成调整生产从现有的图像文件的缩略图和图像对象的过程, ...
随机推荐
- boost::token_compress_on
对于场景:string s = "123456",用"3","4"切分,默认情况下(boost::token_compress_off),切 ...
- 《计算机问题求解》总结——2014年CCF计算机课程改革导教班(2014.07.11)
一:引言 "心想事成".这是自己获得导教班学习机会的最佳概括.2013年年末学习李晓明老师的<人群与网络>课程:随后网络认识烟台大学贺利坚老师,了解到2013年 ...
- LintCode 子树
easy 子树 19% 通过 有两个不同大小的二进制树: T1 有上百万的节点: T2 有好几百的节点.请设计一种算法.判定 T2 是否为 T1的子树. 您在真实的面试中是否遇到过这个题? Yes 例 ...
- android手势事件 快速移动 长按触摸屏 按下触摸屏,并拖动
/* 用户按下触摸屏.快速移动后松开 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float vel ...
- HDOJ 1301 Jungle Roads
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 //HDOJ1301 #include<iostream>#include<c ...
- Golang学习 - io/ioutil 包
------------------------------------------------------------ // Discard 是一个 io.Writer 接口,调用它的 Write ...
- mysql中的longblob类型处理
longblob 对应的 C#数据类型为 byte[] 1.byte[] 与 string 之间的转换 byte[] bb = Encoding.UTF8.GetBytes(ss); string s ...
- SpringMvc多文件上传简单实现
public ResponseItem uploadFile(MultipartHttpServletRequest request,FileItem fileItem,PageData pd) { ...
- 安装TortoiseGit出现提示“您必须安装带有更新版本Windows Installer服务的Windows Service Pack”-解决方法
我的系统是xp sp3安装TortoiseGit时出现了错误提示“您必须安装带有更新版本Windows Installer服务的Windows Service Pack”. 解决方法,到微软官方下载相 ...
- iOS - UI - UISegmentedControl
1.UISegmentedControl NSArray * array = @[@"red",@"green",@"yellow",@&q ...