PHP Upload Class

I wrote this class a long time ago, for PHP 4. Then when I switched to PHP 5, I completely rewrote it for PHP 5. It has undergone many changes, bug fixes, and features have been added. It is far from finished, but unfortunately, I don't use it or maintain it as much anymore. However, that's not to say that it isn't stable, or perfectly suited for use when a simple file needs to be uploaded, it can even handle multiple file uploads.

It contains built in functions for resizing and watermarking images upon upload, and included in the download are files in Arial 40pt for ready for watermarking, all you have to do is set the right path when you call the watermark function.

It's really easy to set up and get running, every variable and option is well commented, explaining exactly what each variable does, however, the inner workings of the code are not as well documented, but everything is pretty straight forward and should be easy to figure out should you need to change something.

An example of how to use the class has been included in the download files, however, don't just copy files to your server and expect it to work! You'll need to customize the settings to your specific website.

The Code

  1. <?php
  2. /*
  3.    
  4.    Sculch Upload Class
  5.    
  6.    This is a PHP class for use with sculch created
  7.    applications, used for uploading files to the
  8.    server.
  9.    
  10.    version:       5.2.9
  11.    author:        Mario Finelli, Jr. <http://www.sculch.com>
  12.  
  13. */
  14.  
  15. class sculch_upload {
  16.  
  17.   public $directory = "uploads/"; # The upload directory.
  18.  
  19.   public $set; # Set a value to apply a manual reaname of file.
  20.  
  21.   // Set $jumble to true to generate a random string of
  22.   // characters to use as the filename, instead of the name
  23.   // the file was uploaded with.
  24.   public $jumble = false;
  25.   // How many charcters to generate when creating a jumbled
  26.   // filename.
  27.   public $length = 10;
  28.  
  29.   // $conflict is the variable that is used to determine
  30.   // conflict handling. If $conflict = 1 then the script will
  31.   // generate a new, unique filename. If $conflict = 2 then the
  32.   // script will overwrite the existing file. If $conflict = 3
  33.   // then the script will throw an error and stop the script.
  34.   public $conflict = 1;
  35.  
  36.   // This is an array of allowable extensions, leave it blank
  37.   // to allow all extensions to be uploaded.
  38.   public $extensions = array();
  39.  
  40.   // $field is set in the __construct function. This is the
  41.   // field value in the HTML form to new check for a file
  42.   // as well as send back the modifed filename if needed.
  43.   private $field;
  44.   private $target; # Target pathname for uploaded file.
  45.   private $filename; # Filename of the uploaded file.
  46.   private $name; # Actual name of file (no extension).
  47.   private $extension; # Extension of the uploaded file.
  48.   private $temp; # Temporary filename of the uploaded file.
  49.   private $jumbled = ""; # Variable used to store jumbled filename.
  50.   private $letters = array("a","b","c","d","e","f","g","h","i","j","k","l","m",
  51.                            "n","o","p","q","r","s","t","u","v","w","x","y","z",
  52.                            "1","2","3","4","5","6","7","8","9","0","-");
  53.  
  54.   private $debug = false; # Used for testing purposes.
  55.   private $ok = false; # Changes to true if the file is uploaded successfully.
  56.  
  57.   private $im;
  58.   private $ims;
  59.   private $w;
  60.   private $h;
  61.   private $x;
  62.   private $y;
  63.  
  64.   private $str;
  65.   private $tim;
  66.   private $w_h;
  67.   private $w_w;
  68.   private $place;
  69.   private $wim;
  70.   private $available;
  71.   private $x_pos;
  72.   private $y_pos;
  73.  
  74.   private $e_upload;
  75.   private $a_upload = true;
  76.   private $e_watermark;
  77.   private $a_watermark = true;
  78.   private $e_resize;
  79.   private $a_resize = true;
  80.  
  81.   private $fx;
  82.   private $fy;
  83.  
  84.   private $fp;
  85.  
  86.   public function upload_errors(){
  87.  
  88.     return $this->e_upload;
  89.    
  90.   }
  91.  
  92.   public function watermark_errors(){
  93.  
  94.     return $this->e_watermark;
  95.  
  96.   }
  97.  
  98.   public function resize_errors(){
  99.  
  100.     return $this->e_resize;
  101.  
  102.   }
  103.  
  104.   public function get_fx(){
  105.  
  106.     return $this->fx;
  107.    
  108.   }
  109.  
  110.   public function get_fy(){
  111.  
  112.     return $this->fy;
  113.    
  114.   }
  115.  
  116.   private function debug($title, $data){
  117.  
  118.     if($this->debug){
  119.    
  120.       echo "<b>$title: </b>$data<br />\n";
  121.      
  122.     }
  123.    
  124.   }
  125.  
  126.   // fix_path() is a function used to remove the trailing
  127.   // slash from $directory if it exists.
  128.   private function fix_path(){
  129.    
  130.     $this->debug("Old Directory", $this->directory);
  131.    
  132.     if(substr($this->directory, -1) == '/'){
  133.    
  134.       $this->directory = substr($this->directory, 0, -1);
  135.      
  136.     }
  137.    
  138.     $this->debug("New Directory", $this->directory);
  139.    
  140.   }
  141.  
  142.   private function check_path(){
  143.  
  144.     if(!is_dir($this->directory)){
  145.    
  146.       $this->debug("Directory Status", "Had to make directory");
  147.       mkdir($this->directory);
  148.      
  149.     } else {
  150.    
  151.       $this->debug("Directory Status", "Directory already existed");
  152.      
  153.     }
  154.    
  155.   }
  156.  
  157.   private function clean_filename(){
  158.  
  159.     //clean name
  160.     //then attach extension with period
  161.     //dont forget to validate extension
  162.     //extension and name get saved to filename
  163.    
  164.     $this->name = preg_replace("/\s+|;|\"|\:|\*|@|'|}|{|=|\+|&|\^|%|\\$|#|\\\|\\/|\\?|<|>|\\||`|~|!/","_",$this->name);
  165.     $this->filename = $this->name.".".$this->extension;
  166.  
  167.   }
  168.  
  169.   private function check_extension(){
  170.  
  171.     if(!is_array($this->extensions)){
  172.    
  173.       $this->e_upload = "your script was not configured properly. The allowed extensions variable needs to be an array";
  174.       $this->a_upload = false;
  175.      
  176.     } else {
  177.    
  178.       if(count($this->extensions) == 0){
  179.      
  180.         // do nothing, all extensions are accepted
  181.         return true;
  182.        
  183.       } else {
  184.      
  185.         $n = count($this->extensions);
  186.         for($e=0; $e<$n; $e++){
  187.          
  188.           $this->extensions[$e] = strtolower($this->extensions[$e]);
  189.          
  190.         }
  191.        
  192.         if(!in_array($this->extension, $this->extensions)){
  193.        
  194.           $this->e_upload = $this->extension." is not an allowable extension";
  195.           $this->a_upload = false;
  196.          
  197.         } else {
  198.        
  199.           //extension checks
  200.           return true;
  201.          
  202.         }
  203.        
  204.       }
  205.      
  206.     }
  207.    
  208.   }
  209.  
  210.   private function jumble(){
  211.  
  212.     for($i=0; $i<$this->length; $i++){
  213.    
  214.       if($i==0||$i==$this->length){
  215.      
  216.         $this->jumbled = $this->jumbled.$this->letters[rand(0,36)];
  217.        
  218.       } else {
  219.      
  220.         $this->jumbled = $this->jumbled.$this->letters[rand(0,35)];
  221.        
  222.       }
  223.      
  224.     }
  225.    
  226.     $this->debug("Jumbled Name",$this->jumbled);
  227.  
  228.   }
  229.  
  230.   private function unique(){
  231.  
  232.     // generate a unique filename
  233.    
  234.     switch($this->conflict){
  235.    
  236.       case 1:
  237.    
  238.         $i = 1; // counter
  239.    
  240.         while(file_exists($this->directory."/".$this->filename)){
  241.    
  242.           $this->filename = $this->name."_$i.".$this->extension;
  243.           $i++;
  244.      
  245.         }
  246.        
  247.         if($i!=1){
  248.        
  249.           $this->name = $this->name."_".($i-1);
  250.          
  251.         }
  252.        
  253.       break;
  254.      
  255.       case 2:
  256.      
  257.         // really there is nothing to do
  258.         // because the file is going to
  259.         // be overwritten
  260.        
  261.       break;
  262.      
  263.       case 3:
  264.      
  265.         // return an error
  266.        
  267.         $this->e_upload = "a file named ".$this->filename." already exists on the server";
  268.         $this->a_upload = false;
  269.        
  270.       break;
  271.      
  272.       default:
  273.      
  274.         $this->e_upload = "your script was not configured properly. Conflict handling needs to have an integer value between one and three, inclusive";
  275.         $this->a_upload = false;
  276.        
  277.       break;
  278.      
  279.     }
  280.    
  281.   }
  282.  
  283.   public function __construct($z){
  284.  
  285.     $this->field = $z;
  286.     $this->fix_path();
  287.        
  288.   }
  289.  
  290.   private function file_details(){
  291.  
  292.     $this->filename = strtolower(basename($_FILES[$this->field]['name']));
  293.     $this->name = preg_replace("/\.[^.]*$/", "", $this->filename);
  294.     $this->extension = substr($this->filename, strrpos($this->filename, '.')+1);
  295.      
  296.   }
  297.  
  298.   public function upload_it(){
  299.  
  300.     $this->file_details();
  301.     $this->check_extension();
  302.     $this->target = $this->directory."/";
  303.     $this->check_path();
  304.     if($this->jumble){
  305.     $this->jumble();
  306.     $this->name = $this->jumbled;
  307.     $this->filename = $this->jumbled.".".$this->extension;
  308.     }
  309.     if($this->set){
  310.     $this->name = $this->set;
  311.     $this->filename = $this->set.".".$this->extension;
  312.     }
  313.     $this->clean_filename();
  314.     $this->unique();
  315.    
  316.     //beginning to move the file
  317.      
  318.     if(!is_uploaded_file($_FILES[$this->field]['tmp_name'])){
  319.      
  320.       $this->e_upload = "there was an attempted upload attack";
  321.        
  322.     } else {
  323.      
  324.       if($this->a_upload){
  325.      
  326.         if(move_uploaded_file($_FILES[$this->field]['tmp_name'], $this->target.$this->filename)){
  327.        
  328.           //file uploaded successfully
  329.           $_POST[$this->field] = $this->filename;
  330.           $this->ok = true;
  331.          
  332.         } else {
  333.        
  334.           $this->e_upload = "there was an attempted upload attack";
  335.          
  336.         }
  337.        
  338.       }
  339.        
  340.     }
  341.  
  342.   }
  343.  
  344.   private function make_source(){
  345.  
  346.     switch($this->extension){
  347.      
  348.       case "png":
  349.        
  350.         $this->ims = imagecreatefrompng($this->target.$this->filename);
  351.         return true;
  352.         break;
  353.          
  354.       case "jpg":
  355.       case "jpeg":
  356.          
  357.         $this->ims = imagecreatefromjpeg($this->target.$this->filename);
  358.         return true;
  359.         break;
  360.          
  361.       case "gif":
  362.        
  363.         $this->ims = imagecreatefromgif($this->target.$this->filename);
  364.         return true;
  365.         break;
  366.          
  367.       default:
  368.        
  369.         return false;
  370.         break;
  371.          
  372.     }
  373.  
  374.   }
  375.  
  376.   private function assign_tim($letter, $f){
  377.  
  378.     // slow, laborious, inefficient, but only way I could figure out
  379.     switch($letter){
  380.            
  381.       case '&': $this->tim = imagecreatefrompng($f."/symbols/ampersand.png");return true;break;
  382.       case '@': $this->tim = imagecreatefrompng($f."/symbols/at.png");return true;break;
  383.       case '^': $this->tim = imagecreatefrompng($f."/symbols/caret.png");return true;break;
  384.       case '<': $this->tim = imagecreatefrompng($f."/symbols/caret_left.png");return true;break;
  385.       case '>': $this->tim = imagecreatefrompng($f."/symbols/caret_right.png");return true;break;
  386.       case ':': $this->tim = imagecreatefrompng($f."/symbols/colon.png");return true;break;
  387.       case ',': $this->tim = imagecreatefrompng($f."/symbols/comma.png");return true;break;
  388.       //case '©': $this->tim = imagecreatefrompng($f."/symbols/copy.png");return true;break;
  389.       case '-': $this->tim = imagecreatefrompng($f."/symbols/dash.png");return true;break;
  390.       case '$': $this->tim = imagecreatefrompng($f."/symbols/dollar.png");return true;break;
  391.       case '!': $this->tim = imagecreatefrompng($f."/symbols/exclamation.png");return true;break;
  392.       case '(': $this->tim = imagecreatefrompng($f."/symbols/parentheses_left.png");return true;break;
  393.       case ')': $this->tim = imagecreatefrompng($f."/symbols/parentheses_right.png.png");return true;break;
  394.       case '%': $this->tim = imagecreatefrompng($f."/symbols/percent.png");return true;break;
  395.       case '.': $this->tim = imagecreatefrompng($f."/symbols/period.png");return true;break;
  396.       case '#': $this->tim = imagecreatefrompng($f."/symbols/pound.png");return true;break;
  397.       case '?': $this->tim = imagecreatefrompng($f."/symbols/question.png");return true;break;
  398.       case '\'': $this->tim = imagecreatefrompng($f."/symbols/quote.png");return true;break;
  399.      case '"': $this->tim = imagecreatefrompng($f."/symbols/quotes.png");return true;break;
  400.      case ';': $this->tim = imagecreatefrompng($f."/symbols/semicolon.png");return true;break;
  401.      case '/': $this->tim = imagecreatefrompng($f."/symbols/slash.png");return true;break;
  402.      case ' ': $this->tim = imagecreatefrompng($f."/symbols/space.png");return true;break;
  403.      case '*': $this->tim = imagecreatefrompng($f."/symbols/star.png");return true;break;
  404.      case '~': $this->tim = imagecreatefrompng($f."/symbols/copy.png");return true;break;
  405.      case '_': $this->tim = imagecreatefrompng($f."/symbols/underscore.png");return true;break;
  406.      case 'A': $this->tim = imagecreatefrompng($f."/big/A.png");return true;break;
  407.      case 'B': $this->tim = imagecreatefrompng($f."/big/B.png");return true;break;
  408.      case 'C': $this->tim = imagecreatefrompng($f."/big/C.png");return true;break;
  409.      case 'D': $this->tim = imagecreatefrompng($f."/big/D.png");return true;break;
  410.      case 'E': $this->tim = imagecreatefrompng($f."/big/E.png");return true;break;
  411.      case 'F': $this->tim = imagecreatefrompng($f."/big/F.png");return true;break;
  412.      case 'G': $this->tim = imagecreatefrompng($f."/big/G.png");return true;break;
  413.      case 'H': $this->tim = imagecreatefrompng($f."/big/H.png");return true;break;
  414.      case 'I': $this->tim = imagecreatefrompng($f."/big/I.png");return true;break;
  415.      case 'J': $this->tim = imagecreatefrompng($f."/big/J.png");return true;break;
  416.      case 'K': $this->tim = imagecreatefrompng($f."/big/K.png");return true;break;
  417.      case 'L': $this->tim = imagecreatefrompng($f."/big/L.png");return true;break;
  418.      case 'M': $this->tim = imagecreatefrompng($f."/big/M.png");return true;break;
  419.      case 'N': $this->tim = imagecreatefrompng($f."/big/N.png");return true;break;
  420.      case 'O': $this->tim = imagecreatefrompng($f."/big/O.png");return true;break;
  421.      case 'P': $this->tim = imagecreatefrompng($f."/big/P.png");return true;break;
  422.      case 'Q': $this->tim = imagecreatefrompng($f."/big/Q.png");return true;break;
  423.      case 'R': $this->tim = imagecreatefrompng($f."/big/R.png");return true;break;
  424.      case 'S': $this->tim = imagecreatefrompng($f."/big/S.png");return true;break;
  425.      case 'T': $this->tim = imagecreatefrompng($f."/big/T.png");return true;break;
  426.      case 'U': $this->tim = imagecreatefrompng($f."/big/U.png");return true;break;
  427.      case 'V': $this->tim = imagecreatefrompng($f."/big/V.png");return true;break;
  428.      case 'W': $this->tim = imagecreatefrompng($f."/big/W.png");return true;break;
  429.      case 'X': $this->tim = imagecreatefrompng($f."/big/X.png");return true;break;
  430.      case 'Y': $this->tim = imagecreatefrompng($f."/big/Y.png");return true;break;
  431.      case 'Z': $this->tim = imagecreatefrompng($f."/big/Z.png");return true;break;
  432.      case 'a': $this->tim = imagecreatefrompng($f."/small/a.png");return true;break;
  433.      case 'b': $this->tim = imagecreatefrompng($f."/small/b.png");return true;break;
  434.      case 'c': $this->tim = imagecreatefrompng($f."/small/c.png");return true;break;
  435.      case 'd': $this->tim = imagecreatefrompng($f."/small/d.png");return true;break;
  436.      case 'e': $this->tim = imagecreatefrompng($f."/small/e.png");return true;break;
  437.      case 'f': $this->tim = imagecreatefrompng($f."/small/f.png");return true;break;
  438.      case 'g': $this->tim = imagecreatefrompng($f."/small/g.png");return true;break;
  439.      case 'h': $this->tim = imagecreatefrompng($f."/small/h.png");return true;break;
  440.      case 'i': $this->tim = imagecreatefrompng($f."/small/i.png");return true;break;
  441.      case 'j': $this->tim = imagecreatefrompng($f."/small/j.png");return true;break;
  442.      case 'k': $this->tim = imagecreatefrompng($f."/small/k.png");return true;break;
  443.      case 'l': $this->tim = imagecreatefrompng($f."/small/l.png");return true;break;
  444.      case 'm': $this->tim = imagecreatefrompng($f."/small/m.png");return true;break;
  445.      case 'n': $this->tim = imagecreatefrompng($f."/small/n.png");return true;break;
  446.      case 'o': $this->tim = imagecreatefrompng($f."/small/o.png");return true;break;
  447.      case 'p': $this->tim = imagecreatefrompng($f."/small/p.png");return true;break;
  448.      case 'q': $this->tim = imagecreatefrompng($f."/small/q.png");return true;break;
  449.      case 'r': $this->tim = imagecreatefrompng($f."/small/r.png");return true;break;
  450.      case 's': $this->tim = imagecreatefrompng($f."/small/s.png");return true;break;
  451.      case 't': $this->tim = imagecreatefrompng($f."/small/t.png");return true;break;
  452.      case 'u': $this->tim = imagecreatefrompng($f."/small/u.png");return true;break;
  453.      case 'v': $this->tim = imagecreatefrompng($f."/small/v.png");return true;break;
  454.      case 'w': $this->tim = imagecreatefrompng($f."/small/w.png");return true;break;
  455.      case 'x': $this->tim = imagecreatefrompng($f."/small/x.png");return true;break;
  456.      case 'y': $this->tim = imagecreatefrompng($f."/small/y.png");return true;break;
  457.      case 'z': $this->tim = imagecreatefrompng($f."/small/z.png");return true;break;
  458.      case '1': $this->tim = imagecreatefrompng($f."/numbers/1.png");return true;break;
  459.      case '2': $this->tim = imagecreatefrompng($f."/numbers/2.png");return true;break;
  460.      case '3': $this->tim = imagecreatefrompng($f."/numbers/3.png");return true;break;
  461.      case '4': $this->tim = imagecreatefrompng($f."/numbers/4.png");return true;break;
  462.      case '5': $this->tim = imagecreatefrompng($f."/numbers/5.png");return true;break;
  463.      case '6': $this->tim = imagecreatefrompng($f."/numbers/6.png");return true;break;
  464.      case '7': $this->tim = imagecreatefrompng($f."/numbers/7.png");return true;break;
  465.      case '8': $this->tim = imagecreatefrompng($f."/numbers/8.png");return true;break;
  466.      case '9': $this->tim = imagecreatefrompng($f."/numbers/9.png");return true;break;
  467.      case '0': $this->tim = imagecreatefrompng($f."/numbers/0.png");return true;break;
  468.      default: return false;break;
  469.            
  470.    }
  471.  
  472.  }
  473.  
  474.  private function get_xy($v, $h, $m){
  475.  
  476.    // do vertical alignment
  477.    switch (strtolower($v)){
  478.          
  479.      case "top":
  480.      case "t":
  481.      case "upper":
  482.      case "u":
  483.      default:
  484.        $this->y_pos = $m;
  485.        break;
  486.              
  487.      case "middle":
  488.      case "c":
  489.      case "m":
  490.      case "center":
  491.        $this->y_pos = $m + ($this->available['y']/2) - ($this->w_h/2);
  492.        break;
  493.              
  494.      case "bottom":
  495.      case "b":
  496.      case "lower":
  497.      case "l":
  498.        $this->y_pos = imagesy($this->ims) - $m - $this->w_h;
  499.        break;
  500.          
  501.    }
  502.          
  503.    // do horizontal alignment
  504.    switch (strtolower($h)){
  505.          
  506.      case "left":
  507.      case "l":
  508.      default:
  509.        $this->x_pos = $m;
  510.        break;
  511.              
  512.      case "middle":
  513.      case "c":
  514.      case "m":
  515.      case "center":
  516.        $this->x_pos = $m + ($this->available['x']/2) - ($this->w_w/2);
  517.        break;
  518.              
  519.      case "right":
  520.      case "r":
  521.        $this->x_pos = imagesx($this->ims) - $m - $this->w_w;
  522.        break;
  523.          
  524.    }
  525.  
  526.  }
  527.  
  528.  private function dimensions($aspect, $nw, $nh, $ow, $oh){
  529.  
  530.    if($aspect){
  531.    
  532.      if(($ow / $nw) >= ($oh / $nh)){
  533.      
  534.        $this->x = $nw;
  535.        $this->y = round(($nw * $oh)/$ow);
  536.        
  537.      } else {
  538.      
  539.        $this->x = round(($nh * $ow)/$oh);
  540.        $this->y = $nh;
  541.        
  542.      }
  543.    
  544.    } else {
  545.      
  546.      $this->x = $nw;
  547.      $this->y = $nh;
  548.        
  549.    }
  550.  
  551.  }
  552.  
  553.  private function output($resource, $overwrite = true, $addition = "_small"){
  554.  
  555.    if($overwrite){
  556.      
  557.      switch($this->extension){
  558.      
  559.        case "png":
  560.          
  561.          imagepng($resource, $this->target.$this->filename);
  562.           break;
  563.          
  564.        case "jpg":
  565.        case "jpeg":
  566.          
  567.          imagejpeg($resource, $this->target.$this->filename, 100);
  568.          break;
  569.          
  570.        case "gif":
  571.        
  572.          imagegif($resource, $this->target.$this->filename);
  573.          break;
  574.          
  575.        default:
  576.        
  577.          // Theoretically this is impossible.
  578.          break;
  579.          
  580.      }
  581.      
  582.    } else {
  583.      
  584.      switch($this->extension){
  585.      
  586.        case "png":
  587.          
  588.          imagepng($resource, $this->target.$this->name.$addition.".".$this->extension);
  589.          break;
  590.          
  591.        case "jpg":
  592.        case "jpeg":
  593.          
  594.          imagejpeg($resource, $this->target.$this->name.$addition.".".$this->extension, 100);
  595.          break;
  596.          
  597.        case "gif":
  598.        
  599.          imagegif($resource, $this->target.$this->name.$addition.".".$this->extension);
  600.          break;
  601.          
  602.        default:
  603.        
  604.          // Theoretically this is impossible.
  605.          break;
  606.          
  607.      }
  608.      
  609.    }
  610.    
  611.     imagedestroy($this->ims);
  612.    @imagedestroy($this->im);
  613.    @imagedestroy($this->wim);
  614.    @imagedestroy($this->tim);
  615.  
  616.  }
  617.  
  618.  public function watermark($s = "copyright string", $f = "location of files", $v = "top", $h = "left", $m = 15){
  619.  
  620.    if($this->ok){
  621.      
  622.       $this->str = str_split($s);
  623.      
  624.       if(substr($f, -1) == '/'){
  625.        
  626.         $f = substr($f, 0, -1);
  627.      
  628.       }
  629.        
  630.       //simple check for correct path
  631.      if(!file_exists($f."/symbols/copy.png")){
  632.        
  633.        $this->e_watermark = "the watermark images could not be found";
  634.         return false;
  635.          
  636.      } else {
  637.        
  638.        $this->tim = imagecreatefrompng($f."/symbols/copy.png");
  639.         $this->w_h = imagesy($this->tim);
  640.         imagedestroy($this->tim);
  641.         $this->debug("Watermark Height",$this->w_h);
  642.         $this->w_w = 0;
  643.         $this->place = 0;
  644.        
  645.         if(!$this->make_source()){
  646.        
  647.           $this->e_watermark = $this->extension." is not currently supported for modification";
  648.           return false;
  649.        
  650.         }
  651.        
  652.        $c = count($this->str);
  653.        $this->debug("Str Length",$c);
  654.          
  655.        for($i=0; $i<$c; $i++){
  656.          
  657.          if(!$this->assign_tim($this->str[$i], $f)){
  658.            
  659.            $this->e_watermark = "an unsupported character (".$this->str[$i].") was attempted. Please check your copyright string";
  660.            return false;
  661.            
  662.          }
  663.            
  664.          $this->w_w += imagesx($this->tim);
  665.          $this->debug("Letter Width",$this->str[$i]." ".imagesx($this->tim));
  666.          imagedestroy($this->tim);
  667.          
  668.       }
  669.          
  670.           $this->debug("Watermark Width", $this->w_w);
  671.           $this->wim = imagecreatetruecolor($this->w_w, $this->w_h);
  672.           $fill = imagecolorallocatealpha($this->wim,0,0,0,127); // apply transparent background
  673.           imagefill($this->wim,0,0,$fill);
  674.          
  675.           for($i=0; $i<$c; $i++){
  676.          
  677.             if(!$this->assign_tim($this->str[$i], $f)){
  678.            
  679.               $this->e_watermark = "an unsupported character was attempted. Please check your copyright string";
  680.               return false;
  681.            
  682.             }
  683.            
  684.             $this->debug("Place of", $this->str[$i].": ".$this->place);
  685.             $xx = imagesx($this->tim);
  686.             imagecopy($this->wim,$this->tim,$this->place,0,0,0,$xx,imagesy($this->tim));
  687.             $this->place += $xx;
  688.             imagedestroy($this->tim);
  689.          
  690.           }
  691.          
  692.           $this->available['x'] = (imagesx($this->ims) - (2*$m));
  693.           $this->available['y'] = (imagesy($this->ims) - (2*$m));
  694.           $this->debug("Available dimensions", $this->available['x']."x".$this->available['y']);
  695.           if($this->w_w > $this->available['x'] || $this->w_h > $this->available['y']){
  696.            
  697.             //need to resize watermark so it fits
  698.             $this->dimensions(true, $this->available['x'], $this->available['y'], $this->w_w, $this->w_h);
  699.                        
  700.             $this->tim = $this->wim;
  701.             $this->wim = imagecreatetruecolor($this->x, $this->y);
  702.             $fill = imagecolorallocatealpha($this->wim,0,0,0,127);
  703.             imagefill($this->wim,0,0,$fill);
  704.             imagecopyresampled($this->wim, $this->tim, 0,0,0,0, $this->x, $this->y, $this->w_w, $this->w_h);
  705.             $this->w_w = $this->x;
  706.             $this->w_h = $this->y;
  707.             imagedestroy($this->tim);
  708.            
  709.           }
  710.          
  711.           $this->get_xy($v, $h, $m);
  712.          
  713.           $this->debug("Watermark horizontal",$this->y_pos);
  714.           if($this->extension == "gif"){
  715.            
  716.             //change gif to true color before watermark paste
  717.             $this->tim = imagecreatetruecolor(imagesx($this->ims), imagesy($this->ims));
  718.             imagecopy($this->tim,$this->ims,0,0,0,0,imagesx($this->ims),imagesy($this->ims));
  719.             $this->ims = imagecreatetruecolor(imagesx($this->tim), imagesy($this->tim));
  720.             imagecopy($this->ims,$this->tim,0,0,0,0,imagesx($this->tim),imagesy($this->tim));
  721.            
  722.           }
  723.          
  724.           imagecopy($this->ims, $this->wim, $this->x_pos,$this->y_pos,0,0,$this->w_w, $this->w_h);
  725.        
  726.         }
  727.  
  728.       // output image here
  729.       if($this->a_watermark){
  730.        
  731.         $this->output($this->ims, true);
  732.        
  733.       }
  734.    
  735.    } else {
  736.    
  737.       if($this->a_upload){
  738.        
  739.         $this->e_watermark = "the file can not be modified before it us uploaded to the server. Chnage the method order in your script";
  740.         $this->e_upload = "modifications were attempted before file was uploaded to server";
  741.         $this->a_upload = false;
  742.        
  743.       }
  744.      
  745.     }
  746.  
  747.  }
  748.  
  749.  public function resize($width, $height, $overwrite = true, $addition = "_small", $aspect = true){
  750.  
  751.    if($this->ok){
  752.    
  753.       if(!$this->make_source()){
  754.        
  755.         $this->e_resize = $this->extension." is not currently supported for modification";
  756.         return false;
  757.        
  758.       }
  759.      
  760.       $this->w = imagesx($this->ims);
  761.       $this->h = imagesy($this->ims);
  762.      
  763.       $this->debug("Old X", $this->w);
  764.       $this->debug("Old Y", $this->h);
  765.      
  766.       // New Dimesions
  767.       $this->dimensions($aspect, $width, $height, $this->w, $this->h);
  768.      
  769.       $this->debug("New X", $this->x);
  770.       $this->debug("New Y", $this->y);
  771.      
  772.       switch($this->extension){
  773.      
  774.         case "png":
  775.         case "jpg":
  776.         case "jpeg":
  777.          
  778.           $this->im = imagecreatetruecolor($this->x, $this->y);
  779.           break;
  780.          
  781.         case "gif":
  782.        
  783.           $this->im = imagecreate($this->x, $this->y);
  784.           break;
  785.          
  786.         default:
  787.        
  788.           // Theoretically this is impossible.
  789.           break;
  790.          
  791.       }
  792.      
  793.       if($this->w < $width && $this->h < $height){
  794.      
  795.         return false;
  796.        
  797.       } else {
  798.      
  799.         imagecopyresampled($this->im, $this->ims, 0,0,0,0, $this->x, $this->y, $this->w, $this->h);
  800.        
  801.       }
  802.      
  803.       if($this->a_resize){
  804.      
  805.         $this->output($this->im, $overwrite, $addition);
  806.        
  807.       }
  808.      
  809.       //if($overwrite){
  810.      
  811.         $this->fx = $this->x;
  812.         $this->fy = $this->y;
  813.        
  814.       //} else {
  815.      
  816.         //$this->fx = $this->w;
  817.         //$this->fy = $this->h;
  818.        
  819.       //}
  820.      
  821.     } else {
  822.    
  823.       if($this->a_upload){
  824.      
  825.         $this->e_resize = "the file can not be modified before it us uploaded to the server. Change the method order in your script";
  826.         $this->e_upload = "modifications were attempted before file was uploaded to server";
  827.         $this->a_upload = false;
  828.        
  829.       }
  830.    
  831.     }
  832.  
  833.  }
  834.  
  835. }
  836. ?>

The Download

Filename sculch-upload.zip
Filesize 222475 bytes
Filetype ZIP Archive
MD5 checksum 31437da6ce4708f541419058db6cbd81
License © 2006-2009 by Sculch, LLC. under the MIT License

Download Now

TAGS: PHP