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面向对象的使用教程 简单数据库连接
Nov 25 PHP
有关 PHP 和 MySQL 时区的一点总结
Mar 26 PHP
php jquery 实现新闻标签分类与无刷新分页
Dec 18 PHP
一步一步学习PHP(7) php 字符串相关应用
Mar 05 PHP
php结合表单实现一些简单功能的例子
Jun 04 PHP
从康盛产品(discuz)提取出来的模板类
Jun 28 PHP
解析link_mysql的php版
Jun 30 PHP
PHP+MYSQL实现用户的增删改查
Mar 24 PHP
PHP SPL标准库之数据结构栈(SplStack)介绍
May 12 PHP
CodeIgniter表单验证方法实例详解
Mar 03 PHP
php使用pclzip类实现文件压缩的方法(附pclzip类下载地址)
Apr 30 PHP
PHP数组操作实例分析【添加,删除,计算,反转,排序,查找等】
Dec 24 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
全国FM电台频率大全 - 1 北京市
2020/03/11 无线电
全国FM电台频率大全 - 29 青海省
2020/03/11 无线电
PHP中Session的概念
2006/10/09 PHP
php+mysql写的简单留言本实例代码
2008/07/25 PHP
利用phpexcel把excel导入数据库和数据库导出excel实现
2014/01/09 PHP
PHP大转盘中奖概率算法实例
2014/10/21 PHP
PHP中定义数组常量(array常量)的方法
2014/11/17 PHP
javascript 火狐(firefox)不显示本地图片问题解决
2008/07/05 Javascript
Javascript日期对象的dateAdd与dateDiff方法
2008/11/18 Javascript
javascript setAttribute, getAttribute 在不同浏览器上的不同表现
2010/08/05 Javascript
JS 实现导航栏悬停效果(续2)
2013/09/24 Javascript
js实现带圆角的多级下拉菜单效果
2015/08/28 Javascript
jqGrid 学习笔记整理——进阶篇(一 )
2016/04/17 Javascript
JavaScript闭包与作用域链实例分析
2019/01/21 Javascript
vue多次循环操作示例
2019/02/08 Javascript
JavaScript数据结构与算法之基本排序算法定义与效率比较【冒泡、选择、插入排序】
2019/02/21 Javascript
Vue.js桌面端自定义滚动条组件之美化滚动条VScroll
2020/12/01 Vue.js
使用Python导出Excel图表以及导出为图片的方法
2015/11/07 Python
python微信跳一跳系列之棋子定位像素遍历
2018/02/26 Python
浅谈Pandas中map, applymap and apply的区别
2018/04/10 Python
Python RabbitMQ消息队列实现rpc
2018/05/30 Python
Python基于pyCUDA实现GPU加速并行计算功能入门教程
2018/06/19 Python
Python设计模式之桥接模式原理与用法实例分析
2019/01/10 Python
python中break、continue 、exit() 、pass终止循环的区别详解
2019/07/08 Python
Python调用接口合并Excel表代码实例
2020/03/31 Python
python如何调用java类
2020/07/05 Python
将不规则的Python多维数组拉平到一维的方法实现
2021/01/11 Python
吃透移动端 1px的具体用法
2019/12/16 HTML / CSS
Alba Moda德国网上商店:意大利时尚女装销售
2016/11/14 全球购物
台湾东南旅游社网站:东南旅游
2019/02/11 全球购物
向全球直邮输送天然健康产品:iHerb.com
2020/05/03 全球购物
linux面试题参考答案(2)
2015/12/06 面试题
纪念一二九运动演讲稿
2014/09/16 职场文书
老公保证书怎么写
2015/02/26 职场文书
失恋33天观后感
2015/06/11 职场文书
SQL Server Agent 服务无法启动
2022/04/20 SQL Server