views:

57

answers:

2

Hi,

I'm looking for the best solution to dynamically change some images extension (from .svg to .png) only if the browser is IE.

I don't know what's the best solution :

  • parse the html code with PHP
  • use jQuery with something like

    $("img.svg2png").attr("src", ...);

  • dealing with htaccess and rewrite rules

  • other solution ?

Thanks !

+3  A: 

You're not revealing many details about what you're doing, but the mod_rewrite solution (catching the USER_AGENT variable and checking whether it is IE, and redirecting internally to the matching .png file) sounds the most elegant to me, because it works sans JavaScript, and you can keep the file extension. The .svg extension should be meaningless as long as the right Content-Type header is sent.

Pekka
@Pekka I have to transform my <img src="img/img1.svg" class="svg2png" />to <img src="img/img1.png" class="svg2png" />I'll give a try to your solution, thanks !
Kaaviar
@Kaaviar you're welcome. Using mod_rewrite, you wouldn't have to transform URLs. It would always be `img1.svg` but if its IE, a PNG would be served under that name.
Pekka
Sorry, this doesn't sound very professional. Why rely on the user agent sniffing, when you can use content negotiation. That's at least what Wikipedia does.
mario
@mario if it's possible to get reliable results using content negotiation, great! Why not add an answer outlining how it's done? I'd be interested to see it.
Pekka
@Pekka: Well okay, scrape that. Even my Firefox sends a silly header. Accept:image/png,image/*;q=0.8,*/*;q=0.5 - so PNG would always have precedence over SVG in Firefox. Might be possible to use qs= values with a .var typemap for Apache, but that's too much effort. So I wonder how Wikipedia does it after all.
mario
@mario maybe they mix both approaches... Interesting. The Wikipedia Article http://en.wikipedia.org/wiki/Scalable_Vector_Graphics doesn't go into detail on how they actually do it.
Pekka
A: 

you could do this:

var imageArray = $('img.svg2png').attr('src').split('.');
var newImage = imageArray[0]+'.png';

$("img.svg2png").attr("src", newImage);

keep in mind that this is assuming that you only have 1 period in the full src of the file (that being extention period)

to break it down, what .split('.') does is create an array of strings where the period is a delimiter. so

imageArray[0] = 'imagesDir/imageName'

and

imageArray[1] = 'svg'

so what you are doing is reconstructing the src with the first part of the image and a new extention.

hope this helps!

JSD