当前位置:首页 » 文件管理 » php图片上传压缩

php图片上传压缩

发布时间: 2025-10-06 21:58:44

‘壹’ 求php图片缩放处理函数

在PHP网站开发过程中,如果建立的网站涉及大量的图片处理,必然涉及到图片的上传和缩放,保持图片不失真,进行图片缩放。使用之前需要下载安装GD库,以支持PHP图片处理。下面结合代码讲解具体的PHP图片缩放处理的思路。
function resizeImage($im,$maxwidth,$maxheight,$name,$filetype)
{
$pic_width = imagesx($im);
$pic_height = imagesy($im);

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("imageresampled"))
{
$newim = imagecreatetruecolor($newwidth,$newheight);
imageresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
}
else
{
$newim = imagecreate($newwidth,$newheight);
imageresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
}

$name = $name.$filetype;
imagejpeg($newim,$name);
imagedestroy($newim);
}
else
{
$name = $name.$filetype;
imagejpeg($im,$name);
}
}
参数说明:
$im 图片对象,应用函数之前,需要用imagecreatefromjpeg()读取图片对象,如果PHP环境支持PNG,GIF,也可使用imagecreatefromgif(),imagecreatefrompng();
$maxwidth 定义生成图片的最大宽度(单位:像素)
$maxheight 生成图片的最大高度(单位:像素)
$name 生成的图片名
$filetype 最终生成的图片类型(.jpg/.png/.gif)
代码注释:
第3~4行:读取需要缩放的图片实际宽高
第8~26行:通过计算实际图片宽高与需要生成图片的宽高的压缩比例最终得出进行图片缩放是根据宽度还是高度进行缩放,当前程序是根据宽度进行图片缩放。如果想根据高度进行图片缩放,可以将第22行的语句改成$widthratio>$heightratio
第28~31行:如果实际图片的长度或者宽度小于规定生成图片的长度或者宽度,则要么根据长度进行图片缩放,要么根据宽度进行图片缩放。
第33~34行:计算最终缩放生成的图片长宽。
第36~45行:根据计算出的最终生成图片的长宽改变图片大小,有两种改变图片大小的方法:ImageCopyResized()函数在所有GD版本中有效,但其缩放图像的算法比较粗糙。ImageCopyResamples(),其像素插值算法得到的图像边缘比较平滑,但该函数的速度比ImageCopyResized()慢。
第47~49行:最终生成经过处理后的图片,如果需要生成GIF或PNG,需要将imagejpeg()函数改成imagegif()或imagepng()
第51~56行:如果实际图片的长宽小于规定生成的图片长宽,则保持图片原样,同理,如果需要生成GIF或PNG,需要将imagejpeg()函数改成imagegif()或imagepng()。
特别说明:
GD库1.6.2版以前支持GIF格式,但因GIF格式使用LZW算法牵涉专利权,因此在GD1.6.2版之后不支持GIF的格式。如果是WINDOWS的环境,只要进入PHP.INI文件找到extension=php_gd2.dll,将#去除,重启APACHE即可。如果是Linux环境,又想支持GIF,PNG,JPEG,需要去下载libpng,zlib,以及freetype字体并安装。
OK,PHP图片压缩函数完成,最后概述一下整个处理的思路:
通过计算实际图片的长宽与规定生成图片的长宽之间的缩放比例,根据实际的需求(按照宽度还是按照高度进行图片缩放)计算出最终生成图片的大小,然后应用PHP图片处理函数对图片进行处理,最后输出图片。
以上就是关于PHP图片处理中如何对图片进行压缩并保持不失真的函数说明。

‘贰’ 随便下载一张图片,使用PHP将图片缩放到原来的1/2

//如果是JPG格式,则生成一个同比例的缩小图
$file="xxx.jpg";

$extend_name=strtolower(substr(strrchr($file,"."),1));

if($extend_name=='jpg'){
$image = imagecreatefromjpeg($full_name);//取原图的数据
}
//如果是gif格式,则生成一个同比例的缩小图
if($extend_name=='gif'){
$image = imagecreatefromgif($full_name);//取原图的数据
}
//如果是png格式,则生成一个同比例的缩小图
if($extend_name=='png'){
$image = imagecreatefrompng($full_name);//取原图的数据
}
//echo $full_name.$full_name_small;
$size=GetImageSize($full_name);
$x=$size[0];
$y=$size[1];
//echo $x." _ ".$y;
//假设首页上的图都是250象素左右,如果缩成150则图像失真太严重,则把所有的图都按这个大小进行等比缩放
//计算缩小比例
$rate=1/2;
$small_x=$size[0]*$rate;
$small_y=$size[1]*$rate;
$small_image = imagecreatetruecolor($small_x, $small_y);
imageCopyResampled($small_image,$image,0,0,0,0,$small_x,$small_y,$x,$y);
if(imagejpeg($small_image,$full_name_small)){
ImageDestroy($small_image);
}else{
}

