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.
<?php $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); imageJpeg($im,"welcome.jpeg"); ImageDestroy($im); ?>
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.
Leave a comment
Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.