1

I have a Ubuntu server and PHP5, and the PHP script files, and all output are in UTF-8. I'm trying to send an image to the output stream, but just garbled chinese characters shows up in the output:

$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
header('Content-type: image/jpeg');
imagejpeg($im);
imagedestroy($im);

any suggestions?

2
  • Are you trying to output textual data and that image in the same script? That’s not possible. Both the textual data and the image are different resources and thus must be different files. Commented Aug 25, 2009 at 18:44
  • no, just the image, nothing is written before / after. Commented Aug 25, 2009 at 18:54

2 Answers 2

1

Your code works perfectly fine on my machine :

<?php
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
header('Content-type: image/jpeg');
imagejpeg($im);
imagedestroy($im);

die;
?>

Are you sure you are not outputing anything before or after that code ? Even any kind of whitespace would be a source of troubles.

Or maybe your script is doing something else somewhere ?


If it still doesn't work, maybe trying with imagettftext, to use a "better" / more complete font than the ones used by imagestring might help ?

Using something like this, for instance :

$font = '/usr/share/fonts/truetype/msttcorefonts/arial.ttf';
imagettftext($im, 20, 0, 10, 20, $text_color, $font, 'A Simple éléphant String');


BTW, did you try without those line :

header('Content-type: image/jpeg');
imagejpeg($im);
imagedestroy($im);

If there is an error/warning/notice, removing those lines might help you seeing those.


And, as a sidenote : using JPEG for images that contain some text generally doesn't give great results, as JPEG is a destructive compression mechanism. Using PNG, in that kind of situation, might get you better results ;-)

Sign up to request clarification or add additional context in comments.

3 Comments

possibly, but I haven't find anything yet. There are 16 included files... it's not my code, I just need to modify some stuff in it, sadly.
Ergh :-( not so nice ;; well, I suppose first thing would be to test the smallest part of code that you can, to determine if your server can generate the image outside of your application ;; then, removing the three lines that output the image might help getting error messages...
I've moved it to a standalone file.
0

Try removing the UTF-8 byte order mark, because it gets prepended to the contents of your JPEG image, rendering it invalid.

1 Comment

first of all that's true! However, after removing the first 3 bytes '' of a downloaded file, it's still an invalid image.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.