title: PHP DomDocument memory errors when uploading files
body: <p>In my Laravel app I have a textarea, which has a text editor which allows people to upload images. I need to get these images from within the text and save them to the server.</p>
  
  <p>The following code works perfectly for small images, but now I'm trying an image that's only 400k and it's throwing memory errors.</p>
  
  <p>I've tried suppressing errors using many different methods from many different articles and posts, to no avail. Any suggestions on how to get this to stop throwing errors?</p>
  
  <pre><code>// $body contains the contents of the POSTed textarea, 
  // which includes HTML and images
  
  public function storeImages($body, $article)
  {
      $dom = new \DomDocument();
      $dom-&gt;loadHtml($body, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
      $images = $dom-&gt;getElementsByTagName('img');
  
      foreach($images as $img)
      {
          $src = $img-&gt;getAttribute('src');
  
          // if the img source is 'data-url'
          if(preg_match('/data:image/', $src))
          {
              preg_match('/data:image\/(?&lt;mime&gt;.*?)\;/', $src, $groups);
              $mimetype = $groups['mime'];
              $filename = uniqid().'.'.$mimetype;
              $filepath = '/media/'.$article-&gt;id.'/'.$filename;
              $image = Image::make($src)
                  -&gt;encode($mimetype, 100)
                  -&gt;resize(1024, null, function ($constraint) {
                      $constraint-&gt;aspectRatio();
                  })
                  -&gt;save(config('filesystems.disks.media.root').'/'.$article-&gt;id.'/'.$filename);
              $img-&gt;removeAttribute('src');
              $img-&gt;removeAttribute('style');
              $img-&gt;removeAttribute('data-filename');
              $img-&gt;setAttribute('src', $filepath);
              $img-&gt;setAttribute('class', 'img-responsive');
              $img-&gt;setAttribute('alt', '');
          }
      }
  
      return $dom-&gt;saveHTML();
  }
  </code></pre>