Topic: Replacing <img> with <object>

I am in the process of modifying my PunBB installation to use <object> tags instead of <img> tags for more alternative content options and  for future compatibility. I am almost finished making the changes and while I have encountered a few a problems in the process, only one problem (that I know of) remains:

The "type" attribute in the <object> tag contains the mime type for the media used in the object.

For example, this is how the PHP would output the HTML for the http://www.w3.org/Icons/valid-css.png .png icon:

<object type="image/png" data="http://www.w3.org/Icons/valid-css.png" width="88" height="31">Valid CSS&#32;</object>

What I want the PHP to do is look for the data file (in this example, valid-css.png) and automatically fill in the mime type in the type attribute type="image/png"

Let's say that a forum user decided to embed a .gif image. How could I rewrite the following PHP (from ../include/parser.php) to automatically write type="image/gif" or write type="image/jpeg" for a .jpg?

Original:

if ($is_signature && $forum_user['show_img_sig'] != '0')
        $img_tag = '<img class="sigimage" src="'.$url.'" alt="'.forum_htmlencode($alt).'" />';
    else if (!$is_signature && $forum_user['show_img'] != '0')
        $img_tag = '<span class="postimg"><img src="'.$url.'" alt="'.forum_htmlencode($alt).'" /></span>';

My current modifications:

    if ($is_signature && $forum_user['show_img_sig'] != '0')
        $img_tag = '<object type="image/png" data="'.$url.'" class="sigimage">'.forum_htmlencode($alt).'</object>';
    else if (!$is_signature && $forum_user['show_img'] != '0')
        $img_tag = '<span class="postimg"><object type="image/png" data="'.$url.'">'.forum_htmlencode($alt).'</object></span>';

Re: Replacing <img> with <object>

You can determine the mime type of some files by the file extension. Use this code to extract the extension of a file:

substr(strrchr($url, '.'), 1)

Re: Replacing <img> with <object>

Thank you for the response. I tried to place it inside of the "type" attribute but it had no effect. Is there a particular way that you would recommend adding it into the rest of the code?

Re: Replacing <img> with <object>

Check for file extension looks like this:

$file_extension = substr(strrchr($url, '.'), 1);
switch ($file_extension) 
{
    case 'png':
        $file_type = 'image/png';
        break;
        ...
}

And add the file type to object tag:

...<object type="'.$file_type.'" ...

Re: Replacing <img> with <object>

It works. Thank you!