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+MYSQL 出现乱码的解决方法
Aug 08 PHP
php 需要掌握的东西 不做浮躁的人
Dec 28 PHP
使用dump函数,给php加断点测试
Jun 25 PHP
浅谈php扩展imagick
Jun 02 PHP
ThinkPHP的I方法使用详解
Jun 18 PHP
ThinkPHP框架设计及扩展详解
Nov 25 PHP
迪菲-赫尔曼密钥交换(Diffie?Hellman)算法原理和PHP实现版
May 12 PHP
PHP Streams(流)详细介绍及使用
May 12 PHP
PHP 中 Orientation 属性判断上传图片是否需要旋转
Oct 16 PHP
PHP实现用户异地登录提醒功能的方法【基于thinkPHP框架】
Mar 15 PHP
启用OPCache提高PHP程序性能的方法
Mar 21 PHP
PHP+fiddler抓包采集微信文章阅读数点赞数的思路详解
Dec 20 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重新实现PHP脚本引擎内置函数
2007/03/06 PHP
PHP下几种删除目录的方法总结
2007/08/19 PHP
PHP+JS+rsa数据加密传输实现代码
2011/03/23 PHP
如何让thinkphp在模型中自动完成session赋值小教程
2014/09/05 PHP
php实现微信公众号主动推送消息
2015/12/31 PHP
利用PHP获取访客IP、地区位置、浏览器及来源页面等信息
2017/06/27 PHP
PHP的Trait机制原理与用法分析
2019/10/18 PHP
VSCode+PHPstudy配置PHP开发环境的步骤详解
2020/08/20 PHP
JavaScript下申明对象的几种方法小结
2008/10/02 Javascript
js 可拖动列表实现代码
2011/12/13 Javascript
JavaScript自定义DateDiff函数(兼容所有浏览器)
2012/03/01 Javascript
jQuery Easyui DataGrid点击某个单元格即进入编辑状态焦点移开后保存数据
2016/08/15 Javascript
js 颜色选择插件
2017/01/23 Javascript
JS实现最简单的冒泡排序算法
2017/02/15 Javascript
JavaScript使用Ajax上传文件的示例代码
2017/08/10 Javascript
基于JavaScript实现前端数据多条件筛选功能
2020/08/19 Javascript
VUE饿了么树形控件添加增删改功能的示例代码
2017/10/17 Javascript
vue中如何去掉空格的方法实现
2018/11/09 Javascript
微信小程序 SOTER 生物认证DEMO 指纹识别功能
2019/12/13 Javascript
浅谈JavaScript中的“!!”作用
2020/08/03 Javascript
element-ui中el-upload多文件一次性上传的实现
2020/12/02 Javascript
Python自动重试HTTP连接装饰器
2015/04/28 Python
Tornado 多进程实现分析详解
2018/01/12 Python
ubuntu 18.04搭建python环境(pycharm+anaconda)
2019/06/14 Python
flask框架url与重定向操作实例详解
2020/01/25 Python
python邮件中附加文字、html、图片、附件实现方法
2021/01/04 Python
英国领先的新鲜松露和最好的松露产品供应商:TruffleHunter
2019/08/26 全球购物
main 主函数执行完毕后,是否可能会再执行一段代码,给出说明
2012/12/05 面试题
排序都有哪几种方法?请列举。用JAVA实现一个快速排序
2014/02/16 面试题
理货员的岗位职责
2013/11/23 职场文书
有兼职工作经历的简历自我评价
2014/03/07 职场文书
2016春季幼儿园大班开学寄语
2015/12/03 职场文书
2019年新郎保证书3篇
2019/10/17 职场文书
Pytorch 统计模型参数量的操作 param.numel()
2021/05/13 Python
Python实现滑雪小游戏
2021/09/25 Python
Mysql将字符串按照指定字符分割的正确方法
2022/05/30 MySQL