1. 程式人生 > 實用技巧 >php 上傳圖片並生成縮圖

php 上傳圖片並生成縮圖

  1 <?php
  2     // clearDir('upload/images/');
  3 
  4     if ($_FILES['file']['error'] == 0) {
  5         $MAX_FILE_SIZE = 300000;
  6         if ($_FILES['file']['size'] > $MAX_FILE_SIZE) {
  7             exit('檔案超出指定大小');
  8         }
  9         
 10         $allowSuffix = ['jpg', 'png', 'jpeg'];
11 12 $myImg = explode('.', $_FILES['file']['name']); 13 14 $mySuffix = array_pop($myImg); 15 16 if (!in_array($mySuffix, $allowSuffix)) { 17 exit('檔案型別不對'); 18 } 19 20 $allowMine = [ 21 'image/jpg', 22
'image/jpeg', 23 'image/pjpeg', 24 'image/gif' 25 ]; 26 27 if (!in_array($_FILES['file']['type'], $allowMine)) { 28 exit('檔案mime型別不對'); 29 } 30 31 $path = "upload/images/"; 32 $name = date('Ymdhis').mt_rand
(0, 9). '.' .$mySuffix; 33 34 if(is_uploaded_file($_FILES['file']['tmp_name'])) { 35 if (move_uploaded_file($_FILES['file']['tmp_name'], $path.$name)) { 36 $thumb_url = create_thumb($path.$name); 37 $thumb_url2 = create_thumb($path.$name, 200, true); 38 echo '上傳成功, 縮圖路徑為: '.$thumb_url; 39 echo '方縮圖路徑為: '.$thumb_url2; 40 } else { 41 echo '上傳失敗'; 42 } 43 } else { 44 exit('不是上傳檔案'); 45 } 46 } else { 47 exit('上傳失敗'); 48 } 49 50 /** 51 * 給圖片生成縮圖 52 */ 53 function create_thumb($url, $max_width = 200, $is_square = false) { 54 $image = imagecreatefromjpeg($url); 55 56 list($ow, $oh) = getimagesize($url); 57 $path_arr = explode('.', $url); 58 59 //如果要求生成正方形的縮圖 60 if ($is_square) { 61 if ($ow > $max_width) { 62 $nw = $max_width; 63 $new_image = imagecreatetruecolor($nw, $nw); 64 $white = imagecolorallocate($new_image, 255, 255, 255); 65 imagefilledrectangle($new_image, 0, 0, $nw, $nw, $white); 66 67 $o_wh = $ow > $oh ? $oh : $ow; 68 imagecopyresampled($new_image, $image, 0, 0, 0, 0, $nw, $nw, $o_wh, $o_wh); 69 imagejpeg($new_image, $path_arr[0].'_thumbf.jpg'); 70 return $path_arr[0].'_thumbf.jpg'; 71 } else { 72 $new_image = imagecreatetruecolor($ow, $ow); 73 $white = imagecolorallocate($new_image, 255, 255, 255); 74 imagefilledrectangle($new_image, 0, 0, $ow, $ow, $white); 75 76 if ($ow > $oh) { 77 imagecopyresampled($new_image, $image, 0, ($ow - $oh)/ 2, 0, 0, $ow, $oh, $ow, $oh); 78 } else { 79 imagecopyresampled($new_image, $image, 0, 0, 0, 0, $ow, $ow, $ow, $ow); 80 } 81 82 imagejpeg($new_image, $path_arr[0].'_thumbf.jpg'); 83 return $path_arr[0].'_thumbf.jpg'; 84 } 85 } else { 86 if ($ow > $max_width) { 87 $percent = $max_width / $ow; 88 $nw = $max_width; 89 $nh = $oh * $percent; 90 $new_image = imagecreatetruecolor($nw, $nh); 91 imagecopyresampled($new_image, $image, 0, 0, 0, 0, $nw, $nh, $ow, $oh); 92 93 imagejpeg($new_image, $path_arr[0].'_thumb.jpg'); 94 return $path_arr[0].'_thumb.jpg'; 95 } else { 96 $new_image = imagecreatetruecolor($ow, $oh); 97 $white = imagecolorallocate($new_image, 255, 255, 255); 98 imagefilledrectangle($new_image, 0, 0, $ow, $oh, $white); 99 imagecopyresampled($new_image, $image, 0, 0, 0, 0, $ow, $oh, $ow, $oh); 100 imagejpeg($new_image, $path_arr[0].'_thumb.jpg'); 101 return $path_arr[0].'_thumb.jpg'; 102 } 103 } 104 } 105 106 //清空資料夾 107 function clearDir($dir) { 108 if (is_dir($dir)) { 109 if ($dh = opendir($dir)) { 110 while ($file=readdir($dh)) { 111 if (is_file($dir.$file)) { 112 unlink($dir.$file); 113 } 114 } 115 116 closedir($dh); 117 } 118 } 119 } 120 ?>