How to Write Text on a Image Using PHP

Sometimes, we need to add watermarks on the images. This is the best way to show your copyright on the images. If the photos are uploaded by users, it is not possible to add text on the images manually. But it is necessary to protect images by adding a watermark. In PHP, it is easy to add a water mark on the images. You can either add your logo or copyright text as watermark on the image. In this tutorial, I will show you how you can write some text on an image at a specific position. This can be done with the php function imageString. This is simple if you know how to use the function.

See the code below and try it. In this code, I am adding my name DeepankerĀ on a image called welcome.jpeg.

$string = "Deepanker";
$im = ImageCreateFromJpeg("welcome.jpeg");
$color = ImageColorAllocate($im, 255, 0, 0);
$px = (Imagesx($im) - 6.5 * strlen($string))/2;
$px = Imagesy($im)- 20;
ImageString($im, 200, $px, $px, $string, $color);

Try this code and let me know if there is any problem in the code.

You can improve this code to adopt the height and width of the images. For this first grab the image dimensions and then adjust your watermark according to it. See this tutorial for advance watermarking technique.

Nord VPN Sale

Deepanker Verma is the founder of Techlomedia. He is a tech blogger, developer and gadget freak.

Similar Articles

2020 UseThisTip | Developed By Deepanker