![]() |
|
|
|||||||
![]() |
|
|
Thread Tools | Rate Thread |
|
|
PM User | #1 |
|
New to the CF scene Join Date: Aug 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
Image size+dimensions check and replacing
I want a script that checks the image size and dimensions, for example 100x100 and 100kb, and if the image is more than these sizes, it replaces it with another one from url. Also the script must be limited to this class:
Code:
<dl class="post-sig"></dl> |
|
|
|
|
|
PM User | #3 | |
|
Senior Coder ![]() Join Date: Jun 2007
Location: Urbana
Posts: 1,802
Thanks: 4
Thanked 250 Times in 238 Posts
![]() ![]() |
Quote:
Code:
function fileSize( url ) {
var XHRt = !window.XMLHttpRequest ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
XHRt.open("HEAD", url, true);
XHRt.send('');
return XHRt.getResponseHeader("Content-Length")
}
//test for firebug:
alert ( fileSize(document.images[0].src) )
//result: 5063
========== img dimensions (WxH) are available as properties of the object. note that they will only be available after the image loads. you can create onload events for images, and direct traffic in the onload to handle the image based upon size, once that is known. one last caveat: firfox uses .naturalWidth and ie uses just plain .width, which is also available in firefox. in ff .width doesn't reflect the actual size of the image. EDIT: rereading the post, i bet the images will be loaded already. use getElmsByClass and gather up the images, then alter each img tag under each of those elements. Code:
function fileSize( url ) {
var XHRt = !window.XMLHttpRequest ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
XHRt.open("HEAD", url, true);
XHRt.send('');
return XHRt.getResponseHeader("Content-Length")
}
function getElementsByClass(class2find) {
var allTags = document.getElementsByTagName("*");
var tagListLength = allTags.length;
var goodTags = [];
for (var index = 0; index < tagListLength; index += 1) {
if (allTags[index].className.indexOf(class2find) > -1) {
goodTags[goodTags.length] = allTags[index];
}
}
return goodTags;
}
function resizeImages() {
var pops = getElementsByClass("post-sig");
for (var i = 0, mx = pops.length; i < mx; i++) {
var them = pops[i].getElementsByTagName("img");
if (them && them[0]) {
for (var i2 = 0, mx = them.length; i2 < mx; i2++) {
var it = them[i2];
if ( Number(it.naturalWidth || it.width) > 100) {
it.src = it.src.replace(/\.jpg/, "-sm.jpg");
}
if ( Number(it.naturalHeight || it.height) > 100) {
it.src = it.src.replace(/\.jpg/, "-sm.jpg");
}
if ( Number(fileSize(it.src)) > 100000) {
it.src = it.src.replace(/\.jpg/, "-sm.jpg");
}
}
}
}
}
if the images are outside your website, remove the file size checking part, as that only works on your files, not remote ones.
__________________
libs mini F ASPmini dnd OO(gen api) tmpl8 apps snippets blog Slides(demo maker) crypto image editor crapplets compressor prepage JSON(browser viewer) time notepad bench Last edited by rnd me; 08-07-2008 at 02:40 PM.. |
|
|
|
|
|
|
PM User | #4 | ||
|
Red Devil Mod ![]() ![]() Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 6,842
Thanks: 49
Thanked 219 Times in 215 Posts
![]() ![]() |
Quote:
Quote:
Again. For a 100% accurate size&dimension of the files use a server-side application. |
||
|
|
|
|
|
PM User | #5 |
|
Senior Coder ![]() Join Date: Jun 2007
Location: Urbana
Posts: 1,802
Thanks: 4
Thanked 250 Times in 238 Posts
![]() ![]() |
i'm sorry, i don't follow you.
as far as i can tell, it is an object: Code:
var t= new XMLHttpRequest
t.send.toString()
//returns: function send() { [native code] }
|
|
|
|
|
|
PM User | #7 | |
|
Senior Coder ![]() Join Date: Jun 2007
Location: Urbana
Posts: 1,802
Thanks: 4
Thanked 250 Times in 238 Posts
![]() ![]() |
Quote:
i would concede that its not a part of ecma script or IE3-6, but i the code i posted works in old IE (5+), as well as safari and opera. thus, frankly, i don't see the relevance of the post, and i feel it distracts from accomplishing the stated goal. i understand what you are saying, and in some senses i agree, but i don't see the point in the first place, or of a debate over semantics i tend throw around loosely. if was a little grumpy, its because it was early over here and i am out of coffee. i am sure you understand. ----- to the OP: sorry to hijack the thread. any luck in getting it to work? on further consideration, i would probably throw away the fileSize checker. i've had good results checking dimensions in current browsers, so that should probably be enough to filter by. |
|
|
|
|
|
|
PM User | #8 |
|
Red Devil Mod ![]() ![]() Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 6,842
Thanks: 49
Thanked 219 Times in 215 Posts
![]() ![]() |
ok now with the request object we agree. The problem is that an AJAX request is not practical in that situation. If an image or two, that will be done in a reasonable time, but for many images to load them via AJAX, to check their size, to replace (using javascript) the bigger ones... that will take some time...
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Rate This Thread | |
|
|