Text to Image Convert using php code . Download source Code
-
Jeetendra Singh - 10 Jan, 2016
Hi Geeks, In this tutorial we will learn that how to Create and save dynamic image in php using gd library or Text to Image Convert using php code . in this example we have a requirement to create a image with specific width and height and specific background and watermark also. So that we have created a PHP Class to generate and save the image to specific folder or path. please follow the steps to achive this task. Step 1. Create a class file for generate image with name class.img.php with following code:
_font1=$font_1; $this->_font2=$font_2; $this->_font_size=$font_size; $this->_font_size_watermark=$font_size_watermark; } public function imagettftext_cr(&$im, $size, $angle, $x, $y, $color, $fontfile, $text) { // retrieve boundingbox $bbox = imagettfbbox($size, $angle, $fontfile, $text); // calculate deviation $dx = ($bbox[2]-$bbox[0])/2.0 - ($bbox[2]-$bbox[4])/2.0; // deviation left-right $dy = ($bbox[3]-$bbox[1])/2.0 + ($bbox[7]-$bbox[1])/2.0; // deviation top-bottom // new pivotpoint $px = $x-$dx; $py = $y-$dy; return imagettftext($im, $size, $angle, $px, $y, $color, $fontfile, $text); } public function generate_img($name,$savepath,$size_arr,$watermark,$rgb=array("34","96","76")) { $image = imagecreate($size_arr['width'],$size_arr['height']); $rgb_background = imagecolorallocate($image,$rgb[0],$rgb[1],$rgb[2]); $grey_shade = imagecolorallocate($image,40,40,40); $white = imagecolorallocate($image,255,255,255); // Local font files, relative to script $otherFont = $this->_font1; $font = $this->_font2; $i=0; while($i<10){ $this->imagettftext_cr($image,12,$this->_font_size_watermark,rand(100,500),rand(200,500),$grey_shade,$otherFont,$watermark); $i++; } // Main Text $w=$size_arr['width'] / 2 ; $h=$size_arr['height'] / 2 ; $this->imagettftext_cr($image,$this->_font_size,0,$w,$h,$white,$font,$name); $this->imagettftext_cr($image,$this->_font_size_watermark,0,$w,$h+30,$white,$otherFont,$watermark); imagejpeg($image,$savepath); } } ?>Step 2. make another php file with any name. and include the class file as follow:
Now create a instance of img class with font file names and sizes as following :
Init with font files and font sizes as follow:
Now generate a single image as follow:
"500","height"=>"500"); //define height and width $savepath="images/my_image_test.jpg"; $watermark="w3school test"; $rgb=array("0","0","0");//define a black rgb scheme $img_obj->generate_img($text_to_image,$savepath,$size_arr,$watermark,$rgb); //call the generate image ?>Generate multiple images in loop as following code :
"1366","height"=>"768"); //define height and width $savepath="images/".$img_name.".jpg"; $watermark="w3school test watermark"; $rgb=array("34","96","76");//define a light green rgb scheme $img_obj->generate_img($title,$savepath,$size_arr,$watermark,$rgb); //call the generate image function // } ?>Complete Code of above snippets:
"500","height"=>"500"); //define height and width $savepath="images/my_image_test.jpg"; $watermark="w3school test"; $rgb=array("0","0","0");//define a black rgb scheme $img_obj->generate_img($text_to_image,$savepath,$size_arr,$watermark,$rgb); //call the generate image //now generate a single image //generate multiple images in loop $text=array("MY IMAGE TEXT 1","MY IMAGE TEXT 2","MY IMAGE TEXT 3","MY IMAGE TEXT 4","MY IMAGE TEXT 5",); foreach($text as $title) { // $img_name=strtolower(str_replace(" ","-",$title)); $size_arr=array("width"=>"1366","height"=>"768"); //define height and width $savepath="images/".$img_name.".jpg"; $watermark="w3school test watermark"; $rgb=array("34","96","76");//define a light green rgb scheme $img_obj->generate_img($title,$savepath,$size_arr,$watermark,$rgb); //call the generate image function // } //generate multiple images in loop ?>[viraldownloader id=113 text=’Download the Complete Code by Sharing this post and click on generate button after sharing’]
Note: please confirm GD library on your php server. and create a images folder in project with 755 or 775 or 777 permissions.