View Full Version : Reducing image size (KB) with PHP
qwikad.com
06-11-2013, 02:49 PM
The line below shows large images, then I just reduce them with width and height. Well, it takes some time to get them uploaded on the page. I'd like to reduce their KB size with PHP. I've seen someone suggesting to compress them with something like imagejpeg($tn, $save, 75); How would I implement something like that in my case?
<?php if($row['picfile']) { ?>
<img src="<?php echo "$datadir[adpics]/$row[picfile]"; ?>" width="90" height="65">
<?php } ?>
Thanks!
james438
06-11-2013, 03:11 PM
<?php
header('Content-type: image/jpeg');
$filename = 'http://www.yoursite.com/images/cowboybebop27.JPG';
$image = imagecreatefromjpeg($filename);
$dest='temp_image.jpg';
imagejpeg($image, null, 10);
imagejpeg($image, $dest, 10);
?>
The above code will pull an image and save it at the file location of temp_image.jpg.
imagejpeg($image, null, 10); //This line displays the image with a quality of 10% which is roughly 1/10 the original file size as well.
imagejpeg($image, $dest, 10); //This line saves the image with an image quality of 10%.
You can read more about it here (http://php.net/manual/en/function.imagejpeg.php).
djr33
06-11-2013, 04:34 PM
That method is somewhat inefficient. It takes a significant amount of processing power to convert images, so it may be a good idea to store the smaller images.
There are a few options:
1. When you upload the image, convert it with that automatically so you have no large images. This could either be triggered when you upload (if you're using an upload form with PHP) or something you run manually. (Or you could create a cron job that runs automatically every day or two to check for new images to convert.)
2. When you load the image gallery, check that each image is small enough; if not, permanently convert it and save the results to the same file.
3. Create a caching system such that each image already has an associated smaller image stored somewhere else.
As needed (except for 3) you can also store a full size image somewhere else if you need to ever view that.
In short, it won't be worth using that much processing power just to save the loading time. It also may not be faster, depending on how fast your server can process these images. You're shifting the work somewhere else, but it's still work. Caching will solve all of that (with any of the methods above).
The code in James's post will be fine for doing the actual conversion, but you may want to add some additional functionality like I've suggested. It can be a little complicated, but it's possible. Let us know if you have questions.
qwikad.com
06-13-2013, 08:57 PM
djr33 what you said makes perfect sense. I guess I can require visitors to post smaller size pictures to begin with and that will make everything upload faster.
djr33
06-13-2013, 09:30 PM
You're already using a serverside language for uploads? It shouldn't be extremely hard to add in auto-resizing if you're happy with that-- for example, set a maximum dimension (width or height) of 500 pixels. Then the script can resize it while uploading. Of course it's good if they can upload smaller images, but not all of your users will know how to resize an image, so this might help. If you require smaller images, they can probably figure it out, though. It's up to you.
Of the various options, automatically resizing upon upload is the easiest. The others (caching and so forth) require the same, but also additional structure such as caching.
james438
06-13-2013, 09:50 PM
I do something rather similar. I have a few upload image programs which revolve around the same format. The original image is uploaded into one folder and the resized image is uploaded into another folder. If the original image is ever needed it is there for cropping or resizing or restoring. The resized image is the image that is almost always displayed though. I am not very familiar with caching though.
djr33
06-14-2013, 04:04 AM
I'm using the term "caching" loosely. Your system would count (and that's, one possibility of, exactly what I meant).
The only way it would be more "caching" that than would be the case where you don't do that when it's uploaded-- you do it when the images are viewed. I set up a system like that once. It did the following for each image:
1. Take the md5_file() value for the file (which is a very fast operation, but maybe not great for a really really busy site).
2. See if that thumbnail already exists.
2b. If not: create a file called "md5_file().jpg" at the thumbnail dimensions and store (cache) it.
3. Display "md5_file().jpg" (which must now exist, one way or the other).
It's very slow the first time you run it (maybe you've just uploaded one hundred new raw files from your digital camera via FTP!) but after that it's fast/immediate, for a large number of images.
This system is useful if you don't know when new images are uploaded AND you want to display them resized immediately (rather than, say, running an image-to-thumbnail script once per week). If you can avoid it, do that. Because you have a PHP-based upload form, what you're doing is the most logical and efficient.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.