簡體   English   中英

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. GD P庫已配置且正在運行。

  2. 我清除了瀏覽器緩存。

  3. 我已經檢查了PHP手冊上的所有功能。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM