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 MsSql server时遇到的中文编码问题

但导到sqlserver 2005后,发现其中文编码只支持GB 和 UCS-2(unicode 16),所以直接在数据库中查询显示正确,但使用php的utf9编码显示时则全是乱码。找了大...

php中3des加密代码(完全与.net中的兼容)

复制代码 代码如下: <?php class Crypt3Des { private $key = ""; private $iv = ""; /** * 构造,传递二个已经进行b...

PHP发送邮件确认验证注册功能示例【修改别人邮件类】

PHP发送邮件确认验证注册功能示例【修改别人邮件类】

本文实例讲述了PHP发送邮件确认验证注册功能。分享给大家供大家参考,具体如下: 类库: require "class.phpmailer.php"; require "class....

php基础学习之变量的使用

复制代码 代码如下: <?php //引用 $one="test"; two=&$one;//相当于传地址,两个变量指向一个地址 //动态变量 $one="######";...

PHP中feof()函数实例测试

本文实例讲述了PHP中的feof()函数的用法,针对feof()函数进行了一定的测试,很有实用价值。具体分析如下: 本文实例运行环境: OS:Mac OS X 10.8.4 PHP:5....