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 相关文章推荐
10条PHP编程习惯助你找工作
Sep 29 PHP
PHP 第二节 数据类型之转换
Apr 28 PHP
解析argc argv在php中的应用
Jun 24 PHP
解析php开发中的中文编码问题
Aug 08 PHP
php权重计算方法代码分享
Jan 09 PHP
php中getservbyport与getservbyname函数用法实例
Nov 18 PHP
PHP5.5迭代生成器用法实例详解
Mar 16 PHP
浅谈PHP正则中的捕获组与非捕获组
Jul 18 PHP
thinkphp分页集成实例
Jul 24 PHP
使用XHProf查找PHP性能瓶颈的实例
Dec 13 PHP
PDO::getAvailableDrivers讲解
Jan 28 PHP
PHP遍历数组的6种方式总结
Nov 17 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
sony ICF-2010 拆解与改装
2021/03/02 无线电
PHP Document 代码注释规范
2009/04/13 PHP
允许phpmyadmin空密码登录的配置方法
2011/05/29 PHP
php 验证码(倾斜,正弦干扰线,黏贴,旋转)
2013/06/29 PHP
php中unserialize返回false的解决方法
2014/09/22 PHP
CakePHP框架Session设置方法分析
2017/02/23 PHP
php中的explode()函数实例介绍
2019/01/18 PHP
PHP抽象类与接口的区别详解
2019/03/21 PHP
JS 自动安装exe程序
2008/11/30 Javascript
javascript 面向对象编程 万物皆对象
2009/09/17 Javascript
JavaScript中的16进制字符(改进)
2011/11/21 Javascript
基于JavaScript实现鼠标悬浮弹出跟随鼠标移动的带箭头的信息层
2016/01/18 Javascript
微信小程序 教程之wxapp视图容器 swiper
2016/10/19 Javascript
Bootstrap模态框禁用空白处点击关闭
2016/10/20 Javascript
Angular 4环境准备与Angular cli创建项目详解
2017/05/27 Javascript
详解vue配置后台接口方式
2019/03/29 Javascript
JavaScript HTML DOM元素 节点操作汇总
2019/07/29 Javascript
vue实现多级菜单效果
2019/10/19 Javascript
解决Vue 刷新页面导航显示高亮位置不对问题
2019/12/25 Javascript
Vue常用的全选/反选的示例代码
2020/02/19 Javascript
微信小程序订阅消息(java后端实现)开发
2020/06/01 Javascript
解决vue初始化项目一直停在downloading template的问题
2020/11/09 Javascript
Python Django实现layui风格+django分页功能的例子
2019/08/29 Python
下载与当前Chrome对应的chromedriver.exe(用于python+selenium)
2020/01/14 Python
Python 使用双重循环打印图形菱形操作
2020/08/09 Python
详解Anaconda安装tensorflow报错问题解决方法
2020/11/01 Python
阿里旅行:飞猪
2017/01/05 全球购物
重写子类方法时,抛出异常的书写注意事项
2015/10/17 面试题
施工安全生产承诺书
2014/05/23 职场文书
检讨书范文1000字
2015/01/28 职场文书
科技馆观后感
2015/06/08 职场文书
安全教育片观后感
2015/06/17 职场文书
高考百日冲刺决心书
2015/09/23 职场文书
MySQL sql_mode的使用详解
2021/05/08 MySQL
分析JVM源码之Thread.interrupt系统级别线程打断
2021/06/29 Java/Android
Valheim服务器 Mod修改安装教程 【ValheimPlus】
2022/12/24 Servers