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 相关文章推荐
给多个地址发邮件的类
Oct 09 PHP
dedecms中常见问题修改方法总结
Mar 21 PHP
php REMOTE_ADDR之获取访客IP的代码
Apr 22 PHP
做了CDN获取用户真实IP的函数代码(PHP与Asp设置方式)
Apr 13 PHP
PHP验证码函数代码(简单实用)
Sep 29 PHP
destoon常用的安全设置概述
Jun 21 PHP
php插入排序法实现数组排序实例
Feb 16 PHP
php使用ob_flush不能每隔一秒输出原理分析
Jun 02 PHP
简述php环境搭建与配置
Dec 05 PHP
PHP实现的激活用户注册验证邮箱功能示例
Jun 06 PHP
win10 apache配置虚拟主机后localhost无法使用的解决方法
Jan 27 PHP
PHP中有关长整数的一些操作教程
Sep 11 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
Yii数据模型中rules类验证器用法分析
2016/07/15 PHP
JavaScript 判断浏览器类型及版本
2009/02/21 Javascript
jQuery实现的立体文字渐变效果
2010/05/17 Javascript
JavaScript常用全局属性与方法记录积累
2013/07/03 Javascript
JS判断不能为空实例代码
2013/11/26 Javascript
Javascript 函数parseInt()转换时出现bug问题
2014/05/20 Javascript
jQuery实现仿Google首页拖动效果的方法
2015/05/04 Javascript
Javascript函数式编程语言
2015/10/11 Javascript
window.location.reload 刷新使用分析(去对话框)
2015/11/11 Javascript
this,this,再次讨论javascript中的this,超全面(经典)
2016/01/05 Javascript
基于AGS JS开发自定义贴图图层
2017/03/31 Javascript
Angular中的ng-template及angular 使用ngTemplateOutlet 指令的方法
2018/08/08 Javascript
javascript将非数值转换为数值
2018/09/13 Javascript
Jquery 获取相同NAME 或者id删除行操作
2020/08/24 jQuery
jQuery+ajax实现文件上传功能
2020/12/22 jQuery
Python的类实例属性访问规则探讨
2015/01/30 Python
详解在Python的Django框架中创建模板库的方法
2015/07/20 Python
Python常见格式化字符串方法小结【百分号与format方法】
2016/09/18 Python
Python+Pyqt实现简单GUI电子时钟
2021/02/22 Python
python如何保证输入键入数字的方法
2019/08/23 Python
python3 webp转gif格式的实现示例
2019/12/10 Python
Python序列化pickle模块使用详解
2020/03/05 Python
Python爬虫实现自动登录、签到功能的代码
2020/08/20 Python
CSS3田字格列表的样式编写方法
2018/11/22 HTML / CSS
美国知名的摄影器材销售网站:Adorama
2017/02/01 全球购物
Booking.com英国官网:全球酒店在线预订网站
2018/04/21 全球购物
欧洲当代手工玻璃和瓷器的领先品牌:LSA International
2018/06/03 全球购物
说说你所熟悉或听说过的j2ee中的几种常用模式?及对设计模式的一些看法
2012/05/24 面试题
保险公司年会主持词
2014/03/22 职场文书
农业开发项目建议书
2014/05/16 职场文书
经理任命书模板
2014/06/06 职场文书
政府个人对照检查材料思想汇报
2014/10/08 职场文书
超搞笑婚前保证书
2015/05/08 职场文书
企业文化学习心得体会
2016/01/21 职场文书
Python使用psutil库对系统数据进行采集监控的方法
2021/08/23 Python
JavaScript的Set数据结构详解
2022/02/18 Javascript