繁体   English   中英

如何使用PHP GD库计算PNG文件中对象的厚度

[英]How to calculate thickness of an object in PNG file using PHP GD library

我有一个PNG框架,我想知道它的厚度。 我能够计算图像本身的宽度/高度。

$frame = imagecreatefrompng('frame.png');
// get frame dimentions
$frame_width = imagesx($frame);
$frame_height = imagesy($frame);

但是无法想出一种计算框架厚度的方法,请看下图,所以看看我的意思。

在此输入图像描述

有什么建议么?

从最后一个答案可以看出,光栅图像文件中没有对象。 但是,您可以通过搜索第一次出现的透明颜色和第一次出现的非透明颜色来计算它们的距离(假设图像的空白区域都是透明的)。

示例代码:

<?php
$img = imagecreatefrompng('./frame.png');//open the image
$w = imagesx($img);//the width
$h = imagesy($img);//the height

$nonTransparentPos = null;//the first non-transparent pixel's position
$transparentPos = null;//the first transparent pixel's position

//loop through each pixel
for($x = 0; $x < $w; $x++){
   for($y = 0; $y < $h; $y++){
        $color = imagecolorsforindex($img,imagecolorat($img,$x,$y));
        if($color['alpha'] < 127 && $nonTransparentPos === null){
            $nonTransparentPos = array($x,$y);
        }
        if($color['alpha'] === 127 && $transparentPos === null){
            $transparentPos = array($x,$y);
        }
   }
   //leave the loop if we have finished finding the two values.
   if($transparentPos !== null && $nonTransparentPos !== null){
        break;
   }
}
$length = $transparentPos[0]-$nonTransparentPos[0];//calculate the two point's x-axis distance
echo $length;
?>

PNG文件中没有任何对象。 您只能通过与imagecolorat()imagecolorsforindex()坐标获得颜色(具有透明度imagecolorsforindex()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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