kennybjr87's avatar

Multiple image uploads with keywords

I'm using a js plugin called dropzone to allow multiple image uploads. After the images are stored on the view I have a foreach statement getting all images and adding a input field for keywords

View

@foreach ($images as $image)
            <div class="row">
                <div class="col-md-6">
                    <img src="{{$image->image}}">
                    <input type="hidden" name="imageIds[]" value="{{$image->id}}">
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        <div class="form-group {{ $errors->has('keyword-tag') ? ' has-error' : '' }}">
                            <label for="keyword-tag">Keyword</label>
                            <select name="keyword-tag[]" id="keyword-tag" style="width: 100%" class="select2" multiple="multiple">
                            </select>
                        </div>
                    </div>
                </div>
            </div>
        @endforeach

Controller

$imageIds = $request->input('imageIds', []);
$keywordTags = $request->input('keyword-tag', []);

 $imageInfo = MassUpload::find($imageIds);

        foreach ($imageInfo as $image) {

            $newImage = Images::create([
                'image'        => '/upload/banner-image/' . $image->image_name,
                'image_size'   => $image->image_size,
                'status'       => 1,
                'image_width'  => $image->image_width,
                'image_height' => $image->image_height,
            ]);
        }

I know if I add the attach method to the controllers foreach I can expect to have all images having the same keywords which is not what I want each image should have its own unique keywords. How can I break this up so that each image has its own unique keywords and how can I get all of that from the controller?

0 likes
6 replies
jlrdw's avatar

I don't understand the keyword thing, when you upload, the common dialog gets the file name. An example, not laravel but similar:

    public function add() {
        if (isset($_POST['submit'])) {
            $lid = DB::table('recents')->count();
            $k = -1;
            if (empty($lid) || strlen($lid) == 0 || is_null($lid)) {
                $lid = 1;
            }
            $newname = '';
            $destinationPath = ROOTDIR . 'upload/imgrecent/';
            $files = Input::file('ufile');
            //$names = [];
            $arrname = [];
            foreach ($files as $file) {
                $k = $k + 1;
                if (empty($file)) {
                    $arrname[$k] = "";
                } else {
                    $file_name = $file->getClientOriginalName();
                    $file_ext = $file->getClientOriginalExtension();
                    $lid = $lid + $k + 1;
                    $fileInfo = pathinfo($file_name);
                    $filename = $fileInfo['filename'];
                    print_r($filename);
                    $arrname[$k] = $filename . $lid . "." . $file_ext;
                    $file->move($destinationPath, (string) $arrname[$k]);
                }
            }
            $pic1 = Cln::fixValue((string) $arrname[0]);
            $pic2 = Cln::fixValue((string) $arrname[1]);
            $pic3 = Cln::fixValue((string) $arrname[2]);
            $pic4 = Cln::fixValue((string) $arrname[3]);
            $comments = Cln::fixValue($_POST['comments']);
            if (!isset($error)) {
                $postdata = array(
                    'pic1' => $pic1,
                    'pic2' => $pic2,
                    'pic3' => $pic3,
                    'pic4' => $pic4,
                    'comments' => $comments
                );
                DB::table('recents')->insert($postdata);
            }
        }//end add        

notice code in foreach

foreach ($files as $file) {
                $k = $k + 1;
                if (empty($file)) {
                    $arrname[$k] = "";
                } else {
                    $file_name = $file->getClientOriginalName(); ///THIS LINE
                    $file_ext = $file->getClientOriginalExtension();
                    $lid = $lid + $k + 1;
                    $fileInfo = pathinfo($file_name);
                    $filename = $fileInfo['filename'];
                    print_r($filename);
                    $arrname[$k] = $filename . $lid . "." . $file_ext;  //give unique name
                    $file->move($destinationPath, (string) $arrname[$k]);
                }
            }

Are you saying your package cannot get the original file name prior to the move? And this

$arrname[$k] = $filename . $lid . "." . $file_ext;

is if I already have rover123.jpg, then it would be like rover456.jpg. Two dogs, same name, but image is

image_name + record_count + 1.jpg
kennybjr87's avatar

For each image, there is an input to enter keywords aka tags so like image1 will have tags/keywords image2 will have its own tags/keywords I currently have the input set as an array but if I insert the attach for the pivot table all images will have the same exact tags and I don't want that each image should have its own.

jlrdw's avatar

Assign any extra parts of the name once they've been uploaded to the temp folder. Like I did.

Cronix's avatar
Cronix
Best Answer
Level 67
$imageIds = $request->input('imageIds', []);
$keywordTags = $request->input('keyword-tag', []);

$imageInfo = MassUpload::find($imageIds);

foreach ($imageInfo as $key => $image) { // use $key as index

    $newImage = Images::create([
        'image'        => '/upload/banner-image/' . $image->image_name,
        'image_size'   => $image->image_size,
        'status'       => 1,
        'image_width'  => $image->image_width,
        'image_height' => $image->image_height,

        // if index exists for $keywordTags, use that, else empty string
        'keywords' => $keywordTags[$key] ?? '' 
    ]);
}
1 like
kennybjr87's avatar

I got it within the views for each I added the $key => $image to add a key to the input name which gave them each a unique name then again added the $key to the controllers foreach and was able to loop through the inputs and add the correct keywords to each image thanks for the hint!

Please or to participate in this conversation.