Location: PHPKode > scripts > Smiley Generator > smiley-generator/class.smiley.php
<?php
  
  // class.smiley.php
  // Author: Zhihua Lai
  // Licence: GPL
  // 2007 


  /*
    Important Instructions:
    
      First, put all your smileys in one folder, say, "images/smileys"
      Set the correct variables when you call constructor of Smiley
      
      $alls="images/smiley.jpg"  - The target smiley.jpg
      $maps="images/smiley.map.html" - The target html map file
      
      Because they are non-existent at the beginning , so just leave them alone for a little bit while
      
      $smileydir="images/smileys/" - The folder has All the smileys  
      $width=720 - The smiley image width
      $space=1 - The space between each individual smiley      

      Then Execute this file, the first time you execute the file, the php examines all the smileys in the folder and put them each by each in a image. Automatically, the html template which contains MAP tag and their co-ordinates are placed.
      
      IMPORTANT: 
      
        You need to save the image by your self and upload to you ftp server and set the correct "$alls" variale so next time the class would skip the looping.
        
        You also need to save the html (by right click and see the source code) and save it in a html file , upload it in a similar way and set the correct "$maps" variable so next time the class would skip looping.
        
        The reason it does not automatically generate image and map to disk is that some ftp servers might disable that. Of course you can easily add "saving" to the program.
        
        OK, have fun! Let me know if anything comes up.
        
        One example is located at my website. Remember to tick "smileys" to see how many smileys i got :)
        
        http://www.zhihua-lai.com/?do=Comments.All&cmode=post
        
        PS. addSmiley is a javascript you need to develop, which inserts the smiley into textarea.

  */

  // action variable, determine 
  if (isset($_GET["action"]))
  {
    $action=$_GET["action"];
  }
  else
  {
    $action="HTML";
  }
  
  // check gd support
  function testgd()
  {
    return (function_exists("imagecreate"));
  }
  // get the image extension
  function ext($fn)
  {
     $found=strrpos($fn,'.');
     if ($found==false)
     {
       $deal='';  
     }
     else
       $deal=(substr($fn,$found+1));
     return('.'.$deal);
  }
  
  // Class Smiley
	class Smiley
	{
	
	  // members
	  var $_smileydir;
	  var $_width;
	  var $_output;
	  var $_x,$_y,$_maxy;
	  var $_imgs=array();
	  var $_space;
	  var $_all;
	  var $_maps;
	  
	  // Constructor
    function Smiley($alls="images/smiley.jpg",$maps="images/smiley.map.html",$smileydir="images/smileys/",$width=720,$space=1)
    {
      $this->_smileydir=$smileydir;
      $this->_width=$width;
      $this->_output="";
      $this->_x=0;
      $this->_y=0;
      $this->_maxy=0;
      $this->_space=$space;
      $this->_all=$alls;
      $this->_maps=$maps;
    }
    
    // Add each individual $url
    function add($url)
    {
      list($width, $height, $type, $attr) = getimagesize($url);
      if ($type>=17) return;
      if ($this->_x+$width>$this->_width)
      {
        $this->_x=0;
        $this->_y+=$this->_maxy+$this->_space;
        $this->_maxy=0;
      }
      array_push($this->_imgs,array($url,$this->_x,$this->_y,$width,$height));
      if ($height>$this->_maxy)
      {
        $this->_maxy=$height;
      }
      $this->_x+=$width+$this->_space;
    }
    
    // Examine the folder
    function loopfolder()
    {
      if (file_exists($this->_maps))
      {
        if (file_exists($this->_all))
        {
          echo "<!-- Both Templates Found - No Loop -->\n";
          return;
        }
        else
        {
          if ($action=="HTML")
          {
            echo "<!-- Action HTML - No Loop -->\n";
            return;
          }
        }
      }      
      $d=opendir($this->_smileydir);
      $_imgv=array();
      while (false !== ($entry = readdir($d)))
	    {    
		    if (($entry=="..")||($entry==".")||(is_dir($this->_smileydir.$entry)))
         continue;
        //list($width, $height, $type, $attr) = getimagesize($this->_smileydir.$entry); 
        array_push($_imgv,($entry));
        //$this->add($this->_smileydir.$entry);
      }
      ksort($_imgv);
      //reset($_imgv);
      foreach (($_imgv) as $key=>$v)
      {
        $this->add($this->_smileydir.$v);
      }
    }
    
    // Generate template, html tag - map
    function html()
    {
      if (file_exists($this->_maps))
      {
        @include_once($this->_maps);
        echo "<!-- From Map Template -->\n";
        if (file_exists($this->_all))
        {
          echo "<img style=\"cursor:pointer\" usemap=\"#smiley\" src=\"".$this->_all."\" width=\"$this->_width\" />\n";      
        }
        else
        {
          echo "<img style=\"cursor:pointer\" usemap=\"#smiley\" src=\"?do=Images.Smiley&action=Image.Get\" width=\"$this->_width\" />\n";
        }        
        return ;
      }
      if (!testgd())
      {
        echo "<div style=\"background-color: rgb($r,$g,$b);width:".$this->_width."px\">";
        
        for ($i=0;$i<count($this->_imgs);$i++)
        {
            $pics=$this->_imgs[$i][0];
            $b=basename($pics);
            $c=basename($pics,ext($b));
            echo "<img style=\"cursor:pointer\" alt=\"$b\" onclick=\"javascript:addSmiley('content','$c')\" src=\"$pics\"/>";
        }
        echo "</div>";
        return;
      }    
      echo "<MAP NAME=\"smiley\">\n";
      for ($i=0;$i<count($this->_imgs);$i++)
      {
          $pics=$this->_imgs[$i][0];
          $x=$this->_imgs[$i][1];
          $y=$this->_imgs[$i][2];
          $w=$x+$this->_imgs[$i][3];
          $h=$y+$this->_imgs[$i][4];
          $b=basename($pics);
          $c=basename($pics,ext($b));          
          echo "<AREA onclick=\"javascript:addSmiley('content','$c')\" ALT=\"$b\" COORDS=\"$x, $y, $w, $h\" SHAPE=\"rect\">\n";
      }      
      echo "</MAP>\n";
      if (file_exists($this->_all))
      {
        echo "<img style=\"cursor:pointer\" usemap=\"#smiley\" src=\"".$this->_all."\" width=\"$this->_width\" />\n";      
      }
      else
      {
        echo "<img style=\"cursor:pointer\" usemap=\"#smiley\" src=\"?action=Image.Get\" width=\"$this->_width\" />\n";
      }
    }
    
    // Get the image
    function getImage($r=255,$g=255,$b=255,$q=75)
    {
      Header("Content-type: image/jpeg");  
      $im = @imagecreate($this->_width,$this->_y+$this->_maxy);
      imagecolorallocate($im, $r, $g, $b);
      for ($i=0;$i<count($this->_imgs);$i++)
      {
          $pics=$this->_imgs[$i][0];
          $x=$this->_imgs[$i][1];
          $y=$this->_imgs[$i][2];
          $w=$this->_imgs[$i][3];
          $h=$this->_imgs[$i][4];
        	$source=imagecreatefromstring(file_get_contents($pics));
        	if ($source)
        	{
        	 imagecopy($im,$source,$x,$y,0,0,$w,$h);
        	}
      }
      imagejpeg($im,'',$q); 
      imagedestroy($im);  
    }
  }
  
  // use default parameters 
  $t=new Smiley();
  
  $t->loopfolder();
  
  // Image or Map Template
  if ($action=="Image.Get")
  {
    $t->getImage();
  }
  else
  {
    $t->html();
  }
  
?>
Return current item: Smiley Generator