Is there anything in PHP that allows you to crop images around the contour of an object in it?

Those. For example, we have a logo in JPEG / PNG / GIF format on a white / transparent background, you need to automatically cut the background around the object to the borders of the object itself.

    1 answer 1

    You have two ways.

    1. ImageMagic and convert function (It should be better in speed)
    2. Go through the pixels and search for "not empty" (There’s more freedom but a slower way)

    On PHP it will be somewhere like this (The code is not optimized)

    function CropEmpty($src, $dst) { $image = imagecreatefromstring(file_get_contents($src)); imagealphablending($image, false); imagesavealpha($image, true); $width = imagesx($image); $height = imagesy($image); //Находим верхнюю крайнюю точку for ($top = 0; $top < $height; $top++) { for ($x = 0; $x < $width; $x++) { if (imagecolorat($image, $x, $top)) { break 2; } } } //Находим нижнюю крайнюю точку for ($bottom = 0; $bottom < $height; $bottom++) { for ($x = 0; $x < $width; $x++) { if (imagecolorat($image, $x, $height - $bottom - 1)) { break 2; } } } //Находим крайнюю левую точку for ($left = 0; $left < $width; $left++) { for ($y = 0; $y < $height; $y++) { if (imagecolorat($image, $left, $y)) { break 2; } } } //Находим крайнюю правую точку for ($right = 0; $right < $width; $right++) { for ($y = 0; $y < $height; $y++) { if (imagecolorat($image, $width - $right - 1, $y)) { break 2; } } } #region newImage $newWidth = $width - ($left + $right); $newHeight = $height - ($top + $bottom); $newImage = imagecreatetruecolor($newWidth, $newHeight); imagealphablending($newImage, false); imagesavealpha($newImage, true); imagefilledrectangle($newImage, 0, 0, $newWidth, $newHeight, imagecolorallocatealpha($newImage, 255, 255, 255, 127)); imagecopy($newImage, $image, 0, 0, $left, $top, $newWidth, $newHeight); imagepng($newImage, $dst); imagedestroy($newImage); #endregion imagedestroy($image); } CropEmpty('https://upload.wikimedia.org/wikipedia/commons/a/ab/Logo_TV_2015.png', __DIR__ . '/test.png'); 

    Well, an example with a trim

      $im = new Imagick($src); $im->trimImage(0); //Погрешность цвета $im->writeImage($dst); 
    • I tried your algorithm, it does not work for me .. it's not clear what he did with the image at all, the output file size is increased, the dimensions for pixel remain the same. - Enshtein
    • About ImageMagic, meant: Imagick :: trimImage ??? - Enshtein
    • Link to the original picture in the studio - Ninazu
    • Most likely it is in your jpeg format, and therefore png takes up more space, as it is a format without loss of quality, unlike jpeg. And it does not have transparency, but my algorithm just cuts transparent pixels. 0hFFFFFF as white and passes by the condition. I think you can modify it to your needs. Add save to the desired format and those colors that need to be considered empty. I think it will be necessary to take into account similar colors. For the human eye, 0xFEFEFE also looks white, but in the condition it may not work))) - Ninazu
    • switched to use convert - everything suits, thank you for the advice! - Enshtein