Crop image with PHP without libraries

Asked

Viewed 198 times

1

I’m bringing some images from the Mysql BD of which some are horizontal and some are vertical. I saw that there are some libraries that make this clipping, but I wonder if there is any way to cut the image without the existence of libraries.

  • 1

    Something like the function imagecrop? Libraries use native functions or specific extensions for this, so just scan the source code of the ones you know and see what you can use.

  • I was reading the answer that took 1 year and 8 months to be idealized when you erased it.

1 answer

2


A basic example.

<?php
$im = imagecreatefrompng('example.png');
$size = min(imagesx($im), imagesy($im));
$im2 = imagecrop($im, ['x' => 0, 'y' => 0, 'width' => $size, 'height' => $size]);
if ($im2 !== FALSE) {
    imagepng($im2, 'example-cropped.png');
}
?>

In the documentation you can find information about.

  • Hello Rafael. Right, but without wanting to abuse rs, how would I call the cropped image? Would it be the variable $im2? <img src='". $img2."'>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.