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.
Related
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.
I have to check few JPEG files for quality level and give to every file corresponding quality mark. Then I must be able to choose better image by this mark.
How to decide about visual image quality?
I'm dealing with photographs. Image dimensions and even compression ratio cannot indicate
visual quality of of the image. For example, if you enlarge the image and save it with bigger quality the visual quality will be reduced...
The code is in PHP.
Any advises will be very thankful!
If you have access to the original image then SSIM is one of the better methods to do this. It'd be pretty slow to do in pure PHP, but if you can execute shell commands, check DSSIM tool (you'll need to adapt it to load JPEGs instead of PNGs).
ImageMagick has compare command that can return Mean Square Error/PSNR of the image (compared to original), but these measurements only check per-pixel differences, so they are a poor way measure distortion caused by blockyness and ringing of JPEG compression.
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.
I am working on a site at the moment, that requires the admins of the site to be able to upload pretty much any size of image, I then need to find a way to get the image down to the size required for the front end of the end the site, all this needs to be done without know what size of image the user is uploading, but the image always needs to scale to 209x293 without looking awful.
Is this even possible?
You should argue with your client, to forget that rule(accept ANY image), and accept rather, only images in that proportion, or better, you can use a tool to crop the image, forcing the user to crop an image in your needed resolution.
Jcrop is a library in Jquery which can help you a lot if you want to create that cropping feature.
Don't know your precise requirements, but since you tagged it with CodeIgniter, you can check out the Image Manipulation Class which has everything you need to do the job.
Not knowing the size of images before uploading is, you know, quite a common problem...Just be careful of the MAX SIZE, which is set in your php.ini.
You might find useful also the page on file uploading right in php's manual.
I want to know if its possible (and how) to check for real image quality (.jpg) using the GD library.
Obviously we need to check for file size, colors, image size, and number of pixels and compare between them.
For example if an 1200x600px image has a size of 60kb probably its in bad quality. But if the image is in grey scale, or uses only some colors, then maybe its in good quality...
So... any ideas?
What you want is a no-reference image quality assessment. I don't know of a specific implementation for PHP but you can take a look in IEEE Transactions on Image Processing for algorithms.
There's an implementation with focus on JPEG compression here:
http://www.cns.nyu.edu/~zwang/files/research/nr_jpeg_quality/index.html