Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Photo Fields in a single CRUD #126

Closed
AlexMilutinovic opened this issue Nov 19, 2017 · 5 comments
Closed

Multiple Photo Fields in a single CRUD #126

AlexMilutinovic opened this issue Nov 19, 2017 · 5 comments

Comments

@AlexMilutinovic
Copy link

I've created CRUD containing two Photo fields (cover and map). Problem is that only the first selected image is being uploaded. So, if I first select the cover image, then map image, only the cover image is being uploaded.

I noticed that both fields are filled with string values. In above-mentioned case, cover field gets correct value (e.g. 1511684065-filename.jpg) and map field gets something like /tmp/jVJthcgcF. If I upload just the map image, it gets uploaded successfully and the filename is correct.

How can I solve this problem?

@PovilasKorop
Copy link
Collaborator

@aleksandarmilutinovic I guess it's a bug inside of the package which is not easy to solve. We are currently not actively supporting the free package fixes and switched to online version at quickadminpanel.com - so that's the one I would recommend if you want quick fix. Otherwise - this issue will wait until someone on the team will find time, a few weeks at least.

@AlexMilutinovic
Copy link
Author

@PovilasKorop Thanks for reply. I'll probably try to fix it and make a pull request. I suppose that pull request verification will be quicker that fixing the bug :)

@AlexMilutinovic AlexMilutinovic changed the title Multiple Photo Fields in single CRUD Multiple Photo Fields in single a CRUD Nov 26, 2017
@AlexMilutinovic AlexMilutinovic changed the title Multiple Photo Fields in single a CRUD Multiple Photo Fields in a single CRUD Nov 26, 2017
@AlexMilutinovic
Copy link
Author

@PovilasKorop Here is a fixed FileUploadTrait. I couldn't create Pull Request so, please, revise and update the trait so this issue can be fixed officially. Thanks in advance.

There was a bug in recreating the request (I've commented the changes). I didn't want to change the entire logic, but just to fix current issue.

Please let me know if there is anything wrong with the fix. Thanks!

<?php

namespace App\Http\Controllers\Traits;

use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;

trait FileUploadTrait
{

    /**
     * File upload trait used in controllers to upload files
     */
    public function saveFiles(Request $request)
    {
        if (!file_exists(public_path('uploads'))) {
            mkdir(public_path('uploads'), 0777);
            mkdir(public_path('uploads/thumb'), 0777);
        }
        $newRequest = null; // Variable to hold a new request created by above array merging
        foreach ($request->all() as $key => $value) {
            if ($request->hasFile($key)) {
                if ($request->has($key . '_w') && $request->has($key . '_h')) {
                    // Check file width
                    $filename = time() . '-' . $request->file($key)->getClientOriginalName();
                    $file     = $request->file($key);
                    $image    = Image::make($file);
                    Image::make($file)->resize(50, 50)->save(public_path('uploads/thumb') . '/' . $filename);
                    $width  = $image->width();
                    $height = $image->height();
                    if ($width > $request->{$key . '_w'} && $height > $request->{$key . '_h'}) {
                        $image->resize($request->{$key . '_w'}, $request->{$key . '_h'});
                    } elseif ($width > $request->{$key . '_w'}) {
                        $image->resize($request->{$key . '_w'}, null, function ($constraint) {
                            $constraint->aspectRatio();
                        });
                    } elseif ($height > $request->{$key . '_w'}) {
                        $image->resize(null, $request->{$key . '_h'}, function ($constraint) {
                            $constraint->aspectRatio();
                        });
                    }
                    $image->save(public_path('uploads') . '/' . $filename);
                    // Determine which request's data to use further
                    $requestDataToMerge = $newRequest == null ? $request->all() : $newRequest->all();
                    // Create new request without changing the original one (prevents removal of specific metadata which disables parsing of a second file)
                    $newRequest = new Request(array_merge($requestDataToMerge, [$key => $filename]));
                } else {
                    $filename = time() . '-' . $request->file($key)->getClientOriginalName();
                    $request->file($key)->move(public_path('uploads'), $filename);
                    // Determine which request's data to use further
                    $requestDataToMerge = $newRequest == null ? $request->all() : $newRequest->all();
                    // Create new request without changing the original one (prevents removal of specific metadata which disables parsing of a second file)
                    $newRequest = new Request(array_merge($requestDataToMerge, [$key => $filename]));
                }
            }
        }
        
        return $newRequest == null ? $request : $newRequest;
    }
}

@PovilasKorop
Copy link
Collaborator

@AlexMilutinovic thanks a lot, one of our team members will test and push the fix, probably next week.

KarolisNarkevicius added a commit that referenced this issue Nov 30, 2017
@prashantshrma
Copy link

@AlexMilutinovic Thank you so much, I was facing the same issue. Thanks a lot. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants