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 相关文章推荐
删除无限级目录与文件代码共享
Jul 12 PHP
PHP4实际应用经验篇(7)
Oct 09 PHP
一个基于PDO的数据库操作类
Mar 24 PHP
php中计算中文字符串长度、截取中文字符串的函数代码
Aug 09 PHP
非常好用的Zend Framework分页类
Jun 25 PHP
ci检测是ajax还是页面post提交数据的方法
Nov 10 PHP
php curl登陆qq后获取用户信息时证书错误
Feb 03 PHP
PHP实现HTML页面静态化的方法
Nov 04 PHP
php验证码的制作思路和实现方法
Nov 12 PHP
PHP二维数组矩形转置实例
Jul 20 PHP
php使用mysqli和pdo扩展,测试对比连接mysql数据库的效率完整示例
May 09 PHP
php解决安全问题的方法实例
Sep 19 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
收音机发烧友应当熟知的100条知识
2021/03/02 无线电
php array_map()数组函数使用说明
2011/07/12 PHP
CodeIgniter启用缓存和清除缓存的方法
2014/06/12 PHP
php保存二进制原始数据为图片的程序代码
2014/10/14 PHP
php+js实现的拖动滑块验证码验证表单操作示例【附源码下载】
2020/05/27 PHP
JavaScript Cookie显示用户上次访问的时间和次数
2009/12/08 Javascript
JQuery实现用户名无刷新验证的小例子
2013/03/22 Javascript
JS.GetAllChild(element,deep,condition)使用介绍
2013/09/21 Javascript
javascript实现简单的Map示例介绍
2013/12/23 Javascript
利用Jquery实现可多选的下拉框
2014/02/21 Javascript
jquery中常用的函数和属性详细解析
2014/03/07 Javascript
javascript模拟C#格式化字符串
2015/08/26 Javascript
更高效的使用JQuery 这里总结了8个小技巧
2016/04/13 Javascript
vue.js指令v-model实现方法
2016/12/05 Javascript
Bootstrap modal使用及点击外部不消失的解决方法
2016/12/13 Javascript
关于vue.js过渡css类名的理解(推荐)
2017/04/10 Javascript
ES6的解构赋值实例详解
2019/05/06 Javascript
详解vue-cli中使用rem,vue自适应
2019/05/06 Javascript
JavaScript实现的开关灯泡点击切换特效示例
2019/07/08 Javascript
微信小程序实现选项卡滑动切换
2020/10/22 Javascript
[01:37]TI4西雅图DOTA2前线报道 VG拿下首胜教练357给出获胜秘诀
2014/07/10 DOTA
[01:09:13]DOTA2-DPC中国联赛 正赛 CDEC vs XG BO3 第三场 1月19日
2021/03/11 DOTA
Python安装pycurl失败的解决方法
2018/10/15 Python
Python构建图像分类识别器的方法
2019/01/12 Python
python代数式括号有效性检验示例代码
2020/10/04 Python
Python confluent kafka客户端配置kerberos认证流程详解
2020/10/12 Python
总监职责范文
2013/11/09 职场文书
中层干部岗位职责
2013/12/18 职场文书
新闻编辑自荐书范文
2014/02/12 职场文书
2014年学校工作总结
2014/11/20 职场文书
环保建议书作文300字
2015/09/14 职场文书
2016年党建工作简报
2015/11/26 职场文书
反邪教教育心得体会
2016/01/15 职场文书
三年级作文之趣事作文
2019/11/04 职场文书
python3使用diagrams绘制架构图的步骤
2021/04/08 Python
Css预编语言及区别详解
2021/04/25 HTML / CSS