I was recently hired by a school that provides distance (online) education to young children. They want to be able to generate and send certificates (jpg or png images) via email once a child has completed certain tests, assignments, etc. Obviously they won't get all of their teachers to re-create each certificate in Photoshop or something and change each childs name, etc; that would be way too time consuming and I doubt their teachers even know how to use Photoshop.
Therefore, I was wondering what the best way to add text to an image in PHP is. The certificate will be like a standard school certificate and the name line will be blank. I'd like the text to go there.
The reason I'm doing this in PHP is because that way the teachers can go to whatever.com/generate-certificate and then type in the child's name in a field and then the certificate will be generated and sent off to the student via email on the fly. Very easy for the teachers.
Hopefully this question made sense. So in a nutshell I just want a library or some other simple way in PHP to add a line of text to an image (probably a jpeg).
Thanks a lot!
If you don't like procedural type, you can use imagick
<?php
/* Create some objects */
$image = new Imagick();
$draw = new ImagickDraw();
$pixel = new ImagickPixel( 'gray' );
/* New image */
$image->newImage(800, 75, $pixel);
/* Black text */
$draw->setFillColor('black');
/* Font properties */
$draw->setFont('Bookman-DemiItalic');
$draw->setFontSize( 30 );
/* Create text */
$image->annotateImage($draw, 10, 45, 0, 'The quick brown fox jumps over the lazy dog');
/* Give image a format */
$image->setImageFormat('png');
/* Output the image with headers */
header('Content-type: image/png');
echo $image;
Source: http://php.net/manual/en/imagick.annotateimage.php
There is great default library called GD which should do the job.
Well my idea how to make what you need:
1. Create maybe several background images for your certifications.
2. Load the image with PHP GD library
3. Write text and save the file, or render it on the fly if there will be less traffic.
http://us1.php.net/manual/en/function.imagefttext.php
<?php
// Create a 300x100 image
$im = imagecreatetruecolor(300, 100);
$red = imagecolorallocate($im, 0xFF, 0x00, 0x00);
$black = imagecolorallocate($im, 0x00, 0x00, 0x00);
// Make the background red
imagefilledrectangle($im, 0, 0, 299, 99, $red);
// Path to our ttf font file
$font_file = './arial.ttf';
// Draw the text 'PHP Manual' using font size 13
imagefttext($im, 13, 0, 105, 55, $black, $font_file, 'PHP Manual');
// Output image to the browser
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>
Related
When I add text to an image with GD Image it seems like it fails to load the rest of the image. This varies with the text string. Not adding text at all, the image appears as expected. The area to the bottom left of the image is white/background, but not transparent.
This is what I'm using to add the text. It's part of a larger project where images are accessed by ID from a database, which contains the filename information then loaded. $Image['fullpath'] is valid and $ErrorMsg is set elsewhere in code (correctly).
$ImgString = file_get_contents($Image['fullpath']);
$image = imagecreatefromstring($ImgString);
$color = imagecolorallocate($image, 255, 255, 255);
$font = "../assets/Noteworthy-Lt.woff";
imagettftext($image, 14, 0, 5, 20, $color, $font, $ErrorMsg);
imagegif($image);
But this renders just fine, if I don't add text.
$ImgString = file_get_contents($Image['fullpath']);
$image = imagecreatefromstring($ImgString);
imagegif($image);
I have discovered my own answer.
It was not "seeing" all the image data because I specified the image size in the header, prior to making changes and writing the text... and therefore changing the size. Once it had that size it stopped looking at the data because it wasn't expecting any data after that.
i am working on simple php code that write text to image . I found this code it is perfect one problem it save image into 72 dpi. I find option where i can change its DPI but not found . If any body have idea
//Set the Content Type
header('Content-type: image/jpeg');
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('pic.jpg');
// Allocate A Color For The Text
$white = imagecolorallocate($jpg_image, 255, 255, 255);
// Set Path to Font File
$font_path = 'arialbd.ttf';
// Set Text to Be Printed On Image
$text = "This is a sunset!";
// Print Text On Image
imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text);
// Send Image to Browser
imagejpeg($jpg_image,'a.jpg');
// Clear Memory
imagedestroy($jpg_image);
Try imageresolution() to set and get the resolution of an image in DPI (dots per inch).
Note: The resolution is only used as meta information when images are read from and written to formats supporting this kind of information (curently PNG and JPEG).
I'm trying to create images from dynamic text data in GD and put a logo in the top corner. The background color of the image will change based on the data passed in so I can't just save the logo as an image with no alpha channel.
I create the image, fill it with the dynamic background color using imagefill(), then add the text using imagettftext() and then load my logo in. I'm having a problem getting the logo into the image without it keeping its background color of 'transparent'. So I would like it to have the dynamic background color behind it that is set with imagefill(). However, it keeps the transparency background it was loaded in with and so writes this part of the png as transparent. I tried calling imagefill() on the logo after it had been loaded in (using the same rgb that sets the background of the destination image) but this didn't do anything.
Below is my code:
$background = $_GET['background'];
$data1 = $_GET['data1'];
$data2 = $_GET['data2'];
$r = $_GET['r'];
$g = $_GET['g'];
$b = $_GET['b'];
$png_image = imagecreate(400, 200);
$gd_text_color = imagecolorallocate($png_image, 255, 255, 255);
$gd_background_color = imagecolorallocate($png_image, $r, $g, $b);
imagefill($png_image, 0, 0, $gd_background);
$text1 = "test test $data1";
$text2 = "test test again $data2";
$font = 'Lato-regular.ttf';
imagettftext($png_image, 18, 0, 20, 20, $gd_text_color, $font, $text1);
imagettftext($png_image, 18, 0, 20, 50, $gd_text_color, $font, $text2);
//trying to get this logo and place it in the corner.
$logo = imagecreatefrompng("images/logo.png");
imagecopy($png_image, $logo, 10, 10, 0, 0, 100, 30);
header('Content-type: image/png');
imagepng($png_image, $filename);
imagedestroy($png_image);
Here's the output of that code: http://i.imgur.com/n25h9Js.png
And here's what the image looks like when loaded into a program that accepts an alpha channel: http://i.imgur.com/3OIRupN.png
Does anyone know how I'd achieve what I'm attempting?
Thanks for your time.
EDIT
To try and explain what I want here's another image. The top image is what I currently get, and the bottom image is what I want. I'm simply trying to load in a transparent PNG that can sit ontop of different colored backgrounds. However I either get it like how it is shown here (transparent background) or as a black background (because I guess the alpha channel isn't being looked at?). Hope this helps. Image:
Edit 2
As per the comment below, I changed it from imagecreate() to imagecreatetruecolor() and now it works fine! I would love an explanation why this solved it if anyone has the time but for now, thank you all who commented or even spent your time looking at this question.
I suspect your imagecreate() may be causing you problems as it creates a palettised image which doesn't have the flexibility or breadth of expression of a true-colour image.
I suggest you replace it with imagecreatetruecolor().
I have a Facebook app with which I'd like to display an image in a user's feed. It's just a small circle, which will be different colours depending on what the user does within my app.
Instead of creating lots of different images to display, one for each possible colour the circle could be, it would be ideal if I could put up a PNG with transparency, and then just change the colour by filling a DIV behind the image with Javascript.
However, it's not clear to me if Facebook will allow that in the feed.
Can include some Javascript in the feed, or is that strictly forbidden?
Or is my only option to have a library of images for all the different colours and have a PHP function which selects the right one to output?
Facebook doesn't allow any scriptable content within feed stories, and this isn't possible to implement what you want this way.
You can easily create simple script that will return colored image according to passed arguments and use it as source for the image.
Something like this may provide you some points:
<?
// create a 200*200 image
$img = imagecreatetruecolor(200, 200);
// get the color from URL arguments or use default one
$rgb = isset($_REQUEST['color']) ? $_REQUEST['color'] : 'FFEEDD';
$color = array(
base_convert(substr($rgb, 0, 2), 16, 10),
base_convert(substr($rgb, 2, 2), 16, 10),
base_convert(substr($rgb, 4, 2), 16, 10),
);
// allocate some colors
$white = imagecolorallocate($img, 255, 255, 255);
$red = imagecolorallocate($img, $color[0], $color[1], $color[2]);
// draw the head
imagefilledarc($img, 100, 100, 200, 200, 0, 360, $red, IMG_ARC_PIE);
// output image in the browser
header("Content-type: image/png");
imagepng($img);
// free memory
imagedestroy($img);
No, facebook doesn't allow embedding JavaScript in feeds for security reasons. Even if you manage to do it, it won't work because certain characters such as <, >, etc will be converted into html entities which means the JavaScript won't work.
Using PHP: How can I create an image to display an email address (to help reduce spam)?
Meaning, if I want to display "joe#example.com" on my web page, since crawlers can easily find that text - I want to display the email address as an image that says "joe#example.com".
How do I do that? I want the font to be:
color: #20c
font: 11px normal tahoma
A simple search that you could easily do....
However: (color, font and string not the ones you specified)
header("Content-type: image/png");
$im = #imagecreate(110, 20)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
Relevant function definitions:
php.net/imagecreate
php.net/imagestring
Use these:
header, to tell the browser to expect an image instead of HTML (PHP's default). The image function doc pages have more information about this.
imagettfbbox, to find out the required size for the image
imagecreatetruecolor, to create the image resource
imagecolorallocate, to allocate a color for the text
imagettftext, to draw the text (read the example, it's almost all you need)
imagepng, to output the image to the browser
You should use gd image processing library.
If you are only doing this for one address you should not be doing this dynamically everytime the page loads for performance reasons. If this is the case you can find amny email obfuscator online such as this one:
http://digitalcolony.com/lab/maskemail/maskemail.aspx