I have to display a bunch of images all of which may be of different dimensions on a webpage using AJAX.I want to adjust their size before displaying them. Is there any way to do this in Javascript. Using PHP's getimagesize() for each image is unnecessary performance hit since there will be many images.
Do you want to adjust the images themselves, or just the way they display? If the former, you want something on the server side. If the latter, you just need to change image.height and image.width.
...but... wouldn't it be better to adjust the image size on the server side rather than transmitting the bytes to the browser and doing it there?
When I say adjust the image size, I don't mean set the height and width in the HTML image tag. If you do that, you are still shipping a large number of bytes from server to client. I mean, actually manipulate the image itself server side.
I have .NET C# code here that takes that approach, but there must be a php way to do it too: http://ifdefined.com/www/gallery.html
Also, by doing it server side, that opens up the possibility of doing the adjustment just once and then saving the adjusted image, which would be very fast.
Well...there's several ways to interrpret this question.
First way, and the way I think you mean, is to simply alter the display size so all images display the same size. For this I would actually use CSS and not JavaScript. Simply create a class that has the appropriate width and height values set, and make all img tags use this class.
Second way is that you want to preserve the aspect ration of all the images, but scale the display size to a sane value. There is a way to access this in JavaScript, but I'll need a bit to write up a quick code sample.
Third way, and I hope you don't mean this way, is to alter the actual size of the image. This is something you'd have to do on the server side, as not only is JavaScript unable to create images, but it wouldn't make any sense, as the full sized image has already been sent.
Try this:
var curHeight;
var curWidth;
function getImgSize(imgSrc)
{
var newImg = new Image();
newImg.src = imgSrc;
curHeight = newImg.height;
curWidth = newImg.width;
}
It's worth noting that in Firefox 3 and Safari, resizing an image by just changing the height and width doesn't look too bad. In other browsers it can look very noisy because it's using nearest-neighbor resampling. Of course, you're paying to serve a larger image, but that might not matter.
My preferred solution for this would be to do the resizing server-side, so you are transmitting less unnecessary data.
If you have to do it client-side though, and need to keep the image ratio, you could use the below:
var image_from_ajax = new Image();
image_from_ajax.src = fetch_image_from_ajax(); // Downloaded via ajax call?
image_from_ajax = rescaleImage(image_from_ajax);
// Rescale the given image to a max of max_height and max_width
function rescaleImage(image_name)
{
var max_height = 100;
var max_width = 100;
var height = image_name.height;
var width = image_name.width;
var ratio = height/width;
// If height or width are too large, they need to be scaled down
// Multiply height and width by the same value to keep ratio constant
if(height > max_height)
{
ratio = max_height / height;
height = height * ratio;
width = width * ratio;
}
if(width > max_width)
{
ratio = max_width / width;
height = height * ratio;
width = width * ratio;
}
image_name.width = width;
image_name.height = height;
return image_name;
}
I was searching a solution to get height and widht of an image using javascript. I got many but all those solution only worked when image present in browser cache.
Finally I got one solution to get image height and width even image does not exist in browser cache. Just I want to share all of you.
<script type="text/javascript" language="javascript1.2">
var imgHeight;
var imgWidth;
function findHHandWW() {
imgHeight = this.width;imgWidth = this.width;return true;
}
function showImage(imgPath) {
var myImage = new Image();
myImage.name = imgPath;
myImage.onload = findHHandWW;
myImage.src = imgPath;
}
</script>
Thanks,
Binod Suman
http://binodsuman.blogspot.com/2009/06/how-to-get-height-and-widht-of-image.html