新建文件index.php,需要在統(tǒng)計(jì)目錄下有個(gè)圖片為q.jpg(可根據(jù)源碼進(jìn)行更改圖片的名稱)
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比阿勒泰網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式阿勒泰網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋阿勒泰地區(qū)。費(fèi)用合理售后完善,10多年實(shí)體公司更值得信賴。
源代碼如下:
?php
$filename="q.jpg";
$per=0.3;
list($width,
$height)=getimagesize($filename);
$n_w=$width*$per;
$n_h=$height*$per;
$new=imagecreatetruecolor($n_w,
$n_h);
$img=imagecreatefromjpeg($filename);
//拷貝部分圖像并調(diào)整
imagecopyresized($new,
$img,0,
0,0,
0,$n_w,
$n_h,
$width,
$height);
//圖像輸出新圖片、另存為
imagejpeg($new,
"q1.jpg");
imagedestroy($new);
imagedestroy($img);
?
使用瀏覽器運(yùn)行過(guò)后,在index.php同級(jí)的目錄下會(huì)有個(gè)q1.jpg,這個(gè)圖片就是等比例縮放后的圖片,路徑可以自己在源代碼里面更改,放在自己的項(xiàng)目當(dāng)中去或?qū)憘€(gè)方法也行
以上所述上就是本文的全部?jī)?nèi)容了,希望對(duì)大家學(xué)習(xí)php語(yǔ)言能夠有所幫助。
具體代碼如下所示:
/**
*
desription
壓縮圖片
*
@param
sting
$imgsrc
圖片路徑
*
@param
string
$imgdst
壓縮后保存路徑
*/
public
function
compressedImage($imgsrc,
$imgdst)
{
list($width,
$height,
$type)
=
getimagesize($imgsrc);
$new_width
=
$width;//壓縮后的圖片寬
$new_height
=
$height;//壓縮后的圖片高
if($width
=
600){
$per
=
600
/
$width;//計(jì)算比例
$new_width
=
$width
*
$per;
$new_height
=
$height
*
$per;
}
switch
($type)
{
case
1:
$giftype
=
check_gifcartoon($imgsrc);
if
($giftype)
{
header('Content-Type:image/gif');
$image_wp
=
imagecreatetruecolor($new_width,
$new_height);
$image
=
imagecreatefromgif($imgsrc);
imagecopyresampled($image_wp,
$image,
0,
0,
0,
0,
$new_width,
$new_height,
$width,
$height);
//90代表的是質(zhì)量、壓縮圖片容量大小
imagejpeg($image_wp,
$imgdst,
90);
imagedestroy($image_wp);
imagedestroy($image);
}
break;
case
2:
header('Content-Type:image/jpeg');
$image_wp
=
imagecreatetruecolor($new_width,
$new_height);
$image
=
imagecreatefromjpeg($imgsrc);
imagecopyresampled($image_wp,
$image,
0,
0,
0,
0,
$new_width,
$new_height,
$width,
$height);
//90代表的是質(zhì)量、壓縮圖片容量大小
imagejpeg($image_wp,
$imgdst,
90);
imagedestroy($image_wp);
imagedestroy($image);
break;
case
3:
header('Content-Type:image/png');
$image_wp
=
imagecreatetruecolor($new_width,
$new_height);
$image
=
imagecreatefrompng($imgsrc);
imagecopyresampled($image_wp,
$image,
0,
0,
0,
0,
$new_width,
$new_height,
$width,
$height);
//90代表的是質(zhì)量、壓縮圖片容量大小
imagejpeg($image_wp,
$imgdst,
90);
imagedestroy($image_wp);
imagedestroy($image);
break;
}
}
總結(jié)
以上所述是小編給大家介紹的PHP等比例壓縮圖片的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
您可能感興趣的文章:php中10個(gè)不同等級(jí)壓縮優(yōu)化圖片操作示例PHP
實(shí)現(xiàn)等比壓縮圖片尺寸和大小實(shí)例代碼php
gd等比例縮放壓縮圖片函數(shù)基于PHP實(shí)現(xiàn)等比壓縮圖片大小php上傳圖片并壓縮的實(shí)現(xiàn)方法PHP實(shí)現(xiàn)圖片上傳并壓縮PHP實(shí)現(xiàn)圖片壓縮的兩則實(shí)例php使用imagick模塊實(shí)現(xiàn)圖片縮放、裁剪、壓縮示例
這里會(huì)使用到三個(gè)文件:
connect.php:連接數(shù)據(jù)庫(kù)
test_upload.php:執(zhí)行SQL語(yǔ)句
upload_img.php:上傳圖片并壓縮
三個(gè)文件代碼如下:
連接數(shù)據(jù)庫(kù):connect.php
?php
$db_host?=?'';
$db_user?=?'';
$db_psw?=?'';
$db_name?=?'';
$db_port?=?'';
$sqlconn=new?mysqli($db_host,$db_user,$db_psw,$db_name);
$q="set?names?utf8;";
$result=$sqlconn-query($q);
if?(mysqli_connect_errno())?{
printf("Connect?failed:?%s\n",?mysqli_connect_error());
exit();
}
?
當(dāng)然使用一些封裝的數(shù)據(jù)庫(kù)類也是可以的。
執(zhí)行SQL語(yǔ)句:test_upload.php
?php
require?("connect.php");
require?("upload_img.php");
$real_img=$uploadfile;?
$small_img=$uploadfile_resize;
$insert_sql?=?"insert?into?img?(real_img,small_img)?values?(?,?)";
$result?=?$sqlconn?-?prepare($insert_sql);
$result?-?bind_param("ss",?$real_img,$small_img);
$result?-?execute();
?
上傳圖片并壓縮:upload_img.php
?php?
//設(shè)置文件保存目錄
$uploaddir?=?"upfiles/";?
//設(shè)置允許上傳文件的類型
$type=array("jpg","gif","bmp","jpeg","png");?
//獲取文件后綴名函數(shù)?
function?fileext($filename)?
{?
return?substr(strrchr($filename,?'.'),?1);?
}?
//生成隨機(jī)文件名函數(shù)?
function?random($length)?
{?
$hash?=?'CR-';?
$chars?=?'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';?
$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;//文件類型錯(cuò)誤
$page_result=$text;
$retArray?=?array('ret_code'?=?$ret_code,'page_result'=$page_result);
$retJson?=?json_encode($retArray);
echo?$retJson;
return;
}?
//生成目標(biāo)文件的文件名?
else
{?
$filename=explode(".",$_FILES['filename']['name']);?
do
{?
$filename[0]=random(10);?//設(shè)置隨機(jī)數(shù)長(zhǎng)度?
$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;
//以上與下面段注釋可以聯(lián)合使用,可以使圖片根據(jù)計(jì)算出來(lái)的比例壓縮
$file_type=$_FILES["filename"]['type'];
function?ResizeImage($uploadfile,$maxwidth,$maxheight,$name)
{
//取得當(dāng)前圖片大小
$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("imagecopyresampled"))
{
$uploaddir_resize?=?imagecreatetruecolor($newwidth,?$newheight);
imagecopyresampled($uploaddir_resize,?$uploadfile,?0,?0,?0,?0,?$newwidth,?$newheight,?$width,?$height);
}
else
{
$uploaddir_resize?=?imagecreate($newwidth,?$newheight);
imagecopyresized($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//默認(rèn)jpg
{
$im?=?imagecreatefromjpeg($uploadfile);
}
if($im)
{
ResizeImage($im,$pic_width_max,$pic_height_max,$uploadfile_resize);
ImageDestroy?($im);
}
}?
?
請(qǐng)按照現(xiàn)實(shí)情況更改connect.php,test_upload.php中對(duì)應(yīng)的信息。
望采納,謝謝。
當(dāng)前標(biāo)題:php數(shù)據(jù)縮圖代碼,php 圖片縮放
本文地址:http://aaarwkj.com/article24/dsisgje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開(kāi)發(fā)、云服務(wù)器、網(wǎng)頁(yè)設(shè)計(jì)公司、、搜索引擎優(yōu)化、營(yíng)銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)