Upon seeing the Fluxiom intro video, I was compelled to figure out how they pulled off iPhoto-like image scaling in a browser. Leveraging the work of others, it’s actually very simple.
If you use the script.aculo.us slider control to capture input values, it’s really just a matter of converting those values into something useful and modifying styles.
Note: OS X folks using Firefox will likely get some stutter, due to a crippling, 3 year old bug.
A simple demo.







Here’s how.
1. Install and reference the Prototype and script.aculo.us libraries.
2. Create the track and slider HTML nodes. (CSS inline for simplification)
<div id=”handle1″ style=”width: 18px; height: 18px;”>
<img src=”/images/content/blog/scaler_slider.gif”/>
</div>
</div>
3. Create a function to be called when the slider value changes. This collects all nodes for resizing, remaps the 0-1 scale to a definable range, and then scales.
var scalePhotos = document.getElementsByClassName(‘scale-image’);
floorSize = .26;
ceilingSize = 1.0;
v = floorSize + (v * (ceilingSize – floorSize));
for (i=0; i < scalePhotos.length; i++) {
scalePhotos[i].style.width = (v*190)+’px’;
}
}
4. Create the slider and map the two events to our function (either inline, or as part of the body onload event). See the Slider docs for usage details.
{axis:’horizontal’, minimum: 0, maximum:200, alignX: 2, increment: 2, sliderValue: 1});
demoSlider.options.onSlide = function(value){
scaleIt(value);
}
demoSlider.options.onChange = function(value){
scaleIt(value);
}
5. Finally, create the HTML for our images.
<div class=”scale-image” style=”width: 190px; padding: 10px; float: left;”>
<img src=”/images/content/blog/scaler_1.jpg” width=”100%”/>
</div>
<div class=”scale-image” style=”width: 190px; padding: 10px; float: left;”>
<img src=”/images/content/blog/scaler_2.jpg” width=”100%”/>
</div>
</div>
There are definitely some areas to clean up (redundant div width specifications, for example), but for a quick demo it will suffice. Tested with Firefox, Safari and Win IE 6.
Update: There appears to be a small issue in IE; the slider disappears when you start dragging.
Update: Fixed. Moved the handle image to an actual img.
Update: Corrected a typo. (Fluxium != Fluxiom)





