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中的正规表达式(二)
Oct 09 PHP
PHP SQLite类
May 07 PHP
用来解析.htpasswd文件的PHP类
Sep 05 PHP
php导出word文档与excel电子表格的简单示例代码
Mar 08 PHP
ThinkPHP分组下自定义标签库实例
Nov 01 PHP
通过修改配置真正解决php文件上传大小限制问题(nginx+php)
Sep 23 PHP
解读PHP的Yii框架中请求与响应的处理流程
Mar 17 PHP
php 替换文章中的图片路径,下载图片到本地服务器的方法
Feb 06 PHP
PHP实现可精确验证身份证号码的工具类示例
May 31 PHP
php微信公众号开发之欢迎老朋友
Oct 20 PHP
php设计模式之策略模式应用案例详解
Jun 17 PHP
php 使用html5 XHR2实现上传文件与进度显示功能示例
Mar 03 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
第二节 对象模型 [2]
2006/10/09 PHP
discuz论坛 用户登录 后台程序代码
2008/11/27 PHP
JS 统计时间
2021/03/09 Javascript
Juqery Html(),append()等方法的Bug解决方法
2010/12/13 Javascript
Prototype源码浅析 Enumerable部分之each方法
2012/01/16 Javascript
jquery通过visible来判断标签是否显示或隐藏
2014/05/08 Javascript
js函数内变量的作用域分析
2015/01/12 Javascript
基于JavaScript实现购物车功能
2017/02/07 Javascript
判断横屏竖屏(三种)
2017/02/13 Javascript
Javascript实现登录记住用户名和密码功能
2017/03/22 Javascript
webpack公共组件引用路径简化小技巧
2018/06/15 Javascript
bootstrap自定义样式之bootstrap实现侧边导航栏功能
2018/09/10 Javascript
Vue中Table组件Select的勾选和取消勾选事件详解
2019/03/19 Javascript
[43:51]2014 DOTA2国际邀请赛中国区预选赛 Dream Times VS TongFu
2014/05/22 DOTA
python 计算文件的md5值实例
2017/01/13 Python
Python BS4库的安装与使用详解
2018/08/08 Python
用Python解数独的方法示例
2019/10/24 Python
使用Python实现微信拍一拍功能的思路代码
2020/07/09 Python
python3实现名片管理系统(控制台版)
2020/11/29 Python
python向xls写入数据(包括合并,边框,对齐,列宽)
2021/02/02 Python
html5 利用canvas手写签名并保存的实现方法
2018/07/12 HTML / CSS
HTML5 Canvas的常用线条属性值总结
2016/03/17 HTML / CSS
英国豪华真皮和布艺沙发销售网站:Darlings of Chelsea
2018/01/05 全球购物
Nip + Fab官网:英国美容品牌
2019/08/26 全球购物
Linux Interview Questions For software testers
2013/05/17 面试题
毕业生个人的自我评价优秀范文
2013/10/03 职场文书
建筑工程专业学生的自我评价
2013/12/25 职场文书
学生党员思想汇报
2013/12/28 职场文书
投资合作协议书范本
2014/04/17 职场文书
护士工作失误检讨书
2014/09/14 职场文书
医院见习报告范文
2014/11/03 职场文书
团员个人总结
2015/02/26 职场文书
治理商业贿赂工作总结
2015/08/10 职场文书
golang如何去除多余空白字符(含制表符)
2021/04/25 Golang
Java 垃圾回收超详细讲解记忆集和卡表
2022/04/08 Java/Android
MySQL创建管理RANGE分区
2022/04/13 MySQL