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 相关文章推荐
PHPnow安装服务[apache_pn]失败的问题的解决方法
Sep 10 PHP
php URL跳转代码 减少外链
Jun 25 PHP
PHP中文件读、写、删的操作(PHP中对文件和目录操作)
Mar 06 PHP
PHP 二维数组根据某个字段排序的具体实现
Jun 03 PHP
30个php操作redis常用方法代码例子
Jul 05 PHP
Laravel 5 学习笔记
Mar 06 PHP
php利用smtp类实现电子邮件发送
Oct 30 PHP
php计划任务之验证是否有多个进程调用同一个job的方法
Dec 07 PHP
PHP记录和读取JSON格式日志文件
Jul 07 PHP
php实现当前页面点击下载文件的实例代码
Nov 16 PHP
PHP实现的同步推荐操作API接口案例分析
Nov 30 PHP
php之可变变量的实例详解
Sep 12 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和数据库结合的一个简单的web实例 代码分析 (php初学者)
2011/07/28 PHP
PHP获取客户端真实IP地址的5种情况分析和实现代码
2014/07/08 PHP
php判断数组中是否存在指定键(key)的方法
2015/03/17 PHP
php将图片文件转换成二进制输出的方法
2015/06/10 PHP
PHP中isset、empty的用法与区别示例详解
2020/11/05 PHP
jquery根据锚点offset值实现动画切换
2014/09/11 Javascript
js实现使用鼠标拖拽切换图片的方法
2015/05/04 Javascript
js显示文本框提示文字的方法
2015/05/07 Javascript
JS建造者模式基本用法实例分析
2015/06/30 Javascript
JavaScript原型及原型链终极详解
2016/01/04 Javascript
JQuery Mobile 弹出式登录框的实现方法
2016/05/28 Javascript
Bootstrap Navbar Component实现响应式导航
2016/10/08 Javascript
关于AngularJs数据的本地存储详解
2017/01/20 Javascript
JavaScript基础进阶之数组方法总结(推荐)
2017/09/04 Javascript
jQuery与vue实现拖动验证码功能
2018/01/30 jQuery
关于js的三种使用方式(行内js、内部js、外部js)的程序代码
2018/05/05 Javascript
解决angular2 获取到的数据无法实时更新的问题
2018/08/31 Javascript
Vue Prop属性功能与用法实例详解
2019/02/23 Javascript
微信小程序 简易计算器实现代码实例
2019/09/02 Javascript
分享Angular http interceptors 拦截器使用(推荐)
2019/11/10 Javascript
vue-router 控制路由权限的实现
2020/09/24 Javascript
[06:25]第二届DOTA2亚洲邀请赛主赛事第二天比赛集锦.mp4
2017/04/03 DOTA
[01:11:21]DOTA2-DPC中国联赛 正赛 Phoenix vs CDEC BO3 第三场 3月7日
2021/03/11 DOTA
python3实现读取chrome浏览器cookie
2016/06/19 Python
利用arcgis的python读取要素的X,Y方法
2018/12/22 Python
python实现图像检索的三种(直方图/OpenCV/哈希法)
2019/08/08 Python
解决pytorch报错:AssertionError: Invalid device id的问题
2020/01/10 Python
python 进程池pool使用详解
2020/10/15 Python
css3弹性盒子flex实现三栏布局的实现
2020/11/12 HTML / CSS
都柏林通行卡/城市通票:The Dublin Pass
2020/02/16 全球购物
Napapijri西班牙在线商店:夹克、外套、运动衫等
2020/11/05 全球购物
冬季施工防火方案
2014/05/17 职场文书
公司演讲稿开场白
2014/08/25 职场文书
使用php的mail()函数实现发送邮件功能
2021/06/03 PHP
IDEA2021.2配置docker如何将springboot项目打成镜像一键发布部署
2021/09/25 Java/Android
vue中使用mockjs配置和使用方式
2022/04/06 Vue.js