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).
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.
Right now I am using a php script (let's say the save-page) to create a jpeg and save it to the server. Although it all works, when opening the save-page, the jpeg is also shown, which I don't really want. I just want the jpeg to be created and saved to the desired destination. To be clear, the saving is already working. The is part of the script that I am using.
header('Content-type: image/jpeg');
$jpg_image = imagecreatefromjpeg('*link to the base image*');
$white = imagecolorallocate($jpg_image, 0, 0, 0);
$font_path = 'interstate.ttf';
imagettftext($jpg_image, 14, 0, 44, 41, $white, $font_path, $text);
imagejpeg($jpg_image);
// save jpg to widget folder
imagejpeg($jpg_image, 'widget/vehicle_1_' . $id . '.jpg');
//free up memory
imagedestroy($jpg_image);
How can I change the script so that it saves the jpg to the widget folder, but not show it on this page when activated? Thanks in advance!
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);
?>
A simple PHP script that I picked up from stackoverflow generates a PNG with a transparent background, writes some text on it then directly outputs it to the client browser:
$font = 25;
$string = 'My Text';
$im = #imagecreatetruecolor(300, 300);
imagesavealpha($im, true);
imagealphablending($im, false);
$white = imagecolorallocatealpha($im, 255, 255, 255, 127);
$red = imagecolorallocate($im, 255, 0, 0);
imagefill($im, 0, 0, $white);
$lime = imagecolorallocate($im, 204, 255, 51);
imagettftext($im, $font, 0, 0, 30, $red, "fonts/tt0588m_.ttf", $string);
header("Content-type: image/png");
imagepng($im);
imagedestroy($im);
The scope is to obtain a simple service that feeds an image based on the parameters passed to it via URL, such as Google Charts (e.g. this QR code image).
So far, so good, except that if I click on the image generated with the code above and want to save it, the browser doesn't recognize it as being a PNG image, but a PHP script (Save as type selector has this option only), as oposed to the Google Charts example, where the resource is clearly identified as a PNG file.
How do I achieve this correct resource identification by the browser?
Browser will use the filename from the URL as the default value in the "Save as..." dialog. You can type another name of course or save the file using the suggested name (text.php) and rename it afterwards.
You can use the Content-disposition header to "suggest" a filename to the browser. Here is an example:
header("Content-type: image/png");
header("Content-disposition: inline; filename=mytext.png");
inline suggests that browser should attempt to display the image. Change it to attachment to suggest that the browser should display the "Save as..." or similar dialog.
filename= should contain the filename of your choice.
im using php and heroku to create some images for my facebook app, but the images arent shown, only picture of an broken image is shown.
Im using sample code from php tutorial website.
<?php
// Create a 100*30 image
$im = imagecreate(100, 30);
// White background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);
// Write the string at the top left
imagestring($im, 5, 0, 0, 'Hello world!', $textcolor);
// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
The code is in located in /src/texttopic.php
Heroku logs shows no errors.
Call the image like so:
<img src="/src/texttopic.php" height="30" width="30">
Set the image height / width accordingly.
Keep in mind that with heroku, if you have 2 or more web dynos running, the image creation may take place on one dyno, but when you request the image, you may be calling another dyno that does not have that image.