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 heredoc和phpwind的模板技术使用方法小结

在PHP的文档中,只是提到了echo可以使用如下命令输出多行字符串(而且其中的变量被自动替换): PHP代码 复制代码 代码如下:echo <<<E...

使用Linux五年积累的一些经验技巧

1. bash中的$相关参数 复制代码 代码如下:$0 - 表示当前文件名  $* - 以空格分离所有参数,形成一个字符串  $@ - 以空格分离所有参数,形成一个字...

PHP封装XML和JSON格式数据接口操作示例

PHP封装XML和JSON格式数据接口操作示例

本文实例讲述了PHP封装XML和JSON格式数据接口操作。分享给大家供大家参考,具体如下: 使用PHP开发app后端的接口时,我们需要返回数据,最常用的就是XML和JSON格式的数据,那...

应用开发中涉及到的css和php笔记分享

关于应用中遇到的一些问题及解决办法,做一些记录,以备后用。本人新手,技术还不熟练,笔记比较幼稚。嘲笑请轻笑。   1、关于层的绝对定位。   有两个div层,其中main类div是整体的...

无JS,完全php面向过程数据分页实现代码

复制代码 代码如下: <?php //登陆页面设置的session,当session中存在name时 //session_start(); //$name = $_SESSION[...