Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active November 13, 2023 11: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/c7d8b4ed0a8f91ff1ff3316bab5f37c8 to your computer and use it in GitHub Desktop.
Save code-boxx/c7d8b4ed0a8f91ff1ff3316bab5f37c8 to your computer and use it in GitHub Desktop.
PHP Crop Image

HOW TO CROP IMAGES IN PHP

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

IMAGE

girl

NOTES

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

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) CREATE IMAGE OBJECT
$img = imagecreatefrompng("girl.png");
// (B) CROP AREA
// CROP 200 X 200 FROM TOP LEFT CORNER (0,0)
$area = [
"x" => 0, "y" => 0,
"width" => 200, "height" => 200
];
// (C) CROP IMAGE
$crop = imagecrop($img, $area);
// (D) SAVE CROPPED IMAGE
imagewebp($crop, "demo-A.webp");
// (E) RECOMMENDED CLEANUP - DESTROY IMAGE OBJECTS
imagedestroy($img);
imagedestroy($crop);
<?php
// (A) CREATE IMAGE OBJECT
$img = imagecreatefrompng("girl.png");
// (B) CROP AREA
// (B1) SOURCE IMAGE DIMENSIONS
$swidth = imagesx($img);
$sheight = imagesy($img);
// (B2) CROP AREA DIMENSIONS (50% OF SOURCE)
$tocrop = 0.5;
$cwidth = ceil($swidth * $tocrop);
$cheight = ceil($sheight * $tocrop);
// (B3) CROP COORDINATES (FROM CENTER OF IMAGE)
$sx = ceil(($swidth / 2) - ($cwidth / 2));
$sy = ceil(($sheight / 2) - ($cheight / 2));
$area = ["x" => $sx, "y" => $sy, "width" => $cwidth, "height" => $cheight];
// (C) CROP IMAGE
$crop = imagecrop($img, $area);
// (D) SAVE CROPPED IMAGE
imagewebp($crop, "demo-B.webp");
// (E) RECOMMENDED CLEANUP - DESTROY IMAGE OBJECTS
imagedestroy($img);
imagedestroy($crop);
<?php
// (A) CREATE IMAGE OBJECT
$img = imagecreatefrompng("girl.png");
// (B) CROP AREA
// (B1) SOURCE IMAGE DIMENSIONS
$swidth = imagesx($img);
$sheight = imagesy($img);
// (B2) CROP AREA DIMENSIONS (50% OF SOURCE)
$tocrop = 0.5;
$cwidth = ceil($swidth * $tocrop);
$cheight = ceil($sheight * $tocrop);
// (B3) CROP COORDINATES (FROM CENTER OF IMAGE)
$sx = ceil(($swidth / 2) - ($cwidth / 2));
$sy = ceil(($sheight / 2) - ($cheight / 2));
$area = ["x" => $sx, "y" => $sy, "width" => $cwidth, "height" => $cheight];
// (C) CROP IMAGE
$crop = imagecrop($img, $area);
// (D) RESIZE & SAVE CROPPED IMAGE
$tosize = 0.5; // SHRINK CROPPED IMAGE TO 50%
$rwidth = ceil($cwidth * $tosize);
$rheight = ceil($cheight * $tosize);
$resized = imagecreatetruecolor($rwidth, $rheight);
imagecopyresampled($resized, $crop, 0, 0, 0, 0, $rwidth, $rheight, $cwidth, $cheight);
imagewebp($resized, "demo-C.webp", 90);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment