I've needed to work with imagick for some projects and found a need for extracting the actual image from a transparent png using both PHP and bash under different circumstances. This file servers as notes on intersting articules/search results that have aided me.
https://www.imagemagick.org/script/escape.php
http://php.net/manual/en/imagick.constants.php
%@ CALCULATED: trim bounding box (without actually trimming)
identify -format "%@" file.png
893x814+1904+518
where you get the actual image size (893x814) and the offset to the canvas (1904,518) in pixels.
$base = new Imagick(realpath('./trim.png'));
$base->trimImage(0);
$geometry = $base->getImageGeometry();
$pageInfo = $base->getImagePage();
printf (
"Width %d Height %d\n",
$geometry['width'],
$geometry['height']
);
printf(
"OffsetX: %d OffsetY %d\n",
$pageInfo['x'],
$pageInfo['y']
);
/*
Gives the output:
Width 179 Height 179
OffsetX: 388 OffsetY 76
*/
Get the image DPI and units, without relying on getImageResolution
which seems to only return meta data and can be null/empty.
$img = new Imagick($input_image);
var_dump($img->identifyFormat('%h, %w, %x, %y, %U'));
Details on the variables that can be used in identifyFormat
can be found here: https://www.imagemagick.org/script/escape.php and the PHP page for the function is here: http://php.net/manual/en/imagick.identifyformat.php
http://stackoverflow.com/questions/26244191/imagemagick-detect-coordinates-of-transparent-areas
convert input.png -alpha extract -format "%@" info:
245x114+4+21
http://stackoverflow.com/questions/33888028/outline-a-transparent-image-using-imagick-php
This resizes a batch of images to 600px wide using parallel to run concurrently. Images are not overwritten, they're renamed to "_small" on completion.
find -iname "*.jpg" | parallel convert {} -resize 600 {.}_small.jpg