欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

php數(shù)據(jù)縮圖代碼,php 圖片縮放

php實現(xiàn)圖片等比例縮放代碼

新建文件index.php,需要在統(tǒng)計目錄下有個圖片為q.jpg(可根據(jù)源碼進行更改圖片的名稱)

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比阿勒泰網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式阿勒泰網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋阿勒泰地區(qū)。費用合理售后完善,10多年實體公司更值得信賴。

源代碼如下:

?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);

?

使用瀏覽器運行過后,在index.php同級的目錄下會有個q1.jpg,這個圖片就是等比例縮放后的圖片,路徑可以自己在源代碼里面更改,放在自己的項目當中去或?qū)憘€方法也行

以上所述上就是本文的全部內(nèi)容了,希望對大家學(xué)習(xí)php語言能夠有所幫助。

PHP等比例壓縮圖片的實例代碼

具體代碼如下所示:

/**

*

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;//計算比例

$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等比例壓縮圖片的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

您可能感興趣的文章:php中10個不同等級壓縮優(yōu)化圖片操作示例PHP

實現(xiàn)等比壓縮圖片尺寸和大小實例代碼php

gd等比例縮放壓縮圖片函數(shù)基于PHP實現(xiàn)等比壓縮圖片大小php上傳圖片并壓縮的實現(xiàn)方法PHP實現(xiàn)圖片上傳并壓縮PHP實現(xiàn)圖片壓縮的兩則實例php使用imagick模塊實現(xiàn)圖片縮放、裁剪、壓縮示例

PHP網(wǎng)站上傳圖片自動壓縮,怎么編程啊,求指

這里會使用到三個文件:

connect.php:連接數(shù)據(jù)庫

test_upload.php:執(zhí)行SQL語句

upload_img.php:上傳圖片并壓縮

三個文件代碼如下:

連接數(shù)據(jù)庫: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();

}

?

當然使用一些封裝的數(shù)據(jù)庫類也是可以的。

執(zhí)行SQL語句: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);?

}?

//生成隨機文件名函數(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;//文件類型錯誤

$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);?//設(shè)置隨機數(shù)長度?

$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ù)計算出來的比例壓縮

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

function?ResizeImage($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("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//默認jpg

{

$im?=?imagecreatefromjpeg($uploadfile);

}

if($im)

{

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

ImageDestroy?($im);

}

}?

?

請按照現(xiàn)實情況更改connect.php,test_upload.php中對應(yīng)的信息。

望采納,謝謝。

當前標題:php數(shù)據(jù)縮圖代碼,php 圖片縮放
本文地址:http://chinadenli.net/article24/dsisgje.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)云服務(wù)器網(wǎng)頁設(shè)計公司搜索引擎優(yōu)化營銷型網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)