使用函式
bool imagecopyresampled ( resource dst_image, resource src_image, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h )
函式參數說明:
dst_image : 輸出目標檔案
src_image : 來源檔案
dst_x: 目標檔案開始點的 x 座標
dst_y: 目標檔案開始點的 y 座標
src_x: 來源檔案開始點的 x 座標
src_y: 來源檔案開始點的 y 座標
dst_w: 目標檔案的長度
dst_h: 目標檔案的高度
src_w: 來源檔案的長度
src_h: 來源檔案的高度
例:將圖像調整為原有尺寸的一半
<?php
// 這個文件
$filename = 'test.jpg';
$percent = 0.5;
// 內容類型
header('Content-Type: image/jpeg');
// 獲取新的尺寸
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
// 重新取樣
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// 輸出
imagejpeg($image_p, null, 100);
?>
例:這個例子會以最大寬度高度為 200 像素顯示一個圖像。
<?php
// 源文件
$filename = 'test.jpg';
// 設置最大寬高
$width = 200;
$height = 200;
// Content type
header('Content-Type: image/jpeg');
// 獲取新尺寸
list($width_orig, $height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// 重新取樣
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// 輸出
imagejpeg($image_p, null, 100);
?>
例1:
$src = imagecreatefromjpeg("./old.jpg"); //讀取來源圖檔
$src_w = imagesx($src); //取得來源圖檔長寬
$src_h = imagesy($src);
$new_w = 100; //新圖檔長寬
$new_h = 100;
$thumb = imagecreatetruecolor($new_w, $new_h); //建立空白縮圖
//設定空白縮圖的背景,如不設定,背景預設為黑色
$bg = imagecolorallocate($thumb,255,0,255); //空白縮圖的背景顏色
imagefilledrectangle($thumb,0,0,$src_w,$src_h,$bg); //將顏色填入縮圖
//執行縮圖
imagecopyresampled($thumb, $src, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src_h);
imagejpeg($thumb, "./new.jpg");
例2:截圖,某個區域
<?
header("Content-type: image/jpeg");
$filename = 'book_rabbit_rule.jpg';
/* 讀取圖檔 */
$im = imagecreatefromjpeg($filename);
/* 圖片要截多少, 長/寬 */
$new_img_width = 120;
$new_img_height = 42;
/* 先建立一個 新的空白圖檔 */
$newim = imagecreate($new_img_width, $new_img_height);
// 輸出圖要從哪邊開始x, y , 原始圖要從哪邊開始 x, y , 要畫多大 x, y(resize) ,
要抓多大 x, y
imagecopyresampled($newim, $im, 0, 0, 7, 174, 120, 42, $new_img_width,
$new_img_height);
/* 放大 成 500 x 500 的圖 */
// imagecopyresampled($newim, $im, 0, 0, 100, 30, 500, 500, $new_img_width,
$new_img_height);
/* 將圖印出來 */
imagejpeg($newim);
/* 資源回收 */
imagedestroy($newim);
imagedestroy($im);
?>
例: 圖片浮水印
<?php
$org_img_path = 'test007.jpg';//原本圖片路徑
$png_img_path = 'test007.png';//浮水印圖片路徑
$org_img_size = getImageSize($org_img_path);//讀取原本圖片大小資訊
$png_img_size = getImageSize($png_img_path);//讀取浮水印圖片大小資訊
$org_img_x = 0;//原本圖片擺放位置X
$org_img_y = 0;//原本圖片擺放位置Y
$png_img_x = 0;//浮水印圖片擺放位置X
$png_img_y = 0;//浮水印圖片擺放位置Y
$org_img_w = $org_img_size[0];//原本圖片寬
$org_img_h = $org_img_size[1];//原本圖片高
$png_img_w = $png_img_size[0];//浮水印圖片寬
$png_img_h = $png_img_size[1];//浮水印圖片高
$org_img = imagecreatefromjpeg($org_img_path);//原本圖片
$png_img = imagecreatefrompng($png_img_path);//浮水印圖片
//合併圖片函式,將 $png_img 合併到 $org_img
imagecopyresampled(
$org_img,//原本圖片
$png_img,//浮水印圖片
$org_img_x,//原本圖片擺放位置X
$org_img_y,//原本圖片擺放位置Y
$png_img_x,//浮水印圖片擺放位置X
$png_img_y,//浮水印圖片擺放位置Y
$org_img_w,//原本圖片寬
$org_img_h,//原本圖片高
$png_img_w,//浮水印圖片寬
$png_img_h //浮水印圖片高
);
Header("Content-type: image/jpeg"); //設定伺服器回應為圖片(重要)
ImageJPEG($org_img);//產生圖片
exit();
?>