2808 explained code solutions for 75 technologies


php-gdHow to crop image


<?php

$file = '/var/www/examples/heroine.png';
$size = getimagesize($file);
$im = imagecreatefrompng($file);

$imf = imagecrop($im, ['x' => 50, 'y' => 50, 'width' => 600, 'height' => 300]);

imagePng($imf, '/tmp/image.png');ctrl + c
/var/www/examples/heroine.png

path to image to crop

getimagesize

returns image size from given path

imagecreatefrompng

creates GD image object from given PNG image

imagecrop

crops given image

'x' => 50, 'y' => 50

top left point to start crop on

'width' => 600, 'height' => 300

size of cropping rectangle

imagePng

saves image in PNG format to the given path


How to crop image, php gd

Usage example

<?php

$file = '/var/www/examples/heroine.png';
$size = getimagesize($file);
$im = imagecreatefrompng($file);

$imf = imagecrop($im, ['x' => 50, 'y' => 50, 'width' => 600, 'height' => 300]);

imagePng($imf, '/tmp/image.png');