PHP

HOW TO CROP IMAGES IN PHP

ORIGINAL IMAGE $img = imagecreatefromjpeg(IMG.JPG");

SIMPLE IMAGE CROP

01

DEFINE CROP AREA - 500 X 300 FROM TOP LEFT CORNER $area = ["x" => 0, "y" => 0, "width" => 500, "height" => 300];

CROP & SAVE IMAGE $crop = imagecrop($img, $area); imagejpeg($crop, "CROP.JPG", 50);

ORIGINAL IMAGE $i = imagecreatefromjpeg(IMG.JPG");

CROP FROM CENTER

02

CROP AREA - 50% FROM CENTER  $tocrop = 0.5; $sw = imagesx($i); $sh = imagesy($i); $cw = ceil($sw * $tocrop); $ch = ceil($sh * $tocrop); $sx = ceil(($sw / 2) - ($cw / 2)); $sy = ceil(($sh / 2) - ($ch / 2)); $area = ["x" => $sx, "y" => $sy, "width" => $cw, "height" => $ch];  

CROP & SAVE IMAGE $crop = imagecrop($i, $area); imagejpeg($crop, "CROP.JPG", 50);