I know how to create a share link
in google drive for each image manually and then use it in an img src element
for display but I want to know if there is a way to automate this with laravel.
I've been reading that one approach seems to be to download the image from drive and convert it into base64, send it to the frontend and display it but I want to know if there is a more convenient way. Can the google api for example create a display link
on its own, which would negate the need of converting the image to base64?
I'm using masbug/flysystem-google-drive-ext adapter for Laravel 9
. My GoogleDriveServiceProvider.php
looks like this:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Storage;
use Google\Client;
use Google\Service\Drive;
use Masbug\Flysystem\GoogleDriveAdapter;
use League\Flysystem\Filesystem;
use Illuminate\Filesystem\FilesystemAdapter;
class GoogleDriveServiceProvider extends ServiceProvider
{
public function register()
{
//
}
public function boot()
{
try {
Storage::extend('google', function($app, $config) {
$options = [];
if (!empty($config['teamDriveId'] ?? null)) {
$options['teamDriveId'] = $config['teamDriveId'];
}
$client = new Client();
$client->setClientId($config['clientId']);
$client->setClientSecret($config['clientSecret']);
$client->refreshToken($config['refreshToken']);
$service = new Drive($client);
$adapter = new GoogleDriveAdapter($service, $config['folder'] ?? '/', $options);
$driver = new Filesystem($adapter);
return new FilesystemAdapter($driver, $adapter);
});
} catch(\Exception $e) {
return $e->getMessage();
}
}
}
which gives me the ability to use google drive
with the Storage
facade like so:
$imageId = Storage::disk('google')->put('', $request->image);
With a local filesystem I would do:
$url = Storage::url($imageId);
but it only concats the imageId
from google drive with a /storage
string:
/storage/bmca03toolPziaMxlGNZluP2Svgg0Jvwd7hJAycA.png
which can't be used in an img src element
. Is there a way to generate the url
from the google drive api?
CodePudding user response:
I think I had the same question quite some time ago and my approach was to use a different link with the file ID concatenated at the end, like so:
https://drive.google.com/uc?id=DRIVE_FILE_ID
You won't need to download anything because you could programmatically create the img elements and add the Drive fileID in the URL. It looks like the only thing you are required to do is have the file publicly accessible.
I obtained that link format from this page https://www.labnol.org/google-drive-image-hosting-220515 in the "Alternate Approach" section, where they explain a way to obtain an embed link to add as src for HTML.
CodePudding user response:
I found out how it's done:
$url = Storage::disk('google')->url($imageId);
will produce an url
that you can directly insert into an img src element
:
https://drive.google.com/uc?id=1MsU3OBHDykSNKpX_veHvIHptnM3yEECz&export=media