PHP  实现等比压缩图片尺寸和大小实例代码


Posted in PHP onOctober 08, 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 相关文章推荐
PHP5.0对象模型探索之抽象方法和抽象类
Sep 05 PHP
解析PHP多种序列化与反序列化的方法
Jun 06 PHP
PHP 提取图片img标记中的任意属性的简单实例
Dec 10 PHP
php多文件上传实现代码
Feb 20 PHP
Laravel 5框架学习之Eloquent 关系
Apr 09 PHP
如何实现php图片等比例缩放
Jul 28 PHP
php中mkdir()函数的权限问题分析
Sep 24 PHP
PHP实现 APP端微信支付功能
Jun 22 PHP
使用vs code编辑调试php配置的方法
Jan 29 PHP
Swoole4.4协程抢占式调度器详解
May 23 PHP
Laravel开启跨域请求的方法
Oct 13 PHP
php反序列化长度变化尾部字符串逃逸(0CTF-2016-piapiapia)
Feb 15 PHP
Laravel Memcached缓存驱动的配置与应用方法分析
Oct 08 #PHP
yii通过小物件生成view的方法
Oct 08 #PHP
php获取服务器操作系统相关信息的方法
Oct 08 #PHP
Yii2创建多界面主题(Theme)的方法
Oct 08 #PHP
php微信开发之自定义菜单完整流程
Oct 08 #PHP
yii2.0数据库迁移教程【多个数据库同时同步数据】
Oct 08 #PHP
yii2高级应用之自定义组件实现全局使用图片上传功能的方法
Oct 08 #PHP
You might like
用js实现的仿sohu博客更换页面风格(简单版)
2007/03/22 Javascript
将Datatable转化成json发送前台实现思路
2013/09/06 Javascript
textarea 控制输入字符字节数(示例代码)
2013/12/27 Javascript
jQuery中wrapAll()方法用法实例
2015/01/16 Javascript
Nodejs中session的简单使用及通过session实现身份验证的方法
2016/02/04 NodeJs
JS随机洗牌算法之数组随机排序
2016/03/23 Javascript
jQuery select自动选中功能实现方法分析
2016/11/28 Javascript
原生JS实现隐藏显示图片 JS实现点击切换图片效果
2021/01/27 Javascript
js学习总结_基于数据类型检测的四种方式(必看)
2017/07/04 Javascript
jQuery常用选择器详解
2017/07/17 jQuery
Bootstrap modal只加载一次数据的解决办法(推荐)
2017/11/24 Javascript
在create-react-app中使用sass的方法示例
2018/10/01 Javascript
Jquery的Ajax技术使用方法
2019/01/21 jQuery
vue 使用外部JS与调用原生API操作示例
2019/12/02 Javascript
vue 封装面包屑组件教程
2020/11/16 Javascript
windows下安装python paramiko模块的代码
2013/02/10 Python
Python中使用urllib2防止302跳转的代码例子
2014/07/07 Python
python多线程用法实例详解
2015/01/15 Python
Python模拟百度登录实例详解
2016/01/20 Python
pyinstaller打包找不到文件的问题解决
2020/04/15 Python
python实现三壶谜题的示例详解
2020/11/02 Python
如何创建一个Flask项目并进行简单配置
2020/11/18 Python
python快速安装OpenCV的步骤记录
2021/02/22 Python
德国滑雪和户外用品网上商店:XSPO
2019/10/30 全球购物
巴西Bo.Bô官方在线商店:经营奢侈品时尚业务
2020/03/16 全球购物
游戏商店:Eneba
2020/04/25 全球购物
留学推荐信写作指南
2014/01/25 职场文书
小学学习雷锋活动总结
2014/07/03 职场文书
装配出错检讨书
2014/09/23 职场文书
意外死亡赔偿协议书
2014/10/14 职场文书
初中生思想道德自我评价
2015/03/09 职场文书
入党函调证明材料
2015/06/19 职场文书
公司车辆管理制度
2015/08/04 职场文书
祝福语集锦:朋友新店开业祝福语
2019/12/10 职场文书
MySQL通过binlog恢复数据
2021/05/27 MySQL
新手初学Java List 接口
2021/07/07 Java/Android