Click here to Skip to main content
Click here to Skip to main content

Write Text Over Image or Picture with PHP

By , 13 Feb 2013
Rate this:
Please Sign up or sign in to vote.

Text Over Image with PHP

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 has 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

Resources:

  • 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.

The post Write Text Over Image or Picture with PHP appeared first on Recent Solutions.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Noor Ahmad Feroozi
Software Developer (Senior) Ministry of Education
Afghanistan Afghanistan
Over eight years of IT industry experience, and have been a part of design and development projects for many exceptional companies during those years, including Kabul Cyber Park, Alpha Omega Services, Ministry of Defense, Spacetech ICT Solutions, Ministry of Education and many others.
 
Technologies I use are standards-based XHTML, CSS, JavaScript, jQuery, PHP, ASP.Net, C#.Net, MS SQL, MySQL and SQLite. Each project I develop is built to be search engine and user friendly, using the latest development principles and techniques. To find out more about what I can do for you, check out my work at http://recentsolutions.net, or get follow me on http://twitter/naferoozi
Follow on   Twitter   Google+

Comments and Discussions

 
Generalmy vote of 5 PinmemberMax_Power_Up19-Feb-13 8:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140421.2 | Last Updated 13 Feb 2013
Article Copyright 2013 by Noor Ahmad Feroozi
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid