php利用GD庫產生縮圖範例程式碼

來源:互聯網
上載者:User
GD庫,是php處理圖形的擴充庫,GD庫提供了一系列用來處理圖片的API,使用GD庫可以處理圖片,或者產生圖片。在網站上GD庫通常用來產生縮圖,或者用來對圖片加浮水印,或者用來產生漢字驗證碼,或者對網站資料產生報表等。在PHP處理映像,可使用GD庫,而GD庫開始時是支援GIF的,但由於GIF使用了有著作權爭議的LZW演算法,會引起法律問題,於是從 GD 庫 1.6 版起所有的 GIF 支援都移除了,但是又在 GD 庫 2.0.28 版起又加了回來。如果使用二者之間版本的 GD 庫時 GIF 相關函數不可用。

本文為大家介紹下php如何利用GD庫產生縮圖的,下面有個不錯的樣本,大家可以參考下

php利用GD庫產生縮圖。

<form method="post" action="suo_do.php" enctype="multipart/form-data"> <input type="file" name="pic" /> <input type="submit" value="上傳1" /> </form> <?php header("content-type:text/html;charset=gbk"); ini_set("date.timezone","Asia/chong"); //判斷檔案是否為空白 if(empty($_FILES)){ echo"上傳檔案過大"; exit; } //判斷檔案上傳是否有錯誤 if($_FILES['pic']['error']){ echo "上傳檔案"; exit; } //判斷檔案類型是否非法擷取檔案尾碼 $allowtype=array("jpg","png","jpeg","gif"); $a=explode('.',$_FILES['pic']['name']); $index=count($a)-1; $ex=strtolower($a[$index]); if(!in_array($ex,$allowtype)){ echo "上傳檔案非法"; exit; } $file=date('YmdHis').rand().".".$ex; $src=$_FILES['pic']['tmp_name']; $des="upload/".$file; $rs=move_uploaded_file($src,$des); //縮圖 //讀取已經上傳圖片 $image=imagecreatefromjpeg($des); $a=getimagesize($des); $w=$a[0]; $h=$a[1]; if($w>$h){ $width=300; $height=$width/$w*$h; }else if($w<$h){ $height=300; $width=$height/$h*$w; }else{ $width=300; $height=300; } //建立空白新圖片 $newimage=imagecreatetruecolor($width, $height); //copy源圖片內容 copy新圖片 imagecopyresized($newimage, $image, 0,0, 0,0, $width, $height, $w, $h); $filename="upload/s_".$file; imagejpeg($newimage,$filename);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.