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);
}
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

Pear DB 新手入门指南教程第1/3页

1. 简介这是一部指导我们如何使用Pear DB扩展。Pear DB,提供这样一系列的类: n 数据库抽象 n 高级错误处理机制 n 以及其它 2. 下载、安装Pear 由于现在Pea...

PHP静态新闻列表自动生成代码

function CreateShtml()  {  ob_start(array("callback_CreateShtml","callback_GoT...

修改PHP脚本使WordPress拦截垃圾评论的方法示例

拦截英文垃圾评论 由于绝大多数的垃圾评论都是英文的,所以国内不少朋友在使用 Some Chinese Please 插件,它可以有效地拦截内容中不带有中文字的comment和trackb...

PHP中strcmp()和strcasecmp()函数字符串比较用法分析

本文实例讲述了PHP中strcmp()和strcasecmp()函数字符串比较用法。分享给大家供大家参考,具体如下: 一、PHP中strcmp()函数用于比较两个字符串(区分大小写),其...

PHP获取字符流中第一个不重复字符的方法

本文实例讲述了PHP获取字符流中第一个不重复字符的方法。分享给大家供大家参考,具体如下: 问题 请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符”...