Add custom text to image in PHP with Example
In this post we will give you information about Add custom text to image in PHP with Example. Hear we will give you detail about Add custom text to image in PHP with ExampleAnd how to use it also give you demo for it if it is necessary.
By adding text over the image, you declare copyrights or define title of the image.
Here in this example, we will create a new image from given file or url using imagecreatefromjpeg
method.
$im = imagecreatefromjpeg($file_name);
Once image has been created then you can allocate a color in following way :
$backgroundColor = imagecolorallocate ($im, 255, 255, 255);
Now using imagestring
method we will draw a string horizontally.
imagestring ($im, $font, $x, $y, $string, $textColor);
imagejpeg
method is used to create jpeg file from the given image.
$a=imagejpeg($im);
Complete code for adding custom text to image
- <?php
- if(isset($_POST['submit'])){
- $file_name=$_FILES['image']['name'];
- $file_size=$_FILES['image']['size'];
- $file_tmp=$_FILES['image']['tmp_name'];
- $file_type=$_FILES['image']['type'];
- move_uploaded_file($file_tmp,$file_name);
- header("Content-type: image/jpeg");
- $string=$_POST['caption'];
- $font=12;
- $width=imagefontwidth($font)*strlen($string);
- $height=imagefontheight($font);
- $im=imagecreatefromjpeg($file_name);
- $x=imagesx($im)-$width;
- $y=imagesy($im)-$height;
- $backgroundColor=imagecolorallocate($im,255,255,255);
- $textColor=imagecolorallocate($im,,,);
- imagestring($im,$font,$x,$y,$string,$textColor);
- $a=imagejpeg($im);
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>PHP : Add Custom Text to Image</title>
- </head>
- <body>
- <form enctype="multipart/form-data" method="POST" action="">
- <input type="file" name="image">
- <input type="text" name="caption">
- <input type="submit" name="submit">
- </form>
- </body>
- </html>
<?php if(isset($_POST['submit'])){ $file_name = $_FILES['image']['name']; $file_size = $_FILES['image']['size']; $file_tmp = $_FILES['image']['tmp_name']; $file_type = $_FILES['image']['type']; move_uploaded_file($file_tmp,$file_name); header ("Content-type: image/jpeg"); $string = $_POST['caption']; $font = 12; $width = imagefontwidth($font) * strlen($string) ; $height = imagefontheight($font) ; $im = imagecreatefromjpeg($file_name); $x = imagesx($im) - $width ; $y = imagesy($im) - $height; $backgroundColor = imagecolorallocate ($im, 255, 255, 255); $textColor = imagecolorallocate ($im, 0, 0,0); imagestring ($im, $font, $x, $y, $string, $textColor); $a=imagejpeg($im); } ?> <!DOCTYPE html> <html> <head> <title>PHP : Add Custom Text to Image</title> </head> <body> <form enctype="multipart/form-data" method="POST" action=""> <input type="file" name="image"> <input type="text" name="caption"> <input type="submit" name="submit"> </form> </body> </html>
Label :
Web Development
Hope this code and post will helped you for implement Add custom text to image in PHP with Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs