Symfony2 display text in image format (png,jpg,..) - php

is there a way (bundle or something other) to display(convert) a text in image format.
from a text .txt input => output : display it on .png

You could use php native functions imagestring, imagettftext or even imagik (requiring you have the GD php extension activated), example with imagettftext (from the php help page) :
<?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);
?>

First create a route for a particular Controller then add following code inside that controller
/**
* Action returns an image which is generated from
* given request text input
* #param type $text
* #return \Symfony\Component\HttpFoundation\Response
*/
public function textToImageAction($text) {
$filename = $this->saveTextAsImage($text);
$response = new Response();
// Set headers
$response->headers->set('Cache-Control', 'private');
$response->headers->set('Content-type', mime_content_type($filename));
$response->headers->set('Content-Disposition', 'attachment; filename="' . basename($filename) . '";');
$response->headers->set('Content-length', filesize($filename));
// Send headers before outputting anything
$response->sendHeaders();
$response->setContent(readfile($filename));
return $response;
}
/**
* Method convert given text to PNG image and returs
* file name
* #param type $text Text
* #return string File Name
*/
public function saveTextAsImage($text) {
// Create the image
$imageCreator = imagecreatetruecolor(100, 30);
// Create some colors
$white = imagecolorallocate($imageCreator, 255, 255, 255);
$grey = imagecolorallocate($imageCreator, 128, 128, 128);
$black = imagecolorallocate($imageCreator, 0, 0, 0);
imagefilledrectangle($imageCreator, 0, 0, 399, 29, $white);
$font = 'arial.ttf';
// Add some shadow to the text
imagettftext($imageCreator, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($imageCreator, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
$file_name = "upload/text_image.png";
imagepng($imageCreator, $file_name);
imagedestroy($imageCreator);
return $file_name;
}

Related

php GD library sometimes generate blank image

I trying to generate a signature from a name using the GD library, and return the png image in base64 format.
With the following code, the function correctly returns the base64 image with the signature:
<?php
function genSignature($name){
// The text to draw
$tamano = 15 * strlen($name);
$fuentes = [
'../fuentesFirma/HomemadeApple-Regular.ttf',
'../fuentesFirma/Allura-Regular.ttf',
'../fuentesFirma/DawningofaNewDay-Regular.ttf',
'../fuentesFirma/HerrVonMuellerhoff-Regular.ttf',
'../fuentesFirma/LaBelleAurore-Regular.ttf',
];
$fuente = $fuentes[array_rand($fuentes, 1)];
// Create the image
$im = imagecreatetruecolor($tamano, 60);
// 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, $tamano, 60, $white);
// Add the text
imagettftext($im, 20, 0, 0, 30, $black, $fuente, $name);
ob_start();
imagepng($im);
$buffer = ob_get_clean();
ob_end_clean();
return base64_encode($buffer);
}
echo genSignature('María'); //Commented line when calling from another script, for testing only.
But if I include the file with the function, and call it from another script, it always returns the same image, a white square (base64). I don't understant where is the problem..
Other script:
<?php
include_once('generarFirma.php'); //in same folder
//....
//some operations...
$signature = genSignature('María');

PHP SLIM : How to return dynamically generated images

I am trying to display a dynamically generated image from SLIM controller but I am getting a blank screen.
Thanks.
public function textToImage($request, $response, $args) {
// The text to draw
$text = "Hello World";
$length=strlen($text);
// Create the image
$im = imagecreatetruecolor($length*12, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 44, 62, 80);
$boxcolor=imagecolorallocate($im, 95, 128, 149);
$font = 'Helvetica.ttf';
imagefilledrectangle($im, 0, 0, $length*9+$capitaladjust, 29, $white);
imagettftext($im, 13, 0, 0, 20, $black, $font, $text);
$response->getBody()->write(base64_encode($im));
$response = $response->withHeader('Content-type', 'image/png');
return $response;
}
base64 encoding $im will not create a valid PNG file. Try using imgpng and then sending the raw contents of the PNG file that it creates.
The code would look something like this:
ob_start();
imagepng($im);
$data = ob_get_contents();
ob_end_clean();
$response->getBody()->write($data);
$response = $response->withHeader('Content-type', 'image/png');
return $response;

Make imagettftext accept the whole pwgen -ny input, not just the first password

So I have this:
https://indigodaddy.koding.com/
(Basically straight out of http://us2.php.net/manual/en/function.imagettftext.php)
<?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 = `../../../bin/pwgen -ny 12`;
// Replace path by your own font path
$font = 'font/DejaVuSans-Bold.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);
?>
How would I make the txt to image conversion accept the entire input of pwgen -ny -12? This outputs 6 columns of 12-char passwords, eg, like this: http://pwgen.net/. I want that entire output converted to the png, not just the first password, as it is doing now.
Any ideas?
** Edit, as requested, sample output of pwgen -ny 12:
Uo/thengohy5 phie=Tahw8ko Cohh1seiz]ua ooY#ee4chei) noo}Mioyei5u au$x5Isaigho
aht6vahGia{j aeph8yoh#kuY EeNgees#ae4k boh5yie1Ic\e aiSh5bahp]ai kiex%ahL0Oob
bei|G{ohm8ae Ieth|ee|wee9 koo#R2iZeot" Nai5Phain(ie Oam*ub0thoth quu6Eyimohd-
geeNgool}ee1 Oow5Pie_sija ab#ahcoo8eeJ ahcoe_r6Vae3 pi%wiedeit4A biegh}u#vu6K
euwei.gei9Of ahr?ai2UquaZ ha3eiXee"koo eiLohf*u1sao ave&aqui8Hie oc-a#z'oh1Le
iv$eeX0eo0Wu rel7aGh6gee& baig3ooN%uz, eoj9eGo.haih Aoka4Uizae:r aiG6aiP.iera
IaY?oo0Aibai pekaeth5Aeh% taav*eiK5Soo zohL5haeng?o teT^u3yash3S xudoh%Dai4ph
tho`R\ahd7ae ohf#e9Ceeph7 Uce7ou{woe8o eeb9jo!Y#aep la7Voo.chae7 te;y5Choo4ie
neeZ5out'oo) ze{iw4Aethok fah3Shoh^Re9 Quoo8au%koot xo2chu>Lae0e Shee#g1eibak
que5Og3aiN&e aw(eetooJ5Ei aeV8ging(eib ohGh9Chah[ch ieYiv5gagh-a kahm2yohR%uo
Eiraivi!o}W0 eo0Oot{ai5sh saT[ae2ahj5O Aefae,l5ieth jo0iJ,ai5eih deer9Aigol'i
aet1Ce,za+fi OFi3exoh)ng3 tae7ooGhaim/ nei$V8Ohh!ae eiy~ia4Itoh; Baisish+ai5d
iib+ooHaoj4t Ayac3aiCh>ee ka/eChoosh2c vanu1Zeex#uy phieB(ai8iev Ou|Gh3zuwo,f
ool3AxiNo~ch zai3Eew&ie*w Zee7eey4eJ*o kooree9Va>B5 FaiB-ah8yahK oha4uP3Oth<o
Aegah2aesh.u eiQuuduu>v1M ae5re8yaXuw% gei5rua1Lu/i eeNg3uyaf"ei Ootee|h8Shai
sa6ed0nieF+e aed8rah;Kai4 boghahH]a7ei eiba$Roh5uu' Ook6oonae`th Vai9taer#oo?
aich7ohMoi/d Ohh1ew-aa"xi Eig%ae]jo2ne urooX1iagh|u iiY8reew,i8d eGhee4fahw(a
ieS%ai1ieh4e pun9Ich0jee; Waer_ai1ieph re)Che`xud0A Aek2wioh\a2u deedae0Ai[be
ohr!a4Ohtae* keNai8aes%oh ookiM3uiRei~ fei-Boo2tu3i coo=c4Yie?w8 ThaiN7fe$o"t
ahw[uChae3va Ej5Evaa&Giix seat|eeZ1yae ee5wiengo]Ba Oo2aThu^p!ai ez{i4too8ohD

Move uploaded file php not working

I want to move a file that has been created using imagettftext and saved as a png. as you can see, in the code below, i used the move_uploaded_file but to no avail. please help.
tq
// Set the content-type
header('Content-Type: image/png');
// Create the image from created image
//$im = imagecreatetruecolor(180, 180);
$im = #imagecreatefromjpeg('poloroid.jpg');
// 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 = 'John...';
$fbid = $_POST["id"];
$text = $_POST["want"];
$fb_email =$_POST["email"];
$fb_name=$_POST["name"];
$uploads_dir = '/uploaded_files';
// Replace path by your own font path
$font = 'verdana.ttf';
//image file name
$name ="$fbid.png";
// Add some shadow to the text
imagettftext($im, 20, 0, 25, 126, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 25, 125, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
//imagepng($im);
imagepng($im,$name,9);
move_uploaded_file($name,"$uploads_dir/$name");
imagedestroy($im);
You are not uploading a file, you are generating one!
imagepng has filename parameter so you can save it to your drive:
$uploads_dir = '/uploaded_files/';
$name = $uploads_dir.$fbid.'.png';
imagepng($im,$name,9);
imagedestroy($im);
try to use rename instead of move_uploaded_file

How to add a background image while creating a text image using php?

I'm currently using the following script--
<?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, 115, 150, 195);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'My Name';
// Replace path by your own font path
$font = 'AGENCYB.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);
?>
But I want to add a background image too. Please help, I'm new to this function especially.
Would something like the following work for you? You want to open the image you want to use as the background, and then write your text over the top.
<?php
// Set the content-type
header('Content-type: image/png');
/* Attempt to open */
$im = #imagecreatefrompng('backgroundimage.png');
/* See if it failed */
if(!$im)
{
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 115, 150, 195);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'My Name';
// Replace path by your own font path
$font = 'AGENCYB.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);
}else
{
//you want to do something here if your image didn't open like maybe fpassthru an alternative image
}
?>

Categories