To administer your WestHost account, please enter your
Domain Name or Server Manager Username.

WestHost: Professional Website Hosting Company








Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16
  1. #11
    Junior Member dandcp's Avatar
    Join Date
    Jul 2004
    Posts
    10

    Default

    Quote Originally Posted by wildjokerdesign View Post
    Memory limit has to do with the amount of memory it takes to process the file not the size of the file. What bit of code do you have on line 245 of bemissform.php? That is the function that is causing you to exceed the memory limit.
    that is what I figured.

    line 245
    Code:
    	$image = imagecreatefromjpeg($filename);

  2. #12

    Default

    The file size of the image has almost no relevance here. You're hitting the memory limit because the image dimensions are large. Large images take more memory to work with, even if they are saved in a small file size. Once the image file is actually opened and rendered, the only thing that matters in the image dimensions.

    Basically, you can think of the imagecreatefrom*() functions in PHP as functions that open the image and convert it to a bitmap in memory. Large image dimensions = more pixels = more memory used. The memory usage for an image is a direct correlation with its dimensions. An image that is 2236 x 2244 means there are 5,017,584 pixels to keep in memory! Again, at this point, file size does not matter.

    Try this: Open Microsoft Paint and make a new image. Go to the Image menu > Attributes. Change the dimensions of your image to be 2236 x 2244. Now save this file as a .bmp image file (bitmap) and look at the file size. Notice that the file size is 14.3 MB. Since you saved it as a bitmap, you are saving a pixel-for-pixel representation of the image, which is similar to how images are represented when they are opened and in memory. In other words, what you learned from this little experiment is that it will take AT LEAST 14.3 MB to keep this image in memory. If the memory limit is 20 MB and your script is doing anything besides opening the image, you're probably going to hit the memory limit.
    So, what's the solution? Either see if the call to imagecreatefromjpeg() failed and throw an error to the user ("Your image is too large to resize"), or check the dimensions of the image beforehand and throw an error to the user if the image is above a certain size threshold.
    Nick Venturella
    WestHost Technical Support
    Contact Support: E-mail | Live Chat

    When you expect more from your Web host
    http://www.westhost.com/

  3. #13
    Moderator wildjokerdesign's Avatar
    Join Date
    Jun 2003
    Location
    Kansas City Mo
    Posts
    5,689

    Default

    Thanks Nick for giving the explanation. I couldn't quite figure out how to word it.

    dandcp, I know that you might not want to make your users re-size their images but it really is the best thing to do. The only other alternative would be to go with an account here that offers more resources or an account over at vps.net.
    Shawn
    Please remember your charity of choice: http://www.redcross.org

    Handy Links: wildjokerdesign.net | Plain Text Editors: EditPlus | Crimson

  4. #14

    Default

    Well, there are two things you can do before you need to upgrade the plan:

    1) You can slightly increase the memory_limit in your php.ini file if you need a bit more memory. Don't set this vastly higher (e.g., don't jump from the default of 20 MB to 100 MB) or you'll cause problems for other people on the server and WestHost may suspend your account, but you can bump this up to, say, 30 MB, without a problem.

    2) Keep the resizing functionality, but put a limit on the maximum image size. This will allow most images to be resized, but still allow the script to exit gracefully if it is given a very large image. You can estimate memory usage (in bytes) for an image by doing a very simply calculation: $width * $height * 4. Simply add an if statement like this before you begin the resize process:
    PHP Code:
    // Don't resize images that require over 8 MB of memory (~max dimensions: 1448 x 1448)
    // Remember that even if your PHP memory_limit is 20 MB, not all 20 MB is available for the user's image
    if ($width_orig $height_orig 8388608) {
        
    // Resize here...
        // ... imagecreatetruecolor
        // ... imagecreatefromjpeg
        // ... imagecopyressampled
    } else {
        print 
    "Sorry, your image was to large to resize. The maximum dimensions are about 1400 x 1400. Please upload a smaller image.";

    Note that you should still put a limit on the resize function like this, even if you do increase the memory_limit! People can always upload a bigger image, so even if you had a dedicated server and set your memory limit to 200 MB, it's still a good idea to check files before you resize them. Just like text inputs from the user, file inputs from the user should also be sanitized or at least checked prior to performing operations on them.
    Nick Venturella
    WestHost Technical Support
    Contact Support: E-mail | Live Chat

    When you expect more from your Web host
    http://www.westhost.com/

  5. #15
    Junior Member dandcp's Avatar
    Join Date
    Jul 2004
    Posts
    10

    Default

    WOW! you guys are quick! and this is great!
    These are the type of replies I love to see. I learn the best this way. Thank you very much Nick for taking time to explain it in great detail. I think I understand now AND know how to deal with it now. I assumed it was something with the dimensions

    I will make some changes in my code and give it a try and then get back to you.

    Again thank you very much.
    Thank you too Shawn

    -Doug

  6. #16
    Junior Member dandcp's Avatar
    Join Date
    Jul 2004
    Posts
    10

    Default

    This works great! I made some code changes and now it at least warns the user of a large file and to try again after resizing.
    I found a bunch of online resizers like http://www.resizeyourimage.com/
    I wish I could automatically send the large photo to a site like this for the user.

    Thanks again!

Posting Permissions

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