简体   繁体   中英

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.

  2. I cleared the browser cache.

  3. I have check all function on php manual.

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

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

My gd configuration:

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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