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实现的杨辉三角求解算法分析

PHP实现的杨辉三角求解算法分析

本文实例讲述了PHP实现的杨辉三角求解算法。分享给大家供大家参考,具体如下: ♥ 前言 对于 杨辉三角 是什么的问题,请参考百度百科的详细解释: 杨辉三角 杨辉三角,是二项...

PHP文件操作实例总结【文件上传、下载、分页】

PHP文件操作实例总结【文件上传、下载、分页】

本文实例讲述了PHP文件操作。分享给大家供大家参考,具体如下: 1、文件上传 上传域: input type="file" 普通文本框(text、password、textarea、r...

php如何调用webservice应用介绍

php如何调用webservice应用介绍

1.1、Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中...

php的sso单点登录实现方法

本文实例讲述了php的sso单点登录实现方法。分享给大家供大家参考。具体分析如下: 这里详细讲到了几点: 1、点击登录跳转到SSO登录页面并带上当前应用的callback地址 2、登录成...

php 查找数组元素提高效率的方法详解

1.php in_array方法说明 PHP查找数组元素是否存在,一般会使用in_array方法。 bool in_array ( mixed $needle , array $hays...