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中配置文件操作 如config.php文件的读取修改等操作

复制代码 代码如下: <?php $name="admin";//kkkk $bb='234'; $db=4561321; $kkk="admin"; ?> 函数定义: 配...

php类自动加载器实现方法

本文实例讲述了php类自动加载器实现方法。分享给大家供大家参考。具体如下: 这里autoload 可兼容以下格式: Cache_File_Json class_xxx.php xxx...

PHP定时自动生成静态HTML的实现代码

但定时生成就受到了一些局限性,大家如果有独立服务器的能在服务器上设置计划任务,但如果是使用虚拟主机的可就不好办了.虽然方法非常多.但使用起来简便容易的,我觉得还是先判断已生成的首页文件的...

php+ajax 文件上传代码实例

本文实例为大家分享了php+ajax 文件上传的具体代码,供大家参考,具体内容如下 html 代码 <form action="{pboot:form fcode=8}" m...

总结PHP内存释放以及垃圾回收

引用赋值 $a = 'apple'; $b = &$a; 上述代码中,我将一个字符串赋值给变量a,然后将a的引用赋值给了变量b。显然,这个时候的内存指向应该是这样的: $a -...