Results 1 to 2 of 2

Thread: "Double" Re-Size Image Script

  1. #1
    Join Date
    Jun 2007

    "Double" Re-Size Image Script

    I need a double duty image re-size script. I need to upload an image and re-size it to a width of around 600px (keeping picture proportionate) and create a thumbnail image - saving both the re-sized bigger picture and the thumbnail image.

    I've tried numerous variations of the below script but cannot get it to work. Part of the script I'm using now and it works OK, but the user has to re-size the picture first on their computer before uploading. I've posted tutorials on how to re-size an image using xnview and other programs but it's just too difficult for most people to figure out. Microsoft does not have a "simple" picture re-sizer for Vista like they do for XP. So, I have to re-size their original picture for them from a massive 2800 x size and larger to something more website friendly like - 600px wide max, and then make a thumbnail.

    PHP Code:
    <form action="imageupload.php" enctype="multipart/form-data" method="post">
    input type="file" size="55" name="newimage"
    PHP Code:
    // This is the temporary file created by PHP 
    $uploadedfile $_FILES['newimage']['tmp_name'];

    // Create an Image from it so we can do the resize
    $src imagecreatefromjpeg($uploadedfile);

    // Capture the original size of the uploaded image

    // For our purposes, I have resized the image to be
    // 600 pixels wide, and maintain the original aspect 
    // ratio. This prevents the image from being "stretched"
    // or "squashed". If you prefer some max width other than
    // 600, simply change the $newwidth variable


    // this line actually does the image resizing, copying from the original
    // image into the $tmp image

    // now write the resized image to disk. I have assumed that you want the
    // resized, uploaded image file to reside in the ./images subdirectory.
    // $filename = "images/". $_FILES['newimage']['name']; Blocked this because I do not want it to put picture into a folder yet.
    // imagejpeg($tmp,$filename,100); Not sure what this does, so I blocked it since it's related to the part that puts it into a folder.

    // The script above re-sizes the original massive image to something more Web friendly.
    // The script below should make a thumbnail of the re-sized image above and save both to the server and insert image info into database.
    // The problem is that the script below isn't connecting with the above script. I think because it's setup to look for an uploaded file instead of a picture that has already been manipulated.

    // The paths to save the image(s):
    $thumb_dir './../../pgallery/thumbs/';
    $photo_dir './../../pgallery/'

    // The max width, or height of the thumbnail: 
    $thumb['width'] = 100
    $thumb['height'] = 80
    $thumb['resolution'] = 80

    // Uploaded file's Temp Name: 
    // $tempname = $_FILES['newimage']['tmp_name']; this looked for an uploaded file named newimage
    // $tempname = $_FILES['$tmp']['tmp_name']; Using $tmp didn't work
    $tempname $tmp// didn't work either

    // Define the image name: 
    // $image_name = $_FILES['newimage']['name']; this looked for an uploaded file named newimage
    // $image_name = $_FILES['$tmp']['name']; Using $tmp didn't work
    $image_name $tmp// didn't work either

    // Is the file uploaded: 
    // if(is_uploaded_file($tempname))

    // Get the orginal dimensions: 
    $o_dims getimagesize($tempname); 

    $o_dims === false || $o_dims[2] != 2
    '<h2 style="color:#f00">Image Upload Failed!!</h2>'); 

    // Put the original dimensions into something easier to work with: 
    $width $o_dims[0]; 
    $height $o_dims[1]; 

    // Create a new image based on the temp: 
    $orig_image imagecreatefromjpeg($tempname); 

    // Get the proportionate thumbnail dimensions: 
    if($height >= $width
    $t_width ceil(($thumb['height']/$height)*$width); 
    $t_height $thumb['height']; 
    $width $height
    $t_width $thumb['width']; 
    $t_height ceil(($thumb['width']/$width)*$height); 

    // Create a new image with our thumb dimensions: 
    $new_image imagecreatetruecolor($t_width$t_height); 

    // Now, copy the old image to the new image magically resizing it: 
    if(imagecopyresampled($new_image$orig_image0000$t_width$t_height$width$height) === false
    '<h2 style="color: #f00;">Image Resizing Failed!!</h2>'); 

    imagejpeg($new_image$thumb_dir.$image_name$thumb['resolution']) === false
    '<h2 style="color:#f00;">Creating Thumb Failed!!</h2>'); 

    move_uploaded_file($tempname$photo_dir.$image_name) === false
    '<h2 style="color:#f00;">Unable to move uploaded file!!</h2>'); 

    // Insert image information into database 
    As you can see, I've tried to bypass the part of the thumbnail script that is looking for an uploaded file rather than the re-sized image. But I can't get the thumbnail part to connect with the first re-size part.

    Any help here would be appreciated.

  2. #2
    Join Date
    Nov 2007
    The first thing I'd do would be to free up the rather large amount of memory required to load the original image. After the smaller "$tmp" image is created you don't need it any more so call imagedestroy($src) to release it.

    There is really no point in writing an image to disk then immediately reading the same image again. This requires encoding the image as a jpeg and then decoding the jpeg back into memory as a bitmap again which is both slow and pollutes your clean source image with jpeg artifacts created during the compression. Get rid of all that file handling stuff and just use the image you already have. $orig_image = $tmp;

    There's also no reason to call move_uploaded_file() as you intend to discard the uploaded file in favor of the version that has been re-sampled to 600px wide.

    If I were you I'd just start rewriting the script from the beginning like this:

    make sure a valid image was uploaded
    create a re-sampled image that is max 600px wide from the uploaded image
    free up the memory used by the uploaded image
    create a re-sampled thumbnail from the 600px wide image
    compress the 600px image to disk
    free up the memory used by the 600px image
    compress the thumb to disk
    free up the memory used by the thumb
    ... any other stuff you need to do

    Hope this was of some help.

    btw imagejpeg() compresses an image into jpeg format. The manual is your friend

Thread Information

Users Browsing this Thread

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

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.14383 seconds
  • Memory Usage 2,903KB
  • Queries Executed 15 (?)
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
  • (2)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (2)postbit
  • (2)postbit_onlinestatus
  • (2)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 (72):
  • 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_postinfo_query
  • fetch_postinfo
  • 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_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