Thumbnail So is a PHP image manipulation library resize (scale) to max side for jpeg and png. Expandable to different file storage providers e.g Dropbox. Default is Local storage, AWS S3.
- PHP >=7.1
composer require magnum34/thumbnail-so
- resizeToMaxSide($max)
- max - The size of the long side of the image after scaling is max pixels.
- save($type, $directorty, $filename)
- type - provider type default: local, s3.
- directory - target path to save.
Example:
use ThumbnailSo\ThumbnailSo;
$img = new ThumbnailSo('image.jpeg');
$img->resizeToMaxSide(150);
$img->save('local', 'example', 'image');
cp .env.example .env
Configuration for AWS S3.
AWS_ACCESS_KEY_ID="PUT_THE_ACCESS_KEY_ID"
AWS_SECRET_ACCESS_KEY="PUT_THE_SECRET_ACCESS_KEY"
AWS_DEFAULT_REGION="PUT_THE_SELECTED_REGION_CODE"
AWS_BUCKET="PUT_YOUR_BUCKET_NAME"
Example:
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__,'.env');
$dotenv->load();
use ThumbnailSo\ThumbnailSo;
$img = new ThumbnailSo('image.jpeg');
$img->resizeToMaxSide(150);
$img->save('s3', 'example', 'image');
Example Dropbox:
- Install Dropbox PHP SDK -> https://github.com/kunalvarma05/dropbox-php-sdk
php composer require kunalvarma05/dropbox-php-sdk
- Create Driver for Dropbox.
DropboxDriver.php
<?php
namespace Dropbox;
use ThumbnailSo\DriverManagerInterface;
use ThumbnailSo\Exceptions\ThumbnailSoException;
use Kunnu\Dropbox\DropboxApp;
use Kunnu\Dropbox\Dropbox;
use Kunnu\Dropbox\DropboxFile;
class DropboxDriver implements DriverManagerInterface {
// Name driver - provider
public function getName(){
return 'dropbox';
}
// Name configuration driver
public function getConfigName(){
return "dropbox";
}
// data configuration driver
public function getConfig(){
return [
'client_id' => $_ENV['DROPBOX_CLIENT_ID'],
'client_secret' => $_ENV['DROPBOX_CLIENT_SECRET'],
'token' => $_ENV['DROPBOX_ACCESS_TOKEN']
];
}
// Record rule for the driver
public function afterSave(string $source_image, string $destination_dir, string $destination_name, string $extension){
$config = $this->getConfig();
try {
$app = new DropboxApp($config['client_id'], $config['client_secret'],$config['token']);
$dropbox = new Dropbox($app);
$dropboxFile = new DropboxFile($source_image);
$dropbox->simpleUpload($dropboxFile , "/{$destination_dir}/{$destination_name}.{$extension}", ['autorename' => true]);
}catch(\Exception $exc){
throw new ThumbnailSoException($exc->getMessage());
}
}
}
- Add environment variables to file .env
DROPBOX_CLIENT_ID="PUT_THE_CLIENT_ID"
DROPBOX_CLIENT_SECRET="PUT_THE_CLIENT_SECRET"
DROPBOX_ACCESS_TOKEN="PUT_THE_ACCESS_TOKEN"
- Launch Thumbnail so for dopbox.
require __DIR__ . '/vendor/autoload.php';
require('./DropboxDriver.php');
use ThumbnailSo\ThumbnailSo;
use ThumbnailSo\RegisterDriver;
use Dropbox\DropboxDriver;
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__,'.env');
$dotenv->load();
// Register driver for Dropbox
$dropbox = new DropboxDriver();
RegisterDriver::set('dropbox',$dropbox );
$img = new ThumbnailSo('./cat.jpeg');
$img->resizeToMaxSide(150);
$img->save('dropbox', 'example', 'cat');
https://github.com/Magnum34/silex-thumbnail-so-example
- More unit tests.
- Docs Thumbnail So API .
Thumbnail So is licensed under the MIT License.
Copyright 2021 Magnum34