PHP

HOW TO COMPRESS IMAGES IN PHP

COMPRESSED IMAGE $imgC = "COMPRESSED.WEBP";

DEFINE THE SETTINGS

01

QUALITY FROM 0 TO 100 0 = LOW QUALITY, SMALL FILE 100 = HIGH QUALITY, BIG FILE $imgQ = 30; 

ORIGINAL IMAGE $imgO = "IMAGE.JPG"; 

GET IMAGE FILE TYPE $imgT = mime_content_type($imgO); $imgT = substr($imgT, 6); $imgT = strtolower($imgT);

OPEN ORIGINAL IMAGE

02

GD FUNCTION TO OPEN IMAGE $opener = "imagecreatefrom$imgT";   OPEN ORIGINAL IMAGE $img = $opener($imgO);

SAVE IMAGE TO FILE imagewebp ($img, $imgC, $imgQ);

NOTES * JPEG does not support transparent background. * Only WEBP PNG GIF do. * Use WEBP JPEG for best results.

COMPRESS THE IMAGE

03