Adding custom text over image or pictures is a good way to declare copyrights or give a simple title information to that graphical image or picture without editing the image itself. PHP made it very easy to add custom text over image or picture by using some basic functions from PHP GD.
complete code for adding text over image
<?php /* image.php */ header("Content-type: image/jpeg"); $imgPath = 'image.jpg'; $image = imagecreatefromjpeg($imgPath); $color = imagecolorallocate($image, 255, 255, 255); $string = "http://recentsolutions.net"; $fontSize = 3; $x = 115; $y = 185; imagestring($image, $fontSize, $x, $y, $string, $color); imagejpeg($image); ?>
Customizing the text for your usage:
- Change the $imgPath to image path you want to add text over it.
- In “$color = imagecolorallocate($image, 255 , 255, 255)” change the color code to color that suites with the image. for example, if the image background is white then change (255, 255, 255) to (0, 0, 0) which is code for black color.
- $string is the text will be added over the image.
- $fontSize is the font size number.
- $x is the text margin from left.
- $y is the text margin from top
- imagecreatefromjpeg (Create a new image from file or URL and returns an image identifier representing the image obtained from the given filename).
- imagecolorallocate (Allocate a color for an image and returns a color identifier representing the color composed of the given RGB components).
- imagestring (Draw a string horizontally)
- imagejpeg (Output image to browser or file)
The image used in this article is taken from http://vectorialpx.net
As always, any comments or suggestion would be really appreciated.