Well, its possible. In Firefox you can use the max-width/max-height styles to limit the size of an individual image. Unfortunately, IE ignores this super-useful style.
I haven’t tried this, but if you want to support IE and you don’t mind something a little less elegant, you can pursue this approach. Remove the width=”100%” from the images and scale the images themselves. While scaling the images either give them the scaled width or their max width, whichever if smaller. This implies that you will need to store their max width somewhere (in the ID as a previous poster suggested, in the image name, in a JS arrary, etc). You will likely still need to scale the DIVs themselves to keep the images nicely arranged in a grid.
Happy coding!
Alright everyone,
I’m making a picture viewer for my personal website at the university.
I’ve no real knowledge of javacript other than what people have prepackaged. (I can change the target and links but creating the script… not gonna happen.)
ANyway, I’m wondering if there is a way that a picture can be put into a table so that it will be as big as possible without being bigger than the window. (Like the auto resize option in IE when only veiwing an image… and one can expand it if one wants.
Is it possible to make the value of the window size to be the value of the picture slider?
Could you possibly link a zip file with all the required pages and a simple example of the post? The same prototype file and the same scriptaculous file that you used? I have tried following your instructions as well as making copies of some of the other posted test pages but cannot get this example to work. All I get when I click on the slider is a circle with a line through it. I have all of the appropriate files linked and in the correct directory but cannot figure out why the slider is not activated. I would appreciate any help that you could provide. Also, thanks much for the great article.
I’ve got it working, but I’ve got a problem, in FireFox (or any other Gecko based browser) the pictures do not align correct when using pictures with different heights, see screendump:
http://www.shrani.si/pics/photo-re214009.jpg
Is there an ‘easy’ solution?
Thanks,
Kasper
Here’s another working example, this time using images of mixed width and height (some landscape some portrait).
There are a few bugs like the fact that until the images have all loaded the layout is distorted but other than that it seems to be working fine.
http://www.photostream.info/image-scaler/
After a lot of tweaking, I’ve partially integrated the image scaler into my personal website and added a cookie function to save the previously set image size:
http://www.interplod.com/
Here is my code and I would really appreciate if someone can enlighten me why my slider is not working?
It does load all the pic however I can’t move the slider towards my right. Its just static does not move at all. I am new to this and yours HELP would be great.
Also I am going to load lots of pictures. Is there any way I can seperate pictures according to specific evenets? Such as NewYears Eve (this will have all the pictures from New years Eve 2006). Then Baseball events for Spring 2006 etc.
Please need yours help.
Thanks
________________________________________________________________________
Vertical Images Demo
Vertical Images: Demo
_______________________________________________________________________________
Do I need to create this slider_test.css file? If so what would be the content of this file.
Thank you
This is awesome. Thank you so much. I’ve got just the palce for this in my bag-of-tricks.
=C=
I thought the same thing after seeing the fluxiom movie, i figured it all out in my head though.
This is excellent. I may add this to one of my blogs just to say that I did it =).
Thanks John for a wonderful tutorial.
Can’t wait to try it out.
Frank
very interesting stuff…thanks for the resource
I’m having a problem
THe images are added to the page dynamically using ajax. Everything works fine in firefox but in ie6 only the very first image is resized
Any ideas, Thanks
My code:
function resize(v){
floorSize = .3;
ceilingSize = 1.0;
v = floorSize + (v * (ceilingSize – floorSize));
for (i=0; i
Rest of code:
for (i=0; i = scalePhotos.length; i++) {
var prev = container[i].style.height;
scalePhotos[i].style.height = (v*190)+’px’;
var scale = container[i].style.height/prev;
container[i].style.width = container[i].style.width/scale+’px’;
}
}
//The below code is only run after the page has been updated//
var demoSlider = new Control.Slider(‘handle1′,’track1′,
{axis:’horizontal’, minimum: 0, maximum:200, alignX: -5, increment: 1, sliderValue: 1});
container = document.getElementsByClassName(‘photocontainer’);
scalePhotos = document.getElementsByClassName(‘photo’);
demoSlider.options.onSlide = function(value){
resize(value);
}
demoSlider.options.onChange = function(value){
resize(value);
}
Great stuff.
I did a version of my own, which can be seen on my site (link below).
It can deal with images of different sizes (thanks to Jamie Longstaff’s demo for giving me the idea on how to do that) and works in the three major browsers.
I wrote the slider script myself (for the sense of satisfaction
).
Thanks for the inspiration.
http://www.theejit.com/slider/
Amazing stuff Really good one
I know you didn’t invent this control style exactly but you inspired my Flash version here:
http://pixelfumes.blogspot.com/2006/04/tray-style-resizer.html. Thought I’d share.
-Sarge
I wanted to do an image resizing site, and I started thinking about how to do it. Then, I remembered this technique.
Your ideas helped me make my idea better.
thank you.
Nice example! I like it a lot but why not just resize the images instead of having them resized by divs?
Nonetheless good work…
Anyone know how to do something to select divs like this?
http://blog.fluxiom.com/2006/3/29/assets-selected
This powerful and flexible, yet easy to use utility will help you to resize thousands of your pictures. Media Resizer quickly and easily prepares your image collections to be published on the web.
http://www.purchaseshareware.com/multimedia-design-media-management/media-resizer5201-1.htm
I wanted to do an image resizing site, and I started thinking about how to do it. Then, I remembered this technique.
Your ideas helped me make my idea better.
thank you.
Thank you very much for this script resource.
Is it possible to place a link to this resource at
http://www.featurepics.com/news/PhotographyTips.aspx?
(Photography tips)
nice resource…thanks!
Very nice and amazing effort. Keep it up.
Nice example! I like it a lot.
http://www.epeaksoft.com/service/odc.htm
for some reason i got the error msg:
this.track has no properties
in the slider.js on row 175
anyone know why?
Great tutorial, worked well for me on my new site.
well i have implemented that on my site, offering free image hosting… altered bit of the code to fix the portrait and landscape images to fix in a specific space…
very beautiful code
http://www.aalaphotos.com
Excelent
hello nice work keeptup
great site and interesting too.
Very nice! Thanks a lot
Works great on Firefox 2 , but slow on IE7…
Good Stuff !! Thanks for sharing.
hey
cool tut
does this work for videos as well??
thx
looks great and working allmost perfectly becose it has some bugs , on Opera 9.20 the slider is working in mirror mode and on IE7 Mozzila and Opera the small image inside the slider it cannot go to full right
maybe you can help with this issue?!
thanks