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数组操作汇总 php数组的使用技巧
Jul 17 PHP
基于php冒泡排序算法的深入理解
Jun 09 PHP
ThinkPHP表单自动提交验证实例教程
Jul 18 PHP
PDO防注入原理分析以及使用PDO的注意事项总结
Oct 23 PHP
PHP内存缓存Memcached类实例
Dec 08 PHP
PHP与Ajax相结合实现登录验证小Demo
Mar 16 PHP
详解PHP实现定时任务的五种方法
Jul 25 PHP
PHP用PDO如何封装简单易用的DB类详解
Jul 30 PHP
phpcms实现验证码替换及phpcms实现全站搜索功能教程详解
Dec 13 PHP
PHP实现二维数组按照指定的字段进行排序算法示例
Apr 23 PHP
YII框架常用技巧总结
Apr 27 PHP
is_file和file_exists效率比较
Mar 14 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
php获取远程文件内容的函数
2015/11/02 PHP
PHP下的浮点运算不准的解决方法
2016/10/27 PHP
PHP生成推广海报的方法分享
2018/04/22 PHP
PHP PDOStatement::fetchObject讲解
2019/02/01 PHP
在PHP中实现使用Guzzle执行POST和GET请求
2019/10/15 PHP
一个js写的日历(代码部分网摘)
2009/09/20 Javascript
Javascript和Java获取各种form表单信息的简单实例
2014/02/14 Javascript
js鼠标点击图片实现随机变换图片的方法
2015/02/16 Javascript
javascript结合CSS实现苹果开关按钮特效
2015/04/07 Javascript
CSS中position属性之fixed实现div居中
2015/12/14 Javascript
javascript设置和获取cookie的方法实例详解
2016/01/05 Javascript
String字符串截取的四种方式总结
2016/11/28 Javascript
完美的js图片轮换效果
2017/02/05 Javascript
基于jQuery实现的单行公告活动轮播效果
2017/08/23 jQuery
webpack4+Vue搭建自己的Vue-cli项目过程分享
2018/08/29 Javascript
详解Vue.js和layui日期控件冲突问题解决办法
2019/07/25 Javascript
微信小程序全局变量GLOBALDATA的定义和调用过程解析
2019/09/23 Javascript
Vue实现背景更换颜色操作
2020/07/17 Javascript
Vue性能优化的方法
2020/07/30 Javascript
[02:42]DOTA2英雄基础教程 杰奇洛
2013/12/23 DOTA
Python中使用摄像头实现简单的延时摄影技术
2015/03/27 Python
日常整理python执行系统命令的常见方法(全)
2015/10/22 Python
举例讲解Python中的死锁、可重入锁和互斥锁
2015/11/05 Python
Python中模块string.py详解
2017/03/12 Python
windows下python安装paramiko模块和pycrypto模块(简单三步)
2017/07/06 Python
Python 判断 有向图 是否有环的实例讲解
2018/02/01 Python
Flask框架中request、请求钩子、上下文用法分析
2019/07/23 Python
python3读取csv文件任意行列代码实例
2020/01/13 Python
对CSS3选择器的研究(详解)
2016/09/16 HTML / CSS
网页布局中CSS样式无效的十个重要原因详解
2017/08/10 HTML / CSS
精彩广告词大全
2014/03/19 职场文书
2015年公务员转正工作总结
2015/04/24 职场文书
老公出轨后的保证书
2015/05/08 职场文书
党员转正大会主持词
2015/07/02 职场文书
借钱欠条怎么写
2015/07/03 职场文书
我的中国梦心得体会范文
2016/01/05 职场文书