Ian
Site Administrator
Registered: 28th Aug 99
Location: Liverpool
User status: Offline
|
Using this to read image dimensions -
function (fileUrl) {
var img = document.createElement('img');
img.src = fileUrl;
img.onload = function () {
var height = img.height;
var aspect = width / height;
width += 16;
height += 18;
window.open(fileUrl,'_blank','width='+width+', scrollbars=yes, height='+height);
};
}
Few extra checks on the production code to make sure it doesn't make a massive window. Works fine for images but it needs to also work for stuff like PDFs, text files etc, anything that can be displayed inline in the browser. Obviously those other file types can't be loaded in to an img element but rather than doing it anyway and then returning null values for the width and height like a proper programming language would, stupid Javascript just doesn't run at all when you give it non-image URLs.
Is there a way which isn't checking the extension for the presence of the strings jpg, jpeg, gif, tiff etc. which I can test and put some default dimensions in there?
Or other approaches. Just uncomfortable overloading the img element if it's going to cause some browser somewhere to crash massively or give out errors about how shonky that approach is, when in fact it's perfectly acceptable to me.
[Edited on 28-08-2011 by Ian]
|
AndyKent
Member
Registered: 3rd Sep 05
User status: Offline
|
I'm no expert, but what's wrong with splitting the string?
Won't any other solution involve the server loading the file to find it's type = slow?
|
Reedy
Member
Registered: 11th Apr 04
Location: Hammersmith
User status: Offline
|
To be honest, i dont think there is a way of getting around that. I usually use php to detect the dimensions of images byt like you mention, you want to be able to detect it for pdf's and stuff then i dont think you can.
Just on the off chance, have you tried <object> instead of img and see if that works?
|
Ian
Site Administrator
Registered: 28th Aug 99
Location: Liverpool
User status: Offline
|
Removed the img.onload and just tested the width attribute straight after the src is assigned, and it is now returning zero for the PDF as opposed to not running.
|