2012-01-27 82 views
0

嘿傢伙我有合併兩張圖片的問題... 我想合併一個PNG文件(稱爲徽章)與useruploaded圖片。 當用戶上傳png或gif文件時,一切正常,但如果他上傳jpeg圖像,輸出圖像看起來很奇怪。看起來這是一個顏色問題。imagecopyresized顏色jpeg不工作

This is an successfully badged image

And this happen with an jpeg image

這裏我的代碼:

//Calculate position for badge (right bottom corner) 
    $badgeRightPosition = $imageWidth - $badgeWidth; 
    $badgeLeftPosition = $imageHeight - $badgeHeight; 

    $image = imagecreatefromstring(file_get_contents($image)); 
    $badge = imagecreatefromstring(file_get_contents($badge)); 

    $trueColorImage = imagecreatetruecolor($imageWidth, $imageHeight); 
    imagealphablending($trueColorImage, true); 
    imagesavealpha($trueColorImage, true); 

    imagealphablending($badge, true); 
    imagesavealpha($badge, true); 

    imagealphablending($image, true); 
    imagesavealpha($image, true); 

imagecopyresized($trueColorImage, $image, 0, 0, 0, 0, $imageWidth, $imageHeight, $imageWidth, $imageHeight); 
imagecopyresized($trueColorImage, $badge, $badgeRightPosition, $badgeLeftPosition, 0, 0, $imageWidth, $imageHeight, $badgeWidth, $badgeHeight); 
+0

你最近怎麼樣?提供一些代碼可以幫助你得到答案。 – Nick 2012-01-27 10:16:05

+0

我也不得不說,如果上傳的圖片大於允許的最大值。寬度,然後圖像將被調整大小。 正是在這裏的問題......調整圖像大小後(如果它是jpg類型)將被損壞 – 2012-01-27 10:26:31

+0

有幫助使用imagecopyresampled()而不是imagecopyresized()? – JochenJung 2012-01-27 12:36:12

回答

0

相反的imagecreatefromstring(file_get_contents($image));我想嘗試直接打開該文件

$imgsrc = @ImageCreateFromJPEG($image); 
if (!$imgsrc) $imgsrc = @ImageCreateFromPNG($image); 
if (!$imgsrc) $imgsrc = @ImageCreateFromGIF($image); 
if (!$imgsrc) $imgsrc = @ImageCreateFromWBMP($image); 

或支票的文件類型,其結束然後使用根據文件結尾的上述功能之一。

+0

也不起作用 – 2012-01-27 11:16:50