SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jan 2005
    Location
    memphis
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Any way to reset memory_limit within script?

    Hello!

    I am trying to upload 6 moderate (3mb each) images and associated comments for each image from a web page. The script creates an array of the image names and then stores the comments in a db..corresponding to the newly created/shrunk image.

    The loop runs until all images are processed. Within this time, the images are resized and optimized using gd, thumbnails are created, etc.

    I'm running into memory_limit problems (currently set to the max of 20M), as is the upload_max_size and post_max_filesize.

    Is there anyway to reset the script after each image is processed?

    PHP Code:
    <?php
      ini_set
    ("memory_limit","20M");
      
    ini_set("post_max_size","20M");
      
    ini_set("upload_max_filesize","20M");
        
        include(
    "config.inc.php");
        
    set_time_limit(0);
        
    // initialization
        
    $result_final "";
        
    $counter 0;

        
    // List of our known photo types
        
    $known_photo_types = array( 
                            
    'image/pjpeg' => 'jpg',
                            
    'image/jpeg' => 'jpg',
                            
    'image/gif' => 'gif',
                            
    'image/bmp' => 'bmp',
                            
    'image/x-png' => 'png'
                        
    );
        
        
    // GD Function List
        
    $gd_function_suffix = array( 
                            
    'image/pjpeg' => 'JPEG',
                            
    'image/jpeg' => 'JPEG',
                            
    'image/gif' => 'GIF',
                            
    'image/bmp' => 'WBMP',
                            
    'image/x-png' => 'PNG'
                        
    );

        
    // Fetch the photo array sent by preupload.php
        
    $photos_uploaded $_FILES['photo_filename'];

        
    // Fetch the photo caption array
        
    $photo_caption $_POST['photo_caption'];

        while( 
    $counter <= count($photos_uploaded) )
        {
            if(
    $photos_uploaded['size'][$counter] > 0)
            {
                if(!
    array_key_exists($photos_uploaded['type'][$counter], $known_photo_types))
                {
                    
    $result_final .= "File ".($counter+1)." is not a photo<br />";
                }
                else
                {
                    
    mysql_query"INSERT INTO gallery_photos(`photo_filename`, `photo_caption`, `photo_category`) VALUES('0', '".addslashes($photo_caption[$counter])."', '".addslashes($_POST['category'])."')" );
                    
    $new_id mysql_insert_id();
                    
    $filetype $photos_uploaded['type'][$counter];
                    
    $extention $known_photo_types[$filetype];
                    
    $filename $new_id.".".$extention;

                    
    mysql_query"UPDATE gallery_photos SET photo_filename='".addslashes($filename)."' WHERE photo_id='".addslashes($new_id)."'" );

    ///////////////////////////////////////////////////////////////////////////////////////////
    list($width$height) = getimagesize($photos_uploaded['tmp_name'][$counter]);

    if (
    $width $height){
    $newheight = ($height 530) / $width;
    $newheight round($newheight2);
    $newwidth 530;
    }else{
    $newheight = ($height 313)/ $width;
    $newheight round($newheight,2);
    $newwidth 313;


    // Load
    $shrunk imagecreatetruecolor($newwidth$newheight);
    $source2 imagecreatefromjpeg($photos_uploaded['tmp_name'][$counter]);

    // Resize

    imagecopyresampled($shrunk$source20000$newwidth$newheight$width$height);
    // Output

    imagejpeg($shrunk'photos/' $filename);

    imagejpeg($shrunk,'photos/2/'.$filename);


    /////////////////////////////////////////////////////////////////////////////////////////////

                    // Let's get the Thumbnail size
                    
    $size GetImageSize"photos/".$filename );
                    if(
    $size[0] > $size[1])
                    {
                        
    $thumbnail_width 100;
                        
    $thumbnail_height = (int)(100 $size[1] / $size[0]);
                    }
                    else
                    {
                        
    $thumbnail_width = (int)(100 $size[0] / $size[1]);
                        
    $thumbnail_height 100;
                    }
                
    $function_suffix $gd_function_suffix[$filetype]; 
    $function_to_read 'ImageCreateFrom' $function_suffix
    $function_to_write 'Image' $function_suffix

    // Read the source file 
    $source_handle $function_to_read($images_dir '/' $filename); 
            
    if (
    $source_handle) { 
     
    // Let's create a blank image for the thumbnail 
     
    $destination_handle 
       
    ImageCreateTrueColor($thumbnail_width$thumbnail_height); 

     
    // Now we resize it 
     
    ImageCopyResampled($destination_handle$source_handle
       
    0000$thumbnail_width$thumbnail_height$size[0], $size[1]); 


    // Let's save the thumbnail 
    $function_to_write($destination_handle$images_dir '/tb_' $filename);

                    
    imagedestroy($shrunk );
                    
    imagedestroy($source2);
                    
    imagedestroy($destination_handle );
                    
    //

                    
    $result_final .= "<img src='"."photos/tb_".$filename."' /> File ".($counter+1)." Added<br />";
                }
            }
        
    $counter++;
        }
    $result_final .= "<p><a href='done.php'>I'm finished uploading photos.</a></p>";

        
    // Print Result
    echo <<<__HTML_END

    <html>
    <head>
        <title>Photos uploaded</title>
    </head>
    <body>
        
    $result_final
    </body>
    </html>

    __HTML_END;


    ?>
    Thanks for any input. (I know my scripting (hacking) isn't the best)

    Zspeed

  2. #2
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not really sure what do you mean by resetting? can't you reset it using ini_set() again after image is processed ?

  3. #3
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do you want memory_limit to be reset? or do you want the memory to be dumped so you don't run out of it? try unsetting some of your bigger variables, tho it looks like you're destroying the images, so i'm not sure if that'd help.

  4. #4
    SitePoint Member
    Join Date
    Jan 2005
    Location
    memphis
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure what I want to do, besides NOT get the error message that I ran out of memory!

    Okay...for real, I'll try the ini_set() - but what should I set it to - or does calling ini_set() by itself dump everything?

    edit: I did ini_set memory_limit and all that, but still get the error.

    I'm just trying not to run out of memory after the first image is run. Then I want that clearing of memory, or whatever it is, so I can process more images.

    Thanks for the help! I'm no expert!!


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
  •