Skip to content

Instantly share code, notes, and snippets.

@erdum
Created May 7, 2024 13:14
Show Gist options
  • Save erdum/77fd190d24a1e26902e9eedadcbfe1d9 to your computer and use it in GitHub Desktop.
Save erdum/77fd190d24a1e26902e9eedadcbfe1d9 to your computer and use it in GitHub Desktop.
Convert And Store Images In WebP formate Queue Job
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Http\UploadedFile;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\ImageManagerStatic as Image;
class StoreImages implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $data;
protected $file_directory;
protected $file_name;
protected $disk_driver;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(
$data,
$file_directory,
$file_name,
$disk_driver = 'local'
) {
$this->data = $data;
$this->file_directory = $file_directory;
$this->file_name = $file_name;
$this->disk_driver = $disk_driver;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
if (gettype($this->data) == 'string') {
$this->save_uploaded_file_to_webp(
$this->data,
$this->file_directory,
$this->file_name,
$thiks->disk_driver
);
}
}
private function save_uploaded_file_to_webp(
UploadedFile $file,
$file_directory,
$file_name,
$disk_driver = 'local'
) {
$image = Image::make($file->path());
$image->encode('webp', 75);
$file_path = "{$file_directory}/{$file_name}.webp";
Storage::disk($disk_driver)->put($file_path, $image->stream());
return $file_path;
}
private function save_base64_to_webp(
$string,
$file_directory,
$file_name,
$disk_driver = 'local'
) {
$handle = fopen('php://memory', 'rw');
$decoded_data = base64_decode($string, true);
if (! $decoded_data || ! getimagesizefromstring($decoded_data)) {
throw new \InvalidArgumentException('Invalid base64 image format.');
}
$decoded_image = imagecreatefromstring($decoded_data);
imagepalettetotruecolor($decoded_image);
imagewebp($decoded_image, $handle, 75);
// imagedestroy($decoded_image);
$file_path = "{$file_directory}/{$file_name}.webp";
Storage::disk($disk_driver)->put($file_path, $handle);
fclose($handle);
return $file_path;
}
private function save_blob_to_webp(
$binary_data,
$file_directory,
$file_name,
$disk_driver = 'local'
) {
$handle = fopen('php://memory', 'rw');
if (! $binary_data || ! getimagesizefromstring($binary_data)) {
throw new \InvalidArgumentException('Invalid image format.');
}
$decoded_image = imagecreatefromstring($binary_data);
imagepalettetotruecolor($decoded_image);
imagewebp($decoded_image, $handle, 75);
// imagedestroy($decoded_image);
$file_path = "{$file_directory}/{$file_name}.webp";
Storage::disk($disk_driver)->put($file_path, $handle);
fclose($handle);
return $file_path;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment