縮小圖片函數
// 若是尺寸過大 >300kb,則減少尺寸
if($attach['size'] > 300000) {
$create_fun_map = array('jpg' => 'imagecreatefromjpeg', 'jpeg' => 'imagecreatefromjpeg', 'gif' => 'imagecreatefromgif', 'png' => 'imagecreatefrompng');
if ($create_fun_map[$attach['extension']]) {
$create_fun = $create_fun_map[$attach['extension']];
$src_img = $create_fun($attach['target']);
imagejpeg($src_img, $attach['target'], 70);
imagedestroy($src_img);
}
} this
圖片處理擴展類:url
class image_ext { /* * 圖片微縮白邊補齊 * 所用的是GD庫 * $suffix 新圖片後綴名稱,若是爲空,則爲width_height; 縮略圖後綴爲: ‘thumb’ * 圖片統一後綴 .jpg */ function PicZoom($pic_path, $width = 200, $height = 200, $suffix = '', $bk_rgb = array(255, 255, 255), $is_del_ori=false) { if (!$pic_path || !file_exists($pic_path)) { return false; } if (!function_exists('imagecreatetruecolor')) { return false; } $arr_src = array(); $arr_tmp = getimagesize($pic_path); if (!$arr_tmp) { return false; //不是圖片 } //得到原始圖像的大小 $arr_src ['width'] = $arr_tmp [0]; $arr_src ['height'] = $arr_tmp [1]; //取得文件擴展名 $mime_tmp = explode('/', $arr_tmp['mime']); $ext = $mime_tmp[1]; //生成的新圖片後綴,一概存成JPG if ($suffix) { if (strpos($suffix, '/') !== false) { return false; } $suffix = trim($suffix, '.'); } $suffix = $suffix ? $suffix : $width . '_' . $height; $arr_src ['new_path'] = $pic_path . '.' . $suffix . '.jpg'; //若是新生成圖片的尺寸寬與高都大於原圖,則不壓縮補白 if ($width > $arr_src ['width'] && $height > $arr_src ['height']) { return $this->savePic($pic_path, $suffix); } //根據比例計算新的長寬 目標的座標x y if ($width / $height > $arr_src ['width'] / $arr_src ['height']) { //以height爲基準 $arr_src ['new_height'] = $height; $arr_src ['new_width'] = $arr_src ['width'] / $arr_src ['height'] * $height; $arr_src ['to_x'] = ($width - $arr_src ['new_width']) / 2; $arr_src ['to_y'] = 0; } else { //以width爲基準 $arr_src ['new_width'] = $width; $arr_src ['new_height'] = $arr_src ['height'] / $arr_src ['width'] * $width; $arr_src ['to_x'] = 0; $arr_src ['to_y'] = ($height - $arr_src ['new_height']) / 2; } //列出建立圖片資源的函數表 $create_fun_map = array('jpg' => 'imagecreatefromjpeg', 'jpeg' => 'imagecreatefromjpeg', 'gif' => 'imagecreatefromgif', 'png' => 'imagecreatefrompng'); if (!$create_fun_map [$ext]) return false; $create_fun = $create_fun_map [$ext]; //參數計算完畢,建立圖像 $dst_img = imagecreatetruecolor($width, $height); $bk_color = imagecolorallocate($dst_img, 0xFF, 0xFF, 0xFF); imagefill($dst_img, 0, 0, $bk_color); $src_img = $create_fun($pic_path); imagecopyresampled($dst_img, $src_img, $arr_src ['to_x'], $arr_src ['to_y'], 0, 0, $arr_src ['new_width'], $arr_src ['new_height'], $arr_src ['width'], $arr_src ['height']); //輸出到文件 //$arr_src ['new_path'] = $oldimgurlArr[0].'/'.$oldimgurlArr[1].'/'.$oldimgurlArr[2].'/'.$oldimgurlArr[3].'/'.date('Ym').'/'.date('d').'/'.$oldimgurlArr[6] . $width . '_' . $height . '.jpg'; $targetpath = dirname($arr_src ['new_path']); dmkdir($targetpath); //若是沒有此文件夾就建立文件夾 imagejpeg($dst_img, $arr_src ['new_path']); //銷燬各方資源 imagedestroy($dst_img); imagedestroy($src_img); if ($is_del_ori) { @unlink($pic_path); } return true; } /* * 圖片另存爲 指定後綴的文件 * 原旨在保存原圖 */ function savePic($pic_path, $suffix = 'original') { if (!$suffix || strpos($suffix, '/') !== false) { return false; } $suffix = trim($suffix, '.'); $new_path = $pic_path . '.' . $suffix . '.jpg'; if (@copy($source, $new_path)) { return true; } elseif (@is_readable($source) && (@$fp_s = fopen($source, 'rb')) && (@$fp_t = fopen($new_path, 'wb'))) { while (!feof($fp_s)) { $s = @fread($fp_s, 1024 * 512); @fwrite($fp_t, $s); } fclose($fp_s); fclose($fp_t); return true; } else { $content = file_get_contents($pic_path); file_put_contents($new_path, $content); return true; } } /* * 上傳指定大小的圖片 */ function uploadedPicZoom($filesArr, $w, $h, $pic_path) { if ((($filesArr ["file"] ["type"] == "image/gif") || ($filesArr ["file"] ["type"] == "image/jpeg") || ($filesArr ["file"] ["type"] == "image/pjpeg")) && ($filesArr ["file"] ["size"] < 204800)) { if ($filesArr ["file"] ["error"] > 0) { print '文件類型錯誤'; return false; } else { $file_name = time() . $filesArr ["file"] ["name"]; if (file_exists($pic_path . $file_name)) { print '文件重名'; return false; } else { $targetpath = dirname($pic_path . $file_name); dmkdir($targetpath); move_uploaded_file($filesArr ["file"] ["tmp_name"], $pic_path . $file_name); return $this->PicZoom($pic_path . $file_name, $w, $h, $w . '_' . $h, $bk_rgb = array(255, 255, 255), true); } } } else { print '格式錯誤或者文件太大'; return false; } } /** * 上傳圖片壓縮截取 生成指定寬高的圖片 * $suffix 新圖片後綴名稱,若是爲空,則爲width_height; 縮略圖後綴爲: ‘thumb’ * 圖片統一後綴 .jpg * */ function PicCompress($pic_path, $width = 100, $height = 100, $suffix = '') { if (!$pic_path || !file_exists($pic_path)) { return false; } if (!function_exists('imagecreatetruecolor')) { return false; } $arr_tmp = getimagesize($pic_path); if (!$arr_tmp) { return false; //不是圖片 } $arr_src = array(); //得到原始圖像的大小 $arr_src ['width'] = $arr_tmp [0]; $arr_src ['height'] = $arr_tmp [1]; //取得文件擴展名 $mime_tmp = explode('/', $arr_tmp['mime']); $ext = $mime_tmp[1]; //生成的新圖片後綴,一概存成JPG if ($suffix) { if (strpos($suffix, '/') !== false) { return false; } $suffix = trim($suffix, '.'); } $suffix = $suffix ? $suffix : $width . '_' . $height; $arr_src ['new_path'] = $pic_path . '.' . $suffix . '.jpg'; //若是新生成圖片的尺寸寬與高都大於原圖,則不壓縮截取 if ($width > $arr_src ['width'] && $height > $arr_src ['height']) { return $this->savePic($pic_path, $suffix); } //若是新生成圖片的尺寸寬或高大於原圖,則按寬或高縮放原圖 if ($width > $arr_src ['width'] || $height > $arr_src ['height']) { //$this->PicZoom($pic_path, $width, $height); return $this->PicSourceZoom($pic_path, $width, $height, $suffix); } //根據比例計算新的長寬 目標的座標x y if ($width / $height > $arr_src ['width'] / $arr_src ['height']) { //以width爲基準 $arr_src ['new_height'] = $width * ($arr_src ['height'] / $arr_src ['width']); $arr_src ['new_width'] = $width; $arr_src ['src_y'] = ($arr_src ['new_height'] - $height) / 2; $arr_src ['src_x'] = 0; } else { //以height爲基準 $arr_src ['new_height'] = $height; $arr_src ['new_width'] = $height * ($arr_src ['width'] / $arr_src ['height']); $arr_src ['src_x'] = ($arr_src ['new_width'] - $width) / 2; $arr_src ['src_y'] = 0; } //列出建立圖片資源的函數表 $create_fun_map = array('jpg' => 'imagecreatefromjpeg', 'jpeg' => 'imagecreatefromjpeg', 'gif' => 'imagecreatefromgif', 'png' => 'imagecreatefrompng'); if (!$create_fun_map [$ext]) return false; $create_fun = $create_fun_map [$ext]; //參數計算完畢,建立圖像 $dst_img = imagecreatetruecolor($width, $height); $bk_color = imagecolorallocate($dst_img, 255, 255, 255); imagefill($dst_img, 0, 0, $bk_color); $src_img = $create_fun($pic_path); imagecopyresampled($dst_img, $src_img, 0, 0, $arr_src ['src_x'], $arr_src ['src_y'], $arr_src ['new_width'], $arr_src ['new_height'], $arr_src ['width'], $arr_src ['height']); //輸出到文件 $targetpath = dirname($arr_src ['new_path']); dmkdir($targetpath); //若是沒有此文件夾就建立文件夾 imagejpeg($dst_img, $arr_src ['new_path'], 95); //銷燬各方資源 imagedestroy($dst_img); imagedestroy($src_img); return true; } /** * 上傳圖片壓縮截取 生成指定寬高的圖片 * 所用的是GD庫 */ function PicCompressNewPath($pic_path, $width = 100, $height = 100, $newpath = '', $suffix = '') { if (!$pic_path || !file_exists($pic_path)) { return false; } if (!function_exists('imagecreatetruecolor')) { return false; } if (empty($newpath)) $newpath = $pic_path; $arr_src = array(); $arr_tmp = getimagesize($pic_path); if (!$arr_tmp) { return false; //不是圖片 } //得到原始圖像的大小 $arr_src ['width'] = $arr_tmp [0]; $arr_src ['height'] = $arr_tmp [1]; //取得文件擴展名 $mime_tmp = explode('/', $arr_tmp['mime']); $ext = $mime_tmp[1]; //生成的新圖片後綴,一概存成JPG if ($suffix) { if (strpos($suffix, '/') !== false) { return false; } $suffix = trim($suffix, '.'); } $suffix = $suffix ? $suffix : $width . '_' . $height; $arr_src ['new_path'] = $newpath . '.' . $suffix . '.jpg'; //根據比例計算新的長寬 目標的座標x y if ($width / $height > $arr_src ['width'] / $arr_src ['height']) { //以width爲基準 $arr_src ['new_height'] = $width * ($arr_src ['height'] / $arr_src ['width']); $arr_src ['new_width'] = $width; $arr_src ['src_y'] = ($arr_src ['new_height'] - $height) / 2; $arr_src ['src_x'] = 0; } else { //以height爲基準 $arr_src ['new_height'] = $height; $arr_src ['new_width'] = $height * ($arr_src ['width'] / $arr_src ['height']); $arr_src ['src_x'] = ($arr_src ['new_width'] - $width) / 2; $arr_src ['src_y'] = 0; } //列出建立圖片資源的函數表 $create_fun_map = array('jpg' => 'imagecreatefromjpeg', 'jpeg' => 'imagecreatefromjpeg', 'gif' => 'imagecreatefromgif', 'png' => 'imagecreatefrompng'); if (!$create_fun_map [$ext]) return false; $create_fun = $create_fun_map [$ext]; //參數計算完畢,建立圖像 $dst_img = imagecreatetruecolor($width, $height); $bk_color = imagecolorallocate($dst_img, 255, 255, 255); imagefill($dst_img, 0, 0, $bk_color); $src_img = $create_fun($pic_path); imagecopyresampled($dst_img, $src_img, 0, 0, $arr_src ['src_x'], $arr_src ['src_y'], $arr_src ['new_width'], $arr_src ['new_height'], $arr_src ['width'], $arr_src ['height']); //輸出到文件 $targetpath = dirname($arr_src ['new_path']); dmkdir($targetpath); //若是沒有此文件夾就建立文件夾 imagejpeg($dst_img, $arr_src ['new_path'], 95); //銷燬各方資源 imagedestroy($dst_img); imagedestroy($src_img); return true; } /** * 圖片按指定的寬,或高縮放,從新生成 * 若是沒有後綴,則直接縮放原圖 * @param $pic_path * @param $width 指定圖片寬 * @param $height 指定圖片高 */ function PicSourceZoom($pic_path, $width = '', $height = '', $suffix = '') { if (!$pic_path || !file_exists($pic_path)) { return false; } if (!$width && !$height) { return false; } if (!function_exists('imagecreatetruecolor')) { return false; } $arr_src = array(); $arr_tmp = getimagesize($pic_path); if (!$arr_tmp) { return false; //不是圖片 } //得到原始圖像的大小 $arr_src ['width'] = $arr_tmp [0]; $arr_src ['height'] = $arr_tmp [1]; //若是新生成圖片的尺寸寬與高都大於原圖,則不壓縮圖片,另存指定尺寸圖片 if ($width > $arr_src ['width'] && $height > $arr_src ['height'] && $suffix) { return $this->savePic($pic_path, $suffix); } //若是新生成圖片的尺寸寬大於原圖,寬需從新指定 if ($width > $arr_src ['width']) { $width = ''; } if ($height > $arr_src ['height']) { $height = ''; } if (!$width && !$height) { return false; } //從新定義給定的等比壓縮寬,高 if ($width && $width > $arr_src ['width']) { $width = $arr_src ['width']; } if ($height && $height > $arr_src ['height']) { $height = $arr_src ['height']; } //取得文件擴展名 $mime_tmp = explode('/', $arr_tmp['mime']); $ext = $mime_tmp[1]; if ($suffix) { if (strpos($suffix, '/') !== false) { return false; } $suffix = trim($suffix, '.'); $arr_src ['new_path'] = $pic_path . '.' . $suffix . '.jpg'; } else { $arr_src ['new_path'] = $pic_path; } //根據給定寬、高計算目標座標x y if ($width && !$height) { $arr_src ['new_width'] = $width; $arr_src ['new_height'] = $arr_src ['height'] / $arr_src ['width'] * $width; $height = $arr_src ['height'] / $arr_src ['width'] * $width; } if (!$width && $height) { $arr_src ['new_height'] = $height; $arr_src ['new_width'] = $arr_src ['width'] / $arr_src ['height'] * $height; $width = $arr_src ['width'] / $arr_src ['height'] * $height; } $arr_src ['to_x'] = 0; $arr_src ['to_y'] = 0; //列出建立圖片資源的函數表 $create_fun_map = array('jpg' => 'imagecreatefromjpeg', 'jpeg' => 'imagecreatefromjpeg', 'gif' => 'imagecreatefromgif', 'png' => 'imagecreatefrompng'); if (!$create_fun_map [$ext]) return false; $create_fun = $create_fun_map [$ext]; //參數計算完畢,建立圖像 $dst_img = imagecreatetruecolor($width, $height); $bk_color = imagecolorallocate($dst_img, 0xFF, 0xFF, 0xFF); imagefill($dst_img, 0, 0, $bk_color); $src_img = $create_fun($pic_path); imagecopyresampled($dst_img, $src_img, $arr_src ['to_x'], $arr_src ['to_y'], 0, 0, $arr_src ['new_width'], $arr_src ['new_height'], $arr_src ['width'], $arr_src ['height']); //輸出到文件 $targetpath = dirname($arr_src ['new_path']); dmkdir($targetpath); //若是沒有此文件夾就建立文件夾 imagejpeg($dst_img, $arr_src ['new_path'], 100); //銷燬各方資源 imagedestroy($dst_img); imagedestroy($src_img); return true; } /* * 原圖生成不一樣尺寸的圖片, * 壓縮截取 */ function manySizesPicCompress($pic_path, $sizesArr, $bk_rgb = array(255, 255, 255)) { if (!is_array($sizesArr) || count($sizesArr) == 0) { echo 'sizesArr error'; } foreach ($sizesArr as $value) { $width = $value['width']; $height = $value['height']; if (!$width || !$height) { continue; } $zoomtype = $value['zoomtype']; if ($zoomtype) { $this->PicCompress($pic_path, $width, $height); } else { $this->PicZoom($pic_path, $width, $height); } } } }