本来用phpthumb来缩略图片是十分方便的,但是最近在sae上写项目发现phpthumb在sae上保存文件时会出问题,想来实现一个简单的按最大宽高等比例缩放图片类也并不困难,于是便自己写了一个方便修改。

需GD库支持,可支持jpg、jpeg、png、gif格式的图片,代码短小适合新手学习用

<?php
class slpic {
//原图片文件,包含路径和文件名
var $orpic;
//原图的临时图像
var $tempic;
//缩略图
var $thpic;
//原宽度
var $width;
//原高度
var $height;
//图片类型
var $type;
//缩略后的宽度
var $thwidth;
//缩略后的高度
var $thheight; function __construct($file){
$this->orpic = $file;
$infos = getimagesize($file);
$this->width = $infos[0];
$this->height = $infos[1];
$this->type = $infos[2];
} //根据用户所指定最大宽高来计算缩略图尺寸
function cal_size($maxwidth, $maxheight){
//缩略图最大宽度与最大高度比
$thcrown = $maxwidth/$maxheight;
//原图宽高比
$crown = $this->width/$this->height;
if($crown/$thcrown >= 1){
$this->thwidth = $maxwidth;
$this->thheight = $maxwidth/$crown;
} else {
$this->thheight = $maxheight;
$this->thwidth = $maxheight*$crown;
}
} function init(){
switch($this->type){
case 1: //GIF
$this->tempic = imagecreatefromgif($this->orpic);
break;
case 2: //JPG
$this->tempic = imagecreatefromjpeg($this->orpic);
break;
case 3: //PNG
$this->tempic = imagecreatefrompng($this->orpic);
break;
default:
echo '暂不支持该图片格式';
}
} function resize($maxwidth, $maxheight){
//初始化图像
$this->init();
//计算出缩略图尺寸
$this->cal_size($maxwidth, $maxheight); $this->thpic = imagecreatetruecolor($this->thwidth, $this->thheight);
imagecopyresampled($this->thpic, $this->tempic, 0, 0, 0 ,0, $this->thwidth, $this->thheight, $this->width, $this->height);
} function save($filename, $type){
switch($type){
case 'jpg':
case 'jpeg':
imagejpeg($this->thpic, $filename);
break;
case 'png':
imagepng($$this->thpic, $filename);
break;
case 'gif':
imagegif($$this->thpic, $filename);
break;
default:
echo '暂不支持您所选择的格式';
}
}
}
?>

PHP按最大宽高等比例缩放图片类的更多相关文章

  1. PHP按最大宽高等比例缩放图片类 http://www.oschina.net/code/snippet_876708_21113

    PHP按最大宽高等比例缩放图片类 http://www.oschina.net/code/snippet_876708_21113 php 等比例缩小图片 http://www.111cn.net/p ...

  2. JavaScript等比例缩放图片

    js等比例缩放图片,这个功能非常实用,当网页加载一个尺寸比较大的图片时,往往会把一个网页撑的变形,页面变得很难看,于是我们就想到了用JS去控制超出一定范围的图片,以稳定页面布局,本代码段就是完成了此功 ...

  3. JS实现等比例缩放图片

    JS实现等比例缩放图片 2014-01-19 21:57 by 龙恩0707, 40 阅读, 0 评论, 收藏, 编辑 JS实现等比例缩放图片 有时候我们前端页面只有500×500像素的宽和高的布局, ...

  4. JS等比例缩放图片,限定最大宽度和最大高度

    JS等比例缩放图片,限定最大宽度和最大高度 JavaScript //图片按比例缩放 var flag=false; function DrawImage(ImgD,iwidth,iheight){ ...

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

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

  6. C#等比例缩放图片

    等比例缩放图片(C#) private Bitmap ZoomImage(Bitmap bitmap, int destHeight, int destWidth) { try { System.Dr ...

  7. DTCMS,添加文章时,内容中第一张图片作缩略图,并且等比例缩放图片

    DTCMS,添加文章时,内容中第一张图片作缩略图 admin/article/article_edit.aspx.cs 导入: using System.Drawing;using System.Dr ...

  8. [Xcode 实际操作]六、媒体与动画-(1)使用图形上下文按一定比例缩放图片

    目录:[Swift]Xcode实际操作 本文将演示如何通过图形上下文,来实现图片缩放的功能. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKi ...

  9. Android 等比例缩放图片

    // 缩放图片 public static Bitmap zoomImg(String img, int newWidth ,int newHeight){ // 图片源 Bitmap bm = Bi ...

随机推荐

  1. HTML+CSS基础学习笔记(2)

    一.无序列表标签ul <ul> <li>信息</li> <li>信息</li> ...... </ul> 代码解释:每项< ...

  2. portal开发"下拉框"“日期框”查询要怎么配置

    下面的这些是我今天的成果! 总的来说是一步一步摸索出来的!还是等感谢超哥的耐心指导,犯了一些错误! 1.比如在wd配置文件中中写id=“check_it_two”,在java中写成 checki_it ...

  3. 常见sql语句及复杂sql语句记录

    1 将A表中的部分字段和B表中的部分字段作为C表的字段 SELECT a.name,b.age   FROM  (SELECT * from A) as a,(SELECT * from B) as ...

  4. .Net开源SqlServer ORM框架SqlSugar整理

    一.链接整理 官方Git源代码地址: https://github.com/sunkaixuan/SqlSugar 最新发布版更新地址:当前版本Release 3.5.2.1 https://gith ...

  5. dispatchkeyevent的调用机制

    http://blog.csdn.net/look85/article/details/23740761 dispatchKeyEvent和onKeyDown关系: 当键盘按下时 首先触发dispat ...

  6. js中eval函数

    后台数据 // 回显复选框用 List<Long> tempRoles = new ArrayList<Long>(); @SuppressWarnings("unc ...

  7. iOS崩溃报告获取一

    在AppDelegate.m文件中实现函数 void UncaughtExceptionHandler(NSException *exception) { /** * 获取异常崩溃信息 */ NSAr ...

  8. power shell upload file to azure storage

    # Azure subscription-specific variables. $storageAccountName = "storage-account-name" $con ...

  9. 【html】【4】html事件集合

    必看参考: http://www.runoob.com/tags/ref-eventattributes.html http://www.cnblogs.com/jiangchongwei/archi ...

  10. windows8一直更新不了的问题————解决方案

    以下是微软官方工程师的详细解答: 尊敬的佐先生: 您好! 感谢您联系微软技术支持!我是微软技术支持工程师,我姓张.我将协助您解决有关问题.您的问题编号是SRX 1274238225 对于您当前的更新问 ...