2011-05-26 36 views
3

我有一個圖像,文本是通過php的形式動態生成的。我有一個徽標圖像的位置保存到一個MySQL數據庫的變量。有沒有辦法拍攝這張照片並將其應用到圖像中的固定位置?如果需要,它將不得不縮小以適合此圖像區域。在PHP的另一幅圖像中放置圖像

我已經有一個腳本,看起來像這樣:

 $img = imagecreatefromjpeg('coupon/coupontemplate.jpg'); 
     $textColor = imagecolorallocate($img, 0, 0, 0); // black text 

     //Write first the coupon title 
     imagefttext($img, 16, 0, 20, 34, $textColor, 'coupon/arialbd.ttf', $title); 

     //Then write the coupon description 
     imagettftextbox($img, 13, 0, 20, 45, $textColor, 'coupon/arial.ttf', $description, 440); 
     //If the checkbox to include logo is checked... 
     if ($_POST['clogo'] == 'y') { 

      $logo = $row['imagecompany']; 
      $logo_file = "../directory/memberimages/$logo"; 
      $logo_file_type = getimagesize($logo_file); 

      if ($logo_file_type['mime'] == 'image/jpg') { 
       $logoImage = imagecreatefromjpeg($logo_file); 
      } else if ($logo_file_type['mime'] == 'image/gif') { 
       $logoImage = imagecreatefromgif($logo_file); 
      } else if ($logo_file_type['mime'] == 'image/png') { 
       $logoImage = imagecreatefrompng($logo_file); 
      } 

      } 

     // Output image to the browser 
     //header('Content-Type: image/jpeg'); 
     //imagejpeg($img); 

     // Or save to file 
     imagejpeg($img, 'my-text.jpg'); 
     imagedestroy($img); 

    } 
//} 

任何人都有想法如何做到這一點 - 從指定位置獲取圖像,並把它的其他形象?謝謝!

+0

意味着你想把徽標放在每個生成的圖像?圖像上的圖像。對?? – diEcho 2011-05-26 08:26:25

+0

@diEcho - yep! :) – Sara 2011-05-26 08:48:21

+1

順便說一句,這樣做的圖像的一般名稱是「水印」 - 這可能會幫助你[找到更多的資源](http://www.google.co.uk/search?sourceid=chrome&ie=UTF- 8 q = PHP +圖像+水印)。 – 2011-05-26 08:53:43

回答

2

合併圖像可以做這樣的事情:reference

<?php 
# If you don't know the type of image you are using as your originals. 
$image = imagecreatefromstring(file_get_contents($your_original_image); 
$frame = imagecreatefromstring(file_get_contents($your_frame_image)); 

# If you know your originals are of type PNG. 
$image = imagecreatefrompng($your_original_image); 
$frame = imagecreatefrompng($your_frame_image); 

imagecopymerge($image, $frame, 0, 0, 0, 0, 50, 50, 100); 

# Save the image to a file 
imagepng($image, '/path/to/save/image.png'); 

# Output straight to the browser. 
imagepng($image); 
+0

你知道幀是否可以是我已經生成的已經生成的圖像嗎? $ img變量? – Sara 2011-05-26 08:49:23

+0

是的,如果它已經在你的文件系統中,你可以:如果你使用imagecreatefromstring函數傳遞這個file_get_contents($ src)... $ src可以是你已經生成的圖像的路徑。否則,如果您已經知道已經生成的文件的圖像類型,則可以使用imagecreatefrompng/jpeg/gif函數,並再次將路徑/網址傳遞到已生成的圖像 – 2011-05-26 08:55:43

相關問題