基于PHP实现等比压缩图片大小


Posted in PHP onMarch 04, 2016

废话不多说了,直接给大家贴php等比压缩图片大小的相关代码了,具体代码如下所示:

<?php
$im = imagecreatefromjpeg('D:\phpplace\.jpeg');
resizeImage($im,,,'xinde','.jpg');
function resizeImage($im,$maxwidth,$maxheight,$name,$filetype)
{
$pic_width = imagesx($im);
$pic_height = imagesy($im);
echo "start-----------------" ;
if(($maxwidth && $pic_width > $maxwidth) && ($maxheight && $pic_height > $maxheight))
{
if($maxwidth && $pic_width>$maxwidth)
{
$widthratio = $maxwidth/$pic_width;
$resizewidth_tag = true;
}
if($maxheight && $pic_height>$maxheight)
{
$heightratio = $maxheight/$pic_height;
$resizeheight_tag = true;
}
if($resizewidth_tag && $resizeheight_tag)
{
if($widthratio<$heightratio)
$ratio = $widthratio;
else
$ratio = $heightratio;
}
if($resizewidth_tag && !$resizeheight_tag)
$ratio = $widthratio;
if($resizeheight_tag && !$resizewidth_tag)
$ratio = $heightratio;
$newwidth = $pic_width * $ratio;
$newheight = $pic_height * $ratio;
if(function_exists("imagecopyresampled"))
{
$newim = imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($newim,$im,,,,,$newwidth,$newheight,$pic_width,$pic_height);
}
else
{
$newim = imagecreate($newwidth,$newheight);
imagecopyresized($newim,$im,,,,,$newwidth,$newheight,$pic_width,$pic_height);
}
$name = $name.$filetype;
imagejpeg($newim,$name);
imagedestroy($newim);
}
else
{
$name = $name.$filetype;
imagejpeg($im,$name);
} 
}

以上代码内容是小编给大家介绍的基于PHP实现等比压缩图片大小的相关内容,代码简单易懂,哪里写的不好,欢迎各位大侠多多提出宝贵意见,小编非常乐意。

PHP 相关文章推荐
PHP新手上路(四)
Oct 09 PHP
Smarty结合Ajax实现无刷新留言本实例
Jan 02 PHP
php下防止单引号,双引号在接受页面转义的设置方法
Sep 25 PHP
php递归实现无限分类生成下拉列表的函数
Aug 08 PHP
PHP、Nginx、Apache中禁止网页被iframe引用的方法
Oct 01 PHP
php将HTML表格每行每列转为数组实现采集表格数据的方法
Apr 03 PHP
PHP工程师VIM配置分享
Dec 15 PHP
基于PHP实现数据分页显示功能
May 26 PHP
php curl上传、下载、https登陆实现代码
Jul 23 PHP
php判断目录存在的简单方法
Sep 26 PHP
Laravel5.5 手动分页和自定义分页样式的简单实现
Oct 15 PHP
php的对象传值与引用传值代码实例讲解
Feb 26 PHP
Zend Framework教程之模型Model基本规则和使用方法
Mar 04 #PHP
Zend Framework教程之Zend_Layout布局助手详解
Mar 04 #PHP
php mailer类调用远程SMTP服务器发送邮件实现方法
Mar 04 #PHP
PHP使用curl模拟post上传及接收文件的方法
Mar 04 #PHP
PHP生成和获取XML格式数据的方法
Mar 04 #PHP
PHP使用fopen与file_get_contents读取文件实例分享
Mar 04 #PHP
PHP截取IE浏览器并缩小原图的方法
Mar 04 #PHP
You might like
2019年中国咖啡业现状与发展趋势
2021/03/04 咖啡文化
PHP遍历数组的几种方法
2012/03/22 PHP
php的$_FILES的临时储存文件与回收机制实测过程
2013/07/12 PHP
php实现改变图片直接打开为下载的方法
2015/04/14 PHP
php如何执行非缓冲查询API
2016/07/22 PHP
js apply/call/caller/callee/bind使用方法与区别分析
2009/10/28 Javascript
js 通用javascript函数库整理
2011/08/14 Javascript
原生js写的放大镜效果
2012/08/22 Javascript
JavaScript 在网页上单击鼠标的地方显示层及关闭层
2012/12/30 Javascript
JQuery遍历DOM节点的方法
2015/06/11 Javascript
JavaScript第一篇之实现按钮全选、功能
2016/08/21 Javascript
简单实现JS计算器功能
2016/12/21 Javascript
angularjs2 ng2 密码隐藏显示的实例代码
2017/08/01 Javascript
Vue2.0+ElementUI实现表格翻页的实例
2017/10/23 Javascript
JavaScript 中使用 Generator的方法
2017/12/29 Javascript
vue的mixins属性详解
2018/03/14 Javascript
基于vue-element组件实现音乐播放器功能
2018/05/06 Javascript
Vue动态生成表格的行和列
2019/07/18 Javascript
d3.js 地铁轨道交通项目实战
2019/11/27 Javascript
[05:39]2014DOTA2国际邀请赛 DK晋级胜者组专访战队国士无双
2014/07/14 DOTA
[02:15]2015国际邀请赛选手档案IG.Ferrari 430
2015/07/30 DOTA
[01:03:33]Alliance vs TNC 2019国际邀请赛小组赛 BO2 第一场 8.16
2019/08/18 DOTA
Python解析树及树的遍历
2016/02/03 Python
详解Python进程间通信之命名管道
2017/08/28 Python
机器学习python实战之决策树
2017/11/01 Python
python多线程之事件Event的使用详解
2018/04/27 Python
浅谈python日志的配置文件路径问题
2018/04/28 Python
一百多行python代码实现抢票助手
2018/09/25 Python
Python3.5文件修改操作实例分析
2019/05/01 Python
python提取xml里面的链接源码详解
2019/10/15 Python
Python求两个字符串最长公共子序列代码实例
2020/03/05 Python
Pycharm IDE的安装和使用教程详解
2020/04/30 Python
使用HTML和CSS3绘制基本卡通图案的示例分享
2015/11/06 HTML / CSS
应届毕业生求职信范例分享
2013/12/17 职场文书
保险公司晨会主持词
2014/03/22 职场文书
Python自动化之批量处理工作簿和工作表
2021/06/03 Python