PHP Classes

File: scan.pimg.php

Recommend this page to a friend!
  Packages of Tony Bogdanov   PIMG   scan.pimg.php   Download  
File: scan.pimg.php
Role: Class source
Content type: text/plain
Description: Class for scanning pixels
Class: PIMG
Process images using multiple operations
Author: By
Last change: 1.1
Date: 16 years ago
Size: 2,163 bytes
 

Contents

Class file image Download
<?php
/* PIMG module: scans a rectangle and returns an array of pimg_pixel instances for each pixel */
class pimg_scan
{
    /* Resources */
    private $pimg;
    
    // PIMG constructor
    function __construct($pimg)
    {
        $this -> pimg = $pimg;
    }
    
    
    
    /*
        Scans a rectangle defined by it's start points x, y and width and height.
        @param: $x - x of start point
        @param: $y - y of start point
        @param: $width - rectangle width
        @param: $height - rectangle height
        @result: an array of pimg_pixel instances for each pixel
    */
    function init($x = 0, $y = 0, $width = 1, $height = 1)
    {
        /* INPUT VALIDATORS */
        if ($x < 0)
        {
            $x = 0;
            $this -> pimg -> setDebug('X start point must be >= 0, using 0 as default', 'notice', __CLASS__);
        }
        if ($y < 0)
        {
            $y = 0;
            $this -> pimg -> setDebug('Y start point must be >= 0, using 0 as default', 'notice', __CLASS__);
        }
        if ($width <= 0)
        {
            $this -> pimg -> setDebug('Width of selection must be > 0, using 1 as default', 'notice', __CLASS__);
            $width = 1;
        }
        if ($height <= 0)
        {
            $this -> pimg -> setDebug('Height of selection must be > 0, using 1 as default', 'notice', __CLASS__);
            $height = 1;
        }
        if ($x + $width > $this -> pimg -> width())
        {
            $width = $this -> pimg -> width() - $x;
            $this -> pimg -> setDebug('Selection\'s destination width is out of image\'s bounds, will scan till the end', 'notice', __CLASS__);
        }
        if ($y + $height > $this -> pimg -> height())
        {
            $height = $this -> pimg -> height() - $y;
            $this -> pimg -> setDebug('Selection\'s destination height is out of image\'s bounds, will scan till the end', 'notice', __CLASS__);
        }
        
        // Go through all pixels
        $result = array();
        for($i = 0; $i < $width; $i++)
            for($j = 0; $j < $height; $j++)
            {
                $color = imagecolorsforindex($this -> pimg -> handle(), imagecolorat($this -> pimg -> handle(), $x + $i, $y + $j));
                $result[$i][$j] = $this -> pimg -> pixel($x + $i, $y + $j, $this -> pimg -> color($color['red'], $color['green'], $color['blue'], $color['alpha']));
            }
        
        // Return the matrix
        return $result;
    }
}
?>