Link to home
Start Free TrialLog in
Avatar of MaRiOsGR
MaRiOsGRFlag for Greece

asked on

wrong Encodng or fonts

The problem is like that.
I have a simple forum phpBB here: http://musicogenic.net/
if you view the source you'll see that the encoding is charset=iso-8859-7

Now lets get to the problem.
I use a little php script that i found somewhere ( i dont remeber), and i use it a signature.
It takes data from the phpbb forum and shows them in an image.

the file is this : http://musicogenic.net/whatever.php
The problem is that when the Last Topic: is in Greek characters, its not shown right, even if the encoding is iso-8859-7 as it should be.

The code of the file this :

<?php

define('IN_PHPBB', true);
$phpbb_root_path = './';
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);

// Get basic (usernames + totals) online
// situation
//
$user_forum_sql = ( !empty($forum_id) ) ? "AND s.session_page = " .
intval($forum_id) : '';
$sql = "SELECT u.username, u.user_id, s.session_logged_in, s.session_ip
    FROM ".USERS_TABLE." u, ".SESSIONS_TABLE." s
    WHERE u.user_id = s.session_user_id
        AND s.session_time >= ".( time() - 300 ) . "
        $user_forum_sql
    ORDER BY u.username ASC, s.session_ip ASC";
if( !($result = $db->sql_query($sql)) )
{
    message_die(GENERAL_ERROR, 'Could not obtain user/online information',
'', __LINE__, __FILE__, $sql);
}

$logged_online = 0;
$prev_user_id = 0;

while( $row = $db->sql_fetchrow($result) )
{
    // User is logged in and therefor not a guest
    if ( $row['session_logged_in'] )
    {
        // Skip multiple sessions for one user
        if ( $row['user_id'] != $prev_user_id )
        {
                $logged_online++;
        }

        $prev_user_id = $row['user_id'];
    }
}

$sql = 'SELECT max(topic_id)
FROM ' . TOPICS_TABLE;

if ( !($result = $db->sql_query($sql)) )
{
    message_die(GENERAL_ERROR, 'Couldn\'t retrieve max of Topic_id data',
'', __LINE__, __FILE__, $sql);
}

while ( $row = $db->sql_fetchrow($result))
{
    $max_topic_id = $row['max(topic_id)'];
}
$sql = 'SELECT topic_title
FROM ' . TOPICS_TABLE . '
WHERE topic_id = ' . $max_topic_id;

if ( !($result = $db->sql_query($sql)) )
{
    message_die(GENERAL_ERROR, 'Couldn\'t retrieve Topic_id data', '',
__LINE__, __FILE__, $sql);
}

while ( $row = $db->sql_fetchrow($result))
{
    $latest_topic = $row['topic_title'];
}

$image = "mypage.png";
$im = imagecreatefrompng($image);
$tc  = ImageColorAllocate ($im, 200, 150, 0);
$red  = ImageColorAllocate ($im, 255, 255, 255);
$blue  = ImageColorAllocate ($im, 255, 255, 255);
$green  = ImageColorAllocate ($im, 255, 255, 255);
$sitename = $board_config['sitename'];

$total_users = get_db_stat('usercount');
$total_posts = get_db_stat('postcount');
$total_topics = get_db_stat('topiccount');
$j = strlen($total_users);
$leerzeichen = $j*6+180;
$newest_userdata = get_db_stat('newestuser');
$newest_user = $newest_userdata['username'];
ImageString($im, 3, 98, 7, "Statistics Of : $sitename", $tc);
ImageString($im, 2, 98, 25, "Users : $total_users -", $tc);
ImageString($im, 2, $leerzeichen, 25, "Online: $logged_online", $red);
ImageString($im, 2, 98, 35, "Latest Topic :", $tc);
ImageString($im, 2, $leerzeichen, 35, "$latest_topic", $blue);
ImageString($im, 2, 98, 45, "Posts: $total_posts in $total_topics
Topics", $tc);
ImageString($im, 2, 98, 55, "Newest registered user :", $tc);
ImageString($im, 2, 260, 55, "$newest_user", $green);
header("Content-Type: image/png; charset:iso-8859-7");
Imagepng($im,'',100);
ImageDestroy ($im);

?>

Any ideas?
Avatar of Bernard Savonet
Bernard Savonet
Flag of France image

Hi,
The only thing sure, is that something is wrong. Isolating which part will be the most difficult, 'cause the solution should be simple.
1 - Check the char code selected in YOUR browser (automatic? greek? 9951-7?) when you have the problem AND when you have not. In some occasions "automatic" can be fooled. At the same time, experiment with manually changing the char code selected. Is there a "magical situation", where all would be fine?
2 - experiment with additional characters before and after your signature, eg, write xxx before and yyy after. Do these extra display correctly? all?

3 - Your line [header("Content-Type: image/png; charset:iso-8859-7")] puzzles me (but I'm not an expert at this)for 2 reasons
a - I would guess that since you have already sent characters to the client, this line is at best uneffective (but I'm very probably wrong)
b - If you say that content is an image, why would character encoding be used?

4 - I would think that the character encoding has to be done not at the header level (since graphics are sent, not text) but at the image preparation level (imagestring). Using imageloadfont might become mandatory...
Avatar of MaRiOsGR

ASKER

Hello Fibo,

1.nothing works, its not a browser matter as it seems.
2.I dont have to do that cause, if the Last Topic in a new thread inside the forum is writte in a)english b)latin characters
in the signature it works fine, so the problem is only when the Topic Subject in the forum is written in Greek characters.
3.To be honest the original script was like that: Content-Type: image/png and I added the charset:iso-8859-7 but it has no effect anyway...its useless and you are right about that!!!! :)
4.how can I do that ? :D
Avatar of siliconbrit
siliconbrit


