2017-09-21 4 views
0

Für meine Website möchte ich eine Galerie erstellen und Fotos hochladen. Dieses Foto muss auf zwei Formate skaliert werden.Laravel 5, Meine Create-Methode ist sehr langsam, wie kann es verbessern?

Ich erstelle eine Methode in meinem Controller, um Galerien zu erstellen, aber ist sehr langsam und benötigt zu viel Speicher. Wie kann ich es verbessern?

Ist es möglich, dies zu tun oder brauche ich einen Ajax-Anruf?

public function store(GalleryCreateRequest $request) 
{ 
    //dd($request->all()); 

    $input = $request->all(); 
    $name = str_replace(' ', '_', $input['name']); 
    if (Input::file('featured_image')) { 
     $featured = Input::file('featured_image'); 
     $extensieFeatured = $featured->getClientOriginalExtension(); 
     if (!file_exists('images/'.$name)) { 
      mkdir('images/'.$name, 0777, true); 
     } 
     $path = 'images/'.$name; 
     $featuredName = str_replace(' ', '_', $input['name']) . '_featured' . '.' . $extensieFeatured; 
     $featured->move($path, $featuredName); 
     $input['featured_image'] = $featuredName; 
     //landcape or Portrait 
     list($width, $height) = getimagesize($path.'/'.$featuredName); 
     if ($width > $height) { 
      //landscape 
      Image::make($path.'/'.$featuredName)->resize(1500, null, function ($constraint) { 
       $constraint->aspectRatio(); 
      })->save($path.'/web_'.$featuredName); 
      Image::make($path.'/'.$featuredName)->resize(300, null, function ($constraint) { 
       $constraint->aspectRatio(); 
      })->save($path.'/thumbs_'.$featuredName); 
     } else { 
      //portrait 
      Image::make($path.'/'.$featuredName)->resize(null, 1500, function ($constraint) { 
       $constraint->aspectRatio(); 
      })->save($path.'/web_'.$featuredName); 
      Image::make($path.'/'.$featuredName)->resize(null, 300, function ($constraint) { 
       $constraint->aspectRatio(); 
      })->save($path.'/thumbs_'.$featuredName); 
     } 
    } 
    $gallery = Galleries::create($input); 
    $gallery->categories()->attach($request->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->getClientOriginalExtension(); 

     $filename = str_replace(' ', '_', $input['name']) . $uploadcount . '.' . $extensie; 
     $file->move($destinationPath, $filename); 
     Photos::create(['file' => $filename, 'galleries_id' => $gallery->id]); 
     //landcape or Portrait 
     list($width, $height) = getimagesize($path.'/'.$featuredName); 
     if ($width > $height) { 
      //landscape 
      Image::make($destinationPath.'/'.$filename)->resize(1500, null, function ($constraint) { 
       $constraint->aspectRatio(); 
      })->save($destinationPath.'/web_'.$filename); 
      Image::make($destinationPath.'/'.$filename)->resize(300, null, function ($constraint) { 
       $constraint->aspectRatio(); 
      })->save($destinationPath.'/thumbs_'.$filename); 
     } else { 
      //portrait 
      Image::make($destinationPath.'/'.$filename)->resize(null, 1500, function ($constraint) { 
       $constraint->aspectRatio(); 
      })->save($destinationPath.'/web_'.$filename); 
      Image::make($destinationPath.'/'.$filename)->resize(null, 300, function ($constraint) { 
       $constraint->aspectRatio(); 
      })->save($destinationPath.'/thumbs_'.$filename); 
     } 
     $uploadcount++; 
    } 

    Session::flash('created_galleries', 'The Gallery has been created with ' . $uploadcount . ' images'); 
    return redirect('/admin/galleries'); 
} 
+0

Was haben Sie versucht, die Leistung zu verbessern? – Skami

+0

Können Sie einige Messwerte hinzufügen? Wie viele Fotos gleichzeitig? zu langsam oder zu viel Erinnerung sind zu subjektiv und nicht sehr sachlich. –

Antwort

0

Wenn Sie Schleife durch viele große Bilder und deren Verarbeitung auch wird es wahrscheinlich ein langer und arbeitsspeicherintensiven Prozess sein, egal was. Es macht Sinn, hierfür eine AJAX-Anfrage zu verwenden. Vielleicht interessiert Sie auch Laravels queue system.

0

Es gibt nur wenige Dinge, die Sie für die Leistung berücksichtigen müssen,

Asynchronous Jobs:

Für zeitaufwändige Aufgaben wie eine große Datei zu Cloud-Storage-Upload oder E-Mail zu senden, wir Asynchronous jobs verwenden.

Grenze hochladen Größe:

In einfachem Szenario sollte den Code funktionieren, aber das Problem ist, wenn ein Benutzer große Datei zu. Nun, offensichtlich sollte es zuerst auf Serverebene und dann auf Codeebene behandelt werden.

Vorschläge:

public function submit(Request $request) 
    { 
     $input = $request->all(); 
     $name = str_replace(' ', '_', $input['name']); 
     //if (Input::file('featured_image')) { 
     // When you already has access to request why requesting again using a proxy call throught Input Facade 

     if ($request->hasFile('featured_image')) { 

      $featured = $request->file('featured_image'); 

      $extensieFeatured = $featured->getClientOriginalExtension(); 

      // Since you're not using else 
      // Mean directory is either there already or you're creating it. 
      // in both cases $request->file()->move() should help 
      /*if (!file_exists('images/' . $name)) { 
       mkdir('images/' . $name, 0777, true); 
      }*/ 

      $path = 'images/' . $name; 

      $featuredName = str_replace(' ', '_', $input['name']) . '_featured' . '.' . $extensieFeatured; 

      $featured->move($path, $featuredName); 

      $input['featured_image'] = $featuredName; 
      //landcape or Portrait 

      // reading a file at disk is 0.2 ms to 10 ms when you already have a file in memory why reading it again. 
      //list($width, $height) = getimagesize($path . '/' . $featuredName); 

      $image = Image::make($featured); 

      if ($featured->width > $featured->height) { 
       //landscape 

       $image->resize(1500, null, function ($constraint) { 
        $constraint->aspectRatio(); 
       })->save($path . '/web_' . $featuredName); 

       $image->resize(300, null, function ($constraint) { 
        $constraint->aspectRatio(); 
       })->save($path . '/thumbs_' . $featuredName); 

      } else { 
       //portrait 
       $image->resize(null, 1500, function ($constraint) { 
        $constraint->aspectRatio(); 
       })->save($path . '/web_' . $featuredName); 

       $image->resize(null, 300, function ($constraint) { 
        $constraint->aspectRatio(); 
       })->save($path . '/thumbs_' . $featuredName); 
      } 
     } 

     $gallery = Galleries::create($input); 
     $gallery->categories()->attach($request->input('categories_list')); 


     $files = $request->file('images'); 
     $uploadcount = 1; 

     // You don't need to check this. 
     // since you're creating directories, mean www-data has the access to the file system 
     // so laravel UploadedFile can handle this. 
     /*if (!file_exists('images/' . $name . '/')) { 
      mkdir('images/' . $name . '/', 0777, true); 
     }*/ 

     $destinationPath = 'images/' . $name . '/'; 
     // You don't need this. 
     /*if (!file_exists($destinationPath . '/')) { 
      mkdir($destinationPath . '/', 0777, true); 
     }*/ 

     // not even this. 
     /*if (!file_exists($destinationPath . '/')) { 
      mkdir($destinationPath . '/', 0777, true); 
     }*/ 
     foreach ($files as $file) { 
      $extensie = $file->getClientOriginalExtension(); 

      $filename = str_replace(' ', '_', $input['name']) . $uploadcount . '.' . $extensie; 

      $file->move($destinationPath, $filename); 

      Photos::create(['file' => $filename, 'galleries_id' => $gallery->id]); 

      // You can repeat the above procedure again here. 
      //landcape or Portrait 
      list($width, $height) = getimagesize($path . '/' . $featuredName); 

      if ($width > $height) { 
       //landscape 
       Image::make($destinationPath . '/' . $filename)->resize(1500, null, function ($constraint) { 
        $constraint->aspectRatio(); 
       })->save($destinationPath . '/web_' . $filename); 
       Image::make($destinationPath . '/' . $filename)->resize(300, null, function ($constraint) { 
        $constraint->aspectRatio(); 
       })->save($destinationPath . '/thumbs_' . $filename); 
      } else { 
       //portrait 
       Image::make($destinationPath . '/' . $filename)->resize(null, 1500, function ($constraint) { 
        $constraint->aspectRatio(); 
       })->save($destinationPath . '/web_' . $filename); 
       Image::make($destinationPath . '/' . $filename)->resize(null, 300, function ($constraint) { 
        $constraint->aspectRatio(); 
       })->save($destinationPath . '/thumbs_' . $filename); 
      } 
      $uploadcount++; 
     } 

     Session::flash('created_galleries', 'The Gallery has been created with ' . $uploadcount . ' images'); 
     return redirect('/admin/galleries'); 
    } 

Wort der Beratung:

dick Schreiben Sie nicht (zu viel Code in einer Funktion) -Controller, versuchen SOA (Service Oriented Architecture) zu verwenden und Feste Prinzipien. Suchen Sie in Lucid