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() 將一幅圖像中的一塊矩形區(qū)域拷貝到另一個圖像中。dst_imagesrc_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).

如果源和目標的寬度和高度不同,則會進行相應的圖像收縮和拉伸。坐標指的是左上角。本函數(shù)可用來在同一幅圖內部拷貝(如果 dst_imagesrc_image 相同的話)區(qū)域,但如果區(qū)域交迭的話則結果不可預知。

參數(shù)

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$source0000$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() 創(chuàng)建的。

參見

imagecopyresampled() - 重采樣拷貝部分圖像并調整大小