‘叁’ 如何利用php把上传的图片压缩

<?php
//Thefile
$filename='test.jpg';
$percent=0.5;

//Contenttype
header('Content-Type:image/jpeg');

//Getnewdimensions
list($width,$height)=getimagesize($filename);
$new_width=$width*$percent;
$new_height=$height*$percent;

//Resample
$image_p=imagecreatetruecolor($new_width,$new_height);
$image=imagecreatefromjpeg($filename);
imageresampled($image_p,$image,0,0,0,0,$new_width,$new_height,$width,$height);

//Output
imagejpeg($image_p,null,100);
?>

http://php.net/manual/en/function.imageresampled.php

‘肆’ php图片可以等比例的缩放吗

可以。

等比例缩放的方法是:

1、载入选区--自由变换。如下图:

2、按住shift+alt键,使用鼠标调整大小,这种情况下,选区会按照等比例的方法进行缩放的。

‘伍’ php上传图片并压缩-thinkphp如何做图片压缩呢

php压缩图片

比如(什么?)上面有不同大小的图片--------语文表达缺主语,含糊导致无法理解。

服务器上面?客户机上面?具体什么软件环境上面?

“有不同大小的图片”,已经存在的图片通常已经压缩过的,像JPEG更是有损压缩。再次压缩必定再次会降低画质。PHP可以再次处理图片,但画质和存储大小不能兼得,画质好就存储大,要存储小就画质差,根据自己的画质需求处理。

thinkphp如何做图片压缩呢?

在上传图片的时候先看看图片有多大,一般来说导航幻灯片的图片单张大小尽量不超100k,产品图不超过20k,这样加载还慢的话就用ajax后加载方法,可以是滚动加载之类,但是对蜘蛛抓取页面并不是很友好。

至于你说的用tp把图片压缩,那只能是将图片的尺寸改成你想要的尺寸,大小的话是web所用格式大小,等页面加载完你又换原图,这样相当于又加载了一遍,还不如做ajax滚动加载。

PHP网站上传图片自动压缩,怎么编程啊,求指

这里会使用到三个文件:

:连接数据库

test_:执行sql语句

upload_:上传图片并压缩

三个文件代码如下:

连接数据库:

<?php

$db_host=''

$db_user=''

$db_psw=''

$db_name=''

$db_port=''

$sqlconn=new_ysqli($db_host,$db_user,$db_psw,$db_name);

$q="set_ames_tf8;";

$result=$sqlconn->query($q);

if(mysqli_connect_errno())_

_rintf("Connect_ailed:%s ",_ysqli_connect_error());

_xit();

}

?>

当然使用一些封装的数据库类也是可以的。

执行SQL语句:test_

<?php

require("");

require("upload_");

$real_img=$uploadfile;

$small_img=$uploadfile_resize;

$insert_sql="insert_nto_mg(real_img,small_img)_alues(?,?)";

$result=$sqlconn->_repare($insert_sql);

$result->_ind_param("ss",$real_img,$small_img);

$result->_xecute();

?>

上传图片并压缩:upload_

<?php

//设置文件保存目录

$uploaddir="upfiles/";

//设置允许上传文件的类型

$type=array("jpg","gif","bmp","jpeg","png");

//获取文件后缀名函数

function_ileext($filename)

{

_eturn_ubstr(strrchr($filename,'.'),1);

}

//生成随机文件名函数

function_andom($length)

{

$hash='CR-'

$chars=''

$max=_trlen($chars)-1;

_t_srand((double)microtime()*1000000);

_or($i=0;$i<$length;$i++)

_

_$hash.=$chars[mt_rand(0,$max)];

_

_eturn$hash;

}

$a=strtolower(fileext($_FILES['filename']['name']));

//判断文件类型

if(!in_array(strtolower(fileext($_FILES['filename']['name'])),$type))

