imagecopyresampled

(PHP 4 >= 4.0.6, PHP 5)

imagecopyresampled -- 重取樣複製部分圖像並調整大小

說明

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 )

imagecopyresampled() 將一幅圖像中的一塊正方形區功能變數複製到另一個圖像中,平滑地插入像素值,因此,尤其是,減小了圖像的大小而仍然保持了極大的清晰度。若果成功則返回 TRUE,失敗則返回 FALSE

dst_imagesrc_image 分別是目的圖像和源圖像的標識符。若果源和目的的寬度和高度不同,則會進行相應的圖像收縮和拉伸。坐標指的是左上角。本函數可用來在同一幅圖內定複製(若果 dst_imagesrc_image 相同的話)區功能變數,但若果區功能變數交迭的話則結果不可預知。

注: 因為調色板圖像限制(255+1 種彩色)有個問題。重取樣或過濾圖像通常需要多於 255 種彩色,計算新的被重取樣的像素及其彩色時採用了一種近似值。對調色板圖像嘗試配置一個新彩色時,若果失敗我們選取了計算結果最接近(理論上)的彩色。這並不總是視覺上最接近的彩色。這可能會產生怪異的結果,例如空白(或是視覺上是空白)的圖像。要略過這個問題,請使用真彩色圖像作為目的圖像,例如用 imagecreatetruecolor() 建立的。

注: 本函數需要 GD 2.0.1 或更高版本。

範例

例子 1. 簡單例子

本例中將把一幅圖像重取樣為原始大小的一半。

<?php
// The file
$filename 'test.jpg';
$percent 0.5;

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width$height) = getimagesize($filename);
$new_width $width $percent;
$new_height $height $percent;

// Resample
$image_p imagecreatetruecolor($new_width$new_height);
$image imagecreatefromjpeg($filename);
imagecopyresampled($image_p$image0000$new_width$new_height$width$height);

// Output
imagejpeg($image_pnull100);
?>

例子 2. 按比例重取樣圖像

本例將把一幅圖像按最寬或最高 200 像素來顯示。

<?php
// The file
$filename 'test.jpg';

// Set a maximum height and width
$width 200;
$height 200;

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width_orig$height_orig) = getimagesize($filename);

if (
$width && ($width_orig $height_orig)) {
    
$width = ($height $height_orig) * $width_orig;
} else {
    
$height = ($width $width_orig) * $height_orig;
}

// Resample
$image_p imagecreatetruecolor($width$height);
$image imagecreatefromjpeg($filename);
imagecopyresampled($image_p$image0000$width$height$width_orig$height_orig);

// Output
imagejpeg($image_pnull100);
?>