jQuery: preview images - javascript

I have a high-quality image and I want to generate img tag with this image.
The problem is that it will take a lot of time to load this image.
So the question is: how can I reduce image's quality without creating another one with low quality?

how can I reduce image's quality without creating another one with low quality?
And the answer is: you can't. If you want to reduce bandwidth you could provide thumbnails for the actual images. Those thumbnails could be dynamically generated from the original images using the server side language of your choice. If you don't reduce those images on the server, you are busted. Once you send them to the client you have already wasted the bandwidth. This reduction has to happen on the server if you want to gain something. You can always set the width and height attributes of the img tag and fool yourself that you are gaining something.

If you save the image, gif or png, with the 'interlaced' option set, it will load in progressively greater detail, initially blurred/fuzzy and increasing resolution as more data is received from the server.
This does have the principal consequence of increased file-sizes, though.
Reference:
Coding Horror, by Jeff Atwood.

Related

Is it possible to interlace every image on my website?

I am creating a website which contains posts. Every post has an image. Feed page of the website is slow because every image takes a lot of time to be downloaded and while it is downloading it is a blank space and it does not look nice. Images are uploaded by users and are of different formats. Is it possible to somehow make it so that images first appear blurry and then they start becoming less and less blurry as they load (I’ve read it is called interlacing)?
I have tried searching on the internet but didn’t find anything on the topic but the usage of .jpeg format which does not seem to work because even the .jpegs I have as post images are not loading interlaced and I cannot exactly force people to use only that format.
Images take time to load because they are relatively large files and the data in them has to be transferred over the network.
Interlacing is a a feature of the JPEG format that gives visible process as the image loads by reordering the image data so it doesn't run sequentially (Wikipedia has diagrams and animations which might be helpful in understanding that.)
If you want interlacing in the image then you need to make sure it is in the image. You can't really change that outside the image.
While it is impractical to ask users to upload images encoded that way, you can write server side code to convert images to meet you standards. When converting them you could also perform other optimisations (such as reducing the dimensions or increasing the compression level) that would speed up the load time. Of course, this will have an impact on image quality which might not be desirable. (Consider Flickr, for example, which provides a degraded image (with smaller file size) by default but allows switching to higher quality or original images on demand).
You could also consider displaying placeholder images while the full version loads. These could also be generated server side from the upload images and have extremely high levels of compression and resizing so you just display a small number of large pixels that give a vague impressions of the average colour over large chunks of the image until the real image is available to the browser.

Dynamically creating low resolution thumbnails from a device's file.uri for gallery images in a cordova project

