PHP: Get real image size from PNG with transparent bg
up vote
-1
down vote
favorite
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
php image png gd
add a comment |
up vote
-1
down vote
favorite
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
php image png gd
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
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
php image png gd
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
php image png gd
php image png gd
asked Nov 22 at 15:19
Snecker
31
31
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
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
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
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
add a comment |
up vote
0
down vote
accepted
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
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
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
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
edited Nov 22 at 16:49
answered Nov 22 at 16:12
myxaxa
53736
53736
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433987%2fphp-get-real-image-size-from-png-with-transparent-bg%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown