php header('Content-type: image/jpeg')显示破碎的图像


php header('Content-type: image/jpeg') shows borken image

我正在尝试使用电子邮件地址作为图像。

我写了以下代码:

<?php 
header('Content-type: image/jpeg');
echo $email= 'ali@alipakistani90.com';
echo $email_length= strlen($email);
echo $font_size= 4;
echo $image_height= imagefontheight($font_size);
echo $image_width= imagefontwidth($font_size) * $email_length;
$image= imagecreate($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$font_color= imagecolorallocate($image, 0, 0, 0);
imagestring($image, $font_size, 0, 0, $email, $font_color);
imagejpeg($image);
?>

我搜索了一下,并尝试了以下解决方案。

  1. 我清空了浏览器缓存

Please Note: I also get broken image when I only the following code:

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

我的gd配置:

GD Support enabled GD Version bundled (2.1.0 compatible) FreeType Support enabled FreeType Linkage with freetype FreeType Version 2.4.10 GIF Read Support enabled GIF Create Support enabled JPEG Support enabled libJPEG Version unknown PNG Support enabled libPNG Version 1.5.14 WBMP Support enabled XPM Support enabled libXpm Version 30411 XBM Support enabled WebP Support enabled

我感谢你的努力。由于

正如@Ultimaer提到的,你应该删除所有的echo。你的代码把一些信息放在你的图像数据的顶部,这可能就是为什么它看起来坏了。

这是工作源:

<?php
header('Content-type: image/jpeg');
$email= 'test@test.com';
$email_length= strlen($email);
$font_size= 4;
$image_height= imagefontheight($font_size);
$image_width= imagefontwidth($font_size) * $email_length;
$image= imagecreate($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$font_color= imagecolorallocate($image, 0, 0, 0);
imagestring($image, $font_size, 0, 0, $email, $font_color);
imagejpeg($image);

删除echo关键字并尝试。

$email= 'ali@alipakistani90.com';
$email_length= strlen($email);
$font_size= 4;
$image_height= imagefontheight($font_size);
$image_width= imagefontwidth($font_size) * $email_length;
$image= imagecreate($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$font_color= imagecolorallocate($image, 0, 0, 0);
imagestring($image, $font_size, 0, 0, $email, $font_color);
header('Content-type: image/jpeg');
imagejpeg($image);
imagedestroy($image);