2012-03-30 54 views
-1

我有一個JPG文件,我想合併另一個圖像PNG文件就可以了。我不知道如何實現這個使用PHP?如何爲圖像加水印?

+1

這是偉大的。謹慎向我們提供一些細節和一些現有的代碼?你有什麼嘗試? – 2012-03-30 15:22:57

+0

一些代碼會很好 – 2012-03-30 15:23:02

+0

你應該把這個問題的標題改爲「使用gd和php的水印圖像」 – 2012-03-30 15:29:05

回答

1

您可以使用gd的imagecopy函數。

此功能用於圖像的一部分從源複製到目標

imagecopy ( 
      $dst_im , // destination image (resource), imagecreatefrom(gif|jpg|png) 
      $src_im , // destination image (resource), imagecreatefrom(gif|jpg|png) 
      $dst_x , // x cordinate in destination where u want the new obj placed 
      $dst_y , // y cordinate in destination where u want the new obj placed 
      $src_x , // x cordinate in source from wher u want the new obj placed 
      $src_y , // y cordinate in source from where u want the new obj placed 
      $src_w , // the width of the object to copy 
      $src_h  // the height of the object to copy 
); 

如在上面的代碼中的註釋指出,你將不得不創建兩個目的地和源的圖像資源。

通常使用

$src = imagecreatefromjpg('image.jpg'); 
$dst = imagecreatefromjpg('watermark'); 

剩餘部分只是簡單的座標來完成。

也別忘了自己造訪imagecopy

0

這裏是在炎熱的做水印使用GD一些例子:

const CORNER_TOP_LEFT  = 1; 
const CORNER_TOP_RIGHT  = 2; 
const CORNER_BOTTOM_LEFT = 3; 
const CORNER_BOTTOM_RIGHT = 4; 

$backgroundImagePath = "img/stamp.png"; 
$corner=CORNER_BOTTOM_RIGHT; 
$alpha=60 

[email protected]($filename); 
$img_info=getimagesize($backgroundImagePath); 

switch ($corner){ 
    case CORNER_TOP_LEFT: 
     if(!imagecopymerge ($this->imageRes, $img_res, 0, 0, 0, 0, $img_info[0], $img_info[1], $alpha)){ 
      throw new RuntimeException("Unable to make stamp!"); 
     } 
     break; 
    case CORNER_TOP_RIGHT: 
     if(!imagecopymerge ($this->imageRes, $img_res, $this->info[0]-$img_info[0], 0, 0, 0, $img_info[0], $img_info[1], $alpha)){ 
      throw new RuntimeException("Unable to make stamp!"); 
     } 
     break; 
    case CORNER_BOTTOM_LEFT: 
     if(!imagecopymerge ($this->imageRes, $img_res, 0, $this->info[1]-$img_info[1], 0, 0, $img_info[0], $img_info[1], $alpha)){ 
      throw new RuntimeException("Unable to make stamp!"); 
     } 
     break; 
    case CORNER_BOTTOM_RIGHT: 
     if(!imagecopymerge ($this->imageRes, $img_res, $this->info[0]-$img_info[0], $this->info[1]-$img_info[1], 0, 0, $img_info[0], $img_info[1], $alpha)){ 
      throw new RuntimeException("Unable to make stamp!"); 
     } 
     break; 
} 

imagejpeg($img_res, "/path/to/save/image.jpg", 100); 
+0

我想合併從PHP創建的jpeg文件,就像這裏$ filename =「pic。 PHP「我想使用PHP,因爲pic.php生成隨機圖片,我想合併圖片。 – 2012-03-31 07:01:19

+0

不清楚你的隨機圖像是如何生成的。請詳細解釋。 – 2012-04-01 01:10:58

+0

以captcha i generete爲例,使用圖像ttf函數,然後我想爲它添加水印 – 2012-04-04 10:12:32