PHP code example of christyoga123 / image-optimizer
1. Go to this page and download the library: Download christyoga123/image-optimizer 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/ */
christyoga123 / image-optimizer example snippets
use Christyoga123\ImageOptimizer\ImageOptimizer;
// Process uploaded file and get optimized temp path
$tempPath = ImageOptimizer::make()
->toWebp()
->maxDimensions(1200, 1200)
->quality(85)
->process($uploadedFile);
// Do something with the optimized file
Storage::disk('public')->put('images/photo.webp', file_get_contents($tempPath));
use Christyoga123\ImageOptimizer\Facades\ImageOptimizer;
$tempPath = ImageOptimizer::make()
->toWebp()
->maxWidth(800)
->process($request->file('image'));
$tempPath = ImageOptimizer::make()
->toJpg()
->quality(90)
->processFromRequest('photo');
if ($tempPath) {
// File was uploaded and processed
$user->update(['avatar' => Storage::putFile('avatars', $tempPath)]);
}
use Christyoga123\ImageOptimizer\ImageOptimizer;
// Optimize before adding to media collection
$tempPath = ImageOptimizer::make()
->toWebp()
->maxDimensions(1200, 1200)
->quality(85)
->process($request->file('photo'));
$model->addMedia($tempPath)
->usingFileName('optimized-photo.webp')
->toMediaCollection('photos');
$optimizer = ImageOptimizer::make()
->toWebp()
->maxWidth(800);
$tempPath = $optimizer->process($request->file('image'));
$filename = $optimizer->getOptimizedFilename($request->file('image'));
// Store to disk
$path = Storage::disk('public')->putFileAs('uploads', $tempPath, $filename);
return response()->json(['path' => $path]);
// app/Http/Requests/StorePhotoRequest.php
public function passedValidation()
{
if ($this->hasFile('photo')) {
$tempPath = ImageOptimizer::make()
->withDefaults()
->process($this->file('photo'));
$this->merge([
'optimized_photo_path' => $tempPath
]);
}
}