phppup Posted April 16, 2020 Share Posted April 16, 2020 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? Quote Link to comment https://forums.phpfreaks.com/topic/310599-image-resizing/ Share on other sites More sharing options...
chhorn Posted April 16, 2020 Share Posted April 16, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310599-image-resizing/#findComment-1576946 Share on other sites More sharing options...
phppup Posted April 16, 2020 Author Share Posted April 16, 2020 What will my result be if I try to reverse it enlarge ajpeg (which seems to be the most common format) ? Quote Link to comment https://forums.phpfreaks.com/topic/310599-image-resizing/#findComment-1576947 Share on other sites More sharing options...
Barand Posted April 16, 2020 Share Posted April 16, 2020 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 The moral here is "never destroy the original". Not only will you lose resolution but you will also lose any embedded exif data. Quote Link to comment https://forums.phpfreaks.com/topic/310599-image-resizing/#findComment-1576949 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.