Results 1 to 7 of 7

Thread: Reducing image size (KB) with PHP

  1. #1
    Join Date
    Oct 2012
    Posts
    157
    Thanks
    21
    Thanked 1 Time in 1 Post

    Default Reducing image size (KB) with PHP

    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?


    Code:
    <?php if($row['picfile']) { ?>
    
    <img src="<?php echo "$datadir[adpics]/$row[picfile]"; ?>" width="90" height="65">
    
    <?php } ?>



    Thanks!

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,729
    Thanks
    82
    Thanked 90 Times in 88 Posts

    Default

    Code:
    <?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.

    Code:
    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.
    Code:
    imagejpeg($image, $dest, 10); //This line saves the image with an image quality of 10%.
    You can read more about it here.
    To choose the lesser of two evils is still to choose evil. My personal site

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Oct 2012
    Posts
    157
    Thanks
    21
    Thanked 1 Time in 1 Post

    Default

    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.

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,729
    Thanks
    82
    Thanked 90 Times in 88 Posts

    Default

    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.
    To choose the lesser of two evils is still to choose evil. My personal site

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Similar Threads

  1. Popup Image Viewer - how to adjust the size of pop up image?
    By dasadler in forum Dynamic Drive scripts help
    Replies: 11
    Last Post: 12-12-2009, 12:07 PM
  2. Replies: 2
    Last Post: 09-19-2007, 07:27 PM
  3. reducing script size with a reference
    By rich1234 in forum Dynamic Drive scripts help
    Replies: 74
    Last Post: 08-20-2006, 07:54 PM
  4. Reducing the Size of HV Menu
    By Gr8briGuy1 in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 04-26-2005, 07:39 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •