告別漏水困擾,改善住宅環境!細心防水團隊
30年經驗,各大企業、豪宅指定合作
輕鬆接案網提供氣泡紙、紙箱、打包技巧
讓您輕鬆搬家、沒有缺失與遺憾

首頁  •  j2h 論壇 • 程式設計討論     • 

[教學]PHP-GD 縮圖失真解決方案

房東:紅妹
發表時間:2010-06-01


// 連結到這頁的圖片語法:點選圖片看大圖
$filename =htmlspecialchars($HTTP_GET_VARS['cn']);
//echo $filename;
//echo exit;
$subname = substr($filename, strrpos($filename, ".")+1); //讀取附檔名
//echo $subname;
//echo exit;
$photo_path = "../../../upload/o/o05/".$filename; // 相片檔案的存放路徑
//echo $photo_path;
//echo exit;
// 依副檔名來判斷相片的圖形格式
switch ( strtoupper($subname) ) {
case "JPEG":
$img_Type = "JPEG";
header("Content-type: image/" . $img_Type); // 傳 Header 給瀏覽器,告知圖檔格式
$src_img = ImageCreateFromJPEG($photo_path); // 視原圖的格式,用不同的方法讀取原圖

// 定義一個新的圖檔
// 將縮圖的尺寸設為原圖的 20%
$new_W = ImageSX($src_img) * 0.2; //取得圖形的寬度
$new_H = ImageSY($src_img) * 0.2; //取得圖形的高度
if ( $new_W > 150 ) { // 不讓縮圖的寬度多於 150,以免相片太大
$new_H = ( 150 / $new_W ) * $new_H;
$new_W = 150;
}

//2.0xx版使用
$dst_img = imagecreatetruecolor($new_W, $new_H);
//舊版使用
//$dst_img = ImageCreate($new_W, $new_H);

// 將原圖複製、調整尺寸,再貼在新圖上
//2.0xx版使用
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));
//舊版使用
//ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));

ImageJPEG($dst_img); // 視原圖的格式,用不同的方法將所建立的新圖檔輸出
break;
case "jpeg":
$img_Type = "JPEG";
header("Content-type: image/" . $img_Type); // 傳 Header 給瀏覽器,告知圖檔格式
$src_img = ImageCreateFromJPEG($photo_path); // 視原圖的格式,用不同的方法讀取原圖

// 定義一個新的圖檔
// 將縮圖的尺寸設為原圖的 20%
$new_W = ImageSX($src_img) * 0.2; //取得圖形的寬度
$new_H = ImageSY($src_img) * 0.2; //取得圖形的高度
if ( $new_W > 150 ) { // 不讓縮圖的寬度多於 150,以免相片太大
$new_H = ( 150 / $new_W ) * $new_H;
$new_W = 150;
}

//2.0xx版使用
$dst_img = imagecreatetruecolor($new_W, $new_H);
//舊版使用
//$dst_img = ImageCreate($new_W, $new_H);

// 將原圖複製、調整尺寸,再貼在新圖上
//2.0xx版使用
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));
//舊版使用
//ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));

ImageJPEG($dst_img); // 視原圖的格式,用不同的方法將所建立的新圖檔輸出
break;
case "JPG":
$img_Type = "JPEG";
header("Content-type: image/" . $img_Type); // 傳 Header 給瀏覽器,告知圖檔格式
$src_img = ImageCreateFromJPEG($photo_path);

$new_W = ImageSX($src_img) * 0.2; //取得圖形的寬度
$new_H = ImageSY($src_img) * 0.2; //取得圖形的高度
if ( $new_W > 150 ) { // 不讓縮圖的寬度多於 150,以免相片太大
$new_H = ( 150 / $new_W ) * $new_H;
$new_W = 150;
}


//2.0xx版使用
$dst_img = imagecreatetruecolor($new_W, $new_H);
//舊版使用
//$dst_img = ImageCreate($new_W, $new_H);

// 將原圖複製、調整尺寸,再貼在新圖上
//2.0xx版使用
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));
//舊版使用
//ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));

ImageJPEG($dst_img);
break;
case "jpg":
$img_Type = "JPEG";
header("Content-type: image/" . $img_Type); // 傳 Header 給瀏覽器,告知圖檔格式
$src_img = ImageCreateFromJPEG($photo_path);

$new_W = ImageSX($src_img) * 0.2; //取得圖形的寬度
$new_H = ImageSY($src_img) * 0.2; //取得圖形的高度
if ( $new_W > 150 ) { // 不讓縮圖的寬度多於 150,以免相片太大
$new_H = ( 150 / $new_W ) * $new_H;
$new_W = 150;
}

//2.0xx版使用
$dst_img = imagecreatetruecolor($new_W, $new_H);
//舊版使用
//$dst_img = ImageCreate($new_W, $new_H);

// 將原圖複製、調整尺寸,再貼在新圖上
//2.0xx版使用
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));
//舊版使用
//ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));

ImageJPEG($dst_img);
break;
case "JPE":
$img_Type = "JPEG";
header("Content-type: image/" . $img_Type); // 傳 Header 給瀏覽器,告知圖檔格式
$src_img = ImageCreateFromJPEG($photo_path);

$new_W = ImageSX($src_img) * 0.2; //取得圖形的寬度
$new_H = ImageSY($src_img) * 0.2; //取得圖形的高度
if ( $new_W > 150 ) { // 不讓縮圖的寬度多於 150,以免相片太大
$new_H = ( 150 / $new_W ) * $new_H;
$new_W = 150;
}

//2.0xx版使用
$dst_img = imagecreatetruecolor($new_W, $new_H);
//舊版使用
//$dst_img = ImageCreate($new_W, $new_H);

// 將原圖複製、調整尺寸,再貼在新圖上
//2.0xx版使用
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));
//舊版使用
//ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));

ImageJPEG($dst_img);
break;
case "GIF":
$img_Type = "GIF";
header("Content-type: image/" . $img_Type); // 傳 Header 給瀏覽器,告知圖檔格式
$src_img = ImageCreateFromGIF($photo_path);

$new_W = ImageSX($src_img) * 0.2; //取得圖形的寬度
$new_H = ImageSY($src_img) * 0.2; //取得圖形的高度
if ( $new_W > 150 ) { // 不讓縮圖的寬度多於 150,以免相片太大
$new_H = ( 150 / $new_W ) * $new_H;
$new_W = 150;
}

//2.0xx版使用
$dst_img = imagecreatetruecolor($new_W, $new_H);
//舊版使用
//$dst_img = ImageCreate($new_W, $new_H);

// 將原圖複製、調整尺寸,再貼在新圖上
//2.0xx版使用
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));
//舊版使用
//ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));

ImageGIF($dst_img);
break;
case "PNG":
$img_Type = "PNG";
header("Content-type: image/" . $img_Type); // 傳 Header 給瀏覽器,告知圖檔格式
$src_img = ImageCreateFromPNG($photo_path);

$new_W = ImageSX($src_img) * 0.2; //取得圖形的寬度
$new_H = ImageSY($src_img) * 0.2; //取得圖形的高度
if ( $new_W > 150 ) { // 不讓縮圖的寬度多於 150,以免相片太大
$new_H = ( 150 / $new_W ) * $new_H;
$new_W = 150;
}

//2.0xx版使用
$dst_img = imagecreatetruecolor($new_W, $new_H);
//舊版使用
//$dst_img = ImageCreate($new_W, $new_H);

// 將原圖複製、調整尺寸,再貼在新圖上
//2.0xx版使用
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));
//舊版使用
//ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));

ImagePNG($dst_img);
break;
case "png":
$img_Type = "PNG";
header("Content-type: image/" . $img_Type); // 傳 Header 給瀏覽器,告知圖檔格式
$src_img = ImageCreateFromPNG($photo_path);

$new_W = ImageSX($src_img) * 0.2; //取得圖形的寬度
$new_H = ImageSY($src_img) * 0.2; //取得圖形的高度
if ( $new_W > 150 ) { // 不讓縮圖的寬度多於 150,以免相片太大
$new_H = ( 150 / $new_W ) * $new_H;
$new_W = 150;
}

//2.0xx版使用
$dst_img = imagecreatetruecolor($new_W, $new_H);
//舊版使用
//$dst_img = ImageCreate($new_W, $new_H);

// 將原圖複製、調整尺寸,再貼在新圖上
//2.0xx版使用
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));
//舊版使用
//ImageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $new_W, $new_H, ImageSX($src_img), ImageSY($src_img));

ImagePNG($dst_img);
break;
}
// 分別釋放 $dst_img 與 $src_img 所佔用的儲存空間
ImageDestroy($dst_img);
ImageDestroy($src_img);
?>



  • 贊助網站       

    廣利不動產-新板特區指名度最高、值得您信賴的好房仲
    您的托付,廣利用心為您服務
    廣利不動產-板橋在地生根最實在--新板特區指名度最高、值得您信賴的好房仲
    完整房訊,房屋、店面熱門精選物件,廣利不動產 優質仲介,房屋租賃、買賣資訊透明,交易真安心!



  •  共 0 人回應

    姓名:
    佈告內容:
    其他選項: