Results 1 to 5 of 5

Thread: Memory problem when uploading large(ish) images

  1. #1
    Join Date
    Nov 2010

    Memory problem when uploading large(ish) images

    Hey all,

    I have a page where I can upload an image and php will create 2 down-scaled images. One is for the thumbnail which is max width of 125px and max height of 95px and the other for the 'main' display which is max width of 550px and max height of 600px - the original is kept and it used as the high quality one (as a link). Previously I just had the large image but then users need to download a lot of data they don't necessarily want..
    I'll try to post my understanding of what my code is doing and hopefully it's not too inaccurate..

    They way I'm resizing is to set the width to x (either 125 or 550) and then scale the height keeping the original ratio - if the heigh exceeds my max I re-do the scaling with height done first.
    PHP Code:
    list($image_width$image_height$image_type) = getimagesize($path);
    $ratio $image_height $image_width;

    $new_width_thumb 125;
    $new_height_thumb $new_width_thumb $ratio;
    $new_height_thumb round($new_height_thumb);

    #Check height after scaling; if thumb height is > 95, resize with height as limiting
    if($new_height_thumb 95)
    $new_height_thumb 95;
    $new_width_thumb $new_height_thumb * (1/$ratio);
    $new_width_thumb round($new_width_thumb);

    I then repeat this for the second scaled image so I have my two scaled dimentions.
    Then I create 'blank images' and copy the image into the newly sized 'blank images'
    PHP Code:
    $blank_image_thumb imagecreatetruecolor($new_width_thumb$new_height_thumb);
    $blank_image_main imagecreatetruecolor($new_width_main$new_height_main);

    //After checking the file type..

    $source_image_thumb imagecreatefromjpeg($path);
    $source_image_main imagecreatefromjpeg($path);
    imagecopyresampled($blank_image_thumb$source_image_thumb0000$new_width_thumb$new_height_thumb$image_width$image_height );
    imagecopyresampled($blank_image_main$source_image_main0000$new_width_main$new_height_main$image_width$image_height );
    imagejpeg($blank_image_thumb$raw_path "thumb_" $file_name);
    imagejpeg($blank_image_main$raw_path "main_" $file_name); 
    I'm thinking the imagecreatefromjpeg or the imagecopyresampled is storing the 'big' image in the ram and that is causing my php to run out of memory (smaller res images work fine - less than say 1500px^2).
    I have read that I can up this limit in my php.ini but I'm not sure if my host will do this and more importantly I read that I'd be better off fixing my code's efficiancy..

    So is there a more efficient way to do what I want without removing any automation? (I don't want to be resizing and uploading 3 images!)

    I hope I've made sense..

  2. #2
    Join Date
    Aug 2004
    The img*() functions work with a bitmap of the image in memory, which requires 4 bytes per pixel (3 colors plus alpha channel), so the memory needed will be the width * height (in pixels) * 4, and this is regardless of how large the source image file is.

    You can adjust max memory at run-time with init_set('memory_limit', <value>). You could use getimagesize() to find the width and height, use the above formula to estimate the amount of memory needed, and the use that with ini_set() plus some additional padding for the rest of the script, the thumbnail images, etc. (You could also define a max size, at which point you could return an error saying the picture needs to be made smaller before processing, should you desire.)
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Join Date
    Nov 2010
    Awesome, thanks so much dude!

  4. #4
    Join Date
    Mar 2011
    On shared hosts, memory limits are often fixed and cannot be overridden (or at least have a ceiling), so that approach may not work for everyone. I was thinking it would be a good idea to process the image in stages. Scale the original down to your 'medium' size, then immediately store the original and use imagedestroy() to release the memory it was using. Store the medium-size image and then scale it down to your thumbnail.

    In the alternative, you could investigate using ImageMagick, which is more efficient than the GD library in using memory. It's easiest if your host has one of the ImageMagick libraries for PHP installed, but for simpler tasks like resizing images, you can just use system() or passthru() to process ImageMagick commands easily enough.
    Rick Trethewey
    Rainbo Design

  5. #5
    Join Date
    Nov 2010
    Ok after some troubles it's now working - I think maybe I had issues with quotation marks..
    Thanks guys!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.12055 seconds
  • Memory Usage 2,892KB
  • Queries Executed 13 (?)
More Information
Template Usage (33):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (2)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (5)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (5)postbit
  • (5)postbit_onlinestatus
  • (5)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (71):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates