导航:首页 > 文件处理 > php上传图片自动压缩

php上传图片自动压缩

发布时间:2022-07-21 21:49:56

php图片上传后被压缩了,变得不清晰了,怎么解决

如果清晰的上传后,不清晰,,那就是设置的压缩质量问题,,,如果是不清晰上传了,这肯定不会清晰了。。。

找到上传的设置里可以找到图片压缩比例。。。。

⑵ php上传图片压缩

html里加一个表单,然后提交到处理上传文件的upload.php

类似下面这样

<fromaction="upload.php"enctype="multipart/from-data"method="post">
<inputtype="file"name="file3"/>
</from>

然后upload.php里先处理图片上传,最终保存一份原始图片文件在服务器上,接着就把文件路径什么的传到你的这个函数里处理图片就OK了

⑶ PHP怎么在上传之前压缩图片

远标老师教我们用fireworks优化下图片,用最高品质jpg就可以.优化后再上传就可以了

⑷ PHP上传图片,并自动修改图片的大小

这个是可以的,用imageresamled()处理之后就file_put_contents()保存替换原文件就可以了

⑸ 有谁做过上传文件自动压缩的东东 - PHP进阶讨论

Zip是通用的,rar不通用用ZipArchive类就行了$zip = new ZipArchive();$filename = \"./test112.zip\";if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {? ? exit(\"cannot open \\n\");}$zip->addFromString(\"testfilephp.txt\" . time(), \"#1 This is a test string added as testfilephp.txt.\\n\");$zip->addFromString(\"testfilephp2.txt\" . time(), \"#2 This is a test string added as testfilephp2.txt.\\n\");$zip->addFile(\"../test/10_while_break_continue.avi\", uniqid().\".avi\");echo \"numfiles: \" . $zip->numFiles . \"\\n\";echo \"status:\" . $zip->status . \"\\n\";$zip->close();

⑹ 如何利用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网站上传图片自动压缩,怎么编程啊,求指

这里会使用到三个文件:

三个文件代码如下:
连接数据库:connect.php

<?php
$db_host='';
$db_user='';
$db_psw='';
$db_name='';
$db_port='';
$sqlconn=newmysqli($db_host,$db_user,$db_psw,$db_name);
$q="setnamesutf8;";
$result=$sqlconn->query($q);
if(mysqli_connect_errno()){
printf("Connectfailed:%s ",mysqli_connect_error());
exit();
}
?>

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

执行SQL语句:test_upload.php

<?php
require("connect.php");
require("upload_img.php");
$real_img=$uploadfile;
$small_img=$uploadfile_resize;
$insert_sql="insertintoimg(real_img,small_img)values(?,?)";
$result=$sqlconn->prepare($insert_sql);
$result->bind_param("ss",$real_img,$small_img);
$result->execute();
?>

上传图片并压缩:upload_img.php

<?php
//设置文件保存目录
$uploaddir="upfiles/";
//设置允许上传文件的类型
$type=array("jpg","gif","bmp","jpeg","png");

//获取文件后缀名函数
functionfileext($filename)
{
returnsubstr(strrchr($filename,'.'),1);
}

//生成随机文件名函数
functionrandom($length)
{
$hash='CR-';
$chars='';
$max=strlen($chars)-1;
mt_srand((double)microtime()*1000000);
for($i=0;$i<$length;$i++)
{
$hash.=$chars[mt_rand(0,$max)];
}
return$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=array('ret_code'=>$ret_code,'page_result'=>$page_result);
$retJson=json_encode($retArray);
echo$retJson;
return;
}

