php利用GD庫產生縮圖

來源:互聯網
上載者:User
  1. header("content-type:text/html;charset=gbk");
  2. ini_set("date.timezone","Asia/chong");
  3. //判斷檔案是否為空白
  4. if(empty($_FILES)){
  5. echo"上傳檔案過大";
  6. exit;
  7. }
  8. //判斷檔案上傳是否有錯誤
  9. if($_FILES['pic']['error']){
  10. echo "上傳檔案";
  11. exit;
  12. }
  13. //判斷檔案類型是否非法擷取檔案尾碼
  14. $allowtype=array("jpg","png","jpeg","gif");
  15. $a=explode('.',$_FILES['pic']['name']);
  16. $index=count($a)-1;
  17. $ex=strtolower($a[$index]);
  18. if(!in_array($ex,$allowtype)){
  19. echo "上傳檔案非法";
  20. exit;
  21. }
  22. $file=date('YmdHis').rand().".".$ex;
  23. $src=$_FILES['pic']['tmp_name'];
  24. $des="upload/".$file;
  25. $rs=move_uploaded_file($src,$des);
  26. //縮圖
  27. //讀取已經上傳圖片
  28. $image=imagecreatefromjpeg($des);
  29. $a=getimagesize($des);
  30. $w=$a[0];
  31. $h=$a[1];
  32. if($w>$h){
  33. $width=300;
  34. $height=$width/$w*$h;
  35. }else if($w<$h){
  36. $height=300;
  37. $width=$height/$h*$w;
  38. }else{
  39. $width=300;
  40. $height=300;
  41. }
  42. //建立空白新圖片
  43. $newimage=imagecreatetruecolor($width, $height);
  44. //copy源圖片內容 copy新圖片
  45. imagecopyresized($newimage, $image, 0,0, 0,0, $width, $height, $w, $h);
  46. $filename="upload/s_".$file;
  47. imagejpeg($newimage,$filename);
複製代碼
php
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.