开发者

Images are not shown in php

I am new in using Php with Images. I had found the code to display Image

<<?php
// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  'A Simple Text String'开发者_如何学Python, $text_color);

// Save the image as 'simpletext.jpg'
imagejpeg($im);

// Free up memory
imagedestroy($im);
?>>

But firefox show some bogus code instead of that Image.here is the link to the Image that Firefox shows http://tinypic.com/r/htw6cm/7 Here is the link to the Image


What are you seeing the the "bytes" of an image. By default your browser will think that your script is about to print some text. So you end up seeing some strange characters.

Since you are printing not text, but image, you need to tell the browser that the contents of your script will be an image. You can do this by sending a header like that:

<?php

header("Content-Type: image/jpeg"); // treat the script as an image

// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);

// Save the image as 'simpletext.jpg'
imagejpeg($im);

// Free up memory
imagedestroy($im);


Use

<?
header('Content-type: image/jpg');

at beginning of your script to send an header for images. so the browser will handle the php output as jpg-image-data


header('Content-type: image/jpg');

not at beginning of your script, but right before starting actual output, i.e on the line above imagejpeg($im);

so, it will let you to see an error if any occurred

<?php
// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);

//send header
header('Content-type: image/jpg');

//output an image
imagejpeg($im);

also I removed some mistakes, errors and useless operators

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