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和ACCESS写聊天室(九)
Oct 09 PHP
PHP 裁剪图片成固定大小代码方法
Sep 09 PHP
深入解析PHP内存管理之谁动了我的内存
Jun 20 PHP
深入解析php中的foreach问题
Jun 30 PHP
10 个经典PHP函数
Oct 17 PHP
PHP类的声明与实例化及构造方法与析构方法详解
Jan 26 PHP
PHP中仿制 ecshop验证码实例
Jan 06 PHP
ubutu 16.04环境下,PHP与mysql数据库,网页登录验证实例讲解
Jul 20 PHP
php使用 readfile() 函数设置文件大小大小的方法
Aug 11 PHP
浅谈PHP中如何实现Hook机制
Nov 14 PHP
php单元测试phpunit入门实例教程
Nov 17 PHP
PHP设计模式(一)工厂模式Factory实例详解【创建型】
May 02 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
php 网上商城促销设计实例代码
2012/02/17 PHP
PHP 利用Mail_MimeDecode类提取邮件信息示例
2014/01/26 PHP
php实现的Captcha验证码类实例
2014/09/22 PHP
php使用gettimeofday函数返回当前时间并存放在关联数组里
2015/03/19 PHP
使用URL传输SESSION信息
2015/07/14 PHP
yii使用bootstrap分页样式的实例
2017/01/17 PHP
PHP 类与构造函数解析
2017/02/06 PHP
PHP根据树的前序遍历和中序遍历构造树并输出后序遍历的方法
2017/11/10 PHP
laravel 之 Eloquent 模型修改器和序列化示例
2019/10/17 PHP
身份证号码前六位所代表的省,市,区, 以及地区编码下载
2007/04/12 Javascript
JavaScript Event学习第三章 早期的事件处理程序
2010/02/07 Javascript
基于jQuery实现中英文切换导航条效果
2016/09/18 Javascript
Angular2  NgModule 模块详解
2016/10/19 Javascript
Nodejs下用submit提交表单提示cannot post错误的解决方法
2016/11/21 NodeJs
jQuery zTree树插件动态加载实例代码
2017/05/11 jQuery
详解使用VUE搭建后台管理系统(vue-cli更新至3.0)
2018/08/22 Javascript
原生js实现随机点名功能
2019/11/05 Javascript
5个你不知道的JavaScript字符串处理库(小结)
2020/06/01 Javascript
python根据出生日期返回年龄的方法
2015/03/26 Python
Windows下Anaconda2安装NLTK教程
2018/09/19 Python
python使用adbapi实现MySQL数据库的异步存储
2019/03/19 Python
python selenium操作cookie的实现
2020/03/18 Python
Html5页面中的返回实现的方法
2018/02/26 HTML / CSS
微信html5页面调用第三方位置导航的示例
2018/03/14 HTML / CSS
曼联官方网上商店:Manchester United Direct
2017/07/28 全球购物
美国市场上最实惠的送餐服务:Dinnerly
2018/03/18 全球购物
顶丰TOPPIK台湾官网:增发纤维假发,告别秃发困扰
2018/06/13 全球购物
速卖通欧盟:Aliexpress EU
2020/08/19 全球购物
工程造价专业大学生自荐信
2013/10/01 职场文书
室内设计专业个人的自我评价
2013/12/18 职场文书
工作交流会欢迎词
2014/01/12 职场文书
2014年政风行风评议工作总结
2014/10/21 职场文书
四风问题党员个人整改措施
2014/10/27 职场文书
挂靠协议书
2015/01/27 职场文书
离婚起诉书范文2015
2015/05/19 职场文书
小学四年级班务总结该怎么写?
2019/08/16 职场文书