當前位置:首頁 » 文件傳輸 » 上傳自動壓縮圖片代碼
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

上傳自動壓縮圖片代碼

發布時間: 2022-09-27 07:27:54

❶ asp網站上傳圖片自動壓縮圖片大小代碼,把以下代碼修改成能夠壓縮圖片的代碼。

要壓縮圖片大小的必須要有組件,就是伺服器要支持這個組件才可以。
下面介紹一個:
ASPJPEG是一款功能相當強大的圖象處理組件,用它可以輕松地做出圖片的縮略圖和為圖片加上水印功能。

實際怎麼樣我沒有用過這個組件不做評價,你可以網上找找。

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

}

❸ C#代碼實現圖片壓縮至40K以下

任何壓縮演算法的壓縮比都不能無限的提高的。
最有效地方法就是降低圖片的解析度,即像素值。圖片的每個像素佔用4個位元組,假設一般JPG格式的壓縮比為1:10的話,40K位元組最多隻能儲存 40000/4*10 = 10萬像素的圖片,即圖片的長乘以寬不超過10萬,一般圖片長寬比是4:3;所以圖片的大小最好不超過 360 X 270 。
降低圖片的解析度就是縮小圖片,用Graphics對象、Bitmap對象就能做到。

Bitmap bmpDest = new Bitmap(360,270);
Bitmap bmpSrc = new Bitmap("Source.jpg");
Graphics g = Graphics.FromImge(bmpDest);
g.DrawImage(bmpSrc, rectDest,rectSrc,GraphicsUnit.Pixel);

或者將PictureBox的長和寬限定在360 X 270

❹ 我想問一下:ASP中,在上傳圖片的時候可不可以將圖片壓縮成相應大小,代碼應該怎麼寫

1、需要自己安裝的組件支持,直接使用組件提供的功能實現,無組件直接使用代碼實現,好像很困難。
2、用ASP.NET,自帶組件多多,圖片隨意轉……

❺ asp.net 將原圖用代碼自動壓縮成規定大小後上傳

/// <summary>
/// 生成縮略圖
/// </summary>
/// <param name="originalImagePath">源圖路徑(物理路徑)</param>
/// <param name="thumbnailPath">縮略圖路徑(物理路徑)</param>
/// <param name="width">縮略圖寬度</param>
/// <param name="height">縮略圖高度</param>
/// <param name="mode">生成縮略圖的方式</param>
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height; switch (mode)
{
case "HW"://指定高寬縮放(可能變形)
break;
case "W"://指定寬,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H"://指定高,寬按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut"://指定高寬裁減(不變形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default:
break;
} //新建一個bmp圖片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一個畫板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //設置高質量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //設置高質量,低速度呈現平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空畫布並以透明背景色填充
g.Clear(System.Drawing.Color.Transparent); //在指定位置並且按指定大小繪制原圖片的指定部分
g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel); try
{
//以jpg格式保存縮略圖
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}

❻ 上傳圖片時讓我壓縮到1M以下,怎樣壓縮

在線圖片壓縮工具

一、首先點擊加號添加需要壓縮的圖片。目前已知支持jpg、png等多種常見的圖片格式,如果上傳圖片並壓縮成功,則代表支持該格式。
二、可以自行修改圖片需要被壓縮到的最大寬高尺寸,默認為圖片原始的寬高尺寸,且寬高比例是自動鎖定的。
三、必須設置圖片被壓縮後,期望輸出的圖片文件的最大佔用空間。(必填項)
四、選擇圖片生成的演算法。默認為混合優先演算法,絕大多數情況下使用默認演算法即可。
五、壓縮的設定值不能小於1kb,但圖片壓縮的最終效果可以小於1kb。

butterpig

❼ PHP網站上傳圖片自動壓縮,怎麼編程啊,求指

這里會使用到三個文件:

  • connect.php:連接資料庫

  • test_upload.php:執行sql語句

  • upload_img.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中對應的信息。

望採納,謝謝。

❽ 如何把圖片壓縮成指定大小

你可以用這種在線的圖片壓縮工具,就可以把圖片文件壓縮到你期望的體積大小,比如你將壓縮數值設置到100kb,稍等幾秒鍾之後,在線圖片壓縮工具就已經把圖片的文件大小壓縮到最高100kb了。換句話說,你設定壓縮到多少kb的期望值,他壓縮完的輸出圖片就是多少kb大小了,非常方便。在線智能圖片壓縮,壓縮圖片體積大小

在線圖片智能壓縮使用步驟:

一、首先點擊加號添加需要壓縮的圖片。目前已知支持對jpg、png、webp、bmp等多種常見的圖片格式進行壓縮,如果選擇圖片後正常顯示並能夠壓縮成功,則代表支持該圖片格式。
二、可以自行修改圖片需要被壓縮到的最大寬高尺寸,默認為圖片原始的寬高尺寸,並且寬高比例是自動鎖定的,確保圖片不會變形。
三、必須設置圖片被壓縮後,期望輸出的壓縮之後圖片文件的最大佔用空間,該選項是必填的。
四、選擇圖片壓縮的演算法。默認為智能混合壓縮演算法,絕大多數情況下使用默認演算法進行圖片壓縮即可。
五、當你設置壓縮後的大小單位為」Kb「時,壓縮大小的設定值不能小於1Kb,但圖片壓縮後的最終文件大小是可以小於1Kb的。