Advertisement
Guest User

Untitled

a guest
Oct 25th, 2012
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.47 KB | None | 0 0
  1. <?php
  2. if (isset($_REQUEST['submit'])) {
  3. //Custom avatar with php
  4. //Author: Joshua Bolduc
  5. //Website: http://www.bolducpress.com
  6. //Disclaimer: This document may be freely used and distributed
  7. //Date: 7/23/2007
  8.  
  9. class avatar
  10. {
  11.     var $filename;              //the filename of the image
  12.     var $width  = 200;          //the final width of your icon
  13.     var $height = 200;          //the final height of the icon
  14.     var $parts  = array();      //the different images that will be superimposed on top of each other
  15. /**
  16.  *  SET WIDTH
  17.  * This function sets the final width of our avatar icon. Because we want the image to be
  18.  * proportional we don't need to set the height (as it will distort the image)
  19.  * The height will automatically be computed.
  20.  *
  21.  * @param Integer $width
  22.  */
  23.     function set_width($width)
  24.     {
  25.         //setting the width
  26.         $this->width  = $width;
  27.     }
  28. /**
  29.  *  SET FILENAME
  30.  * This sets the final filename of our icon. We set this variable if we want
  31.  * to save the icon to the hard drive.
  32.  *
  33.  * @param String $filename
  34.  */
  35.     function set_filename($filename)
  36.     {
  37.         $this->filename = $filename;
  38.     }
  39.        
  40. /**
  41.  * SET BACKGROUND
  42.  * From this function we can add one of two types of backgrounds
  43.  * either an image or a solid color.
  44.  *
  45.  * @param String $background
  46.  */
  47.     function set_background($background)
  48.     {
  49.         $this->background_source = $background;
  50.     }
  51.    
  52. /**
  53.  *  ADD LAYER
  54.  * This is the meat and potatoes of this class (And it's so small!)
  55.  * This function let's us add images to our final composition
  56.  *
  57.  * @param String $filename
  58.  */
  59.     function add_layer($filename)
  60.     {
  61.         //by using the syntax $this->parts[] we are automatically incrementing the array pointer by 1
  62.         //There is no need to do $this->parts[$index] = $filename;
  63.         // $index = $index + 1;
  64.         $this->parts[] = $filename;
  65.     }
  66. /**
  67.  *  BUILD BACKGROUND
  68.  *  This function takes our background information and compiles it
  69.  */
  70.     function build_background()
  71.     {
  72.         //----------------------------------------
  73.         // Getting the height
  74.         //----------------------------------------
  75.         //grabbing the first image in the array
  76.         $first_image = $this->parts[0];
  77.        
  78.         //getting the width and height of that image
  79.         list($width, $height) = getimagesize($first_image);
  80.  
  81.         //finding the height of the final image (from a simple proportion equation)
  82.         $this->height = ($this->width/$width)*$height;
  83.        
  84.        
  85.         //----------------------------------------
  86.         // Compiling the background
  87.         //----------------------------------------
  88.         //the background canvas, it will be the same width and height
  89.         $this->background = imagecreatetruecolor($this->width, $this->height);
  90.         //----------------------------------------
  91.         // Adding a background color
  92.         // I'm going to be sending hex color values (#FFFFFF),
  93.         //---------------------------------------- 
  94.         //checking to make sure it's a color
  95.         if(substr_count($this->background_source, "#")>0)
  96.         {
  97.             //converting the 16 digit hex value to the standard 10 digit value
  98.             $int = hexdec(str_replace("#", "", $this->background_source));
  99.            
  100.             //getting rgb color
  101.             $background_color = imagecolorallocate ($this->background, 0xFF & ($int >> 0x10), 0xFF & ($int >> 0x8), 0xFF & $int);
  102.            
  103.             //filling the background image with that color
  104.             imagefill($this->background, 0,0,$background_color);
  105.        
  106.         //----------------------------------------
  107.         // Adding a background image
  108.         // If $background is not a color, assume that it's an image
  109.         //----------------------------------------
  110.         }else{
  111.             //getting the width and height of the image
  112.             list($bg_w, $bg_h) = getimagesize($this->background_source);
  113.            
  114.             // Make sure that the background image is a png as well.
  115.             $img = imagecreatefrompng($this->background_source);
  116.            
  117.             //This function copies and resizes the  image onto the background canvas
  118.             imagecopyresampled($this->background, $img, 0,0,0,0,$this->width, $this->height, $bg_w, $bg_h);
  119.         }
  120.     }
  121.     /**
  122.      * Build Composition
  123.      * This function compiles all the information and builds the image
  124.     */
  125.     function build_composition()
  126.     {
  127.         //----------------------------------------
  128.         // The Canvas
  129.         // Creating the canvas for the final image, by default the canvas is black
  130.         //----------------------------------------
  131.         $this->canvas = imagecreatetruecolor($this->width, $this->height);
  132.        
  133.         //----------------------------------------
  134.         // Adding the background
  135.         // If the background is set, use it gosh darnit
  136.         //----------------------------------------
  137.         if($this->background)
  138.         {
  139.             imagecopyresampled($this->canvas, $this->background, 0,0,0,0,$this->width, $this->height, $this->width, $this->height);
  140.         }
  141.  
  142.         //----------------------------------------
  143.         // Adding the body parts
  144.         // Here we go, the best part
  145.         //----------------------------------------
  146.         //looping through the array of parts
  147.         for($i=0; $i<count($this->parts); $i++)
  148.         {
  149.             //getting the width and height of the body part image, (should be the same size as the canvas)
  150.             list($part_w, $part_h) = getimagesize($this->parts[$i]);
  151.            
  152.             //storing that image into memory (make sure it's a png image)
  153.             $body_part = imagecreatefrompng($this->parts[$i]);
  154.            
  155.             //making sure that alpha blending is enabled
  156.             imageAlphaBlending($body_part, true);
  157.            
  158.             //making sure to preserve the alpha info
  159.             imageSaveAlpha($body_part, true);
  160.  
  161.             //finally, putting that image on top of our canvas  
  162.             imagecopyresampled($this->canvas, $body_part, 0,0,0,0,$this->width, $this->height, $part_w, $part_h);  
  163.         }
  164.     }
  165. /**
  166.  *  OUTPUT
  167.  *  This function checks to see if we're going to ouput to the header or to a file  
  168.  */
  169.     function output()
  170.     {
  171.         // If the filename is set, save it to a file
  172.         if(!empty($this->filename))
  173.         {
  174.             //notice that this function has the added $this->filename value (by setting this you are saving it to the hard drive)
  175.             imagejpeg($this->canvas, $this->filename,200);
  176.        
  177.         //Otherwise output to the header
  178.         }else{
  179.             //before you can output to the header, you must tell the browser to interpret this document
  180.             //as an image (specifically a jpeg image)
  181.             header("content-type: image/jpeg");
  182.            
  183.             imagejpeg($this->canvas, "member/1637/pic1.jpg");
  184.  
  185.             //Output, notice that I ommitted $this->filename
  186.             imagejpeg($this->canvas, "", 200);
  187.         }
  188.         //Removes the image from the buffer and frees up memory
  189.         imagedestroy($this->canvas);    
  190.     }
  191. /**
  192.  * BUILD
  193.  * The final function, this builds the image and outputs it
  194.  */
  195.     function build()
  196.     {
  197.         //Builds the background
  198.         $this->build_background();
  199.    
  200.         //builds the image
  201.         $this->build_composition();
  202.        
  203.         //outputs the image
  204.         $this->output();
  205.     }
  206. }
  207.  
  208.  
  209.  
  210. $avatar = new avatar;
  211. $base = "base";
  212. $bgcolor = $_GET['bgcolor'];
  213. $hand = $_GET['hand'];
  214. $headgear = $_GET['headgear'];
  215. $leggear = $_GET['leggear'];
  216. $face = $_GET['face'];
  217.  
  218. //setting the width of your final image (the image will
  219. //resize themselves dynamically)
  220. $avatar->set_width(200);
  221.  
  222. //setting your background color to black
  223. $avatar->set_background("#$bgcolor");
  224.  
  225. //you can also send it an image
  226. //$avatar->set_background("my_background_image.png");
  227.  
  228. //ah hah! adding your body parts, think of it like layers
  229. //in photoshop in reverse order.
  230. $avatar->add_layer("base.png");
  231. if($hand != "none"){
  232. $avatar->add_layer("$hand.png");
  233. }
  234. if($headgear != "none"){
  235. $avatar->add_layer("$headgear.png");
  236. }
  237. if($leggear != "none"){
  238. $avatar->add_layer("$leggear.png");
  239. }
  240. if($face != "none"){
  241. $avatar->add_layer("$face.png");
  242. }
  243. $avatar->build();
  244. }
  245. ?>
  246.  
  247.     <script language="JavaScript" type="text/javascript">
  248. function updateImage() {
  249.     var bgcolor=document.getElementById("bgcolor").value,
  250.         face=document.getElementById("face").value,
  251.         hand=document.getElementById("hand").value,
  252.         headgear=document.getElementById("headgear").value,
  253.         leggear=document.getElementById("leggear").value;
  254.     var path="http://my.iheff.net/avatartest1.php?bgcolor=" + encodeURIComponent(bgcolor)
  255.         + "&face=" + encodeURIComponent(face)
  256.         + "&hand=" + encodeURIComponent(hand)
  257.         + "&headgear=" + encodeURIComponent(headgear)
  258.         + "&leggear=" + encodeURIComponent(leggear)
  259.         + "&submit=Submit";
  260.     document.getElementById("resultImage").src = path;  
  261. }</script>
  262.  
  263.  
  264. <form method="post" action="avatartest1.php" name="myform" id="myform">
  265.   <table cellspacing="2" cellpadding="2" border="" id="table-5">
  266.            
  267.         <tr>
  268.           <td align="right">
  269. Background Color          </td>
  270.           <td>
  271.             <div id='myform_bgcolor_errorloc' class="error_strings"></div>
  272.            
  273. <select name="bgcolor" id="bgcolor" onchange="updateImage()">
  274.       <option value="000000">Black</option>
  275.      
  276.       <option value="FFFFFF">White</option>
  277.       <option value="FF7F50">Coral</option>
  278.      
  279.       <option value="DC143C">Crimson</option>
  280.       <option value="E9967A">Salmon</option>
  281.      
  282.       <option value="FFB6C1">Pink</option>
  283.       <option value="BA55D3">Orchid</option>
  284.      
  285.       <option value="008080">Teal</option>
  286.       </select>          </td>
  287.       </tr>
  288.  
  289.         <tr>
  290.           <td align="right">
  291. Hand          </td>
  292.           <td>
  293.             <div id='myform_hand_errorloc' class="error_strings"></div>
  294.            
  295. <select name="hand" id="hand" onchange="updateImage()">
  296.       <option value="beer">Beer</option>
  297.      
  298.       <option value="martini">Martini</option>
  299.       <option value="pickle">Pickle</option>
  300.       </select>          </td>
  301.       </tr>
  302.         <tr>
  303.           <td align="right">
  304. Head Gear          </td>
  305.           <td>
  306.             <div id='myform_headgear_errorloc' class="error_strings"></div>
  307.            
  308. <select name="headgear" id="handgear" onchange="updateImage()">
  309.  
  310.       <option value="hat">Hat</option>
  311.      
  312.       <option value="none">None</option>  
  313.       </select>          </td>
  314.       </tr>
  315.            
  316.            <tr>
  317.           <td align="right">
  318. Leg Gear          </td>
  319.           <td>
  320.             <div id='myform_leggear_errorloc' class="error_strings"></div>
  321.            
  322. <select name="leggear" id="leaggear" onchange="updateImage()">
  323.       <option value="shorts">Shorts</option>
  324.      
  325.       <option value="none">None</option>
  326.      
  327.      
  328.       </select>          </td>
  329.       </tr>
  330.            
  331.                <tr>
  332.           <td align="right">
  333. Face          </td>
  334.           <td>
  335.             <div id='myform_face_errorloc' class="error_strings"></div>
  336.            
  337. <select name="face" id="face" onchange="updateImage()">
  338.       <option value="mustache">Mustache</option>
  339.      
  340.       <option value="none">None</option>
  341.      
  342.      
  343.       </select>          </td>
  344.       </tr>
  345.            <tr>
  346. <td>
  347. <img src="base.png" id="resultImage" alt="Base">
  348. </td>
  349. </tr>  
  350.     <tr>
  351.           <td align="right"></td>
  352.           <td>
  353.             <input type="submit" name="submit" value="Submit" />
  354.           </td>
  355.     </tr>
  356.   </table>
  357.  
  358.     </form>
  359.  
  360.     <script language="JavaScript" type="text/javascript"
  361.     xml:space="preserve">//<![CDATA[
  362. //You should create the validator only after the definition of the HTML form
  363.   var frmvalidator  = new Validator("myform");
  364.     frmvalidator.EnableOnPageErrorDisplay();
  365.     frmvalidator.EnableMsgsTogether();
  366.  
  367.     frmvalidator.addValidation("hand","req","Choose what your avatar holds");
  368.     frmvalidator.addValidation("headgear","req","Choose head gear");
  369.     frmvalidator.addValidation("leggear","req","Choose leg gear");
  370.     frmvalidator.addValidation("face","req","Choose face stuff");
  371.  
  372.  
  373.  
  374.  
  375. //]]></script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement