PHP Classes

File: mods/filter.pimg.php

Recommend this page to a friend!
  Classes of Tony Bogdanov   PIMG   mods/filter.pimg.php   Download  
File: mods/filter.pimg.php
Role: Class source
Content type: text/plain
Description: An experimental module "filters"
Class: PIMG
Process images using multiple operations
Author: By
Last change:
Date: 13 years ago
Size: 3,525 bytes
 

Contents

Class file image Download
<?php
/*
    PIMG USER MODULE
    Athor: ton4y@abv.bg
    Year: 2010
    Status: private (only the author can change it)
    Description:
        This is a collection of filters which you might want to apply to your images,
        each filter is defined by a method and explained in the head comment section
        of the method.
*/
class pimg_mod_filter
{
   
/* Resources */
   
private $pimg;
   
   
// PIMG constructor
   
function __construct($pimg)
    {
       
$this -> pimg = $pimg;
    }
   
   
   
   
/*
        Filter initializer
        @result: callback pointer
    */
   
function init()
    {
       
// Callback
       
return $this;
    }
   
   
   
   
/*
        Groups pixels into squares (cells) creating a pixelization effect
        @param: $size - cell size in pixels
        @param: $method - pixelisation method (stretch by default):
                        - 'pixels' - extremely slow, but most accurate (processes individual pixels) - this method sets the top left pixel's color as the base color of the new cell
                        - 'stretch' - extremely fast, but not so accurate - this method uses a mix bethween all cell pixel's colors as the base color for the new cell
                        - 'stretchpixels' - much faster than 'pixels' and still accurate (the larger the cell size the faster this method works)
                Here are some speed test results for 200x200 image on all methods for an average CPU: AMD Athlon64 QL62 (2 cores - 2.00 Ghz, Notebook):
                pixels:
                    cell size 2px -> 10.5595293999 seconds
                    cell size 10px -> 10.5094329834 seconds
                    cell size 50px -> 10.5749566078 seconds
                stretch:
                    cell size 2px -> 0.036429977417 seconds
                    cell size 10px -> 0.0356071949005 seconds
                    cell size 50px -> 0.0336960315704 seconds
                stretchpixels:
                    cell size 2px -> 2.25982818604 seconds
                    cell size 10px -> 0.1059030056 seconds
                    cell size 50px -> 0.0274861812592 seconds
        @return: a pointer to the caller pimg class for furthur usage
    */
   
function pixelize($size = 5, $method = 'stretch')
    {
       
/* INPUT VALIDATORS */
       
if ($size <= 0)
        {
           
$size = 5;
           
$this -> setDebug('Pixelize cell size must be > 0, using 5 as default', 'notice', __CLASS__);
        }
       
       
// Switch method
       
switch($method)
        {
            case
'pixels':
                for(
$i = 0; $i < $this -> pimg -> width(); $i++)
                    for(
$j = 0; $j < $this -> pimg -> height(); $j++)
                    {
                       
$scan = $this
                                   
-> pimg
                                   
-> scan(floor($i / $size) * $size, floor($j / $size) * $size, 1, 1);
                       
                       
$this
                           
-> pimg
                           
-> pixel($i, $j, $scan[0][0] -> color())
                            ->
draw();
                       
                        unset(
$scan);
                    }
            break;
           
            case
'stretch':
               
$width = $this -> pimg -> width();
               
$height = $this -> pimg -> height();
               
$this
                   
-> pimg
                   
-> stretch($width / $size, $height / $size)
                    ->
stretch($width, $height);
            break;
           
            case
'stretchpixels':
               
$width = $this -> pimg -> width();
               
$height = $this -> pimg -> height();
               
$matrix = array();
               
                for(
$i = 0; $i < floor($width / $size); $i++)
                    for(
$j = 0; $j < floor($height / $size); $j++)
                    {
                       
$scan = $this -> pimg -> scan($i * $size, $j * $size, 1, 1);
                       
$matrix[$i][$j] = $scan[0][0];
                    }
               
               
$new = new pimg_image($matrix);
               
$new -> stretch($width, $height);
                unset(
$this -> pimg);
               
               
// PIMG callback
               
return $new;
            break;
           
            default:
               
$this -> setDebug('The passed pixelize method name is invalid, skipping filter', 'notice', __CLASS__);
        }
       
       
// PIMG callback
       
return $this -> pimg;
    }
}
?>