//生成目标文件的文件名
else
{
$filename=explode(".",$_FILES['filename']['name']);
do
{
$filename[0]=random(10);//设置随机数长度
$name=implode(".",$filename);
//$name1=$name.".Mcncc";
$uploadfile=$uploaddir.$name;
}

while(file_exists($uploadfile));

if(move_uploaded_file($_FILES['filename']['tmp_name'],$uploadfile))
{
if(is_uploaded_file($_FILES['filename']['tmp_name']))
{
$ret_code=1;//上传失败
}
else
{//上传成功
$ret_code=0;
}
}
$retArray=array('ret_code'=>$ret_code);
$retJson=json_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'];

functionResizeImage($uploadfile,$maxwidth,$maxheight,$name)
{
//取得当前图片大小
$width=imagesx($uploadfile);
$height=imagesy($uploadfile);
$i=0.5;
//生成缩略图的大小
if(($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=imagecreatetruecolor($newwidth,$newheight);
imageresampled($uploaddir_resize,$uploadfile,0,0,0,0,$newwidth,$newheight,$width,$height);
}
else
{
$uploaddir_resize=imagecreate($newwidth,$newheight);
imageresized($uploaddir_resize,$uploadfile,0,0,0,0,$newwidth,$newheight,$width,$height);
}

ImageJpeg($uploaddir_resize,$name);
ImageDestroy($uploaddir_resize);
}
else
{
ImageJpeg($uploadfile,$name);
}
}if($_FILES["filename"]['size'])
{
if($file_type=="image/pjpeg"||$file_type=="image/jpg"|$file_type=="image/jpeg")
{
//$im=imagecreatefromjpeg($_FILES[$upload_input_name]['tmp_name']);
$im=imagecreatefromjpeg($uploadfile);
}
elseif($file_type=="image/x-png")
{
//$im=imagecreatefrompng($_FILES[$upload_input_name]['tmp_name']);
$im=imagecreatefromjpeg($uploadfile);
}
elseif($file_type=="image/gif")
{
//$im=imagecreatefromgif($_FILES[$upload_input_name]['tmp_name']);
$im=imagecreatefromjpeg($uploadfile);
}
else//默认jpg
{
$im=imagecreatefromjpeg($uploadfile);
}
if($im)
{
ResizeImage($im,$pic_width_max,$pic_height_max,$uploadfile_resize);

ImageDestroy($im);
}
}
?>

请按照现实情况更改connect.php,test_upload.php中对应的信息。

望采纳,谢谢。

⑻ PHP、HTML5上传图片自动压缩问题

给你个图片处理的类吧,图片剪裁处理后,也就等于将图片压缩了。

/**
*图像处理类
*============================================================================
*Copyright2014大秦科技,并保留所有权利。
*网站地址:http://www.qintech.net;
*============================================================================
*/
classImage{

//生成缩略图的方式
public$thumbType;
//缩略图的宽度
public$thumbWidth;
//缩略图的高度
public$thumbHeight;
//生成缩略图文件名后缀
public$thumbEndFix;
//缩略图文件前缀
public$thumbPreFix;

/**
*构造函数
*/
publicfunction__construct(){
$this->thumbType=1;
$this->thumbWidth=120;
$this->thumbHeight=60;
$this->thumbPreFix='';
$this->thumbEndFix='_thumb';
}

/**
*检测是否为图像文件
*@param$img图像
*@returnbool
*/
privatefunctioncheck($img){
$type=array(".jpg",".jpeg",".png",".gif");
$imgType=strtolower(strrchr($img,'.'));
returnextension_loaded('gd')&&file_exists($img)&&in_array($imgType,$type);
}

/**
*获得缩略图的尺寸信息
*@param$imgWidth原图宽度
*@param$imgHeight原图高度
*@param$thumbWidth缩略图宽度
*@param$thumbHeight缩略图的高度
*@param$thumbType处理方式
*1固定宽度高度自增2固定高度宽度自增3固定宽度高度裁切
*4固定高度宽度裁切5缩放最大边原图不裁切
*@returnmixed
*/
privatefunctionthumbSize($imgWidth,$imgHeight,$thumbWidth,$thumbHeight,$thumbType){
//初始化缩略图尺寸
$w=$thumbWidth;
$h=$thumbHeight;
//初始化原图尺寸
$cuthumbWidth=$imgWidth;
$cuthumbHeight=$imgHeight;
switch($thumbType){
case1:
//固定宽度高度自增
$h=$thumbWidth/$imgWidth*$imgHeight;
break;
case2:
//固定高度宽度自增
$w=$thumbHeight/$imgHeight*$imgWidth;
break;
case3:
//固定宽度高度裁切
$cuthumbHeight=$imgWidth/$thumbWidth*$thumbHeight;
break;
case4:
//固定高度宽度裁切
$cuthumbWidth=$imgHeight/$thumbHeight*$thumbWidth;
break;
case5:
//缩放最大边原图不裁切
if(($imgWidth/$thumbWidth)>($imgHeight/$thumbHeight)){
$h=$thumbWidth/$imgWidth*$imgHeight;
}elseif(($imgWidth/$thumbWidth)<($imgHeight/$thumbHeight)){
$w=$thumbHeight/$imgHeight*$imgWidth;
}else{
$w=$thumbWidth;
$h=$thumbHeight;
}
break;
default:
//缩略图尺寸不变,自动裁切图片
if(($imgHeight/$thumbHeight)<($imgWidth/$thumbWidth)){
$cuthumbWidth=$imgHeight/$thumbHeight*$thumbWidth;
}elseif(($imgHeight/$thumbHeight)>($imgWidth/$thumbWidth)){
$cuthumbHeight=$imgWidth/$thumbWidth*$thumbHeight;
}
//}
}
$arr[0]=$w;
$arr[1]=$h;
$arr[2]=$cuthumbWidth;
$arr[3]=$cuthumbHeight;
return$arr;
}

/**
*图片裁切处理
*@param$img原图
*@paramstring$outFile另存文件名
*@paramstring$thumbWidth缩略图宽度
*@paramstring$thumbHeight缩略图高度
*@paramstring$thumbType裁切图片的方式
*1固定宽度高度自增2固定高度宽度自增3固定宽度高度裁切
*4固定高度宽度裁切5缩放最大边原图不裁切6缩略图尺寸不变,自动裁切最大边
*@returnbool|string
*/
publicfunctionthumb($img,$outFile='',$thumbWidth='',$thumbHeight='',$thumbType=''){
if(!$this->check($img)){
returnfalse;
}
//基础配置
$thumbType=$thumbType?$thumbType:$this->thumbType;
$thumbWidth=$thumbWidth?$thumbWidth:$this->thumbWidth;
$thumbHeight=$thumbHeight?$thumbHeight:$this->thumbHeight;
//获得图像信息
$imgInfo=getimagesize($img);
$imgWidth=$imgInfo[0];
$imgHeight=$imgInfo[1];
$imgType=image_type_to_extension($imgInfo[2]);
//获得相关尺寸
$thumb_size=$this->thumbSize($imgWidth,$imgHeight,$thumbWidth,$thumbHeight,$thumbType);
//原始图像资源
$func="imagecreatefrom".substr($imgType,1);
$resImg=$func($img);
//缩略图的资源
if($imgType=='.gif'){
$res_thumb=imagecreate($thumb_size[0],$thumb_size[1]);
$color=imagecolorallocate($res_thumb,255,0,0);
}else{
$res_thumb=imagecreatetruecolor($thumb_size[0],$thumb_size[1]);
imagealphablending($res_thumb,false);//关闭混色
imagesavealpha($res_thumb,true);//储存透明通道
}
//绘制缩略图X
if(function_exists("imageresampled")){
imageresampled($res_thumb,$resImg,0,0,0,0,$thumb_size[0],$thumb_size[1],$thumb_size[2],$thumb_size[3]);
}else{
imageresized($res_thumb,$resImg,0,0,0,0,$thumb_size[0],$thumb_size[1],$thumb_size[2],$thumb_size[3]);
}
//处理透明色
if($imgType=='.gif'){
imagecolortransparent($res_thumb,$color);
}
//配置输出文件名
$imgInfo=pathinfo($img);
$outFile=$outFile?$outFile:dirname($img).'/'.$this->thumbPreFix.$imgInfo['filename'].$this->thumbEndFix.".".$imgInfo['extension'];

Files::create(dirname($outFile));
$func="image".substr($imgType,1);
$func($res_thumb,$outFile);
if(isset($resImg))
imagedestroy($resImg);
if(isset($res_thumb))
imagedestroy($res_thumb);
return$outFile;
}

}
阅读全文

与php上传图片自动压缩相关的资料

热点内容
如何测试流媒体服务器的并发能力 浏览:159
溯源码有分国家认证的吗 浏览:218
如何通过app查询产检报告 浏览:944
拉结尔安卓手机怎么用 浏览:695
驱动级进程代理源码 浏览:782
androidshape画线 浏览:510
程序员想辞职被拒绝 浏览:101
java面试逻辑 浏览:749
如何下载全英文app 浏览:724
js函数式编程指南 浏览:380
为什么安卓手机相机启动会卡 浏览:341
python中t是什么意思 浏览:765
移动硬盘内存加密 浏览:407
单片机测角度 浏览:864
URL服务器地址怎么填 浏览:438
压缩饼干会导致血糖高吗 浏览:569
cad中xc命令怎么用 浏览:424
戴尔服务器怎么看网卡接口 浏览:823
盐铁论pdf 浏览:424
最短路径的生成算法可用 浏览:457