I am using imagejpg() to display an image on the browser and place text over the image.
I have used this before and it has worked. Now I switched domain hosts and perhaps the PHP version is different as the image is now not displaying.
It displays a small square in the middle of the screen, like a broken link, but the image is definitely there.
Could someone please assist with this?
header('Content-type: image/jpeg');
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('images1/new_image.jpg');
// Allocate A Color For The Text
$red = imagecolorallocate($jpg_image, 255, 0, 0);
$green = imagecolorallocate($jpg_image, 0, 255, 0);
// Set Path to Font File
$font_path = 'canadian.ttf';
$text1 = "hello";
$text2 = "there";
$text3 = "world";
//Floor 17
imagettftext($jpg_image, 12, 0, 272, 17, $green, $font_path, $text1);
imagettftext($jpg_image, 12, 0, 200, 38, $green, $font_path, $text2);
imagettftext($jpg_image, 12, 0, 480, 17, $green, $font_path, $text3);
// Send Image to Browser
imagejpeg($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
Thank you in advance.
Rob
I just figured it out. Answer below
I moved the header down and it sorted out the problem.
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('images1/new_image.jpg');
// Allocate A Color For The Text
$red = imagecolorallocate($jpg_image, 255, 0, 0);
$green = imagecolorallocate($jpg_image, 0, 255, 0);
// Set Path to Font File
$font_path = 'canadian.ttf';
$text1 = "hello";
$text2 = "there";
$text3 = "world";
//Floor 17
imagettftext($jpg_image, 12, 0, 272, 17, $green, $font_path, $text1);
imagettftext($jpg_image, 12, 0, 200, 38, $green, $font_path, $text2);
imagettftext($jpg_image, 12, 0, 480, 17, $green, $font_path, $text3);
header('Content-type: image/jpeg');
// Send Image to Browser
imagejpeg($jpg_image);
// Clear Memory
imagedestroy($jpg_image);
I had the same problem, found out that if there is a blank space before the header, the image doesn't work.
I was able to save the image on the server but not to get the browser to display it.
Removing every blank space did the trick for me.
I think it's the same reason why your script didn't work with the header at the beginning.
Related
I am trying to write text on image and displaying that image on browser. I tried below code but it is not working properly. Below code is showing one empty square (no image). Can anyone tell what's wrong I am doing here.
<?php
header('Content-type: image/jpeg');
$jpg_image = imagecreatefromjpeg('images/travel.jpg');
$white = imagecolorallocate($jpg_image, 255, 255, 255);
$text = "This is a sunset!";
imagettftext($jpg_image, 25, 0, 75, 300, $white, $text);
imagejpeg($jpg_image);
imagedestroy($jpg_image);
?>
The problem in your code is this line
imagettftext($jpg_image, 25, 0, 75, 300, $white, $text);
As mentioned in the doc http://www.php.net/manual/en/function.imagettftext.php
you have to pass font file path too
So just change the line
imagettftext($jpg_image, 15, 0, 15, 15, $white, 'Roboto-Bold.ttf',$text);
and put a font file to the folder and it will work.
If you don't want to give the font file replace the line
imagettftext($jpg_image, 25, 0, 75, 300, $white, $text);
With
imagestring($jpg_image,15,15,15,$text,$white);
Your code will be
<?php
header('Content-type: image/jpeg');
$jpg_image = imagecreatefromjpeg('https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQf66Ol6NAa6sdNhDJT0z1fVfTmjPjxAHkopPwExZ9AqHSqNzHP');
$white = imagecolorallocate($jpg_image, 255, 255, 255);
$text = "This is a sunset!";
imagestring($jpg_image,15,15,15,$text,$white);
imagejpeg($jpg_image);
imagedestroy($jpg_image);
?>
This will work for you
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);
?>
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?
I'm using the following code in the test.php file to generate an image from a text.
<?php
error_reporting(E_ALL);
// 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 = '/home/axxxxxxx/public_html/font.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);
?>
Then I'm trying to display the image in the test2.php as follows
<?php
echo "<img src=\"/test.php\" />";
?>
All I get is the default broken image icon. The path to the font file and image url is correct. All file permission are at 777. The servers do have the GD library.
What might I be doing wrong?
This is caused by the missing font. Please copy the font file in the test.php directory and change code:
$font = '/home/axxxxxxx/public_html/font.ttf';
to
$font = 'font.ttf';
Hope it helps.
My issue was a wrong offset. The image was showing nothing, no text, no errors in the source code, just a blank file. The paths were correct. I thought there was an error in the ttf font but turns out it was just wrong positioning.
Here's what helped me to see a bit of the text:
imagettftext($im, 20, 0, 20, 20, $fg, $font, $text);
This shows a bit of text on the top right.
Full working code:
putenv('GDFONTPATH=' . dirname(__FILE__));
$font = 'arial'; // located next to the script
imagettftext($im, 20, 0, 20, 20, $fg, $font, $text);
Found the answer. As Danak suggested, I saved the file as UTF-8 Without BOM using the notepad++. Then Is simply started displaying the image correctly.
I'm useing Joomla 1.5. I have the script for create image with text over It, but It doesn't work for me:
<?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.TFF";
// 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);
?>
I don't understand why this doesn't work. I found many examples in google, I used It, but always the same.
This error in english means: Image "http://juokoera.lt/a.php" can't be shown, because It have problems (errors).
I found in google, that can be fault by my hosting, I changed It, but the same problem. Help me, please if you can. Thank you very much.
UPDATED:
I got the same error when, code looks like:
dasfasdf
dfas
<?php
header('Content-Type: image/png');
$im = imagecreatetruecolor(400, 30);
$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);
$text = 'Testing...';
$font = "ARIAL.TTF";
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
imagepng($im);
imagedestroy($im); ?>
How can I use additional text in the same php?
I think your problem is here:
$font = "ARIAL.TFF";
in fact, on your server a file named "ARIAL.TFF" doesn't exist, the font extension you are looking for is TTF, not TFF, and in fact "ARIAL.TTF" on your server exists and i just downloaded it, correct that line to:
$font = "ARIAL.TTF";
After that, you should be able to write text on your image
Hope this helps
UPDATE
After question update, i noticed that an header is sent after printing out some text.
Nothing has to be printed out before the header() function, in order for headers to be correctly sent.
In the PHP manual>header() it is the first thing that is explained:
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.