2011-09-08 140 views
1

我是新的使用PHP与图像。 我已经找到了代码,显示图像图片不显示在php

<<?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); 

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

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

但Firefox表现出一些虚假的代码,而不是说Image.here是将图像的链接,火狐显示 http://tinypic.com/r/htw6cm/7 这里是链接到图片

+0

因为它的localhost你手动尝试打开图像? –

+0

是的很明显,它打开好 – Sharpzain120

+0

你可能发送错误的标题。您必须发送带有jpeg图像的'Content-Type:image/jpg'。 – JJJ

回答

2

什么是您看到图像的“字节”。默认情况下,您的浏览器会认为您的脚本即将打印一些文本。所以你最终会看到一些奇怪的人物。

由于您打印的不是文字,而是图像,因此您需要告诉浏览器脚本的内容将是图像。在脚本开始

<?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); 
0

使用

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

发送一个报头的图像:您可以通过发送标题一样,做到这一点。 因此浏览器将处理PHP的输出为JPG图像数据

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

在脚本的开始,但在进行实际输出,即在上述imagejpeg($im);

所以前行权,它可以让你看到一个错误,如果任何发生

<?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); 

还我删除了一些失误,错误和无用的运营商