angelo t. February 2016

Dropzonejs + Image Intervention php return value is random html codes

My upload code is working good using dropzonejs, and my next task is to create a thumbnail for the image that was uploaded. And so I've used Image Intervention and I was able to create a thumbnail, the only problem I have is when I try to combine the two task in one method, dropzonejs is returning a bunch of html codes. and well, actually everytime I put a return statement in my UploadImage method it returns random html codes. I have read the documentation of dropzonejs and I don't have an idea where the codes coming from. any advice is appreciated. thank you in advance for any help and suggestions.

below are my codes.

Route:

Route::post('uploadimage', 'ProductController@uploadImage');

Controller:

public function uploadImage(Request $request){

       //create folder
        $products = new Products();

        $args['foldername'] = $request->input('folname');

        $products->createDirectory($args);

        $origName = $request->file('file')->getClientOriginalName();



        $request->file('file')->move(public_path().env('FOLDER_NAME_IMAGES').$args['foldername'].'/', $origName );

        $this->createThumbnail($request);

    }

public function createThumbnail($request){

    $origName = $request->file('file')->getClientOriginalName();
    $image = new Image();
    $file = $request->file('file');
    $objImage = $image->make($file->getRealPath());
    //create thumbnail
    $objImage->save(public_path().env('FOLDER_NAME_IMAGES').'fad611c8-56c7-4a7a-8af9-a2908267f08f'.'/'.$origName)
        ->resize(200,200)            // ->greyscale()
        ->save(public_path().env('FOLDER_NAME_IMAGES').'fad611c8-56c7-4a7a-8af9-a2908267f08f'.'/'.'thumb-'.$origName);

}

JS code:

var previewNode = document.querySelector("#template");
            previewNode.id = "";
            var previewTemplate = previewNode.parentNode.in        

Answers


angelo t. February 2016

Okay, found the problem.It's the sequence of my methods and most importantly the variable that is being passed to Intervention object is wrong.

so basically the problem is I was trying to save a file that has already been moved.the createThumbnail method should go first before the saving or moving of the original file.

correct code below.

public function uploadImage(Request $request){

       //create folder
        $products = new Products();

        $args['foldername'] = $request->input('folname');

        $products->createDirectory($args);

        $origName = $request->file('file')->getClientOriginalName();

        $this->createThumbnail($request);    

        $request->file('file')->move(public_path().env('FOLDER_NAME_IMAGES').$args['foldername'].'/', $origName );



    }

and also I found out that in my createThumbnail method, it's not accepting the value of the getRealPath() method.

previous code:

$objImage = $image->make($file->getRealPath());

the right way is to just pass the $file variable

correct code:

$file = $request->file('file');
$objImage = $image->make($file);

and also for the random html codes. it's not random, I just missed reading the bottom part of the codes it's basically the html codes of laravel error page. problem solved.

Post Status

Asked in February 2016
Viewed 1,657 times
Voted 4
Answered 1 times

Search




Leave an answer