Im trying to save a resized copy of an image that has been uploaded, but Image Interventions doesent seem to know in what root directory to begin in, or maybe it is just me that dosent know how to configure it propertly.
This is my code in AvatarImageController#store
public function store(Request $request, Game $game)
{
if ($request->hasFile('game_image')) {
$request->validate([
'game_image' => 'mimes:jpeg,png|max:1014'
]);
$file = $request->file('game_image');
$filename_thumbnail = "thumb_". $file->hashName();
$path_full = $request->file('game_image')->store('images/test_folder');
$path_thumb = $request->file('game_image')->storeAs('images/test_folder/thumbs', $filename_thumbnail);
// resize image
$path_thumb = Intervention::make("./storage/app/public/" . $path_thumb)->resize(300, 200);
$path_thumb->save();
$image = new Image;
$image->full = basename($path_full);
$image->thumb = $filename_thumbnail;
$game->images()->save($image);
return back()->with('success', "Success!! Image uploaded.");
}else{
return back()->with('success', 'Ooops.. something went wrong.');
}
abort(500, 'Could not upload image :(');
}
The error i receive:
Intervention\Image\Exception\NotReadableException
Image source not readable
I suspect this is because im not in the correct path, because of these lines from above function:
// resize image
$path_thumb = Intervention::make("./storage/app/public/" . $path_thumb)->resize(300, 200);
$path_thumb->save();
And it is a real hassel working with explicit paths when using git as deployment.
You can make image directly like this:
$uploadedFile = $request->file('game_image');
$image = Image::make($uploadedFile);
Resize it:
$image = $image->resize(300, 200);
and save it like this:
$image_data = $image->encode('jpg')->__toString();
$relative_file_path = 'images/test_folder/';
Storage::put($relative_file_path, $image_data)
use the storage() helper?
Intervention::make(storage_path('app/public/") . $path_thumb)
Related
I want to update image, I want if image is uploaded, image will be updated, if not it does not update the image, then the previous image remains.
in my example it gives empty for image, it update same image is not upload.
annoncesController.php
public function update(Request $request, $id){
$file=File::find($id);
$file->type = $request->type;
if($request->hasFile('image'))
{
$path = $request->image->store('annonces');
$file->image = $path;
}
$file->update([
$file->type = $request->type,
$file->image = $request->image
]);
return Redirect::to("file")
->withSuccess('Great! file has been successfully uploaded.');
}
You are setting the path to the newly uploaded file within the if condition and then overriding it in the update statement with $request->image (probably binary image data). Plus there are few typos/syntax errors in your code as pointed out in comment.
You can try the below
public function update(Request $request, $id){
$file=File::find($id);
if($request->hasFile('image') && $request->file('image')->isValid())
{
//Validate the mime type, file size etc if required
//Store the newly uploaded image and get its path.
$path = $request->image->store('annonces');
//Delete the existing image
Storage::delete($file->image);
//Update the new path
$file->image = $path;
}
$file->type = $request->type;
$file->save();
return Redirect::to("file")
->withSuccess('Great! file has been successfully uploaded.');
}
I am hoping someone here will be able to help with this;
When a post is being updated in my project, I'd like for the image file that was uploaded during the initial post creation time, to be the same, if I don't want to upload a new one during an update.
**Also, if I do upload a new one during the update, I'd like for it to replace the old one. So, the old one gets deleted.
Here's what I currently have for the initial creation of posts;
if ($request->hasFile('post_avatar')){
$postimg = $request->file('post_avatar');
$postImgName = Str::slug($request->post_title) . '.' . $postimg->getClientOriginalExtension();
$destinationPath = public_path('/postImages');
$imagePath = $destinationPath. "/". $postImgName;
$postimg->move($destinationPath, $postImgName);
$post->post_avatar = $postImgName;
}
Thanks in advance!
I am assuming that you keep the name saved for the old image,
You can do like this,
$post_image = Str::slug($post->post_title); // taking from your old Post model instance lets say $post = Post::find(%id);
if ($request->hasFile('post_avatar')){
$image_path = public_path("/postImages/".$post_image);
if (File::exists($image_path)) {
File::delete($image_path);
}
$postimg = $request->file('post_avatar');
$postImgName = Str::slug($request->post_title) . '.' . $postimg->getClientOriginalExtension();
$destinationPath = public_path('/postImages');
$imagePath = $destinationPath. "/". $postImgName;
$postimg->move($destinationPath, $postImgName);
$post->post_avatar = $postImgName;
} else{
$post->post_avatar = $post_image;
}
$post->save();
Edit :
$post_image = Str::slug($post->post_title);
to
$post_image = $post->post_title;
Alright, below is the workflow for achieving what you have asked in your question. While, I'm not providing all the code, I believe this will be sufficient enough to guide you.
public function update(Request $request, $id)
{
$post = Post::findOrFail($id);
if ($request->hasFile('post_avatar')) {
$postimg = $request->file('post_avatar');
if ($post->post_avatar) {
// delete old image
// save new image
} else {
// save new image
}
}
// save post and redirect
}
I am working on uploading a video locally and then use it locally to upload to s3. It successfully uploads to s3 but I get this error:
Image source not readable
/var/www/yt/vendor/intervention/image/src/Intervention/Image/AbstractDecoder
I found this error when I was trying to delete the image after I uploaded it to s3. I think it is the reason the delete does not work. How do I fix the error?
public function avatar(Request $request)
{
$imageData = $request->get('image');
$img = Image::make($request->get('image'))->fit(300)->encode('jpg');
// calculate md5 hash of encoded image
$hash = md5($img->__toString());
// use hash as a name
$path = "avatars/{$hash}.jpg";
// save it locally
$test = $img->save(storage_path($path));
// $url = "/images/{$hash}.jpg"
$url_local = $path;
$path = Storage::putFile('avatars', new File(storage_path($url_local)));
if($path){
//TODO make into event
Storage::disk('local')->delete(storage_path($url_local));
// \Debugbar::error($test);
}
$user = User::find(Auth::id());
$user->avatar_url = $path;
if ($user->save()) {
return response()->json(['avatar' => $user->gen_avatar()]);
}
}
I have integrated a cropping script that returns either an image or an HTMLCanvasElement in my Vue Component. In my component's crop method, I then make an axios post call to my upload function. Here is the crop method:
crop() {
var croppedCanvas = this.cropper.getCroppedCanvas()
var img = croppedCanvas.toDataURL('image/jpeg', 1.0);
var formData = new FormData();
formData.append('image', img) // I can send croppedCanvas here too if I want
axios.post('/api/upload', formData)
}
Now the problem. I can't seem to save the HTMLCanvasElement or the Image as a JPG file. I tried so many different approaches (toDataURL, toBlob, etc.) and have failed in every single one. My latest implementation was as follows:
public function upload(Request $request) {
$input = $request->all();
$img = $input['image'];
Storage::put('picture.jpg', $img);
return response(Response::HTTP_OK);
}
This saves a 26 byte file in the storage/public folder which is unaccessible.
Can someone please help with how to go about this.
I simply want to save the HTMLCanvasElement as a JPG file in my storage/public folder.
Thanks.
Actually as mentioned in the docs over here : https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL
toDataURL method returns back a base64 encoded data so you have to decode it on the server side
you can use the base64_decode method in laravel like this
public function upload(Request $request) {
$img = $request->image;
$img = str_replace('data:image/jpeg;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
Storage::put('picture.jpg', $data);
return response(Response::HTTP_OK);
}
I am trying to use laravel intervention plugin. I installed it without problem but can't use it.
I am trying to make a test function which returns resized image, but without success;
I think the problem may be in image path, please help me fix my code.
function test($img)
{
/* $img = Image::make('public/image1.jpg');
$img->resize(300, 200);
return $img; */
$image = Image::make('http://localhost/cms/digital-cms/public/image1.jpg')->resize(200, 200, function ($c) {
$c->aspectRatio();
$c->upsize();
});
return $image;
//$h=200; $w=200;
//return Image::make(public_path('public/image1.jpg')->resize($h, $w)->response('jpg'));
}
//get image
$image=$request->file('image');
//rename image
$input = time().'.'.$image->getClientOriginalExtension();
//your directory to upload
$destinationPath = 'main/images/company';
//save and resize image
$img = Image::make($image->getRealPath());
$img->resize(20,20, function ($constraint) {
$constraint->aspectRatio();
})->save($destinationPath.'/'.$input);
You should use response function on Image class to return the image
$image = Image::make('http://localhost/cms/digital-cms/public/image1.jpg')->resize(200, 200, function ($c) {
$c->aspectRatio();
$c->upsize();
});
return $image->response();
In my case, I was resizing, cropping (fit) but the final image is still the same as original. Found out that I had to add function encode, to produce the manipulated image
return $image->encode('jpg', 80);