title: PHP GD Image Crop Allowed memory size exhausted
body: <p>I have built an image cropping system on my site that takes an image via user upload, allows the user to crop it, and then crops it via PHP GD and uploads it to the server.</p>
  
  <p>For some reason when I have a very large image, such as 4000 x 6000 (which is most of the images that will be getting uploaded) I get a PHP server error.</p>
  
  <p>After looking at my log I see this error,</p>
  
  <pre><code>PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 37105903 bytes) in Unknown on line 0
  </code></pre>
  
  <p><strong>Note: I have tried <code>ini_set('memory_limit', '-1');</code></strong></p>
  
  <p>Here is the code that retrieves the base 64 encoded image from the HTML form.</p>
  
  <pre><code>$imagebased64 = $_POST['imagebase64']; // base 64 image
  $center_id = $_POST['center_id']; // user id
  $filename = $this-&gt;helper_model-&gt;resize_profile_image($imagebased64, $center_id);    // resize the image and get the file name
  $insert['image'] = $filename;   // insert into database
  </code></pre>
  
  <p>And here is the resize image function:</p>
  
  <pre><code>/**
       * Returns an edit button - used to edit ticket row data
       *
       *
       * @param   base64    $data                         image to be cropped
       * @param   int       $center_id                    hashed center_id to be decrypted
       * @return  string                                  the file name of the image
       */
      public function resize_profile_image($data, $center_id) {
          ini_set('memory_limit', '-1');
          $center_id = $this-&gt;helper_model-&gt;convert_hash_to_center_id($center_id);
  
          // old dimensions
          $org_w = getimagesize($data)[0];
          $org_h = getimagesize($data)[1];
  
          // new dimensions
          $targ_w = 542;
          $targ_h = 671;
  
          $data = substr($data, 22);
          $data = base64_decode($data);
          $data = imagecreatefromstring($data);
  
          $image_p = imagecreatetruecolor($targ_w, $targ_h);
          imagecopyresampled($image_p, $data, 0, 0, 0, 0, $targ_w, $targ_h, $org_w, $org_h);
  
          // Buffering
          ob_start();
          imagepng($image_p);
          $data = ob_get_contents();
          ob_end_clean();
  
          //Store &amp; Display
          $context = stream_context_create([
             'gs' =&gt;[
                  'acl'=&gt; 'public-read', 
                  'Content-Type' =&gt; 'image/jpeg', 
                  'enable_cache' =&gt; true, 
                  'enable_optimistic_cache' =&gt; true,
                  'read_cache_expiry_seconds' =&gt; 300,
              ]
          ]);
  
          $name = $this-&gt;user_model-&gt;get_full_name($center_id);
          $filename = $name.'.jpg';
          file_put_contents(APPPATH.'../assets/developed/profile_images/'.$filename, $data, false, $context);
  
          return $filename;
      }
  </code></pre>