拷貝部分影象並調整大小

imagecopyresized

(PHP 4, PHP 5, PHP 7, PHP 8)

imagecopyresized — 拷貝部分影象並調整大小

說明

imagecopyresized(
    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
): bool

imagecopyresized() 將一幅影象中的一塊矩形區域拷貝到另一個影象中。dst_image 和 src_image 分別是目標影象和源影象的識別符號。

In other words, imagecopyresized() will take an rectangular area from src_image of width src_w and height src_h at position (src_x,src_y) and place it in a rectangular area of dst_image of width dst_w and height dst_h at position (dst_x,dst_y).

如果源和目標的寬度和高度不同,則會進行相應的影象收縮和拉伸。座標指的是左上角。本函式可用來在同一幅圖內部拷貝(如果 dst_image 和 src_image 相同的話)區域,但如果區域交迭的話則結果不可預知。

參數

dst_image

目標圖像資源。

src_image

源圖像資源。

dst_x

x-coordinate of destination point.

dst_y

y-coordinate of destination point.

src_x

x-coordinate of source point.

src_y

y-coordinate of source point.

dst_w

Destination width.

dst_h

Destination height.

src_w

源圖像的寬度。

src_h

源圖像的高度。

返回值

成功時返回 true, 或者在失敗時返回 false。

範例

示例 #1 Resizing an image

這個例子會以一半的尺寸顯示圖片

<?php
// File and new size
$filename = 'test.jpg';
$percent = 0.5;

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

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
imagejpeg($thumb);
?>

以上例程的輸出類似於:

Output of example : Resizing an image

The image will be output at half size, though better quality could be obtained using imagecopyresampled().

註釋

注意:

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

參見

imagecopyresampled() - 重採樣拷貝部分影象並調整大小

Leave a Reply

Your email address will not be published. Required fields are marked *