Create image with dynamic text using php

In this post we use two functions such as watermark_text() and watermark_image() to generate text and images watermarks on images. Integrate this to your web project upload image system dynamic text and produce flyer image.

$font_path = "GeosansLight.ttf";
$font_bold = "Typo Grotesk Demo.otf";
$font_size = 20;       // in pixcels

$fly_name="Hi, I am ".$_SESSION['name'];
  $looking="Looking for";
  $accom= $_POST['accomodation']. " | " .$_POST['furnished'];
  $location_f = $_POST['location'].", ".$cityname;
  $rent_f = "Rent Rs ".$_POST['rent'];
  $tenant_type_f=$_POST['tenant_type'];

function watermark_image($oldimage_name, $new_image_name){
global $image_path;
list($owidth,$oheight) = getimagesize($oldimage_name);
$width = 200;  
$height = 300; 
$im = imagecreatetruecolor($width, $height);
$img_src = imagecreatefromjpeg($oldimage_name);
imagecopyresampled($im, $img_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
$watermark = imagecreatefrompng($image_path);
list($w_width, $w_height) = getimagesize($image_path);        
pos_x = $width - $w_width; 
$pos_y = $height - $w_height;
imagecopy($im, $watermark, $pos_x, $pos_y, 0, 0, $w_width, $w_height);
imagejpeg($im, $new_image_name, 100);
imagedestroy($im);
return true;
}




function watermark_text($oldimage_name, $new_image_name){
global $font_path,$font_bold, $font_size, $location_f,$fly_name,$looking,
$tenant_type_f,$accom,$rent_f;
list($owidth,$oheight) = getimagesize($oldimage_name);
$width = 2300;  
$height = 300;    		
$image = imagecreatetruecolor($width, $height);
$image_src = imagecreatefromjpeg($oldimage_name);
imagecopyresampled($image, $image_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
$yellow = imagecolorallocate($image, 255, 240, 125);
imagettftext($image, 18, 0, 10, 40, $white, $font_bold, $location_f);
imagettftext($image, 18, 0, 30, 90, $black, $font_path, $fly_name);
imagettftext($image, 22, 0, 50, 130,$black, $font_path, $looking);
imagettftext($image, 28, 0, 50, 175, $white, $font_bold, $tenant_type_f);
imagettftext($image, 14, 0, 50, 210, $black, $font_path, $accom);
imagettftext($image, 22, 0, 50, 250, $yellow, $font_bold, $rent_f);
imagejpeg($image, $new_image_name, 100);
imagedestroy($image);
return true;
}
$demo_image= "";

imagettftext — GD library image function used to Write text to the image using TrueType fonts.

imagecolorallocate — Allocate a color for an image.It takes four arguments, Image and RGB color format.

Image formation

$oldimg = "images/flyer_rent.jpg";
$new_name = "uploads/images/flyer_rent_".$_REQUEST[busiid].".jpg";
if(watermark_text($oldimg, $new_name))
$demo_image = $new_name;
echo <img src="'.$demo_image.'" />;

Out Put
flyer_rent_16

Leave a Reply

Your email address will not be published. Required fields are marked *

five + 7 =