基于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
提高PHP编程效率的53个要点(经验小结)
Sep 04 PHP
php 保留字列表
Oct 04 PHP
基于php设计模式中工厂模式详细介绍
May 15 PHP
PHP flush()与ob_flush()的区别详解
Jun 03 PHP
mcrypt启用 加密以及解密过程详细解析
Aug 07 PHP
用PHP和Shell写Hadoop的MapReduce程序
Apr 15 PHP
php实现的替换敏感字符串类实例
Sep 22 PHP
php在windows环境下获得cpu内存实时使用率(推荐)
Feb 08 PHP
网站被恶意镜像怎么办 php一段代码轻松搞定(全面版)
Oct 23 PHP
PHP判断是否微信访问的方法示例
Mar 27 PHP
PHP工厂模式、单例模式与注册树模式实例详解
Jun 03 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
thinkPHP中钩子的使用方法实例分析
2017/11/16 PHP
PHP基于rabbitmq操作类的生产者和消费者功能示例
2018/06/16 PHP
详细讲解JS节点知识
2010/01/31 Javascript
jquery下为Event handler传递动态参数的代码
2011/01/06 Javascript
jQuery效果 slideToggle() 方法(在隐藏和显示之间切换)
2011/06/28 Javascript
用innerhtml提高页面打开速度的方法
2013/08/02 Javascript
Js+Jq获取URL参数的集中方法示例代码
2014/05/20 Javascript
JS完整获取IE浏览器信息包括类型、版本、语言等等
2014/05/22 Javascript
Jquery easyui 实现动态树
2015/11/17 Javascript
Bootstrap每天必学之表格
2015/11/23 Javascript
原生js和jquery分别实现横向导航菜单效果
2016/05/13 Javascript
Highcharts入门之基本属性
2016/08/02 Javascript
详解Node.js中exports和module.exports的区别
2017/04/19 Javascript
nodejs集成sqlite使用示例
2017/06/05 NodeJs
React Native使用Modal自定义分享界面的示例代码
2017/10/31 Javascript
vue-router路由懒加载的实现(解决vue项目首次加载慢)
2018/08/28 Javascript
vue进入页面时滚动条始终在底部代码实例
2019/03/26 Javascript
微信小程序上传帖子的实例代码(含有文字图片的微信验证)
2020/07/11 Javascript
Python实现竖排打印传单手机号码易撕条
2015/03/16 Python
详解Python中的Descriptor描述符类
2016/06/14 Python
python 给DataFrame增加index行名和columns列名的实现方法
2018/06/08 Python
python编写简易聊天室实现局域网内聊天功能
2018/07/28 Python
Pycharm无法使用已经安装Selenium的解决方法
2018/10/13 Python
对Python中Iterator和Iterable的区别详解
2018/10/18 Python
python读取txt文件,去掉空格计算每行长度的方法
2018/12/20 Python
html5的canvas元素使用方法介绍(画矩形、画折线、圆形)
2014/04/14 HTML / CSS
80年代复古T恤:TruffleShuffle
2018/07/02 全球购物
Farfetch美国:奢侈品牌时尚购物平台
2019/05/02 全球购物
电钳专业个人求职信
2014/01/04 职场文书
高中生期末评语
2014/01/28 职场文书
人力资源经理的岗位职责
2014/03/02 职场文书
祖国在我心中演讲稿200字
2014/08/28 职场文书
党员群众路线整改措施及今后努力方向
2014/10/28 职场文书
幼儿园安全教育月活动总结
2015/05/08 职场文书
行政答辩状范文
2015/05/21 职场文书
小程序后台PHP版本部署运行 LNMP+WNMP
2021/04/01 Servers