PHP code example of mohamed7sameer / laravel-tinify

1. Go to this page and download the library: Download mohamed7sameer/laravel-tinify 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/ */

    

mohamed7sameer / laravel-tinify example snippets


    ...
    mohamed7sameer\LaravelTinify\LaravelTinifyServiceProvider::class
    ...

    ...
    'Tinify' => mohamed7sameer\LaravelTinify\Facades\Tinify::class
    ...

$source = Tinify::fromFile("unoptimized.webp")->->toFile("optimized.webp");


$sourceData = file_get_contents("unoptimized.jpg");
$resultData = Tinify::fromBuffer($sourceData)->toBuffer();

$source = Tinify::fromUrl("https://tinypng.com/images/panda-happy.png")->toFile("optimized.png");

$source = Tinify::fromFile("large.jpg");
$resized = $source->resize(array(
    "method" => "fit",
    "width" => 150,
    "height" => 100
));
$resized->toFile("thumbnail.jpg");

$source = Tinify::fromFile("panda-sticker.jpg");
$converted = $source->convert(array("type" => ["image/webp","image/png"]));
$extension = $converted->result()->extension();
$converted->toFile("panda-sticker." . $extension);

$source = Tinify::fromFile("panda-sticker.png");
$converted = $source->convert(array("type" => "image/jpeg"))->transform(array("background" => "#000000"));
$converted->toFile("panda-sticker.jpg");


namespace App\Jobs;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use mohamed7sameer\LaravelTinify\Facades\Tinify;
use PhpParser\Node\Stmt\Return_;
class TinifyFileJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    /**
    * Create a new job instance.
    */
    public $filename;
    public $filepath;
    public $extension;
    public $disk;
    public function __construct($main,$file,$filename)
    {
        $this->filename = $filename;
        $this->extension = $file->getClientOriginalExtension();
        $this->disk = $main->getDisk();
    }
    /**
     * Execute the job.
     */
    public function handle(): void
    {
        $extensions = ['JPEG','PNG','WebP','JPG'];
        $filename= $this->filename;
        $extension= $this->extension;
        $disk= $this->disk;
        try{
            if (in_array(strtoupper($extension), $extensions)) {
                $image = Storage::disk($disk)->get($filename);
                $resultData = Tinify::fromBuffer($image)->toBuffer();
                if($resultData){
                    Storage::disk($disk)->delete($filename);
                    Storage::disk($disk)->put($filename, $resultData);
                }
            }
        }catch( Exception $e){

        }
    }
}


namespace App\Jobs;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use mohamed7sameer\LaravelTinify\Facades\Tinify;
class TinifyImageJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    /**
    * Create a new job instance.
    */
    public $finalPath ;
    public $disk ;
    public function __construct($finalPath,$disk)
    {
        $this->finalPath = $finalPath;
        $this->disk = $disk;
    }
    /**
     * Execute the job.
     */
    public function handle(): void
    {
        try{
            $image = Storage::disk($this->disk)->get($this->finalPath);
            $resultData = Tinify::fromBuffer($image)->toBuffer();
            if($resultData){
                Storage::disk($this->disk)->delete($this->finalPath);
                Storage::disk($this->disk)->put($this->finalPath, $resultData);
            }
        }catch( Exception $e){
        }
    }
}

# QUEUE_CONNECTION=sync
QUEUE_CONNECTION=database

// config/backpack/crud.php

// 'uploaders' => [
//     'withFiles' => [
//         'image'           => \Backpack\CRUD\app\Library\Uploaders\SingleBase64Image::class,
//         'upload'          => \Backpack\CRUD\app\Library\Uploaders\SingleFile::class,
//         'upload_multiple' => \Backpack\CRUD\app\Library\Uploaders\MultipleFiles::class,
//     ],
// ],
'uploaders' => [
    'withFiles' => [
        'image'           => \mohamed7sameer\LaravelTinify\Backpack\Uploaders\SingleBase64Image::class,
        'upload'          => \mohamed7sameer\LaravelTinify\Backpack\Uploaders\SingleFile::class,
        'upload_multiple' => \mohamed7sameer\LaravelTinify\Backpack\Uploaders\MultipleFiles::class,
    ],
],
config/app.php
config/app.php
shell
php artisan make:job TinifyFileJob
php artisan make:job TinifyImageJob
shell
php artisan queue:work