I have images with transparent BG an di need get real size picture in transparent BG...
(Image can be 500x500, but picture in image can be 440x250 and i need get this size - 440x250).... How do this in PHP and GD?
Thanks
so the real image size is a last non transparent pixel on x-side - first non transparent on x-side and the same on the y-side. so all you need is find them:)
transparent is then alpha
is 127: A value between 0 and 127. 0 indicates completely opaque while 127 indicates completely transparent.
- http://php.net/manual/de/function.imagecolorallocatealpha.php - so here you are
<?php
$image = 'test.png';
$image = imagecreatefrompng($image);
$width = imagesx($image);
$height = imagesy($image);
$colors = array();
$x_max = $y_max = 0;
$x_min = $width;
$y_min = $height;
for ($y = 0; $y < $height; ++$y)
{
for ($x = 0; $x < $width; ++$x)
{
$rgb = imagecolorat($image, $x, $y);
$color = imagecolorsforindex($image, $rgb);
if (127 !== $color['alpha']) {
$x_min = min($x_min, $x);
$x_max = max($x_max, $x);
$y_min = min($y_min, $y);
$y_max = max($y_max, $y);
}
}
}
echo 'width: ' . ($x_max - $x_min) . PHP_EOL;
echo 'height: ' . ($y_max - $y_min) . PHP_EOL;
output:
width: 180
height: 180