-1

我需要拍摄背景和头盔的PNG,头盔中间是透明的。在 PHP 中,我将传递它的顶部和左侧坐标,以及 JPG 的宽度和高度。我想把JPG放在PNG下面,把它变成我发送它的大小(根据我给它的大小调整大小),把它放在PNG下面的X和Y处,把PNG放在最前面. 换句话说,JPG 的任何部分在 PNG 边框之外都会被剪掉,生成的图像是以 PNG 的宽度和高度覆盖在 JPG 上的 PNG。

这是我目前拥有的,但它只是将头盔保存在头部应该在的黑色背景中。

$photo1 = imagecreatefromjpeg($_POST['photo']);
    $foto1W = imagesx($photo1);
    $foto1H = imagesy($photo1);
    $photoFrameW = $res['width'];
    $photoFrameH = $res['height'];
    $photoFrame = imagecreatetruecolor($photoFrameW,$photoFrameH);
    imagecopyresampled($photoFrame, $photo1, 0, 0, 0, 0, $photoFrameW, $photoFrameH, $foto1W, $foto1H);

    $photo2 = imagecreatefrompng('images/casco.png');
    $foto2W = imagesx($photo2);
    $foto2H = imagesy($photo2);
    $photoFrame2W = '500';
    $photoFrame2H = '556';

    $photoFrame2    = imagecreatetruecolor($photoFrame2W,$photoFrame2H);
    $trans_colour   = imagecolorallocatealpha($photoFrame2, 0, 0, 0, 127);
    imagefill($photoFrame2, 0, 0, $trans_colour);

    imagecopyresampled($photoFrame2, $photo2, 0, 0, 0, 0, $photoFrame2W, $photoFrame2H, $foto2W, $foto2H);

    imagecopy($photoFrame2, $photoFrame, $res['left']+556, $res['top'], 0, 0, imagesx($photoFrame), imagesy($photoFrame));

    imagejpeg($photoFrame2, "fb_images/$codigo2.jpg");
4

1 回答 1

0

以上述内容为起点,我自己想出来了。问题是双重的,它把错误的图像放在了顶部,我没有使用正确的位置来重新定位图像。我最终在插入之前重新调整大小和重新定位图像。然后在最后插入 JPG 顶部的 PNG。

    $photo1 = imagecreatefromjpeg($_POST['photo']);
    $foto1W = imagesx($photo1);
    $foto1H = imagesy($photo1);
    $photoFrameW = $res['width'];
    $photoFrameH = $res['height'];
    $photoFrame = imagecreatetruecolor(500,556);
    imagecopyresampled($photoFrame, $photo1, $res['left'], $res['top']+556, 0, 0, $photoFrameW, $photoFrameH, $foto1W, $foto1H);

    $photo2 = imagecreatefrompng('images/casco.png');
    $foto2W = imagesx($photo2);
    $foto2H = imagesy($photo2);
    $photoFrame2W = '500';
    $photoFrame2H = '556';

    $photoFrame2    = imagecreatetruecolor($photoFrame2W,$photoFrame2H);
    $trans_colour   = imagecolorallocatealpha($photoFrame2, 0, 0, 0, 127);
    imagefill($photoFrame2, 0, 0, $trans_colour);

    imagecopyresampled($photoFrame2, $photo2, 0, 0, 0, 0, $photoFrame2W, $photoFrame2H, $foto2W, $foto2H);

    imagecopy($photoFrame, $photoFrame2, 0, 0, 0, 0, imagesx($photoFrame2), imagesy($photoFrame2));

    imagejpeg($photoFrame, "fb_images/$codigo2.jpg");
于 2012-08-30T19:14:51.247 回答