2012-06-06 36 views
0

我已經編寫了合併兩個圖像的代碼。用PHP合併圖像

我的代碼是:

$rnd = rand("99000", "99999"); 

$dst_path = "/home/maxioutl/public_html/images/urunler/"; 
$dst_file_name = "tresim-{$rnd}.jpg"; 
$dst_file = $dst_path.$dst_file_name; 

$dst = imagecreatetruecolor(250, 375); 
imagefill($dst, 0, 0, imagecolorallocate($dst, 255, 255, 255)); 

$src = imagecreatefromjpeg("http://www.goldstore.com.tr/upload/product/raw/3.72925.0332.JPG"); 

imagecopymerge($dst, $src, 40, 60, 0, 0, 250, 375, 100); 

imagejpeg($dst, $dst_file, 90); 

結果:

black bg 黑色背景。它在哪裏?

+0

你想要做什麼? – motto

+0

我想要白色背景。 –

回答

0

你寫的代碼疊加了圖像。它不會使用手錶從圖像中刪除白色像素。

它們基本上重疊。

您應該循環瀏覽兩個圖像尺寸,並用黑色像素或透明像素替換每個*白色像素。 *白色像素可能不一定意味着rgb(255,255,255),您可以選擇將rgb(> 235,> 235,> 235)的所有像素視爲「白色」。

0

功能imagecreatetruecolor創建從here兩者完全黑色圖像 :

imagecreatetruecolor()返回一個表示指定大小的 黑色圖像的圖像標識符。

1

這是這樣做的imagecopymerge($dst, $src, 40, 60, 0, 0, 250, 375, 100);聲明。

您正在向它傳遞尺寸250x375,這不是手錶的實際尺寸。因此合併邊界框繼續,並使用黑色。如果你將它評論出來,你可以很容易地看到它,因爲你會像你期望的那樣從填充中獲得白色方塊。

您需要獲取手錶圖形的確切尺寸(即通過getimagesize)並將其傳遞給imagecopymerge,以便在合併時精確切割。

$arrSize = getimagesize($originalFile); 
imagecopymerge($dst, $src, 40, 60, 0, 0, $arrSize[0], $arrSize[1], 100);