How to Serve Watermarked Images with PHP

  1. Home
  2. Knowledge Base
  3. WordPress Plugins
  4. How to Serve Watermarked Images with PHP

So you want to watermark your images, how do you do this? In this technique well show you how we serve our images with a watermark using PHP. Well be building on the Serve Resized Images with PHP tutorial we published previously, which explains basic re-size manipulation of your images.

Stepping through the code, we first get our previously created watermark using the imagecreatefrompng() PHP function, setting it to the variable $watermark for simplicity.

Next we determine the watermark images width and height.

Since we want our watermark to appear in the bottom right of the original image, well subtract the watermark images width and height from the original images width and height to determine its placement. We also subtract 5 pixels for a padded placement.

Lastly well actually apply the watermark image using the imagecopymerge() PHP function.

utting the watermark code together we get the following:


Adding the watermark code to our previous Serve Resized Images with PHP technique code well have the following:

Now we simply call the image in a generic HTML image tag. Be sure to use the alt portion of the image tag so that if the image generation fails text still appears, not to mention this will aid in SEO. Below we are calling an image that has the default 588pixel width/height, keeping the aspect ratio, using an image named uwcmc.jpg for our United Way of Cape May County client.


Was this article helpful?

Related Articles

Leave a Reply