简体   繁体   English

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

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

I am trying to use an email address as an image. 我正在尝试使用电子邮件地址作为图像。

I wrote the following code: 我写了以下代码:

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

?>

I googled out and tried following solutions. 我用谷歌搜索并尝试了以下解决方案。

  1. Gd p library is configured and working. GD P库已配置且正在运行。

  2. I cleared the browser cache. 我清除了浏览器缓存。

  3. I have check all function on php manual. 我已经检查了PHP手册上的所有功能。

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

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

My gd configuration: 我的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

I appreciate your efforts. 感谢您的努力。 Thanks 谢谢

As @Ultimaer mentioned, you should remove all your echo es. 如@Ultimaer所述,您应该删除所有echo Your code is putting some information on top of your image data, that is probably why it looks broken. 您的代码将一些信息放在图像数据之上,这可能就是为什么它看起来很破损的原因。

Here's the working source: 这是工作源:

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

Remove echo keyword and try it. 删除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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM