PHP code example of revolution / laravel-google-photos

1. Go to this page and download the library: Download revolution/laravel-google-photos library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

revolution / laravel-google-photos example snippets


'google' => [
    'client_id'     => env('GOOGLE_CLIENT_ID', ''),
    'client_secret' => env('GOOGLE_CLIENT_SECRET', ''),
    'redirect'      => env('GOOGLE_REDIRECT', ''),
],



return [
    'client_id'        => env('GOOGLE_CLIENT_ID', ''),
    'client_secret'    => env('GOOGLE_CLIENT_SECRET', ''),
    'redirect_uri'     => env('GOOGLE_REDIRECT', ''),
    'scopes'           => [
        'https://www.googleapis.com/auth/photoslibrary.appendonly',
        'https://www.googleapis.com/auth/photoslibrary.readonly.appcreateddata',
        'https://www.googleapis.com/auth/photoslibrary.edit.appcreateddata',
        'https://www.googleapis.com/auth/photospicker.mediaitems.readonly',
    ],
    'access_type'      => 'offline',
    'prompt'           => 'consent select_account',
];

use Revolution\Google\Photos\Facades\Photos;

// Upload a photo (two-step process)
$uploadToken = Photos::withToken($user->refresh_token)
    ->upload($fileContent, $fileName);

$result = Photos::batchCreate([$uploadToken]);

// routes/web.php
Route::get('/auth/google', [AuthController::class, 'redirect']);
Route::get('/auth/google/callback', [AuthController::class, 'callback']);

// AuthController.php
use Laravel\Socialite\Facades\Socialite;

public function redirect()
{
    return Socialite::driver('google')
        ->scopes(config('google.scopes'))
        ->with([
            'access_type' => config('google.access_type'),
            'prompt' => config('google.prompt'),
        ])
        ->redirect();
}

public function callback()
{
    $user = Socialite::driver('google')->user();
    
    $loginUser = User::updateOrCreate(
        ['email' => $user->email],
        [
            'name' => $user->name,
            'refresh_token' => $user->refreshToken, // Store this!
        ]
    );
    
    auth()->login($loginUser);
    return redirect('/home');
}

use Revolution\Google\Photos\Facades\Photos;
use Illuminate\Http\Request;

public function uploadPhoto(Request $request)
{
    $file = $request->file('photo');
    
    // Step 1: Upload file content and get upload token
    $uploadToken = Photos::withToken($request->user()->refresh_token)
        ->upload($file->getContent(), $file->getClientOriginalName());
    
    // Step 2: Create media item from upload token
    $result = Photos::batchCreate([$uploadToken]);
    
    return response()->json($result);
}

use Revolution\Google\Photos\Facades\Photos;

public function listPhotos()
{
    $mediaItems = Photos::withToken(auth()->user()->refresh_token)
        ->listMediaItems();
    
    foreach ($mediaItems as $item) {
        echo $item->getBaseUrl() . "\n";
        echo $item->getFilename() . "\n";
    }
}

use Revolution\Google\Photos\Facades\Photos;
use Google\Photos\Library\V1\PhotosLibraryResourceFactory;

public function createAlbum()
{
    $newAlbum = Photos::withToken(auth()->user()->refresh_token)
        ->createAlbum(PhotosLibraryResourceFactory::album('My New Album'));
    
    return [
        'id' => $newAlbum->getId(),
        'title' => $newAlbum->getTitle(),
        'url' => $newAlbum->getProductUrl(),
    ];
}

use Revolution\Google\Photos\Facades\Photos;

public function listAlbums()
{
    $albums = Photos::withToken(auth()->user()->refresh_token)
        ->listAlbums();
    
    foreach ($albums as $album) {
        echo "Album: " . $album->getTitle() . "\n";
        echo "ID: " . $album->getId() . "\n";
        echo "Items: " . $album->getMediaItemsCount() . "\n";
    }
}

use Revolution\Google\Photos\Facades\Photos;

public function updateAlbumTitle($albumId, $newTitle)
{
    $album = Photos::withToken(auth()->user()->refresh_token)
        ->updateAlbumTitle($albumId, $newTitle);
    
    return $album;
}

use Revolution\Google\Photos\Traits\PhotosLibrary;

class User extends Authenticatable
{
    use PhotosLibrary;

    protected function tokenForPhotoLibrary(): array|string
    {
        return $this->refresh_token;
    }
}

// Usage with trait
$albums = $user->photos()->listAlbums();
$uploadToken = $user->photos()->upload($content, $filename);

use Revolution\Google\Photos\Facades\Picker;
use Revolution\Google\Photos\Support\Token;

// Get access token from refresh token
$accessToken = Token::toAccessToken(auth()->user()->refresh_token);

// Create picker session
$picker = Picker::withToken($accessToken)->create();

// Redirect user to picker
return redirect($picker['pickerUri']);

// Later, check if user finished selecting
$session = Picker::withToken($accessToken)->get($picker['id']);
if ($session['mediaItemsSet']) {
    // Get selected media items
    $mediaItems = Picker::withToken($accessToken)->list($picker['id']);
}

use Revolution\Google\Photos\Facades\Photos;

// Get specific album
$album = Photos::withToken($token)->getAlbum($albumId);

// Search media items
$searchResults = Photos::withToken($token)->searchMediaItems($searchRequest);

// Add media items to album
$photos = Photos::withToken($token)->batchAddMediaItemsToAlbum($albumId, $mediaItemIds);

use Revolution\Google\Photos\Facades\Photos;
use Google\ApiCore\PagedListResponse;

$items = Photos::withToken($token)->listMediaItems();

// Iterate through all items (handles pagination automatically)
foreach ($items as $item) {
    echo $item->getBaseUrl() . "\n";
}

// Or access page information
$page = $items->getPage();
echo "Page token: " . $page->getNextPageToken();

use Revolution\Google\Photos\Facades\Photos;
use Google\ApiCore\ApiException;

try {
    $result = Photos::withToken($token)->upload($content, $filename);
} catch (ApiException $e) {
    Log::error('Google Photos API error: ' . $e->getMessage());
    return response()->json(['error' => 'Upload failed'], 500);
}

// In AppServiceProvider::boot()
use Revolution\Google\Photos\Facades\Photos;

Photos::macro('customUpload', function ($file) {
    $uploadToken = $this->upload($file->getContent(), $file->getClientOriginalName());
    return $this->batchCreate([$uploadToken]);
});

// Usage
$result = Photos::withToken($token)->customUpload($file);