Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active November 12, 2023 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/4793ea2bd1d5c694267fc0d11fa66375 to your computer and use it in GitHub Desktop.
Save code-boxx/4793ea2bd1d5c694267fc0d11fa66375 to your computer and use it in GitHub Desktop.
PHP GD Compress Images

HOW TO COMPRESS IMAGES USING PHP GD

https://code-boxx.com/compress-images-php/

DEMO IMAGE

demo

NOTES

Ensure that the GD extension extension=gd (gd2 prior to PHP8) is enabled in the php.ini file.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<?php
// (A) IMAGE OBJECT
$img = imagecreatefrompng("demo.png");
imagepalettetotruecolor($img);
// (B) COMPRESS IMAGE
// (B1) BY REDUCING QUALITY
imagejpeg($img, "demoA.jpg", 50);
// (B2) BY CHANGING IMAGE FILE TYPE & REDUCING QUALITY
imagewebp($img, "demoA.webp", 50);
echo "DONE";
<?php
// (A) IMAGE OBJECT
$img = imagecreatefrompng("demo.png");
imagepalettetotruecolor($img);
// (B) RESIZE IMAGE
$img = imagescale($img, 200, 200);
// (C) COMPRESS IMAGE
imagewebp($img, "demoB.webp", 50);
echo "DONE";
<?php
function packi ($from, $to, $mw=null, $mh=null, $quality=null) {
// (A) CHECKS
// (A1) SOURCE IMAGE IS READABLE
if (!is_readable($from)) { exit("Cannot read $from"); }
// (A2) ALLOWED IMAGE FILE FORMATS
$extFrom = strtolower(pathinfo($from, PATHINFO_EXTENSION));
$extTo = strtolower(pathinfo($to, PATHINFO_EXTENSION));
if (!in_array($extFrom, ["jpeg", "jpg", "gif", "png", "bmp", "webp"])) { exit("$from - Invalid file format"); }
if (!in_array($extTo, ["jpeg", "jpg", "gif", "png", "webp"])) { exit("$from - Invalid file format"); }
// (B) OPEN SOURCE IMAGE
$fn = "imagecreatefrom" . ($extFrom=="jpg" ? "jpeg" : $extFrom);
$img = $fn($from);
// (C) RESIZE IMAGE
if ($mw!=null || $mh!=null) {
// (C1) SOURCE IMAGE DIMENSIONS
$sw = imagesx($img);
$sh = imagesy($img);
// (C2) RESIZE RATIO
if ($mw != null && $sw>$mw) { $rw = $mw / $sw; } else { $rw = 1; }
if ($mh != null && $sh>$mh) { $rh = $mh / $sh; } else { $rh = 1; }
// (C3) RESIZE USING THE SMALLER RATIO
if ($rw!=1 || $rh!=1) {
$rr = $rw<$rh ? $rw : $rh ;
$img = imagescale($img, floor($rr * $sw), floor($rr * $sh));
}
}
// (D) SAVE & COMPRESS IMAGE
// jpg : 0 to 100
// webp : -1 (default), 0 to 100
// png : -1 (default), 0 (none) to 9
// gif : na (no compression)
if ($extTo=="gif") { imagegif($img, $to); }
else {
if ($extTo=="jpg" && ($quality==null || !is_numeric($quality) || $quality<0 || $quality>100)) { $quality = 30; }
if ($extTo=="webp" && ($quality==null || !is_numeric($quality) || $quality<-1 || $quality>100)) { $quality = -1; }
if ($extTo=="png" && ($quality==null || !is_numeric($quality) || $quality<-1 || $quality>9)) { $quality = -1; }
$fn = "image" . ($extTo=="jpg" ? "jpeg" : $extTo);
$fn($img, $to, $quality);
}
return true;
}
// (E) GO!
packi("D:/http/demo.png", "D:/http/demoC.jpg");
packi("D:/http/demo.png", "D:/http/demoC.png", 200, 0); // max width 200
packi("D:/http/demo.png", "D:/http/demoC.gif", 0, 200); // max height 200
packi("D:/http/demo.png", "D:/http/demoC.webp", 200, 400); // max width 200, max height 400
echo "DONE";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment