基于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 相关文章推荐
详细介绍:Apache+PHP+MySQL配置攻略
Sep 05 PHP
傻瓜化配置PHP环境――Appserv
Dec 13 PHP
CI框架源码阅读,系统常量文件constants.php的配置
Feb 28 PHP
destoon找回管理员密码的方法
Jun 21 PHP
php绘制圆形的方法
Jan 24 PHP
php+html5使用FormData对象提交表单及上传图片的方法
Feb 11 PHP
浅谈PHP中Stream(流)
Jun 08 PHP
php封装的单文件(图片)上传类完整实例
Oct 18 PHP
Thinkphp结合ajaxFileUpload实现异步图片传输示例
Mar 13 PHP
php之header的不同用法总结(实例讲解)
Nov 28 PHP
php微信公众号开发之答题连闯三关
Oct 20 PHP
PHP实现随机发放扑克牌
Apr 21 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
地摊中国 - 珍藏老照片
2020/08/18 杂记
php实现两表合并成新表并且有序排列的方法
2014/12/05 PHP
PHP中大于2038年时间戳的问题处理方案
2015/03/03 PHP
TP(thinkPHP)框架多层控制器和多级控制器的使用示例
2018/06/13 PHP
BOM与DOM的区别分析
2010/10/26 Javascript
js实现杯子倒水问题自动求解程序
2013/03/25 Javascript
JavaScript——DOM操作——Window.document对象详解
2016/07/14 Javascript
详解Vue生命周期的示例
2017/03/10 Javascript
详解node.js搭建代理服务器请求数据
2017/04/08 Javascript
几种响应式文字详解
2017/05/19 Javascript
AngularJS实现注册表单验证功能
2017/10/16 Javascript
如何让node运行es6模块文件及其原理详解
2018/12/11 Javascript
微信小程序的tab选项卡的实现效果
2019/05/15 Javascript
JSX在render函数中的应用详解
2019/09/04 Javascript
详解Nuxt.js中使用Element-UI填坑
2019/09/06 Javascript
Vue-cli 移动端布局和动画使用详解
2020/08/10 Javascript
JavaScript实现图片合成下载的示例
2020/11/19 Javascript
python远程登录代码
2008/04/29 Python
在Python中操作字典之setdefault()方法的使用
2015/05/21 Python
python字符串编码识别模块chardet简单应用
2015/06/15 Python
Python实现身份证号码解析
2015/09/01 Python
Python对文件操作知识汇总
2016/05/15 Python
Django Highcharts制作图表
2016/08/27 Python
python urllib urlopen()对象方法/代理的补充说明
2017/06/29 Python
Pandas 数据框增、删、改、查、去重、抽样基本操作方法
2018/04/12 Python
Pytorch 定义MyDatasets实现多通道分别输入不同数据方式
2020/01/15 Python
Python pyautogui模块实现鼠标键盘自动化方法详解
2020/02/17 Python
python中wheel的用法整理
2020/06/15 Python
Python3爬虫带上cookie的实例代码
2020/07/28 Python
python中_del_还原数据的方法
2020/12/09 Python
python 列表推导和生成器表达式的使用
2021/02/01 Python
python读取图片颜色值并生成excel像素画的方法实例
2021/02/19 Python
日本7net购物网:书籍、漫画、杂志、DVD、游戏邮购
2017/02/17 全球购物
利物浦足球俱乐部官方网上商店:Liverpool FC Official Store
2018/01/13 全球购物
工程管理英文求职信
2014/03/18 职场文书
Python自然语言处理之切分算法详解
2021/04/25 Python