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 相关文章推荐
我的群发邮件程序
Oct 09 PHP
用PHP实现多服务器共享SESSION数据的方法
Mar 16 PHP
joomla内置的表单验证功能使用方法
Jun 11 PHP
php 模拟POST|GET操作实现代码
Jul 20 PHP
php读取本地文件常用函数(fopen与file_get_contents)
Sep 09 PHP
Thinkphp搭建包括JS多语言的多语言项目实现方法
Nov 24 PHP
php中异常处理方法小结
Jan 09 PHP
Yii实现简单分页的方法
Apr 29 PHP
php 一维数组的循环遍历实现代码
Apr 10 PHP
php实现websocket实时消息推送
Mar 30 PHP
ThinkPHP5.0框架验证码功能实现方法【基于第三方扩展包】
Mar 11 PHP
PHP执行系统命令函数实例讲解
Mar 03 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
Linux环境下搭建php开发环境的操作步骤
2013/06/17 PHP
CI框架使用composer安装的依赖包步骤与方法分析
2016/11/21 PHP
利用 fsockopen() 函数开放端口扫描器的实例
2017/08/19 PHP
ThinkPHP框架实现的邮箱激活功能示例
2018/06/15 PHP
jquery控制listbox中项的移动并排序的实现代码
2010/09/28 Javascript
IE6弹出“已终止操作”的解决办法
2010/11/27 Javascript
jQuery实现图片信息的浮动显示实例代码
2013/08/28 Javascript
屏蔽相应键盘按钮操作
2014/03/10 Javascript
JavaScript中使用ActiveXObject操作本地文件夹的方法
2014/03/28 Javascript
Jquery操作js数组及对象示例代码
2014/05/11 Javascript
jQuery的:parent选择器定义和用法
2014/07/01 Javascript
教你在heroku云平台上部署Node.js应用
2014/07/30 Javascript
js父页面与子页面不同时显示的方法
2014/10/16 Javascript
详解js私有作用域中创建特权方法
2016/01/25 Javascript
使用PHP+JavaScript将HTML页面转换为图片的实例分享
2016/04/18 Javascript
将鼠标焦点定位到文本框最后(代码分享)
2017/01/11 Javascript
node.js实现复制文本到剪切板的功能
2017/01/23 Javascript
react高阶组件经典应用之权限控制详解
2017/09/07 Javascript
Webstorm2016使用技巧(SVN插件使用)
2018/10/29 Javascript
基于Element封装一个表格组件tableList的使用方法
2020/06/29 Javascript
js实现3D旋转相册
2020/08/02 Javascript
使用python解析xml成对应的html示例分享
2014/04/02 Python
python安装numpy和pandas的方法步骤
2019/05/27 Python
使用python进行波形及频谱绘制的方法
2019/06/17 Python
django 解决扩展自带User表遇到的问题
2020/05/14 Python
新手学python应该下哪个版本
2020/06/11 Python
python动态规划算法实例详解
2020/11/22 Python
Pytorch 图像变换函数集合小结
2021/02/01 Python
详解CSS3 用border写 空心三角箭头 (两种写法)
2017/09/29 HTML / CSS
详解CSS3 弹性布局快速入门
2019/06/06 HTML / CSS
CSS3制作酷炫的条纹背景
2017/11/09 HTML / CSS
美国眼镜网站:LensCrafters
2020/01/19 全球购物
结婚周年感言
2014/02/24 职场文书
Vue中Object.assign清空数据报错的解决方案
2022/03/03 Vue.js
进行数据处理的6个 Python 代码块分享
2022/04/06 Python
JS前端使用canvas实现扩展物体类和事件派发
2022/08/05 Javascript