經過多次嘗試和錯誤以及咬牙切齒,我終於弄清楚如何將帶有 8 位元 alpha 的 png 合成到 jpg 上。這對我來說並不顯而易見,所以我認為我應該分享。希望這對您有所幫助。
我正在使用它來建立帶有邊框的縮圖影像
<?php
$frame = imagecreatefrompng('path/to/frame.png');
$thumb = imagecreatefromjpeg('path/to/thumbnail.jpg');
$width = imagesx( $frame );
$height = imagesy( $frame );
$img=imagecreatetruecolor( $width, $height );
imagealphablending($img, true);
$transparent = imagecolorallocatealpha( $img, 0, 0, 0, 127 );
imagefill( $img, 0, 0, $transparent );
imagecopyresampled($img,$thumb,32,30,0,0, 130, 100, imagesx( $thumb ), imagesy( $thumb ) );
imagecopyresampled($img,$frame,0,0,0,0, $width,$height,$width,$height);
imagealphablending($img, false);
imagesavealpha($img,true);
header('Content-type: image/png');
imagepng( $img );
imagedestroy($img);
exit;
?>