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










share|improve this question


























    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










    share|improve this question
























      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










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 at 15:19









      Snecker

      31




      31
























          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





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            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

























            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





            share|improve this answer



























              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





              share|improve this answer

























                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





                share|improve this answer














                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






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 22 at 16:49

























                answered Nov 22 at 16:12









                myxaxa

                53736




                53736






























                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    What visual should I use to simply compare current year value vs last year in Power BI desktop

                    Alexandru Averescu

                    Trompette piccolo