2012-08-29 14 views
-1

我需要採用背景和頭盔的PNG,頭盔在中間是透明的。在PHP中,我會傳遞它的頂部和左側座標,以及JPG的寬度和高度。我想將JPG文件放在PNG下面,將其尺寸設置爲我發送它的尺寸(調整大小以適應我給出的尺寸),然後將它放在PNG下方的X和Y處,並將PNG放在最前面。換句話說,JPG的任何部分都在PNG邊界之外被剪切掉,並且得到的圖像是在PNG寬度和高度上覆蓋在JPG上的PNG。PHP Overlay PNG over 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"); 
+2

[你嘗試過什麼?](http://whathaveyoutried.com/) –

+0

你可以張貼一些示例圖像,以幫助我們瞭解你在做什麼?你能告訴我們你的源代碼到目前爲止? – Brad

+0

這是我想用PHP做的: http://i.imgur.com/FsoQt.jpg 到目前爲止,我還沒有嘗試過任何代碼,因爲imagecopy和imagejpeg以及所有這些都很時髦,時間完全理解他們。 – jfreak53

回答

0

以上述爲出發點,自己想出來。問題有兩方面,一是將錯誤的圖像置於頂部,另一方面我沒有使用正確的位置重新定位圖像。我最終在插入之前重新調整大小並重新定位圖像。然後,將PNG插入到最後的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");