问题
I want to use the GD libs, which seems installed but I can't use it.
I look at my gd installation with
php_info();
It returns me this:

Then I tried this:
<pre>
<?php print_r(gd_info())?>
</pre>
Which returns this:
Array
(
[GD Version] => bundled (2.0.34 compatible)
[FreeType Support] => 1
[FreeType Linkage] => with freetype
[T1Lib Support] => 1
[GIF Read Support] => 1
[GIF Create Support] => 1
[JPEG Support] => 1
[PNG Support] => 1
[WBMP Support] => 1
[XPM Support] =>
[XBM Support] => 1
[JIS-mapped Japanese Font Support] =>
)
But when I try to make this:

Using that code:
<?php // content="text/plain; charset=utf-8"
$im = imagecreatetruecolor ( 300, 200);
$black = imagecolorallocate ($im, 0, 0, 0);
$lightgray = imagecolorallocate ($im, 230, 230, 230);
$darkgreen = imagecolorallocate ($im, 80, 140, 80);
$white = imagecolorallocate ($im, 255, 255, 255);
imagefilledrectangle ($im,0,0,299,199 ,$lightgray);
imagerectangle ($im,0,0,299,199,$black);
imagefilledellipse ($im,150,100,210,110,$white);
imagefilledellipse ($im,150,100,200,100,$darkgreen);
header ("Content-type: image/png");
imagepng ($im);
?>
It returns this:
�PNG IHDR,�ݽK�IDATx������H���r,����p�CU�.!$Aw�ZB�h�].��ˀAM��O�5��D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�D�~F_����������7�)D��̎�s�"��po;,^�2?@�o�?���Y�N��覺�n~RM'������~�{���s� ��w� ���Z�\.їQƹ��&*ȍ�i�&��K��v��T�:>�����m�;K5.��}�u�#��T�W"�����nݫM����������K5���noOx�mr�Ǎpc~�;��GKq��䧽��R�8)���R��l��槽@Ok�;��#�_æ�s���h���p�@�%��bgv���1H��E�R���ZI����P~}�8�N"|T��:�(��N�T��8�G����Sx�oX=��P�#�l%�܄������k�Ò�P�\-�����b*�tX)B��z�e"T +JwX#B�T�D�@6*�a��K*v�=�{ d]� I��V��%���d^�y#T G�0i� ��*&�Ƒ1Bk���X�#��@��??�"LxPљl3�.���1�K>E�"�vDѫT��+��0 �lVbV���N��I�3)����f�E�V;lR,e� E�.#�I��A��ua{�a�bJO���Y���gR�b��f�G8�bf��7%����?h�����l�gR'��F�p�%Ŧ�Sm| �8��ƍp�1Ŧ���~p��f�Gx���&� ^z����]����Rlj������o&�e��8��}oU��W"|b_��� w�GL{�D�����fy�m��['���Te��6h�m$�C� ��鉾�����O�� ��A�4����N��b�M��t"|��I-���>@��8ͩ��[FZ����YN"�K3�(���P�!�!�!�!�!�!�!�!�!�!�!�!�!�!�!�!�!�!�!�!�!�!�!�!�!�!�!�!�!�!�!�!�!�!�!�!�!�!�!�!����z�gt8IEND�B`�
回答1:
The correct header isContent-Type
(note the capital t)
From the comments I see you figured out that the content type is replaced with text/plain. This is most likely something your webserver does. It probably sees .php and overrides the output. If you run your file from the command line (php file.php > file.png
) and open the resulting file it probably renders fine.
回答2:
Can you try deleting the blank line between your variable defines and the beginning of your draw operations? It's probably causing PHP to flush the headers, so it can send the blank line as content, before you set the Content-Type header below it.
Either that, or even better, move the Header set up to the top of the script.
回答3:
This kind of error can occurred by empty lines after the PHP closing ?>
. Use a true plain text editor (e.g. vi/vim) to check it, or take a look on the hexdump of the file.
来源:https://stackoverflow.com/questions/19882088/browser-does-not-display-png-image