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/ */
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){
}
}
}