How to determine whether an image is optimized? - php

Ive got a strange task. Specifically i want to check for insane media file sizes for now. If possible, it would be nice if i could take image dimensions into consideration as well. For example, it's acceptable if a large image takes up 80kb, but not for a 20x20 px image.

Going off #BoltClock's comment, you would need to define what makes an image optimized vs non-optimized.
Something like a definition table to go off of in your validation:
Maximum image size = 1MB X 2000px x 2000px
Relative Ratio of size to dimensions of file
The above could be checked easily (without GD for instance) using something like the getimagesize() function.

Related

PHP Reducing Image Quality until filesize is upto say 1mb

I am working on one of my project where user will upload the image. The uploaded image will be displayed using lightbox.
The problem is that user may upload an image of size say 5mb and so on. Because of this it takes large loading time. So I thought to reduce quality of image keeping the dimension same.
I know that we can use, imagejpeg() function, and pass third parameter which is quality say 90, that also reduces file size.
I need all image file size to be max 1mb, not more than that.
So, I am confused in what value should be pass as quality, so as to have optimum quality.
For eg. if uploaded image file size is 1.2mb, then say I will pass 90 as quality, that may bring down the size to less that 1mb, and also quality will be acceptable. Another case if uploaded file size is say 5mb, than if I pass 90 as quality, then file-size may not be less than 1mb. Here I need to pass less quality value (I guess).
So is there any method that helps me in determining optimum quality parameter that should be passed.
Many thanks for your time.
I spent ages messing with this same sort of thing a while back. First of all PNG and JPG formates i found to be quite awkward in guessing the results. JPG photos generally wont convert well to PNG, but vector jpg images will, vector png generally dont convert well to JPG, but photos will. You will find it very hard to ensure that the images get just under 1mb especially if you are not resizing them as well. If you do not resize them you will also hit more troubles - the quality will become less and less the larger the image is (kind of ironic).
You need a few versions of each image. Let me explain.
You want to keep the original, so you can refresh the images based on the original at any time in the future to lets say, increase the quality of the images later on or even get some new dimentions.
Using the original image with lightbox would generally be a bad idea. You would be better to resize it to fit inside say 1024 x 1024. That would more than satisfy most, and would usually give a reasonable file size. You could then offer the original to be downloaded if you wish. Using the largest file possible would usually result from unhappy visitors when the image takes ages to load and could even cost them a lot after some time through bandwidth usage (and you for the same reason).
The only way i can think of to do what you are asking is to create some horrible code performance wise that loops through, gradually decreasing the quality and checking the resulting image size. Once it finds the image size that is below 1mb it comes out the loop and uses that quality setting. I would really advise against this though.
P.S. Outside the scope of this answer i have mentioned in the comments the methodology of a solution i used.

Determine if images are the same, but different sizes

I'm currently dealing with a large number of images (over 3000), and I have two copies of each; one large, one small. The problem is I don't have any kind of link to say which small image maps to which large image.
I know roughly which large image goes to which small image (I have an array which contains up to 5 possibilities for each small image), and I'd like to loop round the array and compare each large image to the small image, to see if it's the same image but resized.
TLDR/Didn't Understand: Is there any easy way in PHP to compare two image that are the same, one being (say) 200x200 and the other being (say) 500x500, and determine if they're an image of the same thing?
The easiest way would be resize both to a small size (16x16 or 32x32 for example depending on how much CPU you want to use keeping track of the names of each file throughout the process.
Then use imagecolorat() to compare pixel colours through each row/column. You can then define a percentage of which need to match to be considered the same picture.
Edit: here's an example of this implemented http://www.thismayhem.com/php/comparing-images-with-php-gd/
Have you tried if resizing 1 of the big image creates the exact same image as one of the smaller ones(manually use Md5 to find that out).
If it does, then your solution is to batch resize the bigger images to a tmp file and md5 it building an array of bigimagemd5s and then you md5 the smaller ones.
This method will only work if the small images were generated using the php GD algorithm and you have the same compression level and size that was used before.
A thing you could do is, take the big picture, scale it to the size of the small picture and check some random pixel-areas there. If the pictures are really different, you could use something like a avg. colour of both images.
But this way is really performance sucking...
No, there is not a same_image_but_in_different_sizes() function that returns true or false.
If I were you, I'd either sort them manually or choose a color histogram based approach.
I just saw that all "images are JPGs, and they're saved in two folders; large and small". Why don't you just throw small away and recreate it, but this time you remember which belongs to which?
I would use the imagemagick library for resize and compare the images. I would call it using exec()
First, resize the large image using the convert command http://www.imagemagick.org/script/convert.php
Then, compare the small image with the resized image using compare command http://www.imagemagick.org/script/compare.php
to sort them manually will take you much, much less time.
Please check this link:
Compare 2 images in php
It seems that using ImageMagick extension is your best bet, although you'd probably have to scale down the larger image to the same size as your smaller one first.

How can I estimation the file size of an image using its width and height?

I have recently written an image resizing program using php, which works by downloading images off another server, resizing them and saving them to our own.
The bad news is that my Hosting account only allows a php memory limit of 64M, and this is just not set up to resize the HUUUUGE file sizes that my client is uploading (3 - 4mb). It spits out a fatal error if it meets these images and breaks the script.
Even though I have notified said client of this drawback, said client continues to upload large images and script keeps breaking.
I can obtain the width and the height of the image before downloading it using getimagesize(), and if I could use this info to work out the total file size I could break out before the image resizer gets going and suppliment the image with a nice "no image available" alternative.
How can I make an accurate estimation of an images file size using its width and height, assuming it has a bit depth of 24?
An image in memory always weights the same weight compressed in JPEG or GIF or BMP. It's called a BIT-MAP. So, if you want to calculate the size of an image in memory, take the width, the height and the bit size to get the bit weight, divide by 8 and you get the bytesite.
$ByteSize = Width*Heigh*(24/8)
Note that it is possible to get more weight from an image in some parts such as a paletized image, it will have to store the image color palette in memory but most of the time this should weight less than a bitmap.
A multiplication.
Just multiply height by width by 3.
and throw in some spare memory to process all these bytes. say, twice the image size.
It has nothing to do with file size though.
before the upload on the server you can't with php,
but you can use javascript!
https://developer.mozilla.org/en/DOM/File
will work in conjunction of and will instantly provide the filesize using
fileInput.files[0].fileSize
read these:
https://developer.mozilla.org/en/Using_files_from_web_applications
http://hacks.mozilla.org/2009/12/multiple-file-input-in-firefox-3-6/
hope this helps!
The size will always depend on the format. When compressing images, the size of the image itself won't have any effect on the filesize. The only way that you can get a good readout of the filesize is if you use a Bitmap (or simmilar) format.
Example, A JPEG could be 10MB in size, and then when rotated only 1 MB in size because of how the compression works.

Image Resizing and Compression

I'm implementing an image upload facility for my website. The uploading facility is complete but what I'm working on at the moment is manipulating the images. For this task I am using PHPThumb (http://phpthumb.gxdlabs.com).
Anyway as I go along I'm coming across potential issues, to do with resizing and compression. Basically I want to acheive the following results:
The ideal image dimensions are: 800px width, 600px height. If an uploaded image exceeds either of these dimensions, it will need be resized to meet the requirements. Otherwise, leave as it is.
The ideal file size is 200kb. If an uploaded image exceeds this then it will need to be compressed to meet this requirement. Otherwise, leave as it is.
So in a nutshell: 1) Check the dimensions, resize if required. 2) Check the filesize, compress if required.
Has anybody done anything like this / could you give me some pointers? Is PHPThumb the correct tool to do this in?
As long as I can see just from the docs, phpThumb should be good enough for this task.
phpThumb can be used in an easy way like this:
Call phpThumb() just like you would a normal image.
Examples:
<IMG SRC="phpThumb.php?src=/image.jpg&w=100">
<IMG SRC="phpThumb.php?src=http://example.com/foo.jpg">
See the "demo" link on
http://phpthumb.sourceforge.net for
more usage examples). Parameters that
can be passed are listed below under
"URL Parameters".
The phpThumb() website says that it has the following image-processing feature:
Quality can be auto-adjusted to fit a certain output byte size.
This looks like what you want. It works by passing a maximum byte size parameter. The readme tells you how:
maxb = MAXimum Byte size - output quality is auto-set to
fit thumbnail into "maxb" bytes (compression
quality is adjusted for JPEG, bit depth is adjusted
for PNG and GIF)
edit: it seems that we were talking about different utilities with the same name. I did not found the same functionality in the other one, so I can only recommend using what I've found.
See the gd extension, in particular the functions getimagesize and imagecopyresized.
Enforcing a file size is more difficult, the best you can do is stick with jpeg and some arbitrary level of compression; for a certain combination of dimensions+compression, you can more or less predict the final file size of the image.
As to PHPThumb, I have no idea, as I've never used it.

phpThumb - change watermark size

I'm using phpThumb in a script to clean my images and add a watermark to them. We have images of very different sizes (from 100px width to 800px), so no matter what watermark image I use, it's either going to look too small or too big on the image.
Do you know of a way to tell phpThumb to resize the watermark? Or is there a way to resize the watermark image (depending on size of image being watermarked)?
Thanks a lot guys!
Ali
I am facing the same issue.
Maybe you could put some if statements in the phpthumb.config file in the DEFAULT PARAMETER SECTION
then depending on the height and width parameters you can decide to use different files with varying sizes for the watermark.
make sure you change $PHPTHUMB_DEFAULTS_GETSTRINGOVERRIDE to false
This is not perfect cause you could not know the source image size to start with.
but it would be a better fit based on the parameters passed to phpthumb.
I guess the only other solution would be to use phpthumb as an object but that would require a lot of work.

Categories