PHP watermarking

后端 未结 3 1581
抹茶落季
抹茶落季 2020-12-09 07:14

i am using this code to create watermark.

    $image = \'1.jpg\';
    $overlay = \'stamp.png\';
    $opacity = \"20\";
    if (!file_exists($image)) {
               


        
3条回答
  •  甜味超标
    2020-12-09 07:56

    The jpg format doesn't support transparency, so conceptually you will have to:

    • grab the pixels from the larger image (the jpeg) and put them into a buffer
    • grab the non-transparent pixels from the smaller image (the watermark) and move them into that buffer, applying the alpha along the way

    You probably want to let a library do this. I like ImageMagick, especially since it's built in to php... here's an example of how to use it for this purpose from PHP:

    // Let's read the images. 
    $glasses = new Imagick(); 
    if (FALSE === $glasses->readImage($dir . '/glasses.png')) 
    { 
        throw new Exception(); 
    } 
    
    $face = new Imagick(); 
    if (FALSE === $face->readImage($dir . '/face.jpg')) 
    { 
        throw new Exception(); 
    } 
    
    // Let's put the glasses on (10 pixels from left, 20 pixels from top of face). 
    $face->compositeImage($glasses, Imagick::COMPOSITE_DEFAULT, 10, 20); 
    

    And here's the link to the PHP manual page for ImageMagick::compositeImage (from which the above example came).

提交回复
热议问题