How to compress any picture? - java

Our site have a posibility to load user pictures. User can load absolutely any file(we believe that it will a picture at the moment). On page we show a lot of pictures thus we have a huge internet traffic. we want to compress pictures on server. I have found following article about picture compressing.
How to compress jpg file?
But there are explained how to compress concrete format. Is there universal way for compressing any picture?

A picture is a very broad term that could also include vector graphics, pictures that are in a format that browsers usually can't display (like psd files).
I would recommend to use the gd lib or imagemagick on the server side to convert them all (or at least these that are supported by your choice) to a standarized format (like jpg) that can be displayed in a webbrowser and get compressed (maybe also resized).
100% of possible image files a user could upload can't be converted afaik.

Related

Speed optimization of Website with a lot of images

I am currently working on a website which involves a lot of images. The problem is all the images are uploaded by the user so I can't do anything to alter the images. The website runs quiet ok on local system but the speed drops too much on the server,it becomes too slow
I'd suggest you to use Timthumb. It creates a thumbnail by generating a URL on the fly and uses very minimal disk space.
If the users of your website are uploading the images, then I presume (there must be) an upload script. Inside of that script or directly after its execution you could compress or rescale the image to size needed on the website, shortening loading time. There is a PHP image processing library called ImageMagick here:
http://php.net/manual/en/book.imagick.php
There is the PHP GD image processing library here:
http://php.net/manual/en/book.image.php
I don't have much personal experience with them, but from my knowledge it looks like one will do the job. Off the top of my head, that's the best solution I can think of, and hopefully it works. There is not a lot you can change about your problem if you don't compress/scale the images, and these are probably your best options. Wish you the best.

Optimize images - Losslessly compress images in Java

Having an ecommerce website, We have thousands of product images. On checking pagespeed on google it shows me something like this:
I was wondering, if there is any built in feature in Java or any third party library is available with which we can losslessly compress all the images that we host. Hence we can save few KBs of our customers.
On searching through internet I found few like punnypng and kraken which are paid, hence we do not have heavy image uploaded every month, subscribing to them is not worth. I would prefer any built in feature in Java or any open source third party library.
I came across JAI, but not sure about whether it addresses this problem or not. Anyone with hands-on experience with this?
P.S. We are using Java 8
Have you looked at classes in the javax.imageio package (http://docs.oracle.com/javase/7/docs/api/javax/imageio/package-summary.html) ?
You can do decoding and re-encoding of the images. The class ImageWriteParam (http://docs.oracle.com/javase/7/docs/api/javax/imageio/ImageWriteParam.html) lets you customize the compression settings.
~600 KB jpeg images are quite large for screen, though not for print. Having several images on a page would mean making more or less "thumb" views being smaller. And provide an individual product page with higher resolution, say 600 KB JPEG.
Standard ImageIO suffices for conversion, see #NicolaFerraro.
Faster page loading can be achieved on the overview page with multiple images, by storing the smaller views into one large image. PNG might then be appriopriate to prevent JPEG artifacts.
To provide a higher resolution for a print, one can use the CSS media setting.
Check thumbnailator. It is great at making smaller images from larger images.
Besides you should consider when you are going to make these smaller images. At each call, at the first call (any keeping a cache), ...

What format of large background image provide in jar?

I have Java Swing application with maximized bounds for JFrame. The application is distributed as jar file up to now.
Initially for background image I have used some large arbitrary image file of png-format. And I do have a code which can load and set image as background for JFrame.
Now, I got a new image from designer in both cdr and png format. Png image is 5 by 4 thousand pixels now.
My requirements:
to minimize image file size
to support different screen resolutions
What would be the best option to meet requirements?
Resize file to 1920x1080 and keep as png, and resize at runtime to smaller images if needed according to resolution
Keep file as cdr or some other vector format. But I am not sure Java can easily read and convert it to BufferedImage
If you have the opportunity to get the file in .svg this will fit your needs the best. You could then have a look at Apache's Batik library and its rasterizer to render the image.
What would be the best option to meet requirements?
Resize file to 1920x1080 and keep as png, and resize at runtime to smaller images ..
..or larger - time goes on, screen resolutions get bigger (& bigger)..
.. if needed according to resolution.
Yes. I think that is the best option.
Loading a library to render an SVG or EPS to BufferedImage at run-time seems like overkill for a BG image.
Deployment
The application is distributed as jar file..
Applications that use images often have a GUI. Does this app. have a GUI?
If so, it would be optimal to deploy it using Java Web Start.
The Jars for JWS can be downloaded lazily and programmatically, and are refreshed when updated, so the image should probably be in a separate Jar of its own to avoid downloading a new version every time there is a bug fix. When putting an image (or compressed media files in general - sound, video,..) use 'no compression'. Zip does nothing good for these already highly optimized, or at least very specialized, data formats.

java library to create, convert or edit images

I have to create thumbnails with images uploaded by my users. The image formats can be PNG, JPG, GIF. I gave a try to java.awt and javax.imageio but it is hard to deal with all the cases (image too large, image too small, image in XXX format, image with transparency...). I would prefer a library simple and not so verbose.
What java library do you use to convert / edit / create images ?
you might like to explore JMagick
Well, we use ImageIO and Apache Sanselan, but JAI or - if you don't mind using native libaries - jmagick should do as well.
image too large, image too small, image in XXX format, image with transparency
I'm not sure there is a library that doesn't have constraints like that. Since the images might be PNG, JPG or GIF, the format problem shouldn't apply.
Transparency is format dependent and thus should not depend on the library.
I chose Scalr, not need for extra ImageMagick or JNI wrapper :
http://www.thebuzzmedia.com/software/imgscalr-java-image-scaling-library/
On the other hand, quality is better with JMagick (if you can compile it and make it work)
Snowbound has the RasterMaster Java Imaging SDK. There is a decompress_bitmap call that will read in PNG, JPG, GIF and many other formats. The IMG_resize_bitmap_bicubic method can then be used to make a nice thumbnail.
You can go to Snowbound and push the "free trial" button to get a 30-day free trial version. The SDK comes with a Thumbnails code sample. If you want to learn more about the methods above before you expose your email address you can view the online documentation at RasterMaster.com.
Full disclosure - yes, I work for Snowbound.

Process the file before uploading it

I got a form with SWFUpload.
Files uploaded to the server are converted server-side (video being compressed, images being resized etc)
Question is - can i delegate some of the functionality to client-side (like image resizing), to save some bandwidth for user.
Video compression via Javascript would probably slow the browser so much that it wouldn't be bandwidth savings and if anything would probably annoy the end user.

Categories