title: Laravel 5, My create method is very slow, how can improve it?
body: <p>For my website I want to create a gallery and upload photos. These photo must resize to two formats.</p>
  
  <p>I build a method in my controller to create galleries, but is very slow and required too much memory. How can I improve it?</p>
  
  <p>Is it possible to do it like this or need i do an ajax call?</p>
  
  <pre><code>public function store(GalleryCreateRequest $request)
  {
     //dd($request-&gt;all());
  
      $input = $request-&gt;all();
      $name = str_replace(' ', '_', $input['name']);
      if (Input::file('featured_image')) {
          $featured = Input::file('featured_image');
          $extensieFeatured = $featured-&gt;getClientOriginalExtension();
          if (!file_exists('images/'.$name)) {
              mkdir('images/'.$name, 0777, true);
          }
          $path = 'images/'.$name;
          $featuredName = str_replace(' ', '_', $input['name']) . '_featured' . '.' . $extensieFeatured;
          $featured-&gt;move($path, $featuredName);
          $input['featured_image'] = $featuredName;
          //landcape or Portrait
          list($width, $height) = getimagesize($path.'/'.$featuredName);
          if ($width &gt; $height) {
              //landscape
              Image::make($path.'/'.$featuredName)-&gt;resize(1500, null, function ($constraint) {
                  $constraint-&gt;aspectRatio();
              })-&gt;save($path.'/web_'.$featuredName);
              Image::make($path.'/'.$featuredName)-&gt;resize(300, null, function ($constraint) {
                  $constraint-&gt;aspectRatio();
              })-&gt;save($path.'/thumbs_'.$featuredName);
          } else {
              //portrait
              Image::make($path.'/'.$featuredName)-&gt;resize(null, 1500, function ($constraint) {
                  $constraint-&gt;aspectRatio();
              })-&gt;save($path.'/web_'.$featuredName);
              Image::make($path.'/'.$featuredName)-&gt;resize(null, 300, function ($constraint) {
                  $constraint-&gt;aspectRatio();
              })-&gt;save($path.'/thumbs_'.$featuredName);
          }
      }
      $gallery = Galleries::create($input);
      $gallery-&gt;categories()-&gt;attach($request-&gt;input('categories_list'));
  
  
      $files = Input::file('images');
      $uploadcount = 1;
      if (!file_exists('images/'.$name.'/')) {
          mkdir('images/'.$name.'/', 0777, true);
      }
      $destinationPath = 'images/'.$name.'/';
      if (!file_exists($destinationPath.'/')) {
          mkdir($destinationPath.'/', 0777, true);
      }
      if (!file_exists($destinationPath.'/')) {
          mkdir($destinationPath.'/', 0777, true);
      }
      foreach ($files as $file) {
          $extensie = $file-&gt;getClientOriginalExtension();
  
          $filename = str_replace(' ', '_', $input['name']) . $uploadcount . '.' . $extensie;
          $file-&gt;move($destinationPath, $filename);
          Photos::create(['file' =&gt; $filename, 'galleries_id' =&gt; $gallery-&gt;id]);
          //landcape or Portrait
          list($width, $height) = getimagesize($path.'/'.$featuredName);
          if ($width &gt; $height) {
              //landscape
              Image::make($destinationPath.'/'.$filename)-&gt;resize(1500, null, function ($constraint) {
                  $constraint-&gt;aspectRatio();
              })-&gt;save($destinationPath.'/web_'.$filename);
              Image::make($destinationPath.'/'.$filename)-&gt;resize(300, null, function ($constraint) {
                  $constraint-&gt;aspectRatio();
              })-&gt;save($destinationPath.'/thumbs_'.$filename);
          } else {
              //portrait
              Image::make($destinationPath.'/'.$filename)-&gt;resize(null, 1500, function ($constraint) {
                  $constraint-&gt;aspectRatio();
              })-&gt;save($destinationPath.'/web_'.$filename);
              Image::make($destinationPath.'/'.$filename)-&gt;resize(null, 300, function ($constraint) {
                  $constraint-&gt;aspectRatio();
              })-&gt;save($destinationPath.'/thumbs_'.$filename);
          }
          $uploadcount++;
      }
  
      Session::flash('created_galleries', 'The Gallery has been created with ' . $uploadcount . ' images');
      return redirect('/admin/galleries');
  }
  </code></pre>