[英]How to calculate thickness of an object in PNG file using PHP GD library
I have a PNG frame and I want to know its thickness. 我有一个PNG框架,我想知道它的厚度。 I am able to calculate the width/height of the image itself. 我能够计算图像本身的宽度/高度。
$frame = imagecreatefrompng('frame.png');
// get frame dimentions
$frame_width = imagesx($frame);
$frame_height = imagesy($frame);
But can't figure out a way to calculate thickness of frame, please see image below so see what I mean. 但是无法想出一种计算框架厚度的方法,请看下图,所以看看我的意思。
Any suggestions? 有什么建议么?
From the last answer it shows that there's no objects in a raster image file. 从最后一个答案可以看出,光栅图像文件中没有对象。 However, you can do it by searching the first occurrence of transparent colour and the first occurrence of the non-transparent colour and calculate the distance of them (assumes that your image's blank area are all transparent). 但是,您可以通过搜索第一次出现的透明颜色和第一次出现的非透明颜色来计算它们的距离(假设图像的空白区域都是透明的)。
Example code: 示例代码:
<?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;
?>
There aren't any objects in a PNG file. PNG文件中没有任何对象。 You can only get a color (with a transparency) by coordinates with imagecolorat()
& imagecolorsforindex()
. 您只能通过与imagecolorat()
和imagecolorsforindex()
坐标获得颜色(具有透明度imagecolorsforindex()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.