{

$text=implode(",",$type);

$ret_code=3;//文件类型错误

$page_result=$text;

$retArray=_rray('ret_code'=>$ret_code,'page_result'=>$page_result);

$retJson=_son_encode($retArray);

_cho$retJson;

_eturn;

}

//生成目标文件的文件名

else

{

$filename=explode(".",$_FILES['filename']['name']);

_o

_

_$filename[0]=random(10);//设置随机数长度

_$name=implode(".",$filename);

_//$name1=$name.".Mcncc";

_$uploadfile=$uploaddir.$name;

_

_hile(file_exists($uploadfile));

_f(move_uploaded_file($_FILES['filename']['tmp_name'],$uploadfile))

_

_if(is_uploaded_file($_FILES['filename']['tmp_name']))

_{

_$ret_code=1;//上传失败

_}

_lse

_//上传成功

_$ret_code=0;

_

_

$retArray=_rray('ret_code'=>$ret_code);

$retJson=_son_encode($retArray);

echo$retJson;

}

//压缩图片

$uploaddir_resize="upfiles_resize/";

$uploadfile_resize=$uploaddir_resize.$name;

//$pic_width_max=120;

//$pic_height_max=90;

//以上与下面段注释可以联合使用,可以使图片根据计算出来的比例压缩

$file_type=$_FILES["filename"]['type'];

function_esizeImage($uploadfile,$maxwidth,$maxheight,$name)

{

//取得当前图片大小

$width=_magesx($uploadfile);

$height=_magesy($uploadfile);

$i=0.5;

//生成缩略图的大小

_f(($width>$maxwidth)_|($height>$maxheight))

_

_/*

_$widthratio=$maxwidth/$width;

_$heightratio=$maxheight/$height;

_

_if($widthratio<$heightratio)

_{

_$ratio=$widthratio;

_}

_else

_{

__$ratio=$heightratio;

_}

_

_$newwidth=$width*$ratio;

_$newheight=$height*$ratio;

_*/

_$newwidth=$width*$i;

_$newheight=$height*$i;

_if(function_exists("imageresampled"))

_{

_$uploaddir_resize=_magecreatetruecolor($newwidth,$newheight);

__mageresampled($uploaddir_resize,$uploadfile,0,0,0,0,$newwidth,$newheight,$width,$height);

_}

_else

_{

_$uploaddir_resize=_magecreate($newwidth,$newheight);

__mageresized($uploaddir_resize,$uploadfile,0,0,0,0,$newwidth,$newheight,$width,$height);

_}

_

_ImageJpeg($uploaddir_resize,$name);

_ImageDestroy($uploaddir_resize);

_

_lse

_

_ImageJpeg($uploadfile,$name);

_

}

if($_FILES["filename"]['size'])

{

_f($file_type=="image/pjpeg"||$file_type=="image/jpg"|$file_type=="image/jpeg")

_

_//$im=_magecreatefromjpeg($_FILES[$upload_input_name]['tmp_name']);

_$im=_magecreatefromjpeg($uploadfile);

_

_lseif($file_type=="image/x-png")

_

_//$im=_magecreatefrompng($_FILES[$upload_input_name]['tmp_name']);

_$im=_magecreatefromjpeg($uploadfile);

_

_lseif($file_type=="image/gif")

_

_//$im=_magecreatefromgif($_FILES[$upload_input_name]['tmp_name']);

_$im=_magecreatefromjpeg($uploadfile);

_

_lse//默认jpg

_

_$im=_magecreatefromjpeg($uploadfile);

_

_f($im)

_

_ResizeImage($im,$pic_width_max,$pic_height_max,$uploadfile_resize);

_

_ImageDestroy($im);

_

}

?>

请按照现实情况更改,test_中对应的信息。

望采纳,谢谢。

热点内容
java素数算法 发布:2025-10-07 01:37:59 浏览:835
前端缓存图片 发布:2025-10-07 01:09:46 浏览:270
tgp缓存文件 发布:2025-10-07 01:04:38 浏览:931
sql学生信息查询 发布:2025-10-07 00:52:20 浏览:809
java等领域 发布:2025-10-07 00:42:50 浏览:398
免费体检云服务器 发布:2025-10-07 00:38:54 浏览:97
云存储更新慢到微信不动 发布:2025-10-07 00:38:43 浏览:444
androidhtc 发布:2025-10-07 00:37:49 浏览:936
编程写地球 发布:2025-10-07 00:33:41 浏览:57
python获取毫秒 发布:2025-10-07 00:33:40 浏览:752