Image Compression in PHP? - php

I want to compress the image with same quality using PHP.What is my situation is I want to display the user images when they loged in.Currently I am showing the original image whatever they uplaod in mysite.So if they uplaod 4MB file I am downloading the 4MB file and showing to them.Instead of that I want to compress the image with same quality.
I want to do it with same height and width also.Like smush it trying to do.But for 4mb files its not working with smush it.
Is there any way to do.How can I achieve it.

You can't compress an image maintaining the same quality and resolution. You can scale it down however (a 4MB JPEG file is pretty huge - if it's meant for screen use only, 1600 pixels or less image would do fine most of the times), using GD, Imagick, etc.

Try to convert them to different formats. gif is the best for small images. There are a lot of converting classes so just look around google

You should resize the images when they have been uploaded to your FTP server.
Of course you will have to deal with a loss of quality and resolution.
Have a look at http://www.white-hat-web-design.co.uk/blog/resizing-images-with-php/

Related

How to resize an image to a smaller size in codeigniter for a single Image upload?

I want to resize the images during upload to a small size so that they take less time while loading on the website.
And I also want to maintain the aspect ratio so that a portrait image doesn't get stretched and a landscape image doesn't get squeezed.That is without changing the actual dimensions of the image.
An Image Manipulation class exists in CodeIgniter and can solve Your problem. Just read the official guide to learn more.
First convert your images to Jpeg, gif and png images as these are compressed images.
In addition you can use ImageMagick to compress the image: Recommendation for compressing JPG files with ImageMagick

php - optimize image file size

I'm making a web application for online comic. My biggest difficulty things are how to save the disk space when store the images (i don't have money ^^). I try to use GD2 to resize the image, convert to jpg, reduce the qualities...but the file size of each image still around 100kb.
For example: MY IMAGE with 650x900 px with some character, text.., no color, just black & white, the file size is 100kb.
But, when i making a NEW BLANK file with the same dimensions in photoshop and fill it all black (i think black color will make the file size more higher), then save on jpg, this new file size just only 8kb.
The question is, why the file size of MY IMAGE (with less black pixel) is higher than the NEW BLANK image with more black pixel? Is there anything inside the image source? And any solution to reduce the file size?
Thank all.
Basically question one is what format are you using.
I suggest you use PNG for black and white images. If it is possible use 1bit PNG.
Image compression i a complex thing. But no not the black pixels make the file size, but the variation of the pixels. But it is really a complex question.
see: http://en.wikipedia.org/wiki/Image_compression and http://en.wikipedia.org/wiki/Portable_Network_Graphics for some info on the subject.
For the best results if your images are currently JPEG-s then you need to apply somekind of noise reduction. For truly black-and-white (not grayscale) bumping up the contrast might help with that. Also strip out all meta information from the image.
One helpful tool to lower file size of images without reducing the quality is TinyPNG. This can cut the file size by up to about 80% without making any noticeable changes to the image itself. It has helped me to cut down my file sizes by an average of about 70%, and I hope it can help you too!

PHP imagecopyresampled + how to get the best results

I have the following working fine:
User uploads an image and the image is resized using imagecopyresampled and imagejpeg (keeping the proportions) so it fits the cropping DIV.
The user then crops the image with JCrop and then the resized above image is then cut into the 5 required crop sizes using imagecopyresampled and imagejpeg.
The problem I've found is by cropping from the resized image doesn't take advantage of the very original image that is often larger and better quality.
So I want to display an image that fits the DIV but crop from the very original image to get the best quality.
When I just change the cropping to the original image the crop is of the wrong part of the image as the sizing is different. This is obvious I assume. Diff sized images.
How can I display an image that fits the DIV for Jcrop but then actually crop off the original which is often larger?
Ideas I'm testing:
displaying the original image in the DIV (not resized by php but resized by CSS). I'm not sure if this will make any difference.
try to use some math based equation to get the correct crop. Unsure how I'll do this currently.
Ideas would be great...
thx
Personally I would go with the maths based solution if you have to work with the full sized image server side. Re-scaling in the browser directly or through CSS is not ideal IMO and differs greatly between browsers. If you use the math based option you could calculate the co-ordinates on the smaller image crop and scale that up to get the right portion of the original image.
On a side note you may find (depending on the image sizes and image types you allow uploaded) that copying and resizing on the PHP side is very CPU and memory intensive. A png file for example of roughly 1024 x 768 in high resolution can take up to 60MB to 80MB of RAM just to resize (per image re-sample due to compression on the png file) and that is regardless of which PHP image manipulation modules (ImageMagick, etc) you use. There is no perfect way to handle image uploads from the PHP server end (other than throwing heaps of memory and CPU at the task). There are some good jQuery solutions however that resize on the client side before the upload (e.g. Plupload, etc) which means you will only be working with a reduced size image on the server side. There are also some JQuery client side cropping scripts which are good. Either way a combination of PHP and jQuery would be the best IMO.

