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 相关文章推荐
在线竞拍系统的PHP实现框架(二)
Oct 09 PHP
用文本文件制作留言板提示(上)
Oct 09 PHP
PHP define函数的使用说明
Aug 27 PHP
zend framework配置操作数据库实例分析
Dec 06 PHP
php基于表单密码验证与HTTP验证用法实例
Jan 06 PHP
PHP中Enum(枚举)用法实例详解
Dec 07 PHP
php中static 静态变量和普通变量的区别
Dec 01 PHP
PHP下 Mongodb 连接远程数据库的实例代码
Aug 30 PHP
PHP实现按之字形顺序打印二叉树的方法
Jan 16 PHP
thinkPHP利用ajax异步上传图片并显示、删除的示例
Sep 26 PHP
PHP的PDO大对象(LOBs)
Jan 27 PHP
laravel 出现command not found问题的解决方案
Oct 23 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
ThinkPHP中的create方法与自动令牌验证实例教程
2014/08/22 PHP
两千行代码的PHP学习笔记汇总
2014/10/05 PHP
PHP中的类型约束介绍
2015/05/11 PHP
PHP排序算法之堆排序(Heap Sort)实例详解
2018/04/21 PHP
PHP获取数据库表中的数据插入新的表再原删除数据方法
2018/10/12 PHP
JS 建立对象的方法
2007/04/21 Javascript
javascript 点击整页变灰的效果(可做退出效果)。
2008/01/09 Javascript
Javascript的构造函数和constructor属性
2010/01/09 Javascript
JavaScript中对循环语句的优化技巧深入探讨
2014/06/06 Javascript
jQuery实现强制cookie过期方法汇总
2015/05/22 Javascript
更靠谱的H5横竖屏检测方法(js代码)
2016/09/13 Javascript
jQuery post数据至ashx实例详解
2016/11/18 Javascript
从零开始学习Node.js系列教程四:多页面实现的数学运算示例
2017/04/13 Javascript
解决AjaxFileupload 上传时会出现连接重置的问题
2017/07/07 Javascript
js下拉菜单生成器dropMenu使用方法详解
2017/08/01 Javascript
jQuery实现获取table中鼠标click点击位置行号与列号的方法
2017/10/09 jQuery
jQuery中each方法的使用详解
2018/03/18 jQuery
Vue.js最佳实践(五招助你成为vuejs大师)
2018/05/04 Javascript
vue监听input标签的value值方法
2018/08/27 Javascript
微信小程序中data-key属性之数据传输(经验总结)
2020/08/22 Javascript
VUE中setTimeout和setInterval自动销毁案例
2020/09/07 Javascript
[01:08]DOTA2次级职业联赛 - Wings 战队宣传片
2014/12/01 DOTA
详解python时间模块中的datetime模块
2016/01/13 Python
python适合人工智能的理由和优势
2019/06/28 Python
python读出当前时间精度到秒的代码
2019/07/05 Python
解决Python中回文数和质数的问题
2019/11/24 Python
Python 使用 environs 库定义环境变量的方法
2020/02/25 Python
keras绘制acc和loss曲线图实例
2020/06/15 Python
Django数据统计功能count()的使用
2020/11/30 Python
中国旅游网站:途牛旅游网
2019/09/29 全球购物
文明演讲稿范文
2014/05/12 职场文书
初中作文评语
2014/12/25 职场文书
个人廉政承诺书
2015/04/28 职场文书
党员转正大会主持词
2015/07/02 职场文书
严以用权专题学习研讨会发言材料
2015/11/09 职场文书
Java字符缓冲流BufferedWriter
2022/04/09 Java/Android