tags:

views:

1146

answers:

2

Hi,

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?

A: 

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

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

Hi,

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

Pascal MARTIN
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.
balint
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...
Pascal MARTIN
I've moved it to a standalone file.
balint