I have a problem with resizing images.
What happens is that if you upload a file larger than the stated parameters, the image is cropped, then saved at 100% quality.
So if I upload a large jpeg which is 272Kb. The image is cropped by 100 odd pixels. The file size then goes up to 1.2Mb.
We are saving images at a 100% quality. I assume that this is what is causing the problem. The image is exported from Photoshop at 30% quality which reduces the file size. Resaving the image at 100% quality creates the same image but I assume with a lot of redundant file data.
Has anyone encountered this before? Does anyone have a solution?
This is what we are using.
$source_im = imagecreatefromjpeg ($file);
$dest_im = imagecreatetruecolor ($newsize_x, $newsize_y);
imagecopyresampled (
$dest_im, $source_im,
0, 0,
$offset_x, $offset_y,
$newsize_x, $newsize_y,
$sourceWidth, $sourceHeight
);
imagedestroy ($source_im);
if ($greyscale) {
$dest_im = $this->imageconvertgreyscale ($dest_im);
}
imagejpeg($dest_im, $save_to_file, $quality);
break;
Saving at 30% then re-saving at 100% will, as you say, create redundant data, whether you crop, resize, whatever.
Unfortunately, JPEG compression accumulates data loss, so compressing at 30%, processing the image, then re-compressing will always look worse than the original compression. The rule of thumb is to avoid compression (especially heavy compression like 30%) until as late in the process as possible. So upload at 100% (or 80ish% if necessary), then compress.
Edit: apparently, jpegtran (google it) can do operations such as cropping without first decompressing the image, as long as the image size follows certain constraints (usually width and height a multiple of 16 pixels). Haven't tried it myself, but it might suite your purposes.
When saving a JPEG with 30% quality, a lot of pixel information is not saved. When opening it again using gd, a new image is created with crisp new pixels. Whether these look good or bad to you (depending on the quality the image was originally saved with) is irrelevant. When saving this new image, you're create a new JPEG file. Setting the quality to 100% will basically save every single pixel, which of course takes a lot of space. (I'm generalizing, but you get the idea.) Whatever quality setting the original was saved at is irrelevant, saving a big image at 100% quality takes a lot of space.
The only solution is to save the image using a lower quality setting; usually something around 70% is virtually indistinguishable from perfect, but saves a lot of bytes. You may also want to try PNG, which is lossless but may (or may not) provide a better compression ratio.
Your assumptions is correct, you are essentially saving it at loss-less. I ran into this with video encoding where I would downsize the resolution but increase it to loss-less and the final size was twice that the original. If it is a small enough image I would save it using a good palette based image type or a type that is native loss-less not jpeg. Either that save it at 50-70% quality and let jpeg do what it is good at.
Related
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!
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.
When I Use:
imagecreatefromjpeg() and the $path parameter is a URL. I save the jpeg at 100 compression... Yet I still get slight quality loss...
Is there a way to prevent this?
There is always a loss in quality when you re-save a jpeg image, you should instead copy the image file.
all jpg images will have artifacts, no matter the compression. if this is a picture with lots of colors and no real sharp edges it shouldnt be a problem, however if its a logo or something where edges are important you should consider using the PNG alternative.
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.
How would one compress GIF image file in PHP5 ?
I know that its possible to do with JPG like this imagejpeg($resource, $filename, $quality)
according to http://us.php.net/manual/en/function.imagejpeg.php
Gifs certainly are lossy and you absolutely can compress them - quite significantly. Here is the PHP code:
$img = imagecreatefromstring(file_get_contents($_FILES["my_field"]["tmp_name"]));
imagetruecolortopalette($img, false, 16); // compress to 16 colors in gif palette (change 16 to anything between 1-256)
imagegif($img, $destination_filename); // $destination_filename is the location on your server where you want to save the compressed gif file
Thanks to Mario, from this link for the first line above: Convert JPG/GIF image to PNG in PHP?
Many people are claiming gifs are lossless, which is not correct. Gifs most certainly can lose data. Loss occurs at the point of save, not at the point of file open, and gif loss behaves differently jpegs loss, which is where people are getting confused. A little bit of reason would also tell that almost EVERY image type that is not a bitmap is indeed lossy, otherwise we may as well just use the bitmap. Beyond that basic understanding, image compression is not the exact science that all these expert "bloggers" are promoting, and programmers will do well to study the data formats and compression algorithms in depth (pun intended), and on their own.
This article http://searchcio-midmarket.techtarget.com/definition/lossless-and-lossy-compression flatly and falsely states "the Graphics Interchange File (GIF) is an image format used on the Web that provides lossless compression." That's like saying, "all rectangles are squares". Some rectangles are certainly squares, nevertheless, the statement is, by logic, 100% false. If you have a black and white image, you can certainly get away with a lossless gif image, but gifs are by no means "lossless". Furthermore, saving a jpeg file at 100% quality is also lossless, but can create a larger file than the original.
As a VERY oversimplified rule: for high speed transfer, use gifs with small palettes for high contrast images (like an image of a black notebook on a white table); use jpegs between 50%-70% on very low contrast images (like an image of a forest); and use gifs with large palettes or jpegs with 63%-85% quality on medium contrast or mixed contrast images (such as images of you and your friends).
LZW compression (for Tiff files) is amazing, but keep in mind it is possible to compress an image and end up with a translation dictionary that, in addition to the compression data itself, is bigger than the original image. Image compression is very complex and no one size fits all, therefore avoid blanket statements when discussing image compression. There's no such thing as the "right" option, only better options.
JPG is a lossy compression. This means that you can use "quality parameter" to adjust size / quality ratio.
GIF is a lossless compression, you cannot get better compression by adjusting quality.
To create a GIF image use imagegif(...)
http://us.php.net/manual/en/function.imagegif.php