2012-03-30 62 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