php圖片上傳壓縮
『壹』 求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_中對應的信息。
望採納,謝謝。