Skip to content

Image Optimization ​

Uploads are optimized automatically (see Image Processing). To convert or compress an image that is already stored, use MediaFlow::optimize() or the Optimize action in the media manager.

php
use MediaFlow\Laravel\Facades\MediaFlow;

$result = MediaFlow::optimize($media, [
    'format'  => 'webp',   // webp | jpeg (or jpg) | png
    'quality' => 80,
    'width'   => 1600,     // optional max width, aspect ratio kept
    'height'  => 1600,     // optional max height
    'action'  => 'copy',   // 'replace' (default) or 'copy'
]);

$result['savings_percent'];   // e.g. 64.2
$result['size_before'];       // bytes
$result['size_after'];        // bytes
$result['width'];
$result['height'];
$result['format'];            // 'webp'
$result['mime'];              // 'image/webp'
$result['media'];             // the updated Media, or the new copy
OptionDefaultNotes
formatwebpwebp, jpeg/jpg or png.
qualityimage default30–100 through the HTTP endpoint.
width, heightnoneMaximum dimensions; the image is only scaled down.
actionreplacereplace overwrites the file and updates the record. copy stores a new file and creates a new Media record in the same folder.

Limits ​

  • Only raster images can be optimized. SVGs and non-images throw InvalidArgumentException.
  • Converting to JPEG fills transparent areas with white.
  • If writing the result to storage fails, a RuntimeException is thrown and the original is left untouched.

HTTP endpoint ​

http
POST /mediaflow/media/{id}/optimize
Content-Type: application/json

{ "format": "webp", "quality": 80, "max_width": 1600, "action": "replace" }

width/height (or max_width/max_height) accept 20–8000. The response contains metrics (the values above) and the updated media.

Released under the MIT License.