php: Storing images in database

Currently i am using gd to converting the image uploaded in jpg with
imagjpeg($data,NULL,85)
and storing it in a blob mysql field. Problem is the quality. Yes the quality is just awful. A screenshot of a window with some text inside becomes pretty hugly. (While with a game screenshot the quality is passable)
Should I use other lib than GD?
Should I use other gd format? (imagepng?)
ps. please note: this is not a duplicate.. i am not asking how to save an img to db
ps2. note that if i should change my currently format/method I have to convert something like 5000-6000 images already stored as jpg in my db
ps3. i use 85 for imagejpeg, can't use a higher value because the image size would grow more than the original version
ps4. I need to store image in db, please stay in topic
Thanks all
================================
REFORMULATING THE QUESTION
I save image (mostly software screenshots, sometime games screenshots) in BLOB field after doing an imagejpeg($resourceFromImageCreateFromString,NULL,85);
Problem I am experiencing is the image uploaded looks pretty bad in confront of the original even if it's the same size. (sometime it gets bigger because of 85)
Is the imagejpeg implementations that sucks?
ps. my simple upload script does the follow calls:
upload file
open it with "rb" (binary)
pass the data to imagecreatefromstring
pass the result to imagejpeg
Thanks again
Is there a special reason you want to store your image inside a BLOB?
What quality argument did you pass to imagejpeg()?
You can set the quality of your output with the 3rd parameter to imagejpeg(). The problem is that JPEG is not the best format for images with a lot of text - you have to choose between turning down the quality and getting a lot of artifacts in your image, or turning up the quality and having a large file size. You might consider using PNG instead.
my first recommendation would be to try saving it as a png as the quality is better.
my second recommendation is to try the quality parameter in imagejpeg($image, $filename, $quality).
recommendation #3 - I worked on a site that I had to rewrite the image storage system because they implemented just what you are doing, storing images as BLOBs in the database. Their system only had about 20K images in it and it was crashing weekly because the database could not handle the load.
The conversion process for 5-6K images should not take more than a couple hours no matter how large the images. When I converted my images to disk storage from db storage, I ran 1K images at a time and each run took 5-10 minutes.
Why do you re-code anyway if it already IS a jpeg? To save space? Well, but then you already have your answer, in part - to save space from jpg to jpg you have to save in lower quality.
You cannot specify quality with imagecopyresampled, with imagejpeg you can. Use a high(er) value for the quality parameter.
Please note that JPEG is a format optimized for storing FOTOS. Images with large areas of the same color, straight lines etc. are much better stored in a lossless format like PNG. Of course, if you have a screenshot taken from a screen with fotos as desk background you have elements of both - so choose your format according to what you want. If the foto-part is more important and you don't care as much about fonts and lines, go with jpeg. Of course JPEG can be just as good quality as PNG for screenshots as well, but then you miss out on most of its compression capabilities, you have to use a 90+% quality setting. At 100% JPEG too is lossless, but by then a PNG might actually be of smaller (file)size.

PHP JPEG Crop : Loss of quality?

I'm developing a web-to-print, poster printing application.
I'm considering using PHP to crop user-uploaded images, and we will ultimately print the PHP-cropped image.
My concern is there will be a difference in 'quality' between the original user uploaded image, and image after it is cropped by PHP.
Will PHP affect the quality of the image when handling it? Or does it preserve the image's quality and simply crop the relevant area?
Many thanks,
BK
JPEG is lossy compression. A bit of oversimplification, but it works by analyzing pixels around other pixels to see how similar they are. Not every pixel is stored, and that means it isn't possible to simply chop bytes out of the image data to perform the crop. If you are outputting JPEG, you will be re-compressing an already compressed image, and you will have some loss in quality. However, if you crop the image and your output is a non-lossy format, then you will not have loss of quality.
To be clear, the loss of the quality isn't in the crop operation. It is in the way the image is compressed itself. If the source image is compressed with JPEG, quality has already been lost. When you crop that image, you aren't losing anything more, but if you were to output JPEG again afterwards, this would require a re-compression, and thus more loss.
So in the end, make your final output PNG or something non-lossy and you have nothing to worry about.

Categories