基于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 相关文章推荐
Zend 输出产生XML解析错误
Mar 03 PHP
PHP的explode和implode的使用说明
Jul 17 PHP
使用JSON实现数据的跨域传输的php代码
Dec 20 PHP
destoon二次开发常用数据库操作
Jun 21 PHP
Twig模板引擎用法入门教程
Jan 20 PHP
Laravel中基于Artisan View扩展包创建及删除应用视图文件的方法
Oct 08 PHP
visual studio code 调试php方法(图文详解)
Sep 15 PHP
Lumen timezone 时区设置方法(慢了8个小时)
Jan 20 PHP
php 后端实现JWT认证方法示例
Sep 04 PHP
Yii2框架操作数据库的方法分析【以mysql为例】
May 27 PHP
Laravel 微信小程序后端搭建步骤详解
Nov 26 PHP
php5.3/5.4/5.5/5.6/7常见新增特性汇总整理
Feb 27 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
PHP中文URL编解码(urlencode()rawurlencode()
2010/07/03 PHP
ThinkPHP5.0框架控制器继承基类和自定义类示例
2018/05/25 PHP
JavaScript 异步调用框架 (Part 1 - 问题 &amp; 场景)
2009/08/03 Javascript
javascript中的array数组使用技巧
2010/01/31 Javascript
textarea 控制输入字符字节数(示例代码)
2013/12/27 Javascript
浅谈js内置对象Math的属性和方法(推荐)
2016/09/19 Javascript
js实现加载更多功能实例
2016/10/27 Javascript
seajs下require书写约定实例分析
2018/05/16 Javascript
详解js静态检查工具eslint配置文件
2018/11/23 Javascript
jQuery实现带3D切割效果的轮播图功能示例【附源码下载】
2019/04/04 jQuery
[38:30]2014 DOTA2国际邀请赛中国区预选赛 LGD-GAMING VS CIS 第一场2
2014/05/24 DOTA
详解Python的迭代器、生成器以及相关的itertools包
2015/04/02 Python
python无限生成不重复(字母,数字,字符)组合的方法
2018/12/04 Python
利用ctypes获取numpy数组的指针方法
2019/02/12 Python
Python代码使用 Pyftpdlib实现FTP服务器功能
2019/07/22 Python
python GUI库图形界面开发之PyQt5单选按钮控件QRadioButton详细使用方法与实例
2020/02/28 Python
解决pycharm下pyuic工具使用的问题
2020/04/08 Python
基于Python词云分析政府工作报告关键词
2020/06/02 Python
详解python 内存优化
2020/08/17 Python
Python 实现PS滤镜中的径向模糊特效
2020/12/03 Python
python中的列表和元组区别分析
2020/12/30 Python
python 使用xlsxwriter循环向excel中插入数据和图片的操作
2021/01/01 Python
pytho matplotlib工具栏源码探析一之禁用工具栏、默认工具栏和工具栏管理器三种模式的差异
2021/02/25 Python
CSS3中animation实现流光按钮效果
2020/12/21 HTML / CSS
Eastbay官网:美国最大的运动鞋网络零售商
2016/07/27 全球购物
美国指甲油品牌:Deco Miami
2017/01/30 全球购物
DC Shoes澳大利亚官方网上商店:购买DC鞋子
2019/10/25 全球购物
超市仓管员岗位职责
2014/04/07 职场文书
航空学院求职信
2014/06/11 职场文书
2014年光棍节活动策划方案(创意集锦)
2014/09/29 职场文书
课外活动总结
2015/02/04 职场文书
教师文明餐桌光盘行动倡议书
2015/04/28 职场文书
python 下载文件的几种方式分享
2021/04/07 Python
浅谈Python数学建模之数据导入
2021/06/23 Python
Django中session进行权限管理的使用
2021/07/09 Python
PO模式在selenium自动化测试框架的优势
2022/03/20 Python