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

yipeiwu_com6年前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的4种常见运行方式

SAPI:Server Application Programming Interface服务端应用编程端口。他就是php与其他应用交互的接口,php脚本要执行有很多中方式,通过web服...

验证token、回复图文\文本、推送消息的实用微信类php代码

本文实例为大家分享了用于验证token,回复图文、文本,向用户推送消息等功能的微信类,具体代码如下 <?php class Wechat{ private $dat...

php中的时间显示

 方法1:  //list($first,$second)=explode(" ",$date_temp);     ...

PHP实现的简单组词算法示例

本文实例讲述了PHP实现的简单组词算法。分享给大家供大家参考,具体如下: <?php //组词算法 function diyWords($arr,$m){ $res...

dedecms中常见问题修改方法总结

新手常见问题,此贴是早些时候我发的,在3.1正式版中.有些问题已得到修正,不过仍可作为修改或学习参考 请大家花点耐心看完这个吧,这些问题很多人都有碰到.何必要不停的发贴子问呢? 另外,看...