Hi Mario,

The problem is nothing to do with the headers you are putting out to the browser.

You are creating an image, and embedding the text within that image, once the image is created, there is no text available that is affected by the charset you are using.

I'm no expert with the GD library, but I believe you need to load the required font when you build your GD object.

Documentation on the GD library is at: http://www.boutell.com/gd/

I once read somewhere that you need to load the Unicode font to do this, but I've never needed to create a GD image with embedded international text, so my advice would be to google for information on "GD unicode fonts" or similar.




ASKER CERTIFIED SOLUTION
Avatar of Gee_Orge
Gee_Orge

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Gee_Orge that worked.
thank you.
Mario,
Would you do us the favor to post the revised script, so that we can make a copy of the result?
I did not think to these kinds of use before, you open me new fields!
Thx.
Bernard
Yes now the code is this :

<?php

define('IN_PHPBB', true);
$phpbb_root_path = './';
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);
// Get basic (usernames + totals) online
// situation
//
$user_forum_sql = ( !empty($forum_id) ) ? "AND s.session_page = " .
intval($forum_id) : '';
$sql = "SELECT u.username, u.user_id, s.session_logged_in, s.session_ip
    FROM ".USERS_TABLE." u, ".SESSIONS_TABLE." s
    WHERE u.user_id = s.session_user_id
        AND s.session_time >= ".( time() - 300 ) . "
        $user_forum_sql
    ORDER BY u.username ASC, s.session_ip ASC";
if( !($result = $db->sql_query($sql)) )
{
    message_die(GENERAL_ERROR, 'Could not obtain user/online information',
'', __LINE__, __FILE__, $sql);
}

$logged_online = 0;
$prev_user_id = 0;

while( $row = $db->sql_fetchrow($result) )
{
    // User is logged in and therefor not a guest
    if ( $row['session_logged_in'] )
    {
        // Skip multiple sessions for one user
        if ( $row['user_id'] != $prev_user_id )
        {
                $logged_online++;
        }

        $prev_user_id = $row['user_id'];
    }
}

$sql = 'SELECT max(topic_id)
FROM ' . TOPICS_TABLE;

if ( !($result = $db->sql_query($sql)) )
{
    message_die(GENERAL_ERROR, 'Couldn\'t retrieve max of Topic_id data',
'', __LINE__, __FILE__, $sql);
}

while ( $row = $db->sql_fetchrow($result))
{
    $max_topic_id = $row['max(topic_id)'];
}
$sql = 'SELECT topic_title
FROM ' . TOPICS_TABLE . '
WHERE topic_id = ' . $max_topic_id;

if ( !($result = $db->sql_query($sql)) )
{
    message_die(GENERAL_ERROR, 'Couldn\'t retrieve Topic_id data', '',
__LINE__, __FILE__, $sql);
}
while ( $row = $db->sql_fetchrow($result))
{
    $latest_topic = $row['topic_title'];
}

//NEW CODE
include ("xtras/ConvertCharset.class.php");
$conv = new ConvertCharset();
$latest_topic = $conv->Convert($latest_topic, "iso-8859-7", "utf-8");
//ENDS HERE

$image = "mypage.png";
$im = imagecreatefrompng($image);
$tc  = ImageColorAllocate ($im, 200, 150, 0);
$red  = ImageColorAllocate ($im, 255, 255, 255);
$blue  = ImageColorAllocate ($im, 255, 255, 255);
$green  = ImageColorAllocate ($im, 255, 255, 255);
$sitename = $board_config['sitename'];

$total_users = get_db_stat('usercount');
$total_posts = get_db_stat('postcount');
$total_topics = get_db_stat('topiccount');
$j = strlen($total_users);
$leerzeichen = $j*6+180;
$newest_userdata = get_db_stat('newestuser');
$newest_user = $newest_userdata['username'];
ImageString($im, 3, 98, 7, "Statistics Of : $sitename", $tc);
ImageString($im, 2, 98, 25, "Users : $total_users -", $tc);
ImageString($im, 2, $leerzeichen, 25, "Online: $logged_online", $red);
ImageString($im, 2, 98, 35, "Latest Topic :", $tc);
// NEW LINE
imagettftext($im, 8, 0, $leerzeichen, 45, $blue, "./courbd.ttf", $latest_topic);
// ENDS HERE
ImageString($im, 2, 98, 45, "Posts: $total_posts in $total_topics Topics", $tc);
ImageString($im, 2, 98, 55, "Newest registered user :", $tc);
ImageString($im, 2, 260, 55, "$newest_user", $green);
header("Content-Type: image/png;");
Imagepng($im,'',100);
ImageDestroy ($im);

?>