Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active November 15, 2023 02:38
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/0404f828d553aefc2a8beb535875a0f2 to your computer and use it in GitHub Desktop.
Save code-boxx/0404f828d553aefc2a8beb535875a0f2 to your computer and use it in GitHub Desktop.
PHP resize images.

RESIZE IMAGES IN PHP

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

IMAGES

resize

NOTES

Please make sure that the GD extension is enabled in the php.ini file – extension=gd (or gd2 prior to PHP8).

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) READ THE ORIGINAL IMAGE
$original = imagecreatefrompng("resize.png"); // original 400 x 600 px
// (B) RESIZE IMAGE
// IMG_NEAREST_NEIGHBOUR, IMG_BILINEAR_FIXED, IMG_BICUBIC, IMG_BICUBIC_FIXED
$resized = imagescale($original, 200, 300, IMG_BICUBIC); // 50% smaller
// (C) SAVE RESIZED IMAGE
imagewebp($resized, "demoA.webp", 70);
// (D) OPTIONAL - CLEAN UP
imagedestroy($original);
imagedestroy($resized);
<?php
function resizer ($source, $destination, $size, $quality=null) : void {
// $source - Original image file
// $destination - Resized image file name
// $size - Single number for percentage resize
// Array of 2 numbers for fixed width + height
// $quality - Optional image quality. JPG & WEBP = 0 to 100, PNG = -1 to 9
// (A) CHECKS
if (!file_exists($source)) { throw new Exception("Source image file not found"); }
$sExt = strtolower(pathinfo($source)["extension"]);
$dExt = strtolower(pathinfo($destination)["extension"]);
$allowed = ["bmp", "gif", "jpg", "jpeg", "png", "webp"];
if (!in_array($sExt, $allowed)) { throw new Exception("$sExt - Invalid image file type"); }
if (!in_array($dExt, $allowed)) { throw new Exception("$dExt - Invalid image file type"); }
if ($quality != null) {
if (in_array($dExt, ["jpg", "jpeg", "webp"]) && ($quality<0 || $quality>100)) { $quality = 70; }
if ($dExt == "png" && ($quality<-1 || $quality>9)) { $quality = -1; }
if (!in_array($dExt, ["png", "jpg", "jpeg", "webp"])) { $quality = null; }
}
// (B) NEW IMAGE DIMENSIONS
if (is_array($size)) {
$new_width = $size[0];
$new_height = $size[1];
} else {
$dimensions = getimagesize($source);
$new_width = ceil(($size/100) * $dimensions[0]);
$new_height = ceil(($size/100) * $dimensions[1]);
}
// (C) RESIZE
$fnCreate = "imagecreatefrom" . ($sExt=="jpg" ? "jpeg" : $sExt);
$original = $fnCreate($source);
$resized = imagescale($original, $new_width, $new_height, IMG_BICUBIC);
// (D) OUTPUT & CLEAN UP
$fnOutput = "image" . ($dExt=="jpg" ? "jpeg" : $dExt);
if (is_numeric($quality)) {
$fnOutput($resized, $destination, $quality);
} else {
$fnOutput($resized, $destination);
}
imagedestroy($original);
imagedestroy($resized);
}
// (E) EXAMPLE USAGE
// (E1) PERCENTAGE RESIZE
resizer("resize.png", "demoB.jpg", 50);
resizer("resize.png", "demoC.png", 25);
// (E2) FIXED DIMENSION RESIZE + QUALITY
resizer("resize.png", "demoD.png", [100, 150], -1);
resizer("resize.png", "demoE.webp", [200, 300], 60);
<?php
// (A) READ THE ORIGINAL IMAGE
$original = imagecreatefrompng("resize.png");
// (B) EMPTY CANVAS WITH REQUIRED DIMENSIONS
$resized = imagecreatetruecolor(150, 150);
// (C) EXTRACT PART OF ORIGINAL IMAGE ONTO EMPTY CANVAS
imagecopyresampled(
$resized, $original, // to resized, from original
0, 0, 100, 200, // dest-x, dest-y, src-x, src-y
150, 150, 200, 200 // dest-w, dest-h, src-w, src-h
);
// (E) SAVE/OUTPUT RESIZED IMAGE
imagewebp($resized, "demoF.webp", 70);
// (F) OPTIONAL - CLEAN UP
imagedestroy($original);
imagedestroy($resized);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment