Support the ongoing development of Laravel.io →
Requests Input
Last updated 1 year ago.
0

Are you using a request to validate the image? I do not resize my image put I do place in a specific folder using an event. In short, the image is validated as follows:

app/http/requests/AttachmentRequest

class AttachmentRequest extends Request {

        ...
        ... ...
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
	return [
                'attachment' => "required|mimes:jpeg,bmp,png",
                'user_id' => "required|integer",
                'story_id' => "required|integer",
        ];
	    }

    public function messages()
    {
        return [
            'attachment.required' => 'Pick a file to upload.',
            'attachment.mimes' => 'Not a valid file type. Valid types include jpeg, bmp and png.'
        ];
    }
}

Then in the controller

app/http/controllers/AttachmentController

    ...
    ... ...
/**
 * Store a newly created resource in storage.
 *
 * @return Response
 */
public function store( AttachmentRequest $request )
{
	// Merge the name of the file being uploaded into the Input array so it can be saved to the database.
	\Input::merge(array('name'=> \Input::file('attachment')->getClientOriginalName() ) );
	// Use the repository method "processPost" to populate and create a new instance of the model.
	if ( $this->AttachmentRepository->processPost( null, \Input::all() ) ) 
	{
		// Fire an event to move the uploaded file to permanent storage
		\Event::fire( new Upload( $this->AttachmentRepository ) );

		return \Response::json(['status' => 'true','message' => 'Successfully uploaded']);
	}
    ...
    ... ...

You will notice I "fire" an event called "upload" which has a handler called "Uploaded" and I pass a copy of my repository into the event...

class Upload extends Event {

   ...
   ... ...

public $Repository;

/**
 * Create a new event instance.
 *
 * @return void
 */
public function __construct( $Repository )
{
	$this->Repository = $Repository;
}

}

Lastly I have the event handler, where you would resize the image and move to your chosen location

app/Handlers/Events/Uploaded.php

...
... ...
class Uploaded {

    ...
    ... ...
/**
 * @param  AuthLogin  $event
 * @return void
 */
public function handle( Upload $event )
{
	\Input::file('attachment')->move( public_path() . '/assets/' .  $event->Repository->getModelName() . '/' . $event->Repository->getModel()->id, $event->Repository->getModel()->name );
}

}

In short, I use the repository pattern, requests and events. And I move my uploaded attachment into a folder under the public directory. The method "getModelName" is a custom method that gets the name of the model without the namespace so if I uploaded my very first attachment with an ID = 1 the folder structure would read

public/assets/Attachment/1/image_name.*

Hope it helps, apologies if it makes no sense. Many ways to skin a cat.

Cheers.

Last updated 9 years ago.
0
Solution

For image resizing I would use http://image.intervention.io/ and save the images in a certain folder after. Then save the filename on the DB.

0

You might wanna checkout my image caching package for imagine php. https://github.com/astroanu/laravel-image-cache this is what i'm using right now for all of my projects.

I've made a trait where you can simply save image file name on to the model itself. uploading is almost the same.

Last updated 9 years ago.
0

jacksoncharles thankou:

but its not solved yet

my controller is

<?php namespace App\Http\Controllers;
use Hash;
use Request;
class UserController extends Controller
{
	
	
	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	 public function __construct()
	{
		$this->middleware('auth');
	}
	public function index(UserController $request )
	{
		
		//$name= Input::file('photo')->move('/lp', 'po');
		if(Input::has('photo')){
			// Merge the name of the file being uploaded into the Input array so it can be saved to the database.
	\Input::merge(array('name'=> \Input::file('photo')->getClientOriginalName() ) );
	// Use the repository method "processPost" to populate and create a new instance of the model.
	if ( $this->AttachmentRepository->processPost( null, \Input::all() ) ) 
	{
		// Fire an event to move the uploaded file to permanent storage
		\Event::fire( new Upload( $this->AttachmentRepository ) );

		return \Response::json(['status' => 'true','message' => 'Successfully uploaded']);
	}
		
		}

	
	   
		$users=\Auth::user();
		return view('user')->with('users',$users);

	}
}

request file is

<?php namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class AttachmentRequest extends Request {


	 * Get the validation rules that apply to the request.
	 *
	 * @return array
	 */
	public function rules()
	{
	return [
				'attachment' => "required|mimes:jpeg,bmp,png",
				'user_id' => "required|integer",
				'story_id' => "required|integer",
		];
		}

	public function messages()
	{
		return [
			'attachment.required' => 'Pick a file to upload.',
			'attachment.mimes' => 'Not a valid file type. Valid types include jpeg, bmp and png.'
		];
	}
}

it showing : Call to a member function getClientOriginalName() on null

0

apparently your \Input::file('photo') is null. Does your form has the multi part form data header ?

0

Sign in to participate in this thread!

Eventy

Your banner here too?

arctushar arctushar Joined 5 Oct 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.