We help IT Professionals succeed at work.

Quality loss in resized image

RajeshKanna
RajeshKanna asked
on
Hi all,

I am using a script to resize images bulk from one folder and stored in another folder.

While I resize the large size images in to the dimension of 525x330 there is no quality loss.

But I resize the small size image to the dimension of 525x330 there is quality loss...


Can any body help  over this..

Thanks in advance...
<?  
 /* 
#  * usage: 
#  * resize.php [destination_width] [destination_height] 
#  * 
#  * destination_width and destination_height are optional 
#  * parameters for destination resize geometry 
#  * 
#  * resize.php will create output directory to save resized 
#  * images leaving originals intact 
#  * 
#  * This script uses "identify" and "convert" utilities which 
#  * are members of the ImageMagick suite of tools 
#  * 
#  */  
   
# // input arguments for destination image size  
 $w2 = $argv[1]; // image width  
 $h2 = $argv[2]; // image height  
#   
# // both values should be numeric or use default size 800x600  
 if (!is_numeric($w2) || !is_numeric($h2)){  
   $w2 = 150; // default image width  
   $h2 = 190; // default image height  
 }  
   
# // prepare directory name for saving resized images  
# // directory name will look like "_800x600"  
 $dir = "Image";  
#   
# // create directory (inside current directory) for saving  
# // resized images (@ means to be quiet - don't display  
# // warning if directory alredy exists)  
 @mkdir($dir); 
 
#   
# // read files inside current directory  
 $files = scandir('.');  
#   
# // open loop for all fetched files in current directory  
 foreach ($files as $file){  
#   // convert file name to lower case  
   $file_lowercase = strtolower($file);  
#   // if file extension is jpg then process image  
   if (substr($file_lowercase, -3) == 'jpg'){  
#     // describe image format and image characteristics  
     $identify = shell_exec("identify $file");  
#     // read image geometry  
     preg_match('/(\d+)x(\d+)/', $identify, $matches);  
     $g1 = $matches[0]; // image geometry  
     $w1 = $matches[1]; // image width  
     $h1 = $matches[2]; // image height  
#     // prepare destination image geometry  
     if ($w1 < $h1) $g2 = $h2 .'x' .$w2;  
     else           $g2 = $w2 .'x' .$h2;  
#     // print current image status  
     print "$file ($g1) -> $dir/$file_lowercase ($g2)\n";  
#     // resize image and save to destination directory  
     system("convert -size $g1 $file -resize $g2 $dir/$file_lowercase");  
   }  
 }  
# ?>

Open in new window

Comment
Watch Question

You should you PHP GD library to resize the image while the imageMagick is also very much popular

http://www.imagemagick.org/script/index.php

http://php.net/manual/en/book.imagick.php

ImageMagick is much faster. At best just compare both libraries and measure that.

   1. Prepare 1000 typical images.
   2. Write two scripts -- one for GD, one for ImageMagick.
   3. Run both of them a few times.
   4. Compare results (total execution time, CPU and I/O usage, result image quality).

you can also try the following

http://mediumexposure.com/smart-image-resizing-while-preserving-transparency-php-and-gd-library/

leakim971Multitechnician
Distinguished Expert 2019

Commented:
>Can any body help  over this..
No way, you can't rebuild what you left.
Most Valuable Expert 2011
Top Expert 2016

Commented:
Image quality is dependent on a thing called "image information" - in broad terms this refers to the amount of detail in the image.  Larger images are able to have more detail than smaller images.  Given any X:Y dimensions, an image that is 1/2 X:Y has 1/4 the image information of the full X:Y image.

When you resize an image from the larger dimension to the smaller, your software is able to preserve much of the appearance of image detail.  It works this way because you start with more image information, and even though you end up with less information, the lines of light and contrast can be preserved.  But when you start with a smaller image, you do not have enough information to "create" the larger image.

In a batch resize operation, the best you are likely to find is Photoshop.  You can program "actions" to address entire directories.  Resize the images by +10% repeatedly until they are the size you want.  A few years ago, "Genuine Fractals" was the lingua franca in this space, but today Photoshop is the best, hands down.

That said, there really is no software that can take a small image and make it large with perfect fidelity.  It's kind of like trying to take a small amount of money and make it a large amount of money.  Computer programs just cannot do that!

HTH, ~Ray

Author

Commented:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_21179706.html?sfQueryTermInfo=1+imag+loss+php+qualiti+resiz


In this the person who host got the clear image it seems. But I did get with his script which expert solution.
Most Valuable Expert 2011
Top Expert 2016

Commented:
A "clear" image may mean different things to different people.  Certain of the GD functions produce better output than others, but the ones that produce relatively poor output are very old, and the PHP man pages are quite clear about the right functions to use.  For an example of this documentation, please see the notes and examples on this page:
http://us.php.net/manual/en/function.imagecopyresized.php

Author

Commented:
Thank Ray,

The which are in small size are also getting quality image if that image was already in good quality.

But the Images which are not quality in nature are loss their quality when resized..

Is there any method or function to resize an image which no is a good quality to a good quality image???????

Author

Commented:
Sorry Ignore the last post...

Thank Ray,

The image which are in small size are also getting quality image if that image was already in good quality.

But the Images which are not quality in nature are loss their quality when resized..

Is there any method or function to resize an image which is not in a good quality to a good quality image???????
Is there any method or function to resize an image which no is a good quality to a good quality image???????  --- There is no such function or method available in php

Commented:
Hi RajeshKanna,
In your question, you have pointed out something what i have learnt from my personal experience. Yes, bigger pictures can be resized to a smaller version without quality loss (or minimal quality loss). But smaller sized images cannot preserve the quality when they are resized to a bigger size. The aspect ratio is responsible for this. Unfortunately, there's no way you can preserver the quality 100%.

I completely agree with Ray in his comment ID: 32983816. He has stated the process very well. You may use Photoshop or Fireworks to enhance the images, but that would be a tedious and time consuming process and still there would be no guarantee of the quality.

But yet, I will still try to help you with your problem. There is a PHP function which does permit you to control the quality of the images which you have generated using PHP. You may try that to generate your bigger images from the smaller ones.

In the example code, the quality is set to 75%, you may set it to 100%. You may use the following code once your image processing is done, to enhance quality:
imagejpeg($im, 'image.jpg', 75);

So I would you say, you should basically take interest in the above command and use it while processing your images. Try it out and let us know if it worked for you.
<?php //This is just an example. You will need to modify as needed for your purpose.
// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);

// Set the content type header - in this case image/jpeg
header('Content-type: image/jpeg');

// Skip the filename parameter using NULL, then set the quality to 75%
imagejpeg($im, 'image.jpg', 75);

// Free up memory
imagedestroy($im);
?>

Open in new window

Most Valuable Expert 2011
Top Expert 2016
Commented:
Regarding this:

Is there any method or function to resize an image which no is a good quality to a good quality image???????  --- There is no such function or method available in php

I would go a step farther in my answer.  There is no such method available in the known universe.  You can sharpen images, enhance highlights, change color schemes, and do all kinds of things to increase visualization of the image data, but each and every such change REMOVES information from the original image.  If you go into the image and add information (such as pasting in a layer with more detail) that is a very different kind of change from trying to manipulate an image to improve its quality.  This is a big issue in both copyright law and the rules of evidence.  "Enhanced" images pose a grave danger to defendant's rights.

As a photographer, I have made many images that I have enhanced in a variety of ways.  HDR is one technique.  Different exposure values are another.  But none of these are faithful to the original image - they are techniques of art and have nothing to do with adding image information.

Hope that helps, ~Ray

Author

Commented:
Thanks a lot Ray...