简体   繁体   中英

PHP GD: Crop image based on contents of image

I have images, which all contain a square with a 3px red (#ff0000) border. The images have different sizes, and the size of the square within the images is variable as well.

Is it possible to use PHP GD to crop the part within the red border? I realize this would need two steps: first of all locating the red border, and second of all doing the cropping. The cropping part is easy, so my question comes down to: how do I recognize the 3px red square border?

Here is the solution I found to find the border. It may not be optimal, but good enough for what I'm trying to achieve.

            <?php
            $im = imagecreatefrompng("image.png");
            $size = getimagesize("image.png");
            $width = $size[0];
            $height = $size[1];

            function find_border($direction, $first_last, $width, $height, $im)
            {
                if($direction == 'v')
                {
                    $attempts = array(round($width/10*5), round($width/10*4), round($width/10*6), round($width/10*3), round($width/10*7), round($width/10*2), round($width/10*8), round($width/10*1), round($width/10*9));
                    $limit = $height;
                }
                else
                {
                    $attempts = array(round($height/10*5), round($height/10*4), round($height/10*6), round($height/10*3), round($height/10*7), round($height/10*2), round($height/10*8), round($height/10*1), round($height/10*9));
                    $limit = $width;
                }

                foreach($attempts AS $attempt)
                {
                    if($first_last == 'f')
                    {
                        for($i=1;$i<$limit;$i++)
                        {
                            if($direction == 'h')
                            {
                                if(imagecolorat($im, $i-1, $attempt)==16711680) return $i;

                            }
                            else
                            {
                                if(imagecolorat($im, $attempt, $i-1)==16711680) return $i;
                            }
                        }
                    }
                    elseif($first_last == 'l')
                    {
                        for($i=$limit-1;$i>0;$i=$i-1)
                        {
                            if($direction == 'h')
                            {
                                if(imagecolorat($im, $i-1, $attempt)==16711680) return $i;

                            }
                            else
                            {
                                if(imagecolorat($im, $attempt, $i-1)==16711680) return $i;
                            }
                        }
                    }
                }
            }


            echo find_border('v', 'f', $width, $height, $im)."<hr>";
            echo find_border('v', 'l', $width, $height, $im)."<hr>";
            echo find_border('h', 'f', $width, $height, $im)."<hr>";
            echo find_border('h', 'l', $width, $height, $im)."<hr>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM