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

yipeiwu_com5年前PHP代码库

废话不多说了,直接给大家贴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 之 写时复制介绍(Copy On Write)

PHP 之 写时复制介绍(Copy On Write)

在开始之前,我们可以先看一段简单的代码: 复制代码 代码如下:<?php   //例一     $foo = 1;  &...

关于crontab的使用详解

使用crontab定时执行php代码,例  每隔十分钟执行一次:/10 * * * * wget -q --sqider http://******一、在Crontab中使用P...

PHP递归实现快速排序的方法示例

本文实例讲述了PHP递归实现快速排序的方法。分享给大家供大家参考,具体如下: 首先我们要理解一下快速排序的原理:找到当前数组中的任意一个元素(一般选择第一个元素),作为标准,新建两个空数...

PHP实现导出带样式的Excel

工作中做导出的时候,需要导出自定义的表格或嫌弃导出的Excel格式太难看了。 需要设置颜色、字号大小、加粗、合并单元格等等。 效果图: PHP代码: /** * 导出文件 * @r...

php intval的测试代码发现问题

<?php $o = 0.1; for($a = 1; $a < 100; $a++){ &n...