Jump to content

Image resizing


phppup

Recommended Posts

As a point of curiosity, I am seeking some clarification.

I have developed code to resize an image, thus reducing the file size by a percentage of it's original.

My first question is, what exactly is being done to the file?

I understand that some data is being removed (or condenced) but how does this effect the image when compared with the original on a webpage?

Next, what should my expectation be if I wanted to reverse the process?

If I am 'shrinking' an image for a faster upload speed and reduced storage capacity, how successful will I be if I wanted to 'restore' the file size before printing a physical photograph?  Will this provide a 'better' end result?

 

Link to comment
Share on other sites

This depends on the image format an what exactly "resizing" means in this way. When you use the compression feature of PNG the dimensions of that image (width, height, color depth) stay the same and that process is perfectly reversible as PNG is a lossless format, the codec just tries to put as much information as possible into as less bytes as needed, this may result in higher CPU usage. Using JPEG compression you destroy image details and it's irreversible. If you change any dimension or cut something off of your image that information is lost, no matter what format you use. But for the exact details i recommend reading the specification of the image codec.

 

Link to comment
Share on other sites

Here's a sample script with a small png image. First it creates a small copy then it enlarges that small copy back to the original size

<?php
$orig = imagecreatefrompng('images/snowman.png');

// copy to thumbnail sized image
$im = imagecreatetruecolor(100,100);
imagecopyresized($im, $orig, 0, 0, 0, 0, imagesx($im), imagesy($im), imagesx($orig), imagesy($orig));
imagepng($im, 'images/snowman_small.png');
imagedestroy($im);
imagedestroy($orig);

// restore thumbnail back to original size
$orig = imagecreatefrompng('images/snowman_small.png');
$im2 = imagecreatetruecolor(400, 400);
imagecopyresized($im2, $orig, 0, 0, 0, 0, imagesx($im2), imagesy($im2), imagesx($orig), imagesy($orig));
imagepng($im2, 'images/snowman_restored.png');
imagedestroy($orig);
imagedestroy($im2);
?>

As you can see, the final image has lost resolution, each pixel being magnified x4

image.thumb.png.f180423be63133d887c645f17dccb370.png

The moral here is "never destroy the original".

Not only will you lose resolution but you will also lose any embedded exif data.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.