php 修改图像大小,PHP中修改图像大小 - PHP - 软件工程|架构设计|网站设计|艺术营销 - 河洛炎黄 - 魏文侯...
PHP中生成图片文件的缩略图,主要利用Imagecopyresampled、imagecopyresized函数分别处理GIF图片和其它类型图片。
生成缩略图的关键是按照比例计算好新图片的宽度和高度。
//根据制定宽度、高度生成图片缩略图
function resizeimage($filename,$width=500,$height=500)
{
//define variable
$oldwidth;
$oldheight;
$oldsize;
$newwidth;
$newheight;
$newsize;
$newfilename;
$imagetype;
$oldimage;
$newimage;
//init variable
$newfilename='';
//create new image
if(file_exists($filename) && preg_match("/s*\.[jpg|gif|jpeg|png]/i",$filename))
{
//根据原始图片大小比例和新的图片宽度高度,计算获取新的实际宽度、高度,主要是为了保持比例
$newsize=makeimagesize($filename,$width,$height);
$newwidth=$newsize['width'];
$newheight=$newsize['height'];
//get new image file name
$newfilename=getpath($filename).getfilename($filename).'resize.'.getextendname($filename);
//get image type 获取文件类型
$imagetype=getimagetype($filename);
//get old image size 获取原始图片大小
$oldsize=getimagesize($filename);
$oldwidth=$oldsize[0];
$oldheight=$oldsize[1];
//get old image content 读取原始图片内容
if($imagetype==IMAGETYPE_GIF)
$oldimage=imagecreatefromgif($filename);
elseif($imagetype==IMAGETYPE_JPEG)
$oldimage=imagecreatefromjpeg($filename);
elseif($imagetype==IMAGETYPE_PNG)
$oldimage=imagecreatefrompng($filename);
else
$oldimage='';
//生成新的图片
if(!empty($oldimage))
{
//create new image
if(function_exists('imagecreatetruecolor') && ($imagetype!=IMAGETYPE_GIF))
{
$newimage=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($newimage,$oldimage,0,0,0,0,$newwidth,$newheight,$oldwidth,$oldheight);
}
else
{
$newimage=imagecreate($newwidth,$newheight);
imagecopyresized($newimage,$oldimage,0,0,0,0,$newwidth,$newheight,$oldwidth,$oldheight);
}
//delete old reisize file
if(file_exists($newfilename))
unlink($newfilename);
//save to file 写入磁盘文件
if($imagetype==IMAGETYPE_GIF)
imagepng($newimage,$newfilename);
elseif($imagetype==IMAGETYPE_JPEG)
imagejpeg($newimage,$newfilename);
elseif($imagetype==IMAGETYPE_PNG)
imagepng($newimage,$newfilename);
else
$newfilename='';
//释放资源
//destroy image resource
imagedestroy($oldimage);
imagedestroy($newimage);
}
else
$newfilename='';
}
//return new filename
return $newfilename;
}
//按照比例计算新的图片大小
function makeimagesize($filename,$width,$height)
{
//define variable
$size;
$oldsize;
$oldwidth;
$oldheight;
$newwidth;
$newheight;
//init variable
$size['width']=0;
$size['height']=0;
//get current image size
$oldsize=getimagesize($filename);
$oldwidth=$oldsize[0];
$oldheight=$oldsize[1];
if($oldwidth>0 && $oldheight>0)
{
$newwidth=$width;
$newheight=round($newwidth*($oldheight/$oldwidth));
if($newheight>$height)
{
$newheight=$height;
$newwidth=round($newheight*($oldwidth/$oldheight));
}
if($newwidth
{
$size['width']=$newwidth;
$size['height']=$newheight;
}
else
{
$size['width']=$oldwidth;
$size['height']=$oldheight;
}
}
return $size;
}
php 修改图像大小,PHP中修改图像大小 - PHP - 软件工程|架构设计|网站设计|艺术营销 - 河洛炎黄 - 魏文侯...相关推荐
- mac电脑怎样修改cad标注尺寸中的数字大小?
安装完CAD后,在使用过程中你发现尺寸标注中的数字太小,想要看清标注尺寸的话需要放大图纸才可以,对于我们的工作而言就会很繁琐,如果此时你也想要将数字字号调大些,却又不知如何操作?快来跟小编学习一下吧! ...
- oracle r修改表名,oracle中修改表名
<<>> answer1: ALTER TABLE old_table_name RENAME TO new_table_name;(大写为系统命令) answer2: SQL ...
- mysql如何限制数据修改密码_MySQL_MySQL数据库中修改密码及访问限制设置详解,MySQL是一个真正的多用户、多 - phpStudy...
MySQL数据库中修改密码及访问限制设置详解 MySQL是一个真正的多用户.多线程SQL数据库服务器.MySQL是以一个客户机/服务器结构的实现,它由一个服务器守护程序mysqld和很多不同的客户程序 ...
- sql修改链接服务器,sqlserver中修改链接服务器
sqlserver中修改链接服务器 内容精选 换一换 RDS for SQL Server实例将默认开启SQL审计功能,并且不支持关闭.SQL审计功能会将对服务级.数据库级.表级的主要变更操作记录进审 ...
- echart vue 图表大小_vue中echarts图表大小适应窗口大小且不需要刷新案例
我就废话不多说了,大家还是直接看代码吧~ 内容如下: // 基于准备好的dom,初始化echarts实例 let myChart = this.$echarts.init(document.getEl ...
- 怎么修改c语言的缓冲区大小,C中的缓冲区大小
我正在使用C Std库方法std :: ostream :: write()观察以下行为. 为了缓冲数据,我正在使用以下C API std::ofstream::rdbuf()->pubsetb ...
- git idea 修改提交信息_idea中修改git提交代码的用户名和邮箱地址
Idea中使用git 命令窗口 依次打开idea->file->settings->tools->Terminal 在shell path 选择git安装目录bin中的bash ...
- python列表修改元素_python list 中修改元素
主要记录下这种用法:columnsList = [column.strip() for column in columnsList] 同样 采用此方式 可以做 replace 等类似的操作 ##在CS ...
- linux文件编码无法修改,在Linux系统中修改文本的字符编码的方法
正如我们所知道的那样,电脑只能够处理低级的二进制值,并不能直接处理字符.当一个文本文件被存储时,文件中的每一个字符都被映射成二进制值,实际存储在硬盘中的正是这些"二进制值".之后当 ...
最新文章
- 六一:如何在Datawhale开源学习小程序中管
- 【C 语言】const 关键字用法 ( 常量指针 - const 在 * 左边 - 修饰数据类型 - 内存不变 | 指针常量 - const 在 * 右边 - 修饰变量 - 指针不变 )
- Linux上常用命令整理(二)—— paste
- Using join buffer (Batched Key Access)
- js学习总结----弹性势能动画之抛物线运动
- MySQL和sql获取当前时间是本月的第几周或本月第一天的问题
- 计算机图形学学习报告,计算机图形学学习报告.doc
- 红袖添香唯美中国风PPT模板
- cocos2dx视频教程进阶篇--第1天--吃西瓜游戏。
- 使用计算机的硬件及参数,硬件参数怎么看?如何选配电脑硬件?
- AD DS 域控与成员计算机的时间一致性
- python判断汉字偏胖_写了2年python,知道 if __name__ == '__main__' 什么意思吗?
- 用 ElementTree 在 Python 中解析 XML
- ubuntu安装ROS运行rosdep init错误,cannot download default sources list from....
- 用户增长——AARRR模型
- Object.entries()方法的使用和实现
- U盘无法格式化(提示U盘文件系统变为了RAW格式)【一般应用】
- Cauchy-Schwarz Inequality
- linux 系统命令被后门修改_Linux、Windows权限维持常用后门学习总结
- Application.java启动项位置不对报错