I am trying to upload an image using Laravel it is working to the point that the images are being successfully placed into the destination folder but when I try to view them within my IDE I am told they are corrupt and that netbeans cannot open them.
I am wondering if my upload process is causing this?
This is my code:
public function handleCreate(){
$book = new Book;
$book->title = Input::get('title');
$book->desc = Input::get('desc');
//Img uploading...
$destinationPath = '';
$filename = '';
if(Input::hasFile('cover')){
$file = Input::file('cover');
$destinationPath = public_path().'/img/';
$filename = str_random(6) . "_" . $file->getClientOriginalName();
$uploadSuccess = $file->move($destinationPath, $filename);
}
$book->cover = $filename;
$book->save();
return Redirect::route('books')->with('msg', 'test');
}
Any help is much appreciated. I know the image is not corrupt before uploading as I can view on my desktop. The issue is only after uploading it.
Related
I'm trying to integrate Intervention/Image into my laravel project to create a thumbnail upon uploading an image.
The image upload itself works fine, it doesn't seem to have any issue recognizing Intervention itself.
Below is the code block. The error seems to happen on the line with the save statement, I'm able to die and dump the contents of $img after it's set.
$file = $request->file('image');
$name = md5($file->getClientOriginalName() . time());
$extension = $file->getClientOriginalExtension();
$fileName = $name . '.' . $extension;
$file->move('./uploads/images/', $fileName);
$img = Image::make($file)->fit(300);
$img->save('/uploads/thumbnails/' . $name, 60, 'jpg');
This is the error I'm getting:
SplFileInfo::getSize(): stat failed for /private/var/folders/87/p5x7mgy914qg9ytf2zccc6q00000gn/T/php3lshFS
After some searching I've found that this could be related to file size upload limits, but I've altered my php.ini file (all of this is local btw) to accept 20MB files and the file I'm trying to upload is less than 100kb. I've also reset both php through homebrew and apache. Still getting the error.
Is there any glaringly obvious issues in my use of Intervention? I'll happily provide more info, this is in the store function in one of my controllers btw.
Untested, but I do it like this:
public function thumbnail(Request $request){
$thumbDir= storage_path('app/public').'/uploads/thumbnails/';
$file = $request->file('image');
$filename = md5($file->getClientOriginalName() . time()).'.jpg';
// $name = md5($file->getClientOriginalName() . time());
// $extension = $file->getClientOriginalExtension();
// $fileName = $name . '.' . $extension;
// $file->move('./uploads/images/', $fileName);
Image::make($file)->encode('jpg', 60)->fit(300, null, function ($c) {
$c->aspectRatio();
$c->upsize();
})->save($thumbDir . $filename);
return back()->with('success','The Image Has Been Added.');
}
Using the below code, it should upload to the path specified in $path but for some reason, it saves the links to all pictures to c:\xampp\tmp with the .tmp extension but will move them to the correct folder anyway. What have I done wrong?
public function store(Request $request){
//
if($file = $request->file('image')){
$name = $file->getClientOriginalName();
$path = 'public/images';
if($file->move($path, $name)){
$post = new Gallery();
$post->image = $request->image;
$post->name = $request->name;
$post->species_id = $request->species_id;
$post->tag = $request->tag;
$post->patreon = $request->tag;
$post->save();
return redirect()->route('admin.gallery.index');
};
};
}
I have deployed a Laravel project in a shared hosting. I have changed my .env file and copied all files from the public folder to the main directory and deleted the public folder. Now the problem is, whenever I am trying to upload an image, I am getting an internal server error. I suppose the problem is the Image Intervention is not getting the right folder to save the image. I have tried the both ways given below:
if ($request->hasfile('admin_pro_pic')) {
$image = $request->file('admin_pro_pic');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('/images/admin/' . $filename);
Image::make($image)->resize(950, 700)->save($location);
$admin->admin_pro_pic = $filename;
}
and
if ($request->hasfile('admin_pro_pic')) {
$image = $request->file('admin_pro_pic');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = '/images/admin/' . $filename;
Image::make($image)->resize(950, 700)->save($location);
$admin->admin_pro_pic = $filename;
}
But None of these is working. Any possible Solution?
Use laravel base_path function, so your code will look like this
if ($request->hasfile('admin_pro_pic')) {
$image = $request->file('admin_pro_pic');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = base_path().'/images/admin/' . $filename;
Image::make($image)->resize(950, 700)->save($location);
$admin->admin_pro_pic = $filename;
}
Answer Update
Issue was fileinfo extension missing or disbaled.
Try This.
use Storage;
use File;
if(!empty($request->file('admin_pro_pic')))
{
$file = $request->file('admin_pro_pic') ;
$fileName = $file->getClientOriginalName() ;
$destinationPath = public_path().'/images/' ;
$file->move($destinationPath,$fileName);
$admin->image=$fileName;
}
Create imges inside public directory.
I am handling it like this:
// check for defined upload folder inside .env file, otherwise use 'public'
$publicUploadDir = env('UPLOAD_PUBLIC', 'public/');
// get file from request
$image = $request->file('admin_pro_pic');
// hasing is not necessary, but recommended
$new['path'] = hash('sha256', time());
$new['folder] = 'images/admin/';
$new['extension'] = $file->extension();
// store uploaded file and retrieve path
$image->storeAs($publicUploadDir, implode($new, '.'));
When i save image with my form i change the file name. Everything works nice except file extension. When i change file name the extension is missing and file is saved without it. Please help me to solve my problem i want to store also image extension.
This is my code where i update image name.
$image = $request->file('image');
if ($image) {
$imgPath = 'public/post-image/';
$imgName = uniqid('img_');
$store = $request->file('image')->storeAs(
$imgPath, $imgName
);
$post->image = $imgName;
}
Try this:
$image = $request->file('image');
if ($image) {
$imgPath = 'public/post-image/';
$imgName = uniqid('img_') . '.' . $image->extension();
$store = $image->storeAs($imgPath, $imgName);
$post->image = $imgName;
}
Hi please help me am new to laravel
I want to store multiple images in table... With this code am unable to save.
Help me for this..
Here in my View
{{Form::open(array('url'=>'businessdirectory/business', 'files'=>true))}}
{{Form::label('image','Upload Image')}}
<div class="form-group">{{Form::file('image[]',array('multiple'=>true))}}
</div>
{{Form::close()}}
In my Controller
if(Input::file('image'))
{
$image = Input::file('image');
foreach($image as $img) {
$destination = 'images';
$filename = $img->getClientOriginalName();
$path = 'images/'.$filename;
$uploadSuccess = $img->move($destination,$filename);
}
}
else
{
$path='images/default.JPG';
}
$business = new Business();
$business->image = $path;
It's not advisable to store images on database. Just save the path of the images instead.
If you really need to store image on db. Make sure you set the column to blob as it need more space. Then, get the image content and type, then save it.
<?php
$image = fopen($image_path, 'rb');
// or
$image = file_get_contents($image_path);
$business = new Business;
$business->image = $image;
$business->imageType = "image/gif"; //
$business->save();
// ...
You need to put below code inside the foreach loop. In every loop you need to insert image path inside a database
$business = new Business();
$business->image = $path;
This is a working code in laravel 5.2.
$files = $request->file('file');
if($request->hasfile('file')){
$destinationPath = 'uploads';
foreach($files as $file){
$image = new Image;
$filename = $file->getClientOriginalName();
$image->name = $filename;
$image->save();
$file->move($destinationPath,$filename);
}