Saving a remote image from facebook graph api - php

Any idea what's wrong?
I end up with 1bytes files and they are wrong. Using Intervention Image & PHP. Tried many ways to get it but nothing... If I just want to display it works but I can't save the picture...
$avatar_url = 'http://graph.facebook.com/' . $id . '/picture?type=square&width=140&height=140&redirect=false';
$avatar_pic_url = json_decode(file_get_contents($avatar_url), true)['data']['url'];
dd($avatar_pic_url);
$avatar = Image::make($avatar_pic_url);
$extension = 'jpg';
// save it
$destinationPath = 'uploads/avatars/';
$filename = uniqid(). '.' . $extension;
$path_to_temp_image = $avatar->dirname . '/' . $avatar->filename;
$key = $destinationPath . $filename;
// Upload avatar to remote storage
$uploadSuccess = Storage::put($key, $path_to_temp_image);

Related

Digital Ocean 403 Forbidden when i'm upload from laravel local

I have a problem when I want to upload a file to digital ocean, there is a 403 error, even though I'm sure the key, secret and endpoint are correct
this is my simple code
$tes= Storage::disk('digitalocean')->putFileAs('url', request()->file('url'), time().'.'.$extension);
dd($tes);
if ($request->hasFile('url')) {
$file = $request->file('url');
$ext1 = $file->getClientOriginalExtension();
// $st_random = str_random(10);
//dd($file);
$st_random = Str::random(10);
$filename = $file->getClientOriginalName();
$filename = explode(".", $filename);
$ext = $ext1;
$filename = $filename[0] . $st_random . "." . $ext;
Storage::disk('digitalocean')->put('/' . $filename, File::get($file));
$csv = 'digitalocean/' . $filename;
}
note
use Storage;
use Str;
use File;
you have to try this file uploading . it may solve your problem .

Image uploaded in Laravel 5.8 keep saving as private image

I am trying upload a single image into a Post and it keeps saving it as a private image--heck, I don't even know where to find that file.
PostController.php
...
if ($request->hasFile('photo')) {
$photo = $request->photo;
$ext = $photo->getClientOriginalExtension();
$filename = uniqid() . '.' . $ext;
$photo->storeAs('public/posts/' . $request->user()->id, $filename);
}
...
storeAs() function takes three parameters.
storeAs(path,name,options)
'options' is where you specify file visibility. In your case :
if ($request->hasFile('photo')) {
$photo = $request->photo;
$ext = $photo->getClientOriginalExtension();
$filename = uniqid() . '.' . $ext;
$photo->storeAs('posts/' . $request->user()->id, $filename,'public');
}

How do I use Image Intervention with Laravel Storage and digital ocean

I am using the Laravel storage feature to store images on DigitalOcean which works fine but I can't seem to add image intervention because I want to use the orientate feature. This is so that images created through the phone doesn't rotate sideways. How do I add image intervention to my existing code?
if ( $request->hasFile('image')) {
$extension = $request->file('image')->extension();
$mimeType = $request->file('image')->getMimeType();
$path = Storage::disk('do')->putFileAs('public/images', $request->file('image'), time() . '.' . $extension);
Storage::disk('do')->setVisibility($path, 'public');
$product->image = $path;
}
So i tried different ways of implementing image intervention haven't had much success but here my updated code. An with that i got a error saying Command (GetRealPath) is not available for driver (Gd).
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('/images/' . $filename);
$extension = $request->file('image')->extension();
//$mimeType = $request->file('image')->getMimeType();
$orientate = Image::make($image)->orientate();
$path = Storage::disk('do')->putFileAs('public/images', $orientate, $filename);
Storage::disk('do')->setVisibility($path, 'public');
$product->image = $path;

Laravel can't upload file in storage folder

I used to store my upload files (images) in my public folder but this time I want to store them in my storage folder and I get this error
Can't write image data to path (C:\laragon\www\mynewsite\storage\images/aboutimage-1522481830.png)
Error comes from this line (where I use intervention/image package)
Image::make($image)->resize(1200, 600)->save($location);
My function:
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = 'aboutimage' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = storage_path('images/' . $filename);
Image::make($image)->resize(1200, 600)->save($location);
$oldFilename = $indexabout->image;
$indexabout->image = $filename;
Storage::delete($oldFilename);
}
any idea?
UPDATE
My files will upload in root/storage folder instead of root/storage/app/public/images
why is that?
Update 2
I changed my function to code below and it's uploading where it suppose to upload but it does not delete the old image
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = '/aboutimage' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = storage_path('app/public/images' . $filename);
Image::make($image)->resize(1200, 600)->save($location);
$oldFilename = $indexabout->image;
$indexabout->image = $filename;
Storage::delete($oldFilename);
}
SOLVED
here is final code:
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = 'aboutimage' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = storage_path('app/public/images/' . $filename); // root storage path
Image::make($image)->resize(1200, 600)->save($location);
Storage::delete('images/' . $indexabout->image); //public storage path
$indexabout->image = $filename;
}
Basically I gave Root/Storage path to store data and Public/Storage path for deleting them in update method.

Delete image after upload new one

I'm working with laravel 5.4 I have a form which I can upload my logoimage and in update function I have this code:
//Save logo
if ($request->hasFile('logo')) {
$avatar = $request->file('logo');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
$location = public_path('avatars/logos/');
$request->file('logo')->move($location, $filename);
$oldFilename = $general_Settings->logo;
$general_Settings->logo = $filename;
Storage::delete($oldFilename);
}
$general_Settings->save();
for updating my image which is work but as you see I have Storage::delete($oldFilename); this part doesn't work and just keep the old image.
what do you think is issue of that?
Solved:
The issue was Filesystem.php I made my local root set to 'root' => public_path('avatars/'), and changed all my functions in my app because no way to save images in sub-folders and delete them just can save in sub-folders.
then my update function become like this:
if ($request->hasFile('logo')) {
$avatar = $request->file('logo');
$filename = 'sitelogo' . '-' . time() . '.' . $avatar->getClientOriginalExtension();
$location = public_path('avatars/');
$request->file('logo')->move($location, $filename);
$general_Settings->logo = $filename;
}
$general_Settings->save();
I hope this help someone.
Since, You have stored logo file name only not full path of file,
You need to give full path from public folder to delete image. Change delete line as:
if ($request->hasFile('logo')) {
$avatar = $request->file('logo');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
$location = public_path('avatars/logos/');
$request->file('logo')->move($location, $filename);
$oldFilename = $general_Settings->logo;
$file_path = "avatars/logos/".$oldFilename;
$general_Settings->logo = $filename;
Storage::delete($file_path);
}
$general_Settings->save();
This might work.

Categories