Advertisement
Guest User

Bug uploadu .exe/.php - fix please

a guest
Apr 7th, 2012
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 33.08 KB | None | 0 0
  1. <?php
  2. /*  ****************************************************************
  3.                     :: _image ::
  4.                     Image upload and manipulation class
  5.                    
  6.                     Version 1.1         (15 Nov 2011)
  7.                    
  8.                     Copyright MJDIGITAL Š2009-2011
  9.                     Written by Mark Jackson
  10.                     All rights reserved
  11.    
  12.     Changelog
  13.         v1.1    Fixed newName on upload
  14.                 Fixed extension replacement on upload
  15.                 Fixed padToFit is turned off when oversize is set to true
  16.                
  17.    
  18.     Thanks for bug fixes/notifications to:
  19.         Yamez - muleofyamez at yahoo     (strict image type chcking)
  20.         Chuck - at Toucan Media Group    (image quality calculation fix
  21.         Frank - at Advanced Virtual     (located bugs with filename replace and oversize + requesting version numbers)
  22.    
  23. *  ****************************************************************/
  24.  
  25. class _image {
  26.     // available properties (variables)    REDEFINE IN PAGE AS NEEDED
  27.    
  28.     // GENERIC - as in used by upload and resize functions
  29.     public $newName         = '';            // new image name (without file extension)
  30.     public $returnType        = 'fullpath';     // specify return type - fullpath | array | bool | blob
  31.     public $safeRename        = 'true';        // rename the new file to remove unsafe characters and spaces
  32.     public $duplicates        = 'u';             // u = make unique / o = overwrite / e = error / a = abort
  33.    
  34.     // UPLOAD FUNCTION
  35.     public $uploadTo        = 'uploaded/';    // folder to upload to (relative to calling document)
  36.    
  37.     // RESIZE FUNCTION
  38.     public $source_file     = '';            // source file to resize
  39.     public $newWidth         = '';            // new image width - e.g. '200' = 200 pixels
  40.     public $newHeight         = '';            // new image height - e.g. '200' = 200 pixels
  41.     public $namePrefix         = '';            // prefix the resized file
  42.     public $newPath         = '';            // path to save the new resized file
  43.     public $aspectRatio     = 'true';        // keep the aspect ratio - true | false
  44.     public $oversize         = 'false';        // resize to fill space - this can cause oversized images but will fill the frame created by w/h supplied - true | false
  45.     public $padToFit         = 'true';        // pad the image with the pad colour to fit the new image dimensions - true | false
  46.     public $upscale         = 'false';        // enlarge smaller images or not - true | false
  47.     public $setPosition        = 'cc';            // set position of image within its canvas - e.g. 'cc' = center x center y OR 'tr' = top right
  48.     public $padColour         = '#FFFFFF';    // set background padding colour - '#XXXXXX' or 'transparent' (transparent requires PNG type)
  49.     public $padTransparent    = 'true';         // if uploading a GIF or PNG then set background as transparent - this overrides $padColour
  50.     public $newImgType        = '';             // force resized image to be jpg | gif | png | wbmp - leave blank to match source type
  51.     public $imgQuality        = '80';            // image quality 1-100 (%)
  52.    
  53.    
  54.    
  55.     // array of all generated error messages
  56.     public $errors = array(
  57.         'no-image'         => '<strong>Error:</strong> Please specify a source image - declare: $mj_Image->source_file = "path_to_your_image_here.jpg";',
  58.         'no-width'         => '<strong>Error:</strong> Please specify a width for the new image - declare: $mj_Image->newWidth = 100;',
  59.         'no-height'     => '<strong>Error:</strong> Please specify a height for the new image - declare: $mj_Image->newheight = 100;',
  60.         'image-exists'     => '<strong>Error:</strong> The image you specified already exists',
  61.         'upl-no-array'    => '<strong>Error:</strong> It appears the form did not submit a valid file - please try again.',
  62.         'upl-ini-max'    => '<strong>Error:</strong> The file uploaded exceeds the filesize limit set on this server',
  63.         'upl-maxsize'    => '<strong>Error:</strong> The file uploaded exceeds the filesize limit set for this form',
  64.         'upl-partial'    => '<strong>Error:</strong> The file was only partially uploaded - please try again',
  65.         'upl-no-file'    => '<strong>Error:</strong> No file was submitted for upload',
  66.         'upl-no-tmpDir'    => '<strong>Error:</strong> The temporary upload directory is missing',
  67.         'upl-cant-write'=> '<strong>Error:</strong> Failed to write to the temporary folder - please check the permissions',
  68.         'upl-ext'        => '<strong>Error:</strong> The upload was stopped due to an invalid file extension',
  69.         'upl-no-size'    => '<strong>Error:</strong> The file submitted for upload had a filesize of zero or was corrupt',
  70.         'upl-failed'    => '<strong>Error:</strong> The upload failed - the file could not be moved to the target location - please check the permissions',
  71.         'no-crop-width' => '<strong>Error:</strong> Please specify a crop width for the new image',
  72.         'no-crop-height'=> '<strong>Error:</strong> Please specify a crop height for the new image',
  73.         'no-crop-x'     => '<strong>Error:</strong> Please specify a crop x position for the new image',
  74.         'no-crop-y'        => '<strong>Error:</strong> Please specify a crop y position for the new image',
  75.         'crop-ext'        => '<strong>Error:</strong> The crop was stopped due to an invalid file extension',
  76.     );
  77.    
  78.     #################################################
  79.    #                     METHODS                    #
  80.    #################################################
  81.  
  82.     // This function runs when this class is instantiated
  83.     public function __construct() {
  84.         //echo 'newImage Constructed';
  85.     }
  86.  
  87.     private function hex2dec($_hex) {
  88.         $_color = str_replace('#', '', $_hex);
  89.         $_ret = array(
  90.             'r' => hexdec(substr($_color, 0, 2)),
  91.             'g' => hexdec(substr($_color, 2, 2)),
  92.             'b' => hexdec(substr($_color, 4, 2))
  93.         );
  94.         return $_ret;
  95.     }
  96.    
  97.     public function cleanUp($str) {
  98.         $str = stripslashes($str);
  99.         $str = str_replace(' ','_',$str);
  100.         $str = str_replace('.JPG','.jpg',$str);
  101.         $str = str_replace('.PNG','.png',$str);
  102.         $str = str_replace('.GIF','.gif',$str);
  103.         $str = preg_replace("/[^A-Za-z0-9_\-\.]/i", "",$str);
  104.         return $str;
  105.     }
  106.    
  107.     public function upload($ar) {
  108.         // ERROR CAPTURE
  109.         if(!isset($ar['name'])) { $this->doDie($this->errors['upl-no-array']); exit; }
  110.         if($ar['error']!=0) {
  111.             switch($ar['error']) {
  112.                 case 1: $this->doDie($this->errors['upl-ini-max']);     exit; break;
  113.                 case 2: $this->doDie($this->errors['upl-maxsize']);     exit; break;
  114.                 case 3: $this->doDie($this->errors['upl-partial']);     exit; break;
  115.                 case 4: $this->doDie($this->errors['upl-no-file']);     exit; break;
  116.                 case 6: $this->doDie($this->errors['upl-no-tmpDir']);     exit; break;
  117.                 case 7: $this->doDie($this->errors['upl-cant-write']);     exit; break;
  118.                 case 8: $this->doDie($this->errors['upl-ext']);         exit; break;
  119.             }
  120.         }
  121.         if($ar['size']==0) { $this->doDie($this->errors['upl-no-size']); exit; }
  122.  
  123.         // create variables
  124.         $img_name         = $ar['name'];
  125.         $img_type         = $ar['type'];
  126.         $img_tmp_name     = $ar['tmp_name'];
  127.         $img_error        = $ar['error'];
  128.         $img_size        = $ar['size'];
  129.        
  130.         // original extension
  131.         $ext = substr(strrchr($img_name,'.'),1);
  132.        
  133.         // rename file to safe filename
  134.         if($this->safeRename=='true') {
  135.             $imgPath = str_replace(basename($img_name),"",$img_name);
  136.             $imgName = ($this->newName!='') ? $this->newName : $img_name;
  137.             $imgName = $this->cleanUp(basename($imgName));
  138.             $img_name = $imgPath.$imgName;
  139.         }
  140.        
  141.         // ensure the extension is correct if the name has been altered
  142.         if(strrchr($img_name,'.')) { // has extension
  143.             $new_ext = substr(strrchr($img_name,'.'),1);
  144.             if($new_ext!=$ext) {
  145.                 $img_name = str_replace($new_ext,'',$img_name).'.'.$ext; // use original extension
  146.             }
  147.         } else { // no extension
  148.             $img_name = $img_name.'.'.$ext; // add in original
  149.         }
  150.        
  151.         // set target path
  152.         $target_path = $this->uploadTo . basename($img_name);
  153.        
  154.         // Handle duplicates
  155.         if(file_exists($target_path)) {
  156.             switch($this->duplicates) {
  157.                 case 'o': break;
  158.                 case 'e': $this->doDie($this->errors['image-exists']); exit;
  159.                 case 'a': return false; break;
  160.                 default: // make unique
  161.                     $im = (strstr(basename($img_name),'.')) ? substr(basename($img_name),0,strrpos(basename($img_name),'.')) : basename($img_name);
  162.                     $ext = str_replace($im,"",basename($img_name));
  163.                     $path = $this->uploadTo;
  164.                     $i=1;
  165.                     while(file_exists($path.$im.$i.$ext)) {
  166.                         $i++;
  167.                     }
  168.                     $imgName = $im.$i.$ext;
  169.                     $imgPath = str_replace(basename($img_name),"",$img_name);
  170.                     $img_name = $imgPath.$imgName;
  171.                     $target_path = $this->uploadTo . $imgName;
  172.             }
  173.         }
  174.        
  175.         // Make path writable // chmod file to 0777 if possible
  176.         if(file_exists($target_path)) {
  177.             @chmod($target_path, 0777);
  178.         }
  179.        
  180.         // Do upload / move image to target path
  181.         if(move_uploaded_file($img_tmp_name, $target_path)) {
  182.            
  183.             // add uploaded file to $this->source_file for resize function quick access
  184.             $this->source_file = $this->uploadTo.basename($img_name);
  185.            
  186.             // Return image, array or blob if required
  187.             switch(strtolower($this->returnType)) {
  188.                 case 'array':
  189.                     $_ar = array(
  190.                         'image'     => basename($img_name),
  191.                         'path'        => $this->uploadTo,
  192.                         'size'        => $img_size
  193.                     );
  194.                     return $_ar; break;
  195.                 case 'fullpath':
  196.                     return (file_exists($target_path)) ? $target_path : false; break;
  197.                 case 'blob':
  198.                     if(file_exists($target_path)) {
  199.                         $fo = fopen($target_path, 'r');
  200.                         $blob = mysql_real_escape_string(fread($fo, filesize($target_path)));
  201.                         fclose($fo);
  202.                         return $blob; break;
  203.                     } else { return false; break; }
  204.                 default:
  205.                     return (file_exists($target_path)) ? true : false; break;
  206.             }
  207.         } else{
  208.             $this->doDie($this->errors['upl-failed']); exit;
  209.         }
  210.     }
  211.    
  212.     public function crop($w=false,$h=false,$x=false,$y=false) {
  213.         // ERROR CAPTURE
  214.         if(!$w) {     $this->doDie($this->errors['no-crop-width']); exit; }
  215.         if(!$h) {     $this->doDie($this->errors['no-crop-height']); exit; }
  216.         if(is_int($x)) {     $this->doDie($this->errors['no-crop-x']); exit; }
  217.         if(is_int($y)) {     $this->doDie($this->errors['no-crop-y']); exit; }
  218.         $sImage = (($this->source_file!='')&&(file_exists($this->source_file))) ? $this->source_file : $this->doDie($this->errors['no-image']);
  219.        
  220.         // make sure the new dimensions are numbers
  221.         $w = (!is_int($w)) ? intval($w) : $w;
  222.         $h = (!is_int($h)) ? intval($h) : $h;
  223.         $x = (!is_int($x)) ? intval($x) : $x;
  224.         $y = (!is_int($y)) ? intval($y) : $y;
  225.        
  226.         // get image details
  227.         $image_info = getimagesize($sImage);
  228.        
  229.         // set source as resource
  230.         switch($image_info['mime']) {
  231.             case 'image/gif':
  232.                 if (imagetypes() & IMG_GIF)  { // not the same as IMAGETYPE
  233.                     $src = imagecreatefromgif($this->source_file);
  234.                     $this->imgType = ($this->newImgType!=='') ? $this->newImgType : 'gif';
  235.                 } else {
  236.                     $this->doDie($this->errors['crop-ext'].' - GIF images are not supported');
  237.                 }
  238.                 break;
  239.             case 'image/jpeg':
  240.                 if (imagetypes() & IMG_JPG)  {
  241.                     $src = imagecreatefromjpeg($this->source_file) ;
  242.                     $this->imgType = ($this->newImgType!=='') ? $this->newImgType : 'jpg';
  243.                 } else {
  244.                     $this->doDie($this->errors['crop-ext'].' - JPEG images are not supported');
  245.                 }
  246.                 break;
  247.             case 'image/png':
  248.                 if (imagetypes() & IMG_PNG)  {
  249.                     $src = imagecreatefrompng($this->source_file);
  250.                     imagealphablending($src, true); // setting alpha blending on (we want to blend this image with the canvas)
  251.                     imagesavealpha($src, true); // save alphablending setting
  252.                     $this->imgType = ($this->newImgType!=='') ? $this->newImgType : 'png';
  253.                 } else {
  254.                     $this->doDie($this->errors['crop-ext'].' - PNG images are not supported');
  255.                 }
  256.                 break;
  257.             case 'image/wbmp':
  258.                 if (imagetypes() & IMG_WBMP)  {
  259.                     $src = imagecreatefromwbmp($this->source_file) ;
  260.                     $this->imgType = ($this->newImgType!=='') ? $this->newImgType : 'wbmp';
  261.                 } else {
  262.                     $this->doDie($this->errors['crop-ext'].' - WBMP images are not supported');
  263.                 }
  264.                 break;
  265.             default:
  266.                 $this->doDie($this->errors['crop-ext'].' - '.$image_info['mime'].' files are not supported');
  267.                 break;
  268.         }
  269.        
  270.         $srcPath = (strstr($sImage,'/')) ? substr($sImage,0,strrpos($sImage,'/')+1) : '';
  271.         $srcName = str_replace($srcPath,'',$sImage);
  272.        
  273.         $path = ($this->newPath!='') ? trim($this->newPath) : trim($srcPath);
  274.         $path = (substr($path,strlen($path)-1,strlen($path))!='/') ? $path.'/' : $path;
  275.         $imgName = ($this->newName!='') ? $this->newName : $srcName;
  276.        
  277.         $imgName = (strstr($imgName,'.')) ? substr($imgName,0,strrpos($imgName,'.')).'.'.$this->imgType
  278.                                           : $imgName.'.'.$this->imgType; // make sure it has the correct extension
  279.        
  280.         $preFix = ($this->namePrefix!='') ? $this->namePrefix : '';
  281.        
  282.         // if path doesn't exist then create it and chmod to 0777
  283.         if(!file_exists($path)) { mkdir($path, 0777); }
  284.        
  285.         // rename file to safe filename
  286.         if($this->safeRename=='true') {
  287.             $imgName = $this->cleanUp($imgName);
  288.         }
  289.        
  290.         // Handle duplicates
  291.         if(file_exists($path.$preFix.$imgName)) {
  292.             switch($this->duplicates) {
  293.                 case 'o': break;
  294.                 case 'e': $this->doDie($this->errors['image-exists']); exit;
  295.                 case 'a': return false; break;
  296.                 default: // make unique
  297.                     $im = (strstr($imgName,'.')) ? substr($imgName,0,strrpos($imgName,'.')) : $imgName;
  298.                     $i=1;
  299.                     while(file_exists($path.$im.$i.'.'.$this->imgType)) {
  300.                         $i++;
  301.                     }
  302.                     $imgName = $im.$i.'.'.$this->imgType;
  303.             }
  304.         }
  305.        
  306.         // Source dimensions
  307.         $s_width = imagesx($src);
  308.         $s_height = imagesy($src);
  309.        
  310.         // Create target image
  311.         $canvas = imagecreatetruecolor($w, $h);
  312.        
  313.         //echo ($canvas.' - '.$src.' - '.$x.' - '.$y.' - 0 - 0 - '.$s_width.' - '.$s_height.' - '.$s_width.' - '.$s_height);
  314.        
  315.         // Copy image
  316.         imagecopyresampled($canvas, $src, $x, $y, 0, 0, $s_width, $s_height, $s_width, $s_height);
  317.        
  318.         // output image
  319.         switch($this->imgType) {
  320.             case 'gif':     $newImg = imagejpeg($canvas, $path.$preFix.$imgName, $this->imgQuality);
  321.             case 'png':
  322.                 $quality = (intval($this->imgQuality) > 90) ? 9 : round(intval($this->imgQuality)/10);
  323.                 //imagealphablending($canvas, false);
  324.                 //imagesavealpha($canvas, true);
  325.                 $newImg = imagepng($canvas, $path.$preFix.$imgName,$quality);
  326.             case 'wbmp':     $newImg = imagewbmp($canvas, $path.$preFix.$imgName);
  327.             default:         $newImg = imagejpeg($canvas, $path.$preFix.$imgName, $this->imgQuality);
  328.         }
  329.        
  330.         // clean up
  331.         imagedestroy($src);
  332.         imagedestroy($canvas);
  333.        
  334.         // Return image, array or blob if required
  335.         switch(strtolower($this->returnType)) {
  336.             case 'array':
  337.                 $_ar = array(
  338.                     'image'     => $imgName,
  339.                     'prefix'    => $preFix,
  340.                     'path'        => $path,
  341.                     'height'    => $h,
  342.                     'width'        => $w
  343.                 );
  344.                 return $_ar; break;
  345.             case 'fullpath':
  346.                 return (file_exists($path.$preFix.$imgName)) ? $path.$preFix.$imgName : false; break;
  347.             case 'blob':
  348.                 if(file_exists($path.$preFix.$imgName)) {
  349.                     $fo = fopen($path.$preFix.$imgName, 'r');
  350.                     $blob = mysql_real_escape_string(fread($fo, filesize($path.$preFix.$imgName)));
  351.                     fclose($fo);
  352.                     return $blob; break;
  353.                 } else { return false; break; }
  354.             default:
  355.                 return (file_exists($path.$preFix.$imgName)) ? true : false; break;
  356.         }
  357.     }
  358.    
  359.     public function resize() {
  360.         // ERROR CAPTURE
  361.         if($this->newWidth=='') {     $this->doDie($this->errors['no-width']); exit; }
  362.         if($this->newHeight=='') {     $this->doDie($this->errors['no-height']); exit; }
  363.         $sImage = (($this->source_file!='')&&(file_exists($this->source_file))) ? $this->source_file : $this->doDie($this->errors['no-image']);
  364.        
  365.         // make sure the new dimensions are numbers
  366.         $this->newWidth = (!is_int($this->newWidth)) ? intval($this->newWidth) : $this->newWidth;
  367.         $this->newHeight = (!is_int($this->newHeight)) ? intval($this->newHeight) : $this->newHeight;
  368.        
  369.         // get image details
  370.         $image_info = getimagesize($sImage);
  371.        
  372.         // If oversize is used then turn off padToFit
  373.         if($this->oversize=='true') {
  374.             $this->padToFit = 'false';
  375.         }
  376.        
  377.         // select the filetype based on file MIME
  378.         // set source as resource
  379.         switch ($image_info['mime']) {
  380.             case 'image/gif':
  381.                 if (imagetypes() & IMG_GIF)  { // not the same as IMAGETYPE
  382.                     $src = imagecreatefromgif($this->source_file);
  383.                     $this->imgType = ($this->newImgType!=='') ? $this->newImgType : 'gif';
  384.                 } else {
  385.                     $this->doDie($this->errors['upl-ext'].' - GIF images are not supported');
  386.                 }
  387.                 break;
  388.             case 'image/jpeg':
  389.                 if (imagetypes() & IMG_JPG)  {
  390.                     $src = imagecreatefromjpeg($this->source_file) ;
  391.                     $this->imgType = ($this->newImgType!=='') ? $this->newImgType : 'jpg';
  392.                 } else {
  393.                     $this->doDie($this->errors['upl-ext'].' - JPEG images are not supported');
  394.                 }
  395.                 break;
  396.             case 'image/png':
  397.                 if (imagetypes() & IMG_PNG)  {
  398.                     $src = imagecreatefrompng($this->source_file);
  399.                     imagealphablending($src, true); // setting alpha blending on (we want to blend this image with the canvas)
  400.                     imagesavealpha($src, true); // save alphablending setting
  401.                     $this->imgType = ($this->newImgType!=='') ? $this->newImgType : 'png';
  402.                 } else {
  403.                     $this->doDie($this->errors['upl-ext'].' - PNG images are not supported');
  404.                 }
  405.                 break;
  406.             case 'image/wbmp':
  407.                 if (imagetypes() & IMG_WBMP)  {
  408.                     $src = imagecreatefromwbmp($this->source_file) ;
  409.                     $this->imgType = ($this->newImgType!=='') ? $this->newImgType : 'wbmp';
  410.                 } else {
  411.                     $this->doDie($this->errors['upl-ext'].' - WBMP images are not supported');
  412.                 }
  413.                 break;
  414.             default:
  415.                 $this->doDie($this->errors['upl-ext'].' - '.$image_info['mime'].' files are not supported');
  416.                 break;
  417.         }
  418.        
  419.         $srcPath = (strstr($sImage,'/')) ? substr($sImage,0,strrpos($sImage,'/')+1) : '';
  420.         $srcName = str_replace($srcPath,'',$sImage);
  421.        
  422.        
  423.         $path = ($this->newPath!='') ? trim($this->newPath) : trim($srcPath);
  424.         $path = (substr($path,strlen($path)-1,strlen($path))!='/') ? $path.'/' : $path;
  425.         $imgName = ($this->newName!='') ? $this->newName : $srcName;
  426.        
  427.         $imgName = (strstr($imgName,'.')) ? substr($imgName,0,strrpos($imgName,'.')).'.'.$this->imgType
  428.                                           : $imgName.'.'.$this->imgType; // make sure it has the correct extension
  429.        
  430.         $preFix = ($this->namePrefix!='') ? $this->namePrefix : '';
  431.        
  432.         // if path doesn't exist then create it and chmod to 0777
  433.         if(!file_exists($path)) { mkdir($path, 0777); }
  434.        
  435.         // rename file to safe filename
  436.         if($this->safeRename=='true') {
  437.             $imgName = $this->cleanUp($imgName);
  438.         }
  439.        
  440.         // Handle duplicates
  441.         if(file_exists($path.$preFix.$imgName)) {
  442.             switch($this->duplicates) {
  443.                 case 'o': break;
  444.                 case 'e': $this->doDie($this->errors['image-exists']); exit;
  445.                 case 'a': return false; break;
  446.                 default: // make unique
  447.                     $im = (strstr($imgName,'.')) ? substr($imgName,0,strrpos($imgName,'.')) : $imgName;
  448.                     $i=1;
  449.                     while(file_exists($path.$im.$i.'.'.$this->imgType)) {
  450.                         $i++;
  451.                     }
  452.                     $imgName = $im.$i.'.'.$this->imgType;
  453.             }
  454.         }
  455.        
  456.         // Source dimensions
  457.         $s_width = imagesx($src);
  458.         $s_height = imagesy($src);
  459.        
  460.         // canvas dimensions
  461.         $c_width = $this->newWidth;
  462.         $c_height = $this->newHeight;
  463.        
  464.         // maintain the aspect ratio
  465.         if($this->aspectRatio=='true') {
  466.             if($s_width > $s_height) {
  467.                 if($this->oversize=='true') { // resize to fill the frame - oversized images
  468.                     $resize_pc = ($this->newHeight/$s_height);
  469.                         $this->newWidth = round($s_width*$resize_pc);
  470.                 } else {
  471.                     $resize_pc = ($this->newWidth/$s_width);
  472.                     // make sure the new dimensions fit into defined space
  473.                     if(round($s_height*$resize_pc)<=$this->newHeight) {
  474.                         $this->newHeight = round($s_height*$resize_pc);
  475.                     } else {
  476.                         $resize_pc = ($this->newHeight/$s_height);
  477.                         $this->newWidth = round($s_width*$resize_pc);
  478.                     }
  479.                 }
  480.             } else {
  481.                 if($this->oversize=='true') { // resize to fill the frame - oversized images
  482.                     $resize_pc = ($this->newWidth/$s_width);
  483.                     $this->newHeight = round($s_height*$resize_pc);
  484.                 } else {
  485.                     $resize_pc = ($this->newHeight/$s_height);
  486.                     // make sure the new dimensions fit into defined space
  487.                     if(round($s_width*$resize_pc)<=$this->newWidth) {
  488.                         $this->newWidth = round($s_width*$resize_pc);
  489.                     } else {
  490.                         $resize_pc = ($this->newWidth/$s_width);
  491.                         $this->newHeight = round($s_height*$resize_pc);
  492.                     }
  493.                 }
  494.             }
  495.         }
  496.        
  497.         if($this->padToFit!='true') { // if padding not required then set canvas size to new image size aspect
  498.             $c_width = $this->newWidth;
  499.             $c_height = $this->newHeight;
  500.         }
  501.        
  502.         if($this->upscale!='true'){
  503.             // do not upscale image
  504.             if(($s_width<=$this->newWidth)&&($s_height<=$this->newHeight)) {
  505.                 $this->newWidth = $s_width;
  506.                 $this->newHeight = $s_height;
  507.             }
  508.         }
  509.          
  510.        
  511.         // set the position of source in the canvas
  512.         if($this->padToFit=='true') {
  513.             // set positions
  514.             $top = $left = 0;
  515.             $right = $c_width-$this->newWidth;
  516.             $cenX = ($c_width/2)-($this->newWidth/2);
  517.             $cenY = ($c_height/2)-($this->newHeight/2);
  518.             $bottom = $c_height-$this->newHeight;
  519.             switch(true) {
  520.                 case (strstr($this->setPosition,',')): // pixel x,y entered
  521.                     $dim = explode($this->setPosition,',');
  522.                     $x = intval(@$dim[0]);
  523.                     $y = intval(@$dim[1]);
  524.                     $toPos = array('dx'=>$x,'dy'=>$y,'sx'=>0,'sy'=>0); break;
  525.                 case ($this->setPosition=='tl'): // top left
  526.                     $toPos = array('dx'=>$left,'dy'=>$top,'sx'=>0,'sy'=>0); break;
  527.                 case ($this->setPosition=='tr'): // top right
  528.                     $toPos = array('dx'=>$right,'dy'=>$top,'sx'=>0,'sy'=>0); break;
  529.                 case ($this->setPosition=='tc'): // top centre
  530.                     $toPos = array('dx'=>$cenX,'dy'=>$top,'sx'=>0,'sy'=>0); break;
  531.                 case ($this->setPosition=='bl'): // bottom left
  532.                     $toPos = array('dx'=>$left,'dy'=>$bottom,'sx'=>0,'sy'=>0); break;
  533.                 case ($this->setPosition=='br'): // bottom right
  534.                     $toPos = array('dx'=>$right,'dy'=>$bottom,'sx'=>0,'sy'=>0); break;
  535.                 case ($this->setPosition=='bc'): // bottom centre
  536.                     $toPos = array('dx'=>$cenX,'dy'=>$bottom,'sx'=>0,'sy'=>0); break;
  537.                 case ($this->setPosition=='cl'): // centre left
  538.                     $toPos = array('dx'=>$left,'dy'=>$cenY,'sx'=>0,'sy'=>0); break;
  539.                 case ($this->setPosition=='cr'): // centre right
  540.                     $toPos = array('dx'=>$right,'dy'=>$cenY,'sx'=>0,'sy'=>0); break;
  541.                 default: // centred horz + vert
  542.                     $toPos = array('dx'=>$cenX,'dy'=>$cenY,'sx'=>0,'sy'=>0); break;
  543.             }
  544.         } else {
  545.             $toPos = array('dx'=>0,'dy'=>0,'sx'=>0,'sy'=>0);
  546.         }
  547.        
  548.         // Create target image
  549.         $canvas = imagecreatetruecolor($c_width, $c_height);
  550.        
  551.         // colour the canvas
  552.         if($this->padColour=='transparent')  {
  553.             $trans_colour = imagecolorallocatealpha($canvas, 0, 0, 0, 127);
  554.             imagefill($canvas, 0, 0, $trans_colour);
  555.             imagealphablending($canvas, true);
  556.             imagesavealpha($canvas, true);
  557.         } else {
  558.             $col = $this->hex2dec($this->padColour);
  559.             $bgCol = imagecolorallocate($canvas, $col['r'], $col['g'], $col['b']);
  560.             imagefill($canvas, 0, 0, $bgCol);
  561.         }
  562.  
  563.         // Copy image
  564.         imagecopyresampled($canvas, $src, $toPos['dx'], $toPos['dy'], $toPos['sx'], $toPos['sy'], $this->newWidth, $this->newHeight, $s_width, $s_height);
  565.        
  566.         // Output
  567.         if($this->padColour=='transparent')  {
  568.             switch($this->imgType) {
  569.                 case 'gif': $newImg = imagegif($canvas, $path.$preFix.$imgName);
  570.                 default:    
  571.                     $quality = (intval($this->imgQuality) > 90) ? 9 : round(intval($this->imgQuality)/10);
  572.                     //imagealphablending($canvas, false);
  573.                     //imagesavealpha($canvas, true);
  574.                     $newImg = imagepng($canvas, $path.$preFix.$imgName, $quality);
  575.                    
  576.             }
  577.         } else {
  578.             switch($this->imgType) {
  579.                 case 'gif':     $newImg = imagejpeg($canvas, $path.$preFix.$imgName, $this->imgQuality);
  580.                 case 'png':
  581.                     $quality = (intval($this->imgQuality) > 90) ? 9 : round(intval($this->imgQuality)/10);
  582.                     //imagealphablending($canvas, false);
  583.                     //imagesavealpha($canvas, true);
  584.                     $newImg = imagepng($canvas, $path.$preFix.$imgName,$quality);
  585.                 case 'wbmp':     $newImg = imagewbmp($canvas, $path.$preFix.$imgName);
  586.                 default:         $newImg = imagejpeg($canvas, $path.$preFix.$imgName, $this->imgQuality);
  587.             }
  588.         }
  589.        
  590.         // clean up
  591.         imagedestroy($src);
  592.         imagedestroy($canvas);
  593.        
  594.         // Return image, array or blob if required
  595.         switch(strtolower($this->returnType)) {
  596.             case 'array':
  597.                 $_ar = array(
  598.                     'image'     => $imgName,
  599.                     'prefix'    => $preFix,
  600.                     'path'        => $path,
  601.                     'height'    => $c_height,
  602.                     'width'        => $c_width,
  603.                     'bgcolor'    => $this->padColour
  604.                 );
  605.                 return $_ar; break;
  606.             case 'fullpath':
  607.                 return (file_exists($path.$preFix.$imgName)) ? $path.$preFix.$imgName : false; break;
  608.             case 'blob':
  609.                 if(file_exists($path.$preFix.$imgName)) {
  610.                     $fo = fopen($path.$preFix.$imgName, 'r');
  611.                     $blob = mysql_real_escape_string(fread($fo, filesize($path.$preFix.$imgName)));
  612.                     fclose($fo);
  613.                     return $blob; break;
  614.                 } else { return false; break; }
  615.             default:
  616.                 return (file_exists($path.$preFix.$imgName)) ? true : false; break;
  617.         }
  618.     }
  619.  
  620.     #######################################################
  621.    #
  622.    #        EXTRA IMAGE METHODS
  623.    #
  624.    #######################################################
  625.    
  626.     public function get_image_width($img) {
  627.         if(file_exists($img)) {
  628.             list($width, $height) = getimagesize($img);
  629.             return $width;
  630.         } else {
  631.             return 0;
  632.         }
  633.     }
  634.    
  635.     public function get_image_height($img) {
  636.         if(file_exists($img)) {
  637.             list($width, $height) = getimagesize($img);
  638.             return $height;
  639.         } else {
  640.             return 0;
  641.         }
  642.     }
  643.    
  644.     public function get_image_size($img) {
  645.         if(file_exists($img)) {
  646.             list($width, $height) = getimagesize($img);
  647.             return array('width'=>$width,'height'=>$height);
  648.         } else {
  649.             return array('width'=>0,'height'=>0);
  650.         }
  651.     }
  652.    
  653.     public function doDie($msg,$file="",$line="") {                     // doDie - calls a die function with custom error
  654.         if (($file!='')&&($line!='')) {
  655.         die(    '<h4>Error:</h4>'.$msg.'<br/><br/>'.
  656.                  '<strong>File:</strong> '.$file.
  657.                 ' - <strong>on line:</strong> '.$line.
  658.                 '</body></html>');
  659.         } else {
  660.         die(    $msg);
  661.         }
  662.     }
  663. } // END OF CLASS
  664. /*
  665.  
  666. YOU CAN DELETE EVERYTHING FROM HERE ONWARDS (although you may want to keep a closing ?>)
  667.  
  668. public $uploadTo        = 'uploaded/';
  669.     public $source_file     = '';
  670.     public $newWidth         = '';    
  671.     public $newHeight         = '';
  672.     public $newName         = '';
  673.     public $namePrefix         = '';
  674.     public $newPath         = '';
  675.     public $duplicates        = 'u'; // u = make unique / o = overwrite / e = error / a = abort
  676.     public $safeRename        = 'true';
  677.     public $aspectRatio     = 'true';
  678.     public $padToFit         = 'true';
  679.     public $upscale         = 'false';
  680.     public $setPosition        = 'cc';
  681.     public $padColour         = '#FFFFFF';
  682.     public $padTransparent    = 'true'; // if uploading a GIF or PNG then set background as transparent - this overrides $padColour
  683.     public $returnType        = 'fullPath';
  684. */
  685. if(isset($_FILES['image'])) {
  686. $myImage = new _image;
  687. $myImage->uploadTo = 'testDir/';
  688. $res = $myImage->upload($_FILES['image']);
  689. if($res) {
  690.     // RESIZE
  691.     //$myImage->padColour = '#222222';
  692.     $myImage->newWidth = 400;
  693.     $myImage->newHeight = 300;
  694.     $i = $myImage->resize();
  695.     echo $i;
  696. }
  697. } else {
  698. ?>
  699. <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
  700.     <p>
  701.         <input type="file" name="image" id="image" />
  702.     </p>
  703.     <p>
  704.         <input type="submit" name="button" id="button" value="Submit" />
  705.     </p>
  706. </form>
  707. <?php } ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement