www.webdeveloper.com
Results 1 to 2 of 2

Thread: image uploading/resizing memory crash

  1. #1
    Join Date
    Mar 2008
    Posts
    54

    Exclamation image uploading/resizing memory crash

    Hi!

    Made a script for uploading and resizing images, and then making thumbnails of them.

    Problem is that the script crashes with no error messages (let's say it just stops) if the image is over 1mb or so (big image in other words).

    It seems like it is a memory crash, or that the scripts use too much memory (my webhotel only gives me 24mb).

    What can I do about it? Is there anything in the script I can change??

    PHP Code:
    <form action="<?php echo $_SERVER['PHP_SELF'];  ?>?objectnr=<?php echo $_GET['objectnr']; ?>" method="post" enctype="multipart/form-data" id="new_image" name="new_image">
            <input name="new_image1" id="new_image" size="30" type="file" class="fileUpload" />
            <br />
            <input name="new_image2" id="new_image" size="30" type="file" class="fileUpload" />
            <br />
            <input name="new_image3" id="new_image" size="30" type="file" class="fileUpload" />
            <br />
            <input name="new_image4" id="new_image" size="30" type="file" class="fileUpload" />
            <br />
            <input name="new_image5" id="new_image" size="30" type="file" class="fileUpload" />
            <br />
            <input name="new_image6" id="new_image" size="30" type="file" class="fileUpload" />
            <br />
            <input type="hidden" value="<?php echo $objectnr?>" name="objectnr" />
      <button name="submit" type="submit" class="submitButton">Upload/Resize Image</button>
    </form>
    <?php 
    if (isset($_POST['submit'])){
    echo 
    '<br> <p>Stäng denna ruta om du är klar.</p>';
    }
    $max_no_img="6"// Maximum number of images value to be set here

    for($i=1$i<=$max_no_img$i++){

    //foreach ($_FILES["new_image"]["error"] as $key => $error) {
    //if ($error == UPLOAD_ERR_OK) {
    $new='new_image'.$i;
        if (isset (
    $_FILES[$new])){
        
    //$imagename = $_FILES['new_image']['name'];
        
    $imagename $objectnr."-".$i;
        
    $ext ".jpg";
        
    $source $_FILES[$new]['tmp_name'];
        
    $target "../images/rooms/".$imagename.$ext;
            if (
    $source!==""){
            echo 
    "<br><br>Imagename: ".$imagename.$ext;
            echo 
    "<br>Source: ".$source;
            echo 
    "<br>Target: ".$target;
            }
        
    move_uploaded_file($source$target);
        
    $imagepath $imagename;
        
    $save "../images/rooms/" $imagepath.$ext//This is the new file you saving
        
    $file "../images/rooms/" $imagepath.$ext//This is the original file
        
        
    list($width$height) = getimagesize($file); 
        if (
    $height>$width)
        {
        
    $modwidth "600"
        
    $diff $width $modwidth;
        
    $modheight $height $diff;
        }
        else {
        
    $modwidth "800"
        
    $diff $width $modwidth;
        
    $modheight $height $diff;
        }


    //----Here is where the script stops!

        
    $tn imagecreatetruecolor($modwidth$modheight);
        
    $image imagecreatefromjpeg($file);
        
    imagecopyresampled($tn$image0000$modwidth$modheight$width$height); 

        
    imagejpeg($tn$save100); 
        
    imagedestroy($tn);
        
    imagedestroy($image);
        
        
    //Thumbnails
        
    $save "../images/rooms/" $imagepath."s".$ext//This is the new file you saving
        
    $file "../images/rooms/" $imagepath.$ext//This is the original file
        
        
    list($width$height) = getimagesize($file); 
        
        
    $modwidth "100"
        
        
    $diff $width $modwidth;
        
        
    $modheight $height $diff;
        
    $tn imagecreatetruecolor($modwidth$modheight);
        
    $image imagecreatefromjpeg($file);
        
    imagecopyresampled($tn$image0000$modwidth$modheight$width$height); 
        
        
    imagejpeg($tn$save100);
        
    imagedestroy($tn);
        
    imagedestroy($image);
            if (
    $source!==""){
            echo 
    "<br>Large image: ".$file;
            echo 
    "<br>Thumbnail: ".$save;
            }
        }
    }
    Any ideas? I believe it is a memorycrash. How can I make it use less memory? Any smarter ways of doing this?

  2. #2
    Join Date
    Mar 2004
    Posts
    369
    You might consider trying to change the memory limit:
    PHP Code:
    ini_set('memory_limit', -1); 
    I don't know if your ISP will permit that or not. It may or may not allow you to work with larger files.

    If you want your script to be more graceful about large images, you'll need to get some idea of how bit the image can be. I believe the amount of memory allocated for imagecreatetruecolor is a multiple of width*height. You can use the function memory_get_usage and do some experimenting to determine how large an image you can actually work with. This is not a very exact business.

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



Recent Articles