bool imagecopyresampled(
GdImage $dst, GdImage $src,
int $dstX, int $dstY, // 目標貼上座標
int $srcX, int $srcY, // 來源裁剪起始座標
int $dstW, int $dstH, // 目標寬高 (縮放後)
int $srcW, int $srcH // 來源裁剪寬高
)
**一次完成「裁剪區域選取」+「縮放/放大」+「貼上目標」**。
前端 (如 Cropper.js、imgAreaSelect) 傳送:`x, y, width, height` (相對原圖座標)。
<?php
function cropAndResize(string $srcPath, int $srcX, int $srcY, int $srcW, int $srcH, int $dstW, int $dstH, string $outputPath): bool
{
$src = imagecreatefromstring(file_get_contents($srcPath));
if (!$src) return false;
$dst = imagecreatetruecolor($dstW, $dstH);
// 關鍵:來源座標 ($srcX,$srcY) + 來源寬高 ($srcW,$srcH) → 目標 ($dstW,$dstH)
$ok = imagecopyresampled($dst, $src, 0, 0, $srcX, $srcY, $dstW, $dstH, $srcW, $srcH);
if ($ok) {
imagejpeg($dst, $outputPath, 90);
}
imagedestroy($src);
imagedestroy($dst);
return $ok;
}
// 使用:前端傳 x=100, y=12, w=200, h=200 → 輸出 100x100
cropAndResize('avatar_original.jpg', 100, 12, 200, 200, 100, 100, 'avatar_100.jpg');
來源座標 (0,0),來源寬高 = 原圖寬高,目標寬高按比例計算。
function createThumbnail(string $srcPath, int $maxW, int $maxH, string $outputPath): bool
{
$src = imagecreatefromstring(file_get_contents($srcPath));
if (!$src) return false;
$srcW = imagesx($src);
$srcH = imagesy($src);
// 計算等比尺寸
$ratio = min($maxW / $srcW, $maxH / $srcH);
$dstW = (int)round($srcW * $ratio);
$dstH = (int)round($srcH * $ratio);
$dst = imagecreatetruecolor($dstW, $dstH);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $dstW, $dstH, $srcW, $srcH);
imagejpeg($dst, $outputPath, 90);
imagedestroy($src);
imagedestroy($dst);
return true;
}
適合封面圖、Hero Banner:圖片**填滿**目標框,超出部分裁掉。
function cropCover(string $srcPath, int $dstW, int $dstH, string $outputPath): bool
{
$src = imagecreatefromstring(file_get_contents($srcPath));
if (!$src) return false;
$srcW = imagesx($src);
$srcH = imagesy($src);
$ratioSrc = $srcW / $srcH;
$ratioDst = $dstW / $dstH;
if ($ratioSrc > $ratioDst) {
// 來源較寬 → 以高度為基準縮放,寬度超出再裁剪
$srcY = 0;
$srcH2 = $srcH;
$srcW2 = (int)round($srcH * $ratioDst);
$srcX = (int)(($srcW - $srcW2) / 2);
} else {
// 來源較高 → 以寬度為基準
$srcX = 0;
$srcW2 = $srcW;
$srcH2 = (int)round($srcW / $ratioDst);
$srcY = (int)(($srcH - $srcH2) / 2);
}
$dst = imagecreatetruecolor($dstW, $dstH);
imagecopyresampled($dst, $src, 0, 0, $srcX, $srcY, $dstW, $dstH, $srcW2, $srcH2);
imagejpeg($dst, $outputPath, 90);
imagedestroy($src);
imagedestroy($dst);
return true;
}
<!-- HTML -->
<img id="avatar" src="original.jpg" alt="頭像">
<button id="cropBtn">確認裁剪</button>
<script src="cropper.min.js"></script>
<script>
const cropper = new Cropper(document.getElementById('avatar'), {
aspectRatio: 1, // 正方形
viewMode: 1,
autoCropArea: 1,
responsive: true,
});
document.getElementById('cropBtn').onclick = () => {
const data = cropper.getData(true); // {x, y, width, height, rotate, scaleX, scaleY}
fetch('crop.php', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(data)
}).then(r => r.json()).then(res => {
if (res.success) location.reload();
});
};
</script>
`imagecopyresampled` 是 GD 圖片處理的瑞士刀:**裁剪座標 (srcX,srcY) + 裁剪尺寸 (srcW,srcH) + 目標尺寸 (dstW,dstH)** 任意組合,涵蓋裁剪、縮略、Cover、Contain (白邊) 所有需求。封裝為三大函式 (`cropAndResize`、`createThumbnail`、`cropCover`) 即可滿足 95% 業務場景。