Skip to content

Latest commit

 

History

History
35 lines (25 loc) · 1014 Bytes

how-to-crop-image.md

File metadata and controls

35 lines (25 loc) · 1014 Bytes

How 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');
  • /var/www/examples/heroine.png - path to image to crop
  • getimagesize - returns image size from given path
  • imagecreatefrompng - creates lib: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

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');