title: PHP: Handle memory & code low memory usage
body: <p>Yes I am having an issue with PHP memory. My host offer max. to use 64M memory limit, and somehow when i upload+crop functions exceeds this memory, and im receiving an error(not always though, depends on image size)</p>
  
  <p>When I upload an image with around 1 mb or lower, it uploads, and you can crop and save it.</p>
  
  <p>But when i upload an image with around 2 mb or higher (my limit on the uploadfunction is 6mb), it uploads, and then when you crop the image and want to save it i get "out of memory error."</p>
  
  <p>I tried to imagedestroy() after any function in gd, i don't use anymore, but this didn't affect it. Although im only using GDfunctions in the case crop</p>
  
  <p>So is there anything that i can free my memory off in the case upload?</p>
  
  <p>Or maybe in general how to free the php memory usage?</p>
  
  <p>Here's my script:</p>
  
  <pre><code>switch($action) {
  
      case 'upload':
  
  
      $userid = $USER;
      $user = getUserInfo($userid);
      $lasttime = $user['showURP']["photo_lasttime"];
  
      $upload_name = "file";
      $max_file_size_in_bytes = 8388608;
      $extension_whitelist = array("jpg", "gif", "png", "jpeg");
  
      /* checking extensions */
      $path_info = pathinfo($_FILES[$upload_name]['name']);
      $file_extension = $path_info["extension"];
      $is_valid_extension = false;
      foreach ($extension_whitelist as $extension) {
          if (strcasecmp($file_extension, $extension) == 0) {
              $is_valid_extension = true;
              break;
          }
      }
      if (!$is_valid_extension) {
          echo "{";
          echo        "error: 'Filtype not allowed!'\n";
          echo "}";
          exit(0);
      }
      /* checking filetype*/
  switch(exif_imagetype($_FILES[$upload_name]['tmp_name'])) {
      case IMAGETYPE_GIF:
      case IMAGETYPE_JPEG:
      case IMAGETYPE_PNG:
          break;
      default:
          echo "{";
          echo        "error: 'This is no real image!'\n";
          echo "}";
          exit(0);
  }
  
      /* file size check */
      $file_size = @filesize($_FILES[$upload_name]["tmp_name"]);
      if (!$file_size || $file_size &gt; $max_file_size_in_bytes) {
          echo "{";
          echo        "error: 'Filesize exceeds the limit: 6MB'\n";
          echo "}";
          exit(0);
      }
  
  
  
  
      if(isset($_FILES[$upload_name]))
          if ($_FILES[$upload_name]["error"] &gt; 0) {
              echo "Error: " . $_FILES["file"]["error"] . "&lt;br /&gt;";
              } else  {
                  $userfile = stripslashes($_FILES[$upload_name]['name']);
                  $file_size = $_FILES[$upload_name]['size'];
                  $file_temp = $_FILES[$upload_name]['tmp_name'];
                  $file_type = $_FILES[$upload_name]["type"];
                  $file_err = $_FILES[$upload_name]['error'];
                  $file_name = $userfile;
  
                  if(move_uploaded_file($file_temp, "images/profilePhoto/".$file_name)) {
                      echo "{";
                      echo        "msg: '".$file_name."'";
                      echo "}";
                  }
              }
      break;
  
      case 'crop':
      if ($_SERVER['REQUEST_METHOD'] == 'POST')   {
  
  
          $jpeg_quality = 90;
  
          $src = "images/profilePhoto/".$_POST['fname'];
          $ext= pathinfo($src, PATHINFO_EXTENSION);
  
  
      if($ext == "jpg" OR $ext == "jpeg" OR $ext == "JPG"){
          $img_r = imagecreatefromjpeg($src); // &lt;-- im getting out of memory error on this line
  
      }elseif($ext == "png" OR $ext == "PNG"){
          $img_r = imagecreatefrompng($src);
      }elseif($ext == "gif" OR $ext == "GIF"){
          $img_r = imagecreatefromgif($src);
      }
  
          if($_POST['fixed'] == 0) {
             $targ_w = $_POST['w'];
             $targ_h = $_POST['h'];
          }
          else {
              $targ_h = $_POST['sizeh']; 
              $targ_w = $_POST['sizew']; 
          }
  
          $dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
  
          imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
          $targ_w,$targ_h,$_POST['w'],$_POST['h']);
          imagedestroy($img_r); // free memory
  
      $path_thumbs = "images/profilePhoto/thumbs";
          $filename = $USER . ".jpg";
          $thumb_path = $path_thumbs . '/' . $filename;
  
          imagejpeg($dst_r,$thumb_path,$jpeg_quality);
          imagedestroy($dst_r); // free memory
  
          echo json_encode(array(
              'filename'=&gt;$thumb_path
          ));
          unlink($src);       
  
  
      }   
      break;
  }
  </code></pre>
  
  <p>My host is only supporting GD lib for cropping images, so imagemagick cant be used.</p>
  
  <p>And as i mentioned previously, my host has a memory_limit on 64M, that cant be set</p>