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 相关文章推荐
解决dede生成静态页和动态页转换的一些问题,及火车采集入库生成动态的办法
Mar 29 PHP
用PHP读取flv文件的播放时间长度
Sep 03 PHP
php下载文件源代码(强制任意文件格式下载)
May 09 PHP
Chrome Web App开发小结
Sep 04 PHP
php实现判断访问来路是否为搜索引擎机器人的方法
Apr 15 PHP
PHP Streams(流)详细介绍及使用
May 12 PHP
PHP解密Unicode及Escape加密字符串
May 17 PHP
PHP实现的json类实例
Jul 28 PHP
php+ajax实现带进度条的上传图片功能【附demo源码下载】
Sep 14 PHP
利用PHP访问带有密码的Redis方法示例
Feb 09 PHP
php使用include 和require引入文件的区别
Feb 16 PHP
php面向对象基础详解【星际争霸游戏案例】
Jan 23 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中session使用示例
2014/03/29 PHP
php中最简单的字符串匹配算法
2014/12/16 PHP
php计算函数执行时间的方法
2015/03/20 PHP
javascript 当前日期加(天、周、月、年)
2009/08/09 Javascript
JavaScript Timer实现代码
2010/02/17 Javascript
Javascript模块化编程(一)AMD规范(规范使用模块)
2013/01/17 Javascript
javascript算法题:求任意一个1-9位不重复的N位数在该组合中的大小排列序号
2015/04/01 Javascript
整理Javascript函数学习笔记
2015/12/01 Javascript
详解Javascript模板引擎mustache.js
2016/01/20 Javascript
Node.js插件安装图文教程
2016/05/06 Javascript
详解JavaScript中|单竖杠运算符的使用方法
2016/05/23 Javascript
Javascript对象字面量的理解
2016/06/22 Javascript
用jQuery的AJax实现异步访问、异步加载
2016/11/02 Javascript
使用JS批量选中功能实现更改数据库中的status状态值(批量展示)
2016/11/22 Javascript
手把手教你使用vue-cli脚手架(图文解析)
2017/11/08 Javascript
vue实现提示保存后退出的方法
2018/03/15 Javascript
微信小程序按钮点击跳转页面详解
2019/05/06 Javascript
[02:04]2016国际邀请赛中国区预选赛VG.R晋级之路
2016/07/01 DOTA
Python编程之黑板上排列组合,你舍得解开吗
2017/10/30 Python
python实现发送邮件功能代码
2017/12/14 Python
python+matplotlib绘制3D条形图实例代码
2018/01/17 Python
Python使用正则表达式获取网页中所需要的信息
2018/01/29 Python
Django框架使用内置方法实现登录功能详解
2019/06/12 Python
Django如何批量创建Model
2020/09/01 Python
python将下载到本地m3u8视频合成MP4的代码详解
2020/11/24 Python
python爬虫中url管理器去重操作实例
2020/11/30 Python
床上用品全球在线购物:BeddingInn
2016/12/18 全球购物
英国在线房屋中介网站:Yopa
2018/01/09 全球购物
Supersmart英国:欧洲市场首批食品补充剂供应商之一
2018/05/05 全球购物
SQL Server 2000数据库的文件有哪些,分别进行描述。
2015/11/09 面试题
2014基层党员干部学习全国两会心得体会
2014/03/17 职场文书
给市场的环保建议书
2014/05/14 职场文书
2014教师年度思想工作总结
2014/11/10 职场文书
2014年纪检监察工作总结
2014/11/11 职场文书
撤诉状格式范本
2015/05/19 职场文书
tensorflow学习笔记之tfrecord文件的生成与读取
2021/03/31 Python