PHP Image handler.. and why PHP is limited in usefulness


2008-03-07 Digg! icurtain Delcious icurtain Technorati icurtain


This little application was an attempt to map an image into an array of pixel objects using the PHP GD libraries and it's made me painfully aware of how limited PHP is in it's ability to handle large or complex data structures.

Below is a simple piece of code that attempts to copy each pixel value from an image into a pixel object and store it in a 2 dimensional array - anything over 150x150px and you run out of memory/if you don't store it then it takes forever to process

The only way to implement this kind of functionality within PHP is to write a specific extension for that purpose.. but then that's not really PHP anymore and it means you have to mess around with your server configuration in order to make it work... oh for java on cheap boxes :(

  1.          
  2.  
  3.            
  4.  
  5.          class Pixel{ 
  6.  
  7.            
  8.  
  9.                      private $red = null; 
  10.  
  11.                      private $green = null; 
  12.  
  13.                      private $blue = null; 
  14.  
  15.                     
  16.  
  17.                      public function setFromCI($ci){ 
  18.  
  19.                                  $this->setRed($ci['red']); 
  20.  
  21.                                  $this->setGreen($ci['green']); 
  22.  
  23.                                  $this->setBlue($ci['blue']); 
  24.  
  25.            
  26.  
  27.                      } 
  28.  
  29.                     
  30.  
  31.                      public function getRed(){ 
  32.  
  33.                                  return $this->red; 
  34.  
  35.                      } 
  36.  
  37.                     
  38.  
  39.                      public function setRed($red){ 
  40.  
  41.                                  $this->red = $red; 
  42.  
  43.                      } 
  44.  
  45.                     
  46.  
  47.                      public function getGreen(){ 
  48.  
  49.                                  return $this->green; 
  50.  
  51.                      } 
  52.  
  53.                     
  54.  
  55.                      public function setGreen($green){ 
  56.  
  57.                                  $this->green = $green; 
  58.  
  59.                      } 
  60.  
  61.            
  62.  
  63.                      public function getBlue(){ 
  64.  
  65.                                  return $this->blue; 
  66.  
  67.                      } 
  68.  
  69.            
  70.  
  71.                      public function setBlue($blue){ 
  72.  
  73.                                  $this->blue = $blue; 
  74.  
  75.                      } 
  76.  
  77.            
  78.  
  79.          } 
  80.  
  81.            
  82.  
  83.            
  84.  
  85.          class Picture{ 
  86.  
  87.            
  88.  
  89.                      private $image = null; 
  90.  
  91.                      private $imageArray = null; 
  92.  
  93.            
  94.  
  95.                      public function loadImage($url){ 
  96.  
  97.                                              $this->image = imagecreatefromjpeg($url); 
  98.  
  99.                                             
  100.  
  101.                                              //print_r($this->image); 
  102.  
  103.                      } 
  104.  
  105.            
  106.  
  107.                      public function stats(){ 
  108.  
  109.                                  echo 'width: '.imagesx($this->image).' height: '.imagesy($this->image); 
  110.  
  111.                      } 
  112.  
  113.            
  114.  
  115.                      public function toArray(){ 
  116.  
  117.                                  $this->imageArray=null; 
  118.  
  119.                                  $x = imagesx($this->image); 
  120.  
  121.                                  $y = imagesy($this->image); 
  122.  
  123.                                 
  124.  
  125.                                 
  126.  
  127.                                  for ($i=0;$i<$x;$i++){ 
  128.  
  129.                                              for ($j=0;$j<$y;$j++){ 
  130.  
  131.                                                          $p = new Pixel(); 
  132.  
  133.                                                          $rgb = imagecolorat($this->image, $i, $j); 
  134.  
  135.                                                          $p->setFromCI(imagecolorsforindex($this->image, $rgb)); 
  136.  
  137.                                                          //echo 'i'.$i.'j'.$j.'-'; 
  138.  
  139.                                                          $this->imageArray[$i][$j] = $p; 
  140.  
  141.                                              } 
  142.  
  143.                                  }           
  144.  
  145.            
  146.  
  147.                      } 
  148.  
  149.            
  150.  
  151.                      public function fromArray(){ 
  152.  
  153.                                  $x = count($this->imageArray); 
  154.  
  155.                                  $y = count($this->imageArray,COUNT_RECURSIVE)/$x-1; 
  156.  
  157.                                  echo ' x '.$x.' y '.$y; 
  158.  
  159.                                  //$img = ImageCreateTrueColor($width, $height) 
  160.  
  161.                     
  162.  
  163.                      } 
  164.  
  165.                     
  166.  
  167.                      public function getPixelAt($i, $j){ 
  168.  
  169.                                  if($this->imageArray[$i][$j]!=null){ 
  170.  
  171.                                              return $this->imageArray[$i][$j]; 
  172.  
  173.                                  } 
  174.  
  175.            
  176.  
  177.                      }           
  178.  
  179.                     
  180.  
  181.          } 
  182.  
  183.            
  184.  
  185.          ?> 
  186.  
  187.            
  188.  
  189.            
  190.  
  191.            
  192.  
  193.          
  194.  
  195.            
  196.  
  197.            
  198.  
  199.          $pic = new Picture(); 
  200.  
  201.          $pic->loadImage('home.jpg'); 
  202.  
  203.          $pic->toArray(); 
  204.  
  205.          echo $pic->stats(); 
  206.  
  207.            
  208.  
  209.          $x = $pic->getPixelAt(114,114); 
  210.  
  211.          print('
    '); 
  212.  
  213.          echo $x->getGreen(); 
  214.  
  215.          print('
    '); 
  216.  
  217.          echo $x->getBlue(); 
  218.  
  219.          print('
    '); 
  220.  
  221.          echo $x->getRed(); 
  222.  
  223.          print('
    '); 
  224.  
  225.            
  226.  
  227.          $pic->fromArray(); 
  228.  
  229.            
  230.  
  231.          ?>