I may be off the mark here but I wanted to find out if there actually is a solution to this problem before I try and solve it.
I'm using cordova on iOS and accessing the device's image gallery, fetching a photo's file.uri (the least memory hungry way according to phongap docs) and then rendering this into a roughly 50px x 50px image on the page.
The problem I've run into is that once I have loaded 4 or more photos onto the page, the performance starts to suffer, focus on other form elements becomes slow, and somewhat 'jerky'. I believe that this has to do with the fact that I am obviously rendering a high resolution image in a tiny box and when you have multiple high res images on one page you put a serious strain on the device's resources. This is on an iPhone 5, I would imagine the performance to be worse still on a 4s and 4.
Is there a way to programatically create 'low res' versions of the image(s) and then use these low res images on my page? In theory I have access to the image and its location, so can I not convert/compress/downscale these on the fly for the purpose of improving performance and then ditch these thumbnails/temp images once any upload of the actual image files is complete?
You could use a html canvas to create low res images.
Beter try this.
Take that image url in a string.
NSString *imgUrl=YourImageURL;
To get low resolution images you can divide the occurences of 1024x768 with 200x200.
NSString *strURLLowRes = [imgUrl stringByReplacingOccurrencesOfString:#"1024x768" withString:#"200x200"];
Now you can load the low resolution image.
Hope this helps you.

Showing a page content at once

I have a website which contains some images in index.php
The problem I am facing is the whole page is not loading at once, I think images are taking some time to load
So what I have done is, I am showing an loading image at first and then after some time I am showing the page, that resolves the problem. But I am curious to know is there any other better way to do this?
I prefer to optimise the hell out of my images.
PNG images
You can use pngcrush to optimise your PNG files for you, but personally I find that once I'm done with it pngcrush only succeeds in making it bigger.
Use Indexed-PNG wherever possible. This will limit you to 256 colours, and most graphics editors won't allow partial transparency in Indexed-PNG (but it is possible - you just need the right editor. I use a custom PHP script with the GD image library) but you can expect to drop file size down to just a tiny fraction of what it was.
Reduce the amount of colours overall. PNG compression works best with blocks of the same colour, so reducing the number of colours improves compression.
GIF images
Especially for animations, there's a lot of things you can do.
Reduce the number of frames. Avoid duplicate frames at all costs, and just set the previous frame to have a longer display time.
Use combine rather than replace if possible. You will again have problems with transparent areas, but by using combine you can have each subsequent frame only change the stuff that... changes. This avoids the redundancy of re-writing the entire image if only a small part changes. GIMP has a useful filter "Animation > Optimize for GIF" which will do this for you.
Reduce colours as much as possible. GIF is limited to 256 colours, but if you can limit yourself to 32 or so, you'll get a much smaller file.
Using the above techniques, I once managed to shove 8MB of raw image data into a 125kb animated GIF.
JPG images
JPG is great for photos, but cameras have a tendency to write MASSIVE files.
Play around with the compression factor. Start at around 40%, and slowly bring it up until it looks acceptable. GIMP will show you a preview and the resulting filesize, so make use of that to find an acceptable compromise.
Scale the image down. You don't need 9 megapixels or however massive resolution cameras take now...
The above should help you reduce the amount of size taken by your images. Obviously, you should also cache images appropriately, so they only need to be retrieved once. Also make sure that you specify width and height on image elements so that the browser can reserve the space for them and avoid jumping around as they load...
And you should be pretty good.
It's hard to say what other options are available without knowing what the page looks like, but one option is to reserve space for the images so that the page text renders quickly in the correct position, and the images then load later.

How to scale image with same quality using jquery?

I am having sequence of images in HTML. All are large size images, I am scaling theme in initial display and on click all enlarge.
So the issue is, when image is scaled down, it shows pixelated output. (See link)
If anyone have solution to have consistent output when image is scaled, please give suggestions.
I think you have to scale them not dynamically in the browser. Create thumbnails on serverside by using libs like imagick or gd.
The quality of an image scaled down by the browser will depend on the image scaler used by the browser. Most of these have no method of resampling, and simply remove pixels from the image to reduce its size - hence the pixellated look.
There is nothing you can do about this, other than to use separate images for the small and large versions.
As floating concept is there the resolution varies differently in system.So its better to use good quality image.
Check this link http://codecanyon.net/item/imageresize/59935
If it is useful for you tell me I can send you the code.

Can I resize images using JavaScript (not scale, real resize)

I need to dynamically load and put on screen huge number of images — it can be something like 1000–3000. Usually these pictures are of different size, and I'm getting their URLs from user. So, some of these pictures can be 1024x800 or 10x40 pixels.
I wrote a good JS script showing them nicely on one page (ala Google Images Search style), but they are still very heavy on RAM used (a hundred 500K images on one page is not good), so I thought about the option of really resizing images. Like making an image that’s 1000x800 pixels something like 100x80, and then forget (free the ram) of the original one.
Can this be done using JavaScript (without server side processing)?
I would suggest a different approach: Use pagination.
Display, say, 15 images. Then the user click on 'next page' and the next page is shown.
Or, even better, you can script that when the user reaches the end of the page the next page is automatically loaded.
If such thing is not what you want to do. Maybe you want to do a collage of images, then maybe you can check CSS3 transforms. I think they should be fast.
What you want to do is to take some pressure from the client so that it can handle all the images. Letting it resize all the images (JavaScript is client side) will do exactly the opposite because actually resizing an image is usually way more expensive than just displaying it (and not possible with browser JS anyway).
Usually there is always a better solution than displaying that many items at once. One would be dynamic loading e.g. when a user scrolls down the page (like the new Facebook profiles do) or using pagination. I can't imagine that all 1k - 3k images will be visible all at once.
There is no native JS way of doing this. You may be able to hack something using Flash but you really should resize the images on the server because:
You will save on bandwidth transferring those large 500K images to the client.
The client will be able to cache those images.
You'll get a faster loading page.
You'll be able to fit a lot more thumbnail images in memory and therefore will require less pagination.
more...
I'm (pretty) sure it can be done in browsers that support canvas. If this is a path you would like to take you should start here.
I see to possible problems with the canvas approach:
It will probably take a really long time (relatively speaking) to resize many images. Because of this, you're probably going to have to look into utilizing webworkers.
Will the browser actually free up any memory if you remove the image from the DOM and/or delete/null all references to those images? I don't know.
And some pretty pictures of a canvas-resized image:
this answer needs a ninja:--> Qk

Categories