Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Image character encoding

    I've made a PHP script which, when a non-HTML page is requested, passes it through by setting the appropriate MIME type header and then uses readfile($file_to_pass); to send the file to the browser.

    Unfortunately, this seems to have run into a strange issue. I'm trying to convert my whole database/site to UTF-8 for pleasant international consistency, but since changing the encoding of a few of the PHP files, images no longer display correctly.

    http://andrewsteele.co.uk/shared/tem...co.uk/leaf.jpg is an example.

    The weird thing is that if you save the image, open it in a text editor of your choice and then re-save it in 8-bit encoding rather than the UTF-8 it seems to open in by default, the image then works perfectly.

    I thought that character sets didn't affect images.

    What's going on, and what can I do?!

    Any advice appreciated.

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Location
    Brasília, Brazil
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can you show some code?

  • #3
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    It's probably because GD doesn't work with UTF8. PHP doesn't fully support UTF yet.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #4
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Some code:

    PHP Code:
    function scms_pass_file($file_to_pass)
    {
        
    //set $ext, the file extension (the substring of everything after the dot in the basename(), ie filename, part of the path), and make it ready for a MySQL query (just adds 'quotes')
        
    $ext scms_db_2db(substr(basename($file_to_pass), strrpos(basename($file_to_pass), '.')+1));
        
    //find out the MIME type from the database
        
    if(!$mime_type scms_db_query("SELECT mime_type FROM scms_mimetypes WHERE ext=".$ext))
        
    //if the query above fails, look for no extension - the default MIME type
        
    $mime_type scms_db_query("SELECT mime_type FROM scms_mimetypes WHERE ext=''"); }
        
    //use them ($ext is the extension of your file)
        //scms_db_query returns FALSE when no rows are selected, so the if can simply be if the output variable
        
    if ($mime_typeheader("Content-Type: ".$mime_type);
        
    header("Content-Length: ".@filesize($file_to_pass));
        
    header("Cache-Control: max-age=86400");
        
    readfile($file_to_pass);
        return 
    TRUE;
    }

    //call the just-defined function to pass the file
    scms_pass_file($scms_input['pass']); 
    I have also noticed (now that my browser cache has expired!) that the same problem happens with CSS files. For some reason the change of encoding inserts invalid characters into the CSS and means that some instructions do not parse correctly.
    Last edited by almightymaster; 10-07-2006 at 11:27 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •