簡體   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