PHP: Changing image quality when conditions are met?

I have a PHP script where the user can upload images. I want the script to lower the image quality (jpeg) if the file size is larger than the "X" kbytes.

Something like that:

if( $_FILES['uploaded_img']['size'] > $file_size_limit ){ // code that lowers the quality of the uploaded image but keeps the image width and height } 

What is the best approach for this?

ps: I do not want to change the width and height of the image.

+1
php
Sep 23
source share
2 answers

Sure. Do something like that.

 $upload = $_FILES['uploaded_img']; $uploadPath = 'new/path/for/upload/'; $uploadName = pathinfo($upload['name'], PATHINFO_FILENAME); $restrainedQuality = 75; //0 = lowest, 100 = highest. ~75 = default $sizeLimit = 2000; if($upload['size'] > $sizeLimit) { //open a stream for the uploaded image $streamHandle = @fopen($upload['tmp_name'], 'r'); //create a image resource from the contents of the uploaded image $resource = imagecreatefromstring(stream_get_contents($streamHandle)); if(!$resource) die('Something wrong with the upload!'); //close our file stream @fclose($streamHandle); //move the uploaded file with a lesser quality imagejpeg($resource, $uploadPath . $uploadName . '.jpg', $restrainedQuality); //delete the temporary upload @unlink($upload['tmp_name']); } else { //the file size is less than the limit, just move the temp file into its appropriate directory move_uploaded_file($upload['tmp_name'], $uploadPath . $upload['name']); } 

This will support any image format supported by PHP GD (suppose it is installed on your server. Most likely). If the image is less than the limit, it simply loads the original image in the path you specify.

+3
Sep 24
source share

Your basic approach (which is implemented in Austin's answer) will work for a while, but it is important to remember that quality! = File size . Although they tend to correlate, it is possible (even general) that a decrease in the quality of the jpeg file will actually lead to a LARGER file. This is due to the fact that any JPEG downloaded to your system is already performed using the JPEG compression formula (often with quality 79 or 80). Depending on the source image, this process will create artifacts / modify the resulting image. When you run this already optimized image using the jpeg compression algorithm for the second time, it doesn’t “know” what the original image looked like ... so it processes the incoming jpeg as if it is a completely new file without losses and tries to copy as much it's possible ... including any artifacts created in the original process. Combine this with the fact that the original jpeg compression has already used most of the “simple” compression tricks; in the end, it is likely that the compression will create a crappier image (copy of the copy problem) for the second time, but not less than the file.

I did some tests to see where the crop was, and it is not surprising if the original image had a low compression ratio (q = 99), a lot of space was saved with re-compression to q = 75. If the original was compressed at q = 75 (quite common for graphical program defaults), the secondary compression q = 75 looked worse, but resulted in the same file size as the original. If the initial level had a lower compression level (q = 50), then the secondary compression q = 75 led to a significantly larger file (for these tests I used three complex photographs ... it is obvious that images with specific tastes / compositions will have different characteristics through these compression). Note. I use Fireworks cs4 for this test ... I understand that these quality indicators do not have standardization between platforms .

As noted in the comments below, the transition from file formats such as PNG to JPEG will ultimately be much smaller (albeit without any transparency), but from JPEG → JPEG (or GIF-> JPEG, especially for simple or small format , heaven) often does not help.

Regardless, you can still try using the compression method described by Austin, but make sure you compare the file sizes of the two images when you're done. If there is a small increase or a new file is larger, then the original image is returned by default.

+3
Sep 24
source share



All Articles