php使用GD创建保持宽高比缩略图的方法


Posted in PHP onApril 17, 2015

本文实例讲述了php使用GD创建保持宽高比缩略图的方法。分享给大家供大家参考。具体如下:

/**
* Create a thumbnail image from $inputFileName no taller or wider than
* $maxSize. Returns the new image resource or false on error.
* Author: mthorn.net
*/
function thumbnail($inputFileName, $maxSize = 100)
{
 $info = getimagesize($inputFileName);
  $type = isset($info['type']) ? $info['type'] : $info[2];
  // Check support of file type
 if ( !(imagetypes() & $type) )
 {
   // Server does not support file type
   return false;
 }
  $width = isset($info['width']) ? $info['width'] : $info[0];
 $height = isset($info['height']) ? $info['height'] : $info[1];
  // Calculate aspect ratio
 $wRatio = $maxSize / $width;
 $hRatio = $maxSize / $height;
  // Using imagecreatefromstring will automatically detect the file type
 $sourceImage = imagecreatefromstring(file_get_contents($inputFileName));
  // Calculate a proportional width and height no larger than the max size.
 if ( ($width <= $maxSize) && ($height <= $maxSize) )
 {
   // Input is smaller than thumbnail, do nothing
   return $sourceImage;
 }
 elseif ( ($wRatio * $height) < $maxSize )
 {
   // Image is horizontal
   $tHeight = ceil($wRatio * $height);
   $tWidth = $maxSize;
 }
 else
 {
   // Image is vertical
   $tWidth = ceil($hRatio * $width);
   $tHeight = $maxSize;
 }
  $thumb = imagecreatetruecolor($tWidth, $tHeight);
  if ( $sourceImage === false )
 {
   // Could not load image
   return false;
 }
  // Copy resampled makes a smooth thumbnail
 imagecopyresampled($thumb,$sourceImage,0,0,0,0,$tWidth,$tHeight,$width,$height);
 imagedestroy($sourceImage);
  return $thumb;
}
 /**
* Save the image to a file. Type is determined from the extension.
* $quality is only used for jpegs.
* Author: mthorn.net
*/
function imageToFile($im, $fileName, $quality = 80)
{
 if ( !$im || file_exists($fileName) )
 {
   return false;
 }
  $ext = strtolower(substr($fileName, strrpos($fileName, '.')));
  switch ( $ext )
 {
  case '.gif':
  imagegif($im, $fileName);
  break;
  case '.jpg':
  case '.jpeg':
  imagejpeg($im, $fileName, $quality);
  break;
  case '.png':
  imagepng($im, $fileName);
  break;
  case '.bmp':
  imagewbmp($im, $fileName);
  break;
  default:
  return false;
 }
  return true;
}
$im = thumbnail('temp.jpg', 100);
imageToFile($im, 'temp-thumbnail.jpg');

希望本文所述对大家的php程序设计有所帮助。

PHP 相关文章推荐
php cli 方式 在crotab中运行解决
Feb 08 PHP
php和mysql中uft-8中文编码乱码的几种解决办法
Apr 19 PHP
解析php入库和出库
Jun 25 PHP
THINKPHP2.0到3.0有哪些改进之处
Jan 04 PHP
PHP类的封装与继承详解
Sep 29 PHP
PHP结合Mysql数据库实现留言板功能
Mar 04 PHP
CI框架封装的常用图像处理方法(缩略图,水印,旋转,上传等)
Nov 22 PHP
自制PHP框架之模型与数据库
May 07 PHP
thinkphp5.0整合phpsocketio完整攻略(绕坑)
Oct 12 PHP
Laravel5.1 框架控制器基础用法实例分析
Jan 04 PHP
PHP强制转化的形式整理
May 22 PHP
TP - 比RBAC更好的权限认证方式(Auth类认证)
Mar 09 PHP
PHP中preg_match正则匹配中的/u、/i、/s含义
Apr 17 #PHP
php和editplus正则表达式去除空白行
Apr 17 #PHP
PHP生成唯一订单号的方法汇总
Apr 16 #PHP
微信access_token的获取开发示例
Apr 16 #PHP
微信自定义菜单的处理开发示例
Apr 16 #PHP
php简单操作mysql数据库的类
Apr 16 #PHP
PHP扩展程序实现守护进程
Apr 16 #PHP
You might like
什么是MVC,好东西啊
2007/05/03 PHP
用php获取远程图片并把它保存到本地的代码
2008/04/07 PHP
PHP mcrypt可逆加密算法分析
2011/07/19 PHP
php程序总是提示验证码输入有误解决方案
2015/01/07 PHP
php使用GD库创建图片缩略图的方法
2015/06/10 PHP
浅谈PHP中output_buffering
2015/07/13 PHP
PHP共享内存用法实例分析
2016/02/12 PHP
PHP操作FTP类 (上传、下载、移动、创建等)
2016/03/31 PHP
Thinkphp实现站点静态化的方法详解
2017/03/21 PHP
mac系统下安装多个php并自由切换的方法详解
2017/04/21 PHP
js继承 Base类的源码解析
2008/12/30 Javascript
利用javascript实现一些常用软件的下载导航
2009/08/03 Javascript
基于jQuery的ajax功能实现web service的json转化
2009/08/29 Javascript
来自国外的30个基于jquery的Web下拉菜单
2012/06/22 Javascript
JavaScript与DOM组合动态创建表格实例
2012/12/23 Javascript
使用CSS样式position:fixed水平滚动的方法
2014/02/19 Javascript
jQuery新窗口打开外链接
2016/07/21 Javascript
bootstrap table 数据表格行内修改的实现代码
2017/02/13 Javascript
JS如何判断浏览器类型和详细区分IE各版本浏览器
2017/03/04 Javascript
js实现城市级联菜单的2种方法
2017/06/23 Javascript
react实现菜单权限控制的方法
2017/12/11 Javascript
webpack 从指定入口文件中提取公共文件的方法
2018/11/13 Javascript
[12:21]VICI vs TNC (BO3)
2018/06/07 DOTA
Python 常用的安装Module方式汇总
2017/05/06 Python
Django 登陆验证码和中间件的实现
2018/08/17 Python
对Pytorch中Tensor的各种池化操作解析
2020/01/03 Python
Regatta官网:英国最受欢迎的户外服装和鞋类品牌
2019/05/01 全球购物
编写一个类体现构造,公有,私有方法,静态,私有变量
2013/08/10 面试题
师范生实习自我鉴定
2013/11/01 职场文书
房产代理公证处委托书
2014/04/04 职场文书
学校法制宣传日活动总结
2014/11/01 职场文书
2014年党务工作总结
2014/11/25 职场文书
2015年建筑工作总结报告
2015/05/04 职场文书
红楼梦读书笔记
2015/06/25 职场文书
Go语言并发编程 sync.Once
2021/10/16 Golang
详解Flutter和Dart取消Future的三种方法
2022/04/07 Java/Android