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');
}
Was haben Sie versucht, die Leistung zu verbessern? – Skami
Können Sie einige Messwerte hinzufügen? Wie viele Fotos gleichzeitig? zu langsam oder zu viel Erinnerung sind zu subjektiv und nicht sehr sachlich. –