How to Add text to an image in php

Hi All,

Today in this article we are going to learn the concept to edit and image dynamically using php script.

Here are the simple steps to write strings on an image.

Add String to an Image:

You need 3 files.   Click here Download The Whole Code 

  1. test.php
  2. main.php
  3. base image

base

This is Base Image

test.php

<?php
 
//select a base image
$rImg = ImageCreateFromJPEG("base.jpg");
 
//define color
$cor = imagecolorallocate($rImg, 255, 255, 255);
 
//define image, font style, location of string, String, color.
imagestring($rImg,5,556,252,urldecode($_GET['name']),$cor);
 
//Header output
header('Content-type: image/jpeg');
imagejpeg($rImg,NULL,100);
 
?>

main.php

<html>
<head></head>
<body>
<img src="test.php?name=I am not jack sparrow" alt="" />
</body>
</html>

Output of main.php is:-

fig1

 

 


Code For Add Custom Font On Image:

If you want to use custom fonts and with large font size

Use the following code.

test.php

<?php
 
//select a base image
$rImg = ImageCreateFromJPEG("base.jpg");
 
//define color
$cor = imagecolorallocate($rImg, 255, 255, 255);
 
//select font family.
$font = 'BickhamScriptProSemibold.ttf';
//define image, font size, x-y co-ordinate (can change, but o recommended), x co-ordinates, y co-ordinates, color, font style, string.
imagettftext($rImg, 39.5, 0, 500, 230, $cor, $font, urldecode($_GET['name']));

 
//Header output
header('Content-type: image/jpeg');
imagejpeg($rImg,NULL,100);
 
?>

main.php

<html>
<head></head>
<body>
<img src="test2.php?name=I am not jack sparrow" alt="" />

</body>
</html>

fig4


Edit An Image with Custom Font Size and Style to Download:

If you want to download the edited image use following code.

Here we need on extra file “download.php”

File will also save on server so we have to define different name every time to remove redundancy problem.

So we have to define image name dynamically too.

Let’s read out following code to achieve our requirement.

In the following code you will have a text box fill the string in text box and submit. A download link will be on appears to download image.

 

test.php

<?php
 
$rImg = ImageCreateFromJPEG("base.jpg");
 
$cor = imagecolorallocate($rImg, 255, 0, 0);
 

$font = 'BickhamScriptProSemibold.ttf';
imagettftext($rImg, 39.5, 0, 330, 380, $cor, $font, urldecode($_GET['name']));

header('Content-type: image/jpeg');
imagejpeg($rImg, "birthdayimage".urldecode($_GET['name'])."".urldecode($_GET['x']).".jpg");

header('location:main.php?name='.$_GET['name'].'&x='.$_GET['x'].'');   
?>

main.php

<html>
    <body>
        <form method="POST"> 
            <input type="text" name="username"/><input type="submit" name="subpic" />
		</form>
<?php
if (isset($_POST['subpic'])) {
    $x = rand(0000, 9999);
    header('location:test.php?name='.$_POST['username'].'&x='.$x.'');
}
if (isset($_GET['name'])&&isset($_GET['x'])) {
       $file = "birthdayimage".$_GET['name']."".$_GET['x'].".jpg";
    echo "<a href='download.php?name=".$file."'>download</a> "; 
}
?>
</body>
</html>

download.php

<?php
$name= $_GET['name'];

    header('Content-Description: File Transfer');
    header('Content-Type: application/force-download');
    header("Content-Disposition: attachment; filename="" . basename($name) . "";");
    header('Content-Transfer-Encoding: image/jpeg');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($name));
    ob_clean();
    flush();
    readfile($name); //showing the path to the server where the file is to be download
    exit;
?>



Click here Download The Whole Code 

fig4

 

fig3

fig2

2 Comments

  • canadian pharmacy cialis

    March 25, 2018 at 11:30 am Reply

    I always spent my half an hour to read this website’s posts everyday along with a mug of
    coffee.

  • Best Bibek

    May 30, 2018 at 4:12 am Reply

    I was really looking for this script to download. I tried creating myself but failed. Thanks for giving download option admin.

Post a Comment