PHP text to image - php

I got good result in PHP/GD to write text to image template, text wraps fine but is not "smooth", here is the code I am using:
<?php
header("Content-type: image/png");
$text = "go to school go to school go to school go to school go to school go to school go to school go to school go to school go to school go to school ";
$arrText=explode("\n",wordwrap($text,60,"\n"));
$im = imagecreatefrompng("template.png");
$y = 15; //vertical position of text
foreach($arrText as $arr)
{
$white = imagecolorallocate($im,0,0,0); //sets text color
imagestring($im,5,15,$y,trim($arr),$white); //create the text string for image,added trim() to remove unwanted chars
$y = $y+20;
}
imagepng($im);
imagedestroy($im);
?>

Please try this:-
Remember : set correct path for font $font = 'arial.ttf';
header('Content-type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'go to school go to school go to school go to school go to school go to school go to school go to school go to school go to school go to school';
// Replace path by your own font path
$font = 'arial.ttf';
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);

Try turning on anti-aliasing:
imageantialias($im, true);
Does that make it look any better?
Or else try imagettftext with a truetype font:
imagettftext ( $im, 15, 0, 15, $y, $white, 'fontfile.ttf', trim($arr));

Related

Create an image from data details in site

I want to ask on how to create an image based on updated data on my site?
For example like this image:
[![x][1]][1]
The details of the image is get from this url: [Pine][2].
That's the full code:
It's show an image with updated data based on that link.
Can someone show me what I have to find in google for the details like this? what type is this code?
You should use GD library or Imagick(ImageMagick)
Example of image creation with GD
To install or enable already installed GD extension (https://stackoverflow.com/a/44720393/8579824)
<?php
// Set the content-type
header('Content-type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'arial.ttf';
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>

Hindi text to Image - words not appearing properly PHP

I want to display hindi words on an image. Here is the code for it, it is working properly but it does not display "chote E ki matra" correctly. Example "अब हिन्दी" is displayed as : http://prntscr.com/8rj8mq
Also hindi.txt file contains these :
अब हिन्दी
मे टाइप
करना बहुत आसान है
Below php script read one line randomly and put it on image and display it. Can someone help me to correct this....
<?php
$word= file("hindi.txt");
$random = rand(0,2);
$text = $word[$random];
header('Content-Type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
// Replace path by your own font path
$font = 'Mangal.ttf';
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>

Weird image glitch when using imagepng()

I am trying to make a gamecard for a RSPS. This is my first time using PHP to generate text on images and when trying to apply this weird glitch happens:
But, when the original photo looks like this:
As you can see the corners are not curved like they should be. But, in the original it comes out fine. I am not talking about the location of the numbers.
Here is the code I am using:
<?php
header('Content-Type: image/png');
// Image Creation
$image_file = "SoulSplitCard.png";
$im = imagecreatefrompng($image_file);
//Colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
$rscol = imagecolorallocate($im, 23, 113, 183);
// Levels (for now)
$text = '99';
$text2 = '87';
// Font
$font = 'arial.ttf';
//Text Applications
imagettftext($im, 15, 0, 150, 35, $rscol, $font, $text);
imagettftext($im, 15, 0, 150, 81, $rscol, $font, $text2);
// Using imagepng() instead of imagejpeg()
imagepng($im);
imagedestroy($im);
?>
What is causing this to happen?

Create White Box around Text GD-Lib

i want to add a white box around some text i add to an image via GD-Lib.
but i don't know how to do this best.
Here is my current code:
<?php
$textImg = imagecreatefromjpeg($tempImage);
$black = imagecolorallocate($textImg, 0, 0, 0);
$font = 'lib/verdana.ttf';
// Add the text
imagettftext($textImg, 20, 0, imagesx($textImg)*$textData['x']/100, imagesy($textImg)*$textData['y']/100, $black, $font, $textData['text']);
imagejpeg($textImg,$tempImage,$jpegQuality);
?>
I hope you can help me out.
You can use imagettfbbox() to get the coordinates of the bounding box by passing the same settings you use for the text itself (same text, font and size etc).
Once you have these coordinates you can use imagerectangle() to draw a border around the text, or you can use imagefilledrectangle() to draw a solid rectangle. Be sure to call it before you render the text with imagettftext()
A basic example is below but will need some tweaking as most of it is from memory and I suspect the $x and $y calculation could be done better as it probably doesn't work with varying canvas sizes as it is now. However, it demonstrates the principle.
// Set the content-type
header('Content-Type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $black);
// The text to draw
$text = 'Testing';
// Replace path by your own font path
$font = 'verdana.ttf';
// Add the text
$bbox = imagettfbbox(20, 0, $font, $text);
$x = $bbox[1] + (imagesx($im) / 2) - ($bbox[4]);
$y = $bbox[3] + (imagesy($im) / 2) - ($bbox[5]);
imagerectangle($im, 0, 0, $x, $y, $white);
imagettftext($im, 20, 0, 0, 20, $white, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagejpeg($im);
imagedestroy($im);

Outputting image with underlined text using php GD library

What is the best way to display underlined text and output the result as image with GD or any other library?
You can try using the Unicode underline combining character U+0332.
<?php
// Set the content-type
header('Content-type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = "̲U̲d̲e̲r̲l̲i̲n̲e";
// Replace path by your own font path
$font = 'arial.ttf';
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
There are lots of FREE PHP CAPTCHA out there that come with a lot of customization, download one and see what exactly happens behind the scene. Also have a look at this link
HTH
I am using this...
$font = imageloadfont($font_file);
$font_width = ImageFontWidth($font);
$font_height = ImageFontHeight($font);
$str_width = strlen($text)*$font_width;
ImageLine($image, $left, $top+$font_height, $left+$str_width, $top+$font_height, $color);

Categories