PHP GD header issue - php
A couple of months ago I enabled GD on localhost to play around with it a bit, used various scripts found online to get a better understanding of what it does, all good and well.
But now I feel like I'm descending into insanity here because when trying to repeat the above with the exact same scripts I keep getting header errors, both on local and remote host. I figure I must have somehow completely forgotten how do do it right so I need a brief refresher on this.
For example, this was the first script I ever tried:
$my_img = imagecreate( 200, 80 );
$background = imagecolorallocate( $my_img, 0, 0, 255 );
$text_colour = imagecolorallocate( $my_img, 255, 255, 0 );
$line_colour = imagecolorallocate( $my_img, 128, 255, 0 );
imagestring( $my_img, 4, 30, 25, "thesitewizard.com",
$text_colour );
imagesetthickness ( $my_img, 5 );
imageline( $my_img, 30, 45, 165, 45, $line_colour );
header( "Content-type: image/png" );
imagepng( $my_img );
imagecolordeallocate( $line_color );
imagecolordeallocate( $text_color );
imagecolordeallocate( $background );
imagedestroy( $my_img );
Source
Worked fine back then, now when I run it I get this:
Warning: Cannot modify header information - headers already sent by (output started at H:\xampp\htdocs\tests\script.php:1) in H:\xampp\htdocs\tests\script.php on line 11
�PNG ��� IHDR�������P���!�#��� PLTE���������M�)B����IDATH��1�0EA��N�����:��8Q�JN�d�Z����! � ���k�Z⊜���k��g�˕��|K$�M��|)��$ů8DfRQB���-��[I��𦂫tźR�+���%�"��Ut)(�K�#dF�e�zKF9&�g|C7i��d��\G�Y � �3y�Ƌ�_�%�����IEND�B�
Warning: Wrong parameter count for imagecolordeallocate() in H:\xampp\htdocs\tests\script.php on line 13
Warning: Wrong parameter count for imagecolordeallocate() in H:\xampp\htdocs\tests\script.php on line 14
Warning: Wrong parameter count for imagecolordeallocate() in H:\xampp\htdocs\tests\script.php on line 15
Same thing for the second script I ever tried:
// create a 200*200 image
$img = imagecreatetruecolor(200, 200);
// allocate some colors
$white = imagecolorallocate($img, 255, 255, 255);
$red = imagecolorallocate($img, 255, 0, 0);
$green = imagecolorallocate($img, 0, 255, 0);
$blue = imagecolorallocate($img, 0, 0, 255);
// draw the head
imagearc($img, 100, 100, 200, 200, 0, 360, $white);
// mouth
imagearc($img, 100, 100, 150, 150, 25, 155, $red);
// left and then the right eye
imagearc($img, 60, 75, 50, 50, 0, 360, $green);
imagearc($img, 140, 75, 50, 50, 0, 360, $blue);
// output image in the browser
header("Content-type: image/png");
imagepng($img);
// free memory
imagedestroy($img);
Source
That now outputs:
Warning: Cannot modify header information - headers already sent by (output started at H:\xampp\htdocs\tests\script.php:1) in H:\xampp\htdocs\tests\script.php on line 22
�PNG ��� IHDR�����������":9���gIDATx����v�H�a<�4Ƹ���ܕ��:/{�v�}��z����C��(�~��҆�Uj1�jƖ�~)y[�x��JW��+/n�q�9)eɽ?�n�q%Pfg��f� �s+��I;I��l ��2s��-R����u 9���b�}��H�d�B�v��R��2��Kr�h�u�v�8���y�U��|��^O�T�'�:��I *R!��r�%���Z����$_�L�ʴVd�W�U�'��tk��2\�XA�5�s���ϔGL0�Q���:0O|M�W� ����j�ܼC���U}�՚���FŹǂ�UO�֝���ά�OW�z�k���N"��� ���1F��"ls{r�?�#��/v���ZSw1i���? i�8�]��C��A��xN?xw�죙��ۮ�9 -&�C�h�N���"r��B��!������j�Ǹ�_"ե>�Љ�u��q����9ɫ�T��5�s0*���*xݣ> �X!�w�����R�u �~�-O7�"�� <��·v���K��i�;�Dv��c��P�q��h,)~C��w�����9��<��N�!&h,�������q�5���v&h,������&��k��X\'��7�l�F�.�-�B����Ƃ�ğ'� ^Ajw�1�X0��X"�*��z�.�&"+|{��v/a+0�1o�i0G!l�~�o�+_�� �_��� 8�r|��F���k��!������}>��Ǟ7Xbb=�]�Rd��1��f�d�}�Nـ�k�ۉ���[5�k��6��y��I��jc�~/��˭N�ڍ�� �s�����4L�X0Ac��4L�X0����;�a�Ƃ &h,����Ƃ%�q�,�Nh,����Ƃ1���=�V�h,�����,|c�?L,����Ƃn�pϮs<&��ơ"��bq2�tX��w�0��h��bb�^����4�¿�+���; �p1�\�_����ܶ�\o�Z�$����Yxz˟Q�O:��"��8� �Ov���|���78 ���WԸZ�X����(p\=����? y�a:��N���ޚ�4��{ ~b��[S$�UO��ᡷ�a�=��[���=֩��Z_6����w��|�}�CBf"�K�$${��3�E^�vMϨc$ڻ��f�Pu+W�+��;{�L �6Dލ�w�G������J��۪��T�c�rl��n_J�E]�|��m����}���an>���Խ����IEND�B`�
However, the third script I ever tried still works:
// read the post data
$data = array('100','200','300','400','500','350','270');
$x_fld = array('Sun','Mon','Tue','Wen','Thu','Fir','Sat');
$max = 0;
for ($i=0;$i<7;$i++){
if ($data[$i] > $max)$max=$data[$i]; // find the largest data
}
$im = imagecreate(320,255); // width , height px
$white = imagecolorallocate($im,255,255,255); // allocate some color from RGB components remeber Physics
$black = imagecolorallocate($im,0,0,0); //
$red = imagecolorallocate($im,255,0,0); //
$green = imagecolorallocate($im,0,255,0); //
$blue = imagecolorallocate($im,0,0,255); //
//
// create background box
//imagerectangle($im, 1, 1, 319, 239, $black);
//draw X, Y Co-Ordinate
imageline($im, 10, 5, 10, 230, $blue );
imageline($im, 10, 230, 300, 230, $blue );
//Print X, Y
imagestring($im,3,15,5,"Students",$black);
imagestring($im,3,280,240,"Days",$black);
imagestring($im,5,100,50,"Simple Graph",$red);
imagestring($im,5,125,75,"by Vijit",$green);
// what next draw the bars
$x = 15; // bar x1 position
$y = 230; // bar $y1 position
$x_width = 20; // width of bars
$y_ht = 0; // height of bars, will be calculated later
// get into some meat now, cheese for vegetarians;
for ($i=0;$i<7;$i++){
$y_ht = ($data[$i]/$max)* 100; // no validation so check if $max = 0 later;
imagerectangle($im,$x,$y,$x+$x_width,($y-$y_ht),$red);
imagestring( $im,2,$x-1,$y+1,$x_fld[$i],$black);
imagestring( $im,2,$x-1,$y+10,$data[$i],$black);
$x += ($x_width+20); // 20 is diff between two bars;
}
imagejpeg( $im, "graph.jpeg", 90);
imagedestroy($im);
echo "<img src='graph.jpeg'><p></p>";
Source
It seems that the fact it doesn't output a header is why it works, though why the others used to work and now don't are beyond me, especially since the errors are also being replicated on an unrelated remote host, am I simply being clueless and calling the scripts erroneously? And before anybody asks, yes, I'm sure GD has been fully enabled, here are the relevant specs from phpinfo:
GD Support: enabled
GD Version: bundled (2.0.34 compatible)
FreeType Support: enabled
FreeType Linkage: with freetype
FreeType Version: 2.1.9
T1Lib Support: enabled
GIF Read Support: enabled
GIF Create Support: enabled
JPG Support: enabled
PNG Support: enabled
WBMP Support: enabled
XBM Support: enabled
Can someone please enlighten this befuddled girl?
The reason is very simple (apart from the warnings about imagecolordeallocate()).
You need to encode and save your script file as UTF-8 withuot BOM or ASCII. It is currently saved as UTF-8 which is causing the problem due to the extra BOM character.
The BOM character in UTF-8 encoded pages messes up the output as it is sent as the first character. BOM use is optional, and, if used, it appear at the start of the text stream.
Use any advanced text-editor or IDE like Notepad++ to encode the script file and save it.
it's not header problem.
It's Wrong parameter count for imagecolordeallocate() causing whole mess
While this one is plain and clear.
You might want to check there's no whitespace before you open your PHP tags, as this would put the server into text/html mode.
EDIT: Ah yeah, scratch that. It's what Col. Shrapnel said. The error is forcing the page into text/html.
Do you have any whitespace in your file (probably outside of <?php ... ?> tags)? Any stray whitespace will be sent to the client, after which headers can no longer be sent. Based on the line number in your error message, there probably is whitespace before your opening <?php, so ensure that it is the first thing in the script.
Also, from your error messages, you can see that you're calling imagecolordeallocate with the wrong number of parameters. The first argument is supposed to be your image handle, so the correct call would be
imagecolordeallocate($my_img, $line_color);
imagecolordeallocate($my_img, $text_color);
imagecolordeallocate($my_img, $background);
The error messages being outputted would also be sent to the client which would prevent headers from being set.
As the error says, some thing is getting output from the file H:\xampp\htdocs\tests\script.php on line 1
Your problem is path of folder
Create a folder for image for example.: barla
After change your header
header("Content-type: barla/image/png");
Related
PHP - imagettftext not working
I am trying to generate images containing data from my database, and I want to use a custom font for it, so I looked up the imagettftext. I am following its syntax, but it's not printing anything. What am I doing wrong? When I use imagestring it works fine. But that does not use any custom font. I have placed my font.tff file in the same folder as my script. Here is my code: header('Content-Type: image/png'); $image = #imagecreate(400, 110); $backgrund = imagecolorallocate($image, 70, 130, 180); $textcolor = imagecolorallocate($image, 255, 255, 255); $font = 'font.ttf'; // TTF imagettftext($image, 10, 0, 15, 15, $textcolor, $font, 'Test TTF'); // Normal text imagestring($image, 2, 15, 30, 'Test Normal Font', $textcolor); imagepng($image, 'images/test.png'); imagedestroy($image); Am I missing something? The first line is completely blank. I also tried adjusting the font size (it maybe is different than regular imagestring?) but still nothing. The font is a .ttf font file.
PHP imagettftext Is Not Showing Text
Website: https://bimmr6696.000webhostapp.com/signs/Sign.php?Line1=&Line2=asd&Line3=&Line4= PhpInfo: https://bimmr6696.000webhostapp.com/signs/test.php Currently I have the following code: $img = LoadPNG('sign.png'); //$font = imageloadfont('minecraft.ttf'); // Add the text imagettftext($img, 20, 0, 5,5, $black, 5, "Test Text"); imagestring($img, 5, 5, 50, 'Test Text', $text_color); // Create image instances $dest = imagecreatetruecolor(80, 40); // Copy imagecopy($dest, $img, 0, 0, 20, 13, 80, 40); // Set the content type header - in this case image/png header('Content-type: image/png'); // Output the image imagejpeg($img); imagejpeg($dest); imagedestroy($dest); imagedestroy($img); When I use imagestring I'm able to get the text to show, but the text is too small which causes me to need imagettftext, although nothing shows when I use this. I've just about run out of ideas to try and solve this and so any help would be very appreciated. TLDR: I need to either find out why imagettftext doesn't work or find a way to change font size with imagestring Deleted everything then retyped it all out and for some reason that seemed to fix it... Because this isn't an actual answer I'm not going to post it below...
Display Chinese characters over an image
I am trying to use PHP to display some Chinese characters over an image. The code is provided below. <?php $im = imagecreatetruecolor(60, 20); $white = imagecolorallocate($im, 255, 255, 255); $grey = imagecolorallocate($im, 128, 128, 128); $black = imagecolorallocate($im, 0, 0, 0); imagefilledrectangle($im, 0, 0, 60, 20, $white); $text = '你好'; $font = 'simsun.ttc'; imagettftext($im, 10, 0, 5, 15, $black, $font, $text); imagepng($im, 'pic.jpg'); imagedestroy($im); ?> I can display those two Chinese characters on my PC correctly but I cannot display them on my server. When I typed in the following command line, and the warning message showed up. $ php font.php PHP Warning: imagettftext(): Could not read font in /usr/share/nginx/html/model/font.php on line 9 I did some search on the internet and found that some said imagettftext may not be able to handle ASCII codes whose values are greater than 127. The point is I am able to display them on my PC. I wonder how I can fix this problem.
I found the problem. Two things: first, you need to use the right font file; second, you need to use the full path of the font file. Namely, I should use $font = '/usr/share/nginx/html/model/fireflysung.ttf'; NOT $font = 'fireflysung.ttf';
How to make transparent text to jpeg?
Everywhere is talking about adding text on transparent image. I have successfully added text like watermark (so mean transparent text). Can you suggest me how to give opacity 50% ? Here is code - $text = "GIF-KING"; $font = "arial.ttf"; $image = imagecreatefromgif('tmpimg/myimage.gif'); $text_color = imagecolorallocate($image, 198, 60, 147); //imagestring($image,36, 10, 20, $text, $text_color); imagettftext($image, 30, 0,10, 290, $text_color, $font, $text);
You'd need imagecolorallocatealpha instead of imagecolorallocate, alpha blending is not available 100%, so take care of error messages. imagecolorallocatealpha() behaves identically to imagecolorallocate() with the addition of the transparency parameter alpha. http://php.net/imagecolorallocatealpha
Captcha image generating problems when using font PHP
its working perfect, until i try to use a font for text. This is my code.. The 3er line commented now is the one that works fine... but in this example im trying to replace this line with imagettftext.. not lucky. Whats my error? $newImage = imagecreatefromjpeg( "orsil_secure.jpg" ); $txtColor = imagecolorallocate($newImage, 0, 0, 0); //imagestring($newImage, 5, 10, 27, $ranStr, $txtColor); imagettftext($newImage, 5, 10, 27, $txtColor, $font_path, $ranStr); header( "Content-type: image/jpeg" ); imagejpeg($newImage); Oh yes in the previous lines is the route to the font here: // random number $ranStr = md5(microtime()); $ranStr = substr($ranStr, 0, 6); // Set Path to Font File $font_path = 'captcha.TTF';
It looks like you have the wrong parameters in your call to imagettftext. Presumably, the 5 corresponds to the font used in imagestring, but has no place in imagettftext. You also need to specify the size and angle. For example: imagettftext($newImage, 12, 45, 10, 27, $txtColor, $font_path, $ranStr); ^^ ^^ || || || ------ angle ----------- size In this example: size=12px or 12pt, depending on whether you're using GD1 or GD2 angle = 45° Obviously, you'll want to use your own values here. If it still isn't working, then it's likely that the path to your font file is wrong. Check to see if it's in the same folder as the PHP script.
I think you are missing a parameter in your imagettftext. If you look at http://php.net/manual/en/function.imagettftext.php it has size, angle, x and y. You are missing one of the 4 (im guessing the angle). So it needs to be something like: imagettftext($newImage, 5, 0, 10, 27, $txtColor, $font_path, $ranStr); Where 0 is for angle.