Upload image with different name but keep file extension - php

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;
}

Related

How to resize image before upload in laravel?

I want to resize image beofre upload it into database. I store image in this way
if ($image) {
$image_name = hexdec(uniqid());
$ext = strtolower($image->getClientOriginalExtension());
$image_full_name = $image_name . '.' . $ext;
$upload_path = 'foods/';
$upload_path1 = 'images/foods/';
$image_url = $upload_path . $image_full_name;
$success = $image->move($upload_path1, $image_full_name);
}
Now, I want to resize image before uploading it. I try to use intervation package, I try this
if ($image) {
$image_name = hexdec(uniqid());
$ext = strtolower($image->getClientOriginalExtension());
$image_full_name = $image_name . '.' . $ext;
$upload_path = 'foods/';
$upload_path1 = 'images/foods/';
$image_url = $upload_path . $image_full_name;
$img = Image::make($image)->resize(300, 200);
$img->save($upload_path1, 60, $image_full_name);
}
and I got this error
"Encoding format (1691942233153059.jpg) is not supported."
What is the best way to resize image before upload and also set image with unique name?
1- use getRealPath() inside Image::make()
2- save image in particular path.
if($request->hasFile('image')) {
$image = $request->file('image');
$file_name = $image->getClientOriginalName();
$image_resize = Image::make($image->getRealPath());
$image_resize->resize(300, 100);
$image_resize->save(public_path('img/' .$file_name));
}

delete image and thumbnail image in laravel

i try to delete some image file in laravel project. i follow tutorial on website, and its work, but tutorial only deleted image based name in column image. the thumbnail on folder image didnt deleted. how to delete image also the thumbnail image? pls help.
this is the store function.
if ($request->hasFile('image'))
{
$file = $request->file('image');
$image = $file->getClientOriginalName();
$destination = public_path() . '/galeri/';
$successUploaded = $request->file('image')->move($destination, $file->getClientOriginalName());
if($successUploaded)
{
$extension = $file->getClientOriginalExtension();
$thumbnail = str_replace(".{$extension}", "_thumb.{$extension}", $image);
Image::make($destination . '/' . $image)
->resize(250, 170)
->save($destination . '/' . $thumbnail);
}
$photo->image = $image;
}
$photo->save();
and this the destroy function
$photo = Photo::where('id', $id)->first();
File::delete('galeri/'.$photo->image);
if($photo){
$photo->delete();
}
Delete a single file
File::delete($filename);
Delete multiple files
File::delete($file1, $file2, $file3);
Delete an array of files
$files = array($file1, $file2);
File::delete($files);

Failed to rename image and upload using storeAs in Laravel

I created a form to store article with an image , and generate a resized version as thumbnail from it.
I want the image to be renamed after the article slug and stored in the "public/img/articles-images " directory but i keep receiving : "Image source not readable" error
This is the image upload handler function in my controller :
private function handleRequest($request)
{
$data = $request->all();
if ($request->hasFile('image')) {
$image = $request->file('image');
$fileName = $request->slug;
$successUploaded = $image->storeAs('img/articles-images', $fileName);
if($successUploaded) {
$width = config('cms.image.thumbnail.width');
$height = config('cms.image.thumbnail.height');
$extension = $image->getClientOriginalExtension();
$thumbnail = str_replace(".{$extension}", "_thumb.{$extension}", $fileName);
Image::make('img/articles-images' . '/' . $fileName)
->resize($width, $height)
->save('img/articles-images' . '/' . $thumbnail);
}
$data['image'] = $fileName;
}
return $data;
}
storeAs() method, which receives the path, the file name, and the (optional) disk as its arguments :
$successUploaded = $request->file('image')->storeAs(
'images', $fileName
);
I solved it ! Apparently there was no need to storeAs() method at all , the new code is like below :
if ($request->hasFile('image')) {
$image = $request->file('image');
$fileName = $request->slug.'.' .$image->getClientOriginalExtension();
$destination = $this->uploadPath;
$successUploaded = $image->move($destination, $fileName);
// //

get error while file upload in laravel

I am trying to upload image in laravel. But when i upload it gives error.
Call to a member function getClientOriginalExtension() on null
Here is my Blade file form
{{Form::open(array('url' => '/AddNewBlog','id'=>'blogadd' ,
'method' => 'post','class'=>'form-row','files'=>true,
"enctype"=>"multipart/form-data"))}}
And here is controller
$imagename_bg = time() . '.' . $photo->getClientOriginalExtension();
$destinationPath = public_path('/uploads/blog');
$thumb_img = Image::make($photo->getRealPath())->resize(750, 450);
$thumb_img->save($destinationPath . '/' . $imagename_bg, 80);
$photo->move($destinationPath, $imagename_bg);
Please help me how to resolve this problem.
I am not able to understand your code. if you are looking for uploading image and resize using intervention package try this:-
if($request->hasFile('blogimage')){
if (Input::file('blogimage')->isValid()) {
$file = Input::file('image');
$img = Image::make($file);
$img->resize(400,270);
$name = pathinfo($_FILES['image']['name']);
$destination = public_path('/uploads/blog');
$ext = $name['extension'];
$rand= time().str_random(6).date('h-i-s').'.'.$ext;
$img->save($destination.$rand);
}
}
Or Without intervention pacakge:-
if($request->hasFile('blogimage')){
if (Input::file('blogimage')->isValid()) {
$file = Input::file('blogimage');
$destination = public_path('/uploads/blog');
$ext= Input::file('blogimage')->getClientOriginalExtension();
$mainFilename =time().str_random(5).date('h-i-s');
$file->move($destination, $mainFilename.".".$ext);
}
}
Hope it helps!
$photo = $request->file('file_name');
You only need to use this:
$photo = $request->file("blogimage");
instead of:
$photo = $request->input("blogimage")
Hope this will fixed your problem!

how to save multiple images in table using laravel php

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);
}

Categories