PHP覆盖PNG over JPG然后调整大小并将JPG定位到PNG的掩码

时间:2012-08-29 23:24:04

标签: php image gd

我需要佩戴背景和头盔的PNG,头盔中间是透明的。在PHP中,我将传递它的顶部和左侧坐标,以及JPG的宽度和高度。我想把JPG拿到PNG下面,把它放到我发送它的大小(调整大小或更小到我给它的大小),把它放在PNG下面的X和Y并使PNG最前面。换句话说,JPG的任何部分都在PNG边界之外被切除,结果图像是PNG在PNG宽度和高度上覆盖的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");

1 个答案:

答案 0 :(得分:0)

使用以上作为起点自己想出来。问题是双重的,它是将错误的图像放在上面而我没有使用正确的位置来重新定位图像。我在插入之前最终重新调整尺寸并重新定位图像。然后在最后将JPG插入JPG顶部。

    $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");
相关问题