3

我需要在图像上添加水印。我已经使用此代码解决了问题,效果很好,但图像位于左/下角。如何在图像中心设置居中水印?

$img = 'test.jpg';
// Load the image where the logo will be embeded into
$image = imagecreatefromjpeg($img);


// Load the logo image
$logoImage = imagecreatefrompng("watermark.png");
imagealphablending($logoImage, true);

// Get dimensions
$imageWidth=imagesx($image);
$imageHeight=imagesy($image);

$logoWidth=imagesx($logoImage);
$logoHeight=imagesy($logoImage);     

// Paste the logo
imagecopy(
   // destination
   $image, 
   // source
   $logoImage, 
   // destination x and y 
   $imageWidth-$logoWidth, $imageHeight-$logoHeight,    
   // source x and y
   0, 0,
   // width and height of the area of the source to copy
   $logoWidth, $logoHeight);

// Set type of image and send the output
header("Content-type: image/png");
imagePng($image);

// Release memory
imageDestroy($image);
imageDestroy($imageLogo); 
4

2 回答 2

9

代替

// destination x and y 
   $imageWidth-$logoWidth, $imageHeight-$logoHeight,

// destination x and y 
   ($imageWidth-$logoWidth)/2, ($imageHeight-$logoHeight)/2

,

于 2013-08-18T12:22:36.093 回答
0

http://gloryplus.com/index.php?route=product/product&path=81&product_id=285

     for($i=0; $i < $count; $i++)
  {
      ("'".$im[$i]."'");

        $imVar = imagecreatefromjpeg($im[$i]);
        // First we create our stamp image manually from GD
        $stamp = imagecreatefromgif('a.gif');  //imagecreatetruecolor(100, 54);

        // Set the margins for the stamp and get the height/width of the stamp image
        $marge_right = 10;
        $marge_bottom = 10;
        $sx = imagesx($stamp);
        $sy = imagesy($stamp);


        // Merge the stamp onto our photo with an opacity (transparency) of 50%
        imagecopymerge($imVar, $stamp, imagesx($imVar) - $sx - $marge_right, imagesy($imVar) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp), 50);

        // Save the image to file and free memory
        imagepng($imVar, "./image/image[$i].png");
    }
于 2015-05-07T06:08:30.120 回答