PHP 实现缩略图


Posted in PHP onMarch 09, 2021
/*
$uploaded - 已上传的文件,可以理解为原图片
$uptype - 图片类型
$filename - 生成的缩略图的文件名(可包含路径)
$a_width - 缩略图宽度
$a_height - 缩略图高度
*/
	function creat_thumb($uploaded,$uptype,$filename,$a_width,$a_height)
	{
		$im = '';

		if($uptype == 'image/pjpeg' || $uptype == 'image/jpeg')
		{
			$im = imagecreatefromjpeg($uploaded);
		}
		else if($uptype == 'image/x-png' || $uptype == 'image/png')
		{
			$im = imagecreatefrompng($uploaded); 
		}
		else if($uptype == 'image/gif')
		{
			$im = imagecreatefromgif($uploaded); 
		}

		$width = imagesx($im); 
		$height = imagesy($im);

		//确保原图比要生成的缩略图宽高要大
		//计算宽高比例,哪个值大就按照哪个作为基准
		//如果宽高相等,则忽略
		if($width > $a_width || $height > $a_height)
		{
			if($width >= $height)
			{
				$newwidth = $a_width;
				$newheight = ($height * $a_width) / $width;
				
				
				$nx = 0;
				$ny = 0;
			}
			else
			{
				$newheight = $a_height;
				$newwidth = ($width * $a_height) / $height;
				$nx = 0;
				$ny = 0;
			}
			
			if(function_exists("imagecopyresampled"))
			{ 
				$newim = imagecreatetruecolor($newwidth, $newheight);
				if($uptype == 'image/x-png' || $uptype == 'image/png')
				{
					$alpha = imagecolorallocatealpha($newim, 0, 0, 0, 127);
					imagefill($newim, 0, 0, $alpha);
				}
				imagecopyresampled($newim, $im,  0, 0,$nx,$ny,  $newwidth, $newheight, $width, $height); 
			}
			else
			{ 
				$newim = imagecreate($newwidth, $newheight); 
				imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 
			}
			if($uptype == 'image/x-png' || $uptype == 'image/png')
			{
				imagesavealpha($newim, true);
				imagepng($newim,$filename);
			}
			else
			{
				imagejpeg($newim,$filename); 
			}
			imagedestroy($newim);
		}
	}

 

PHP 相关文章推荐
PHP入门
Oct 09 PHP
PHP开发中四种查询返回结果分析
Jan 02 PHP
php中日期加减法运算实现代码
Dec 08 PHP
PHP基础知识回顾
Aug 16 PHP
解析smarty模板中类似for的功能实现
Jun 18 PHP
php数组中包含中文的排序方法
Jun 03 PHP
ThinkPHP3.1新特性之Action参数绑定
Jun 19 PHP
php运行提示:Fatal error Allowed memory size内存不足的解决方法
Dec 17 PHP
php用户注册时常用的检验函数实例总结
Dec 22 PHP
php中socket通信机制实例详解
Jan 03 PHP
PHP字符串逆序排列实现方法小结【strrev函数,二分法,循环法,递归法】
Jan 13 PHP
老生常谈PHP面向对象之标识映射
Jun 21 PHP
PHP 裁剪图片
Mar 09 #PHP
PHP 使用位运算实现四则运算的代码
Mar 09 #PHP
让你的PHP,APACHE,NGINX支持大文件上传
Mar 09 #PHP
PHP常用字符串输出方法分析(echo,print,printf及sprintf)
Mar 09 #PHP
PHP中echo与print区别点整理
Mar 09 #PHP
PHP filter_var() 函数, 验证判断EMAIL,URL等
Mar 09 #PHP
PHP读取文件或采集时解决中文乱码
Mar 09 #PHP
You might like
收音机的保养
2021/03/01 无线电
PHP使用NuSOAP调用Web服务的方法
2015/07/18 PHP
PHP实现简单数字分页效果
2015/07/26 PHP
PHP文件打开关闭及读写操作示例解析
2020/08/06 PHP
window.onload 加载完毕的问题及解决方案(下)
2009/07/09 Javascript
高性能WEB开发 flush让页面分块,逐步呈现 flush让页面分块,逐步呈现
2010/06/19 Javascript
JavaScript让网页出现渐隐渐显背景颜色的方法
2015/04/21 Javascript
ztree获取选中节点时不能进入可视区域出现BUG如何解决
2015/12/03 Javascript
jQuery Mobile开发中日期插件Mobiscroll使用说明
2016/03/02 Javascript
Node.js插件安装图文教程
2016/05/06 Javascript
Bootstrap CDN和本地化环境搭建
2016/10/26 Javascript
NodeJS和BootStrap分页效果的实现代码
2016/11/07 NodeJs
JavaScript队列、优先队列与循环队列
2016/11/14 Javascript
JS如何实现在页面上快速定位(锚点跳转问题)
2017/08/14 Javascript
Node.js使用cookie保持登录的方法
2018/05/11 Javascript
Vue动态控制input的disabled属性的方法
2018/06/26 Javascript
JavaScript设计模式之职责链模式应用示例
2018/08/07 Javascript
vue基于两个计算属性实现选中和全选功能示例
2019/02/08 Javascript
vue项目中微信登录的实现操作
2020/09/08 Javascript
python在Windows8下获取本机ip地址的方法
2015/03/14 Python
Python判断直线和矩形是否相交的方法
2015/07/14 Python
Python MD5加密实例详解
2017/08/02 Python
Python实现的视频播放器功能完整示例
2018/02/01 Python
python实现批量修改图片格式和尺寸
2018/06/07 Python
详解python配置虚拟环境
2019/04/08 Python
python实现简单井字棋小游戏
2020/03/05 Python
将keras的h5模型转换为tensorflow的pb模型操作
2020/05/25 Python
Python新建项目自动添加介绍和utf-8编码的方法
2020/12/26 Python
Black Halo官方网站:购买连衣裙、礼服和连体裤
2018/06/13 全球购物
意大利奢侈品多品牌集合店:TheDoubleF
2019/08/24 全球购物
MAC彩妆澳洲官网:M·A·C AU
2021/01/17 全球购物
如何用Lucene索引数据库
2016/02/23 面试题
客户代表自我评价范例
2013/09/24 职场文书
甜品店的创业计划书范文
2014/01/02 职场文书
2014年学前班工作总结
2014/12/08 职场文书
PyTorch中的torch.cat简单介绍
2022/03/17 Python