Created
July 29, 2015 03:12
-
-
Save Zerquix18/8d1688c91ca53d3d1b0b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class uploadFile { | |
public $filename; | |
private $filepath = './'; | |
public $fullpath; | |
public $error; | |
public function __construct() { | |
return true; | |
} | |
public function set_dir( $dir ) { | |
return is_dir($dir) ? $this->filepath = $dir : false; | |
} | |
public function change_format( $path ) { | |
// de png a jpg. | |
chmod($path, 0777); | |
$x = imagecreatefrompng($path); | |
imagejpeg($x, $path, 80) or die("error!"); // buena calidad, igual es JPG y pesa menos. | |
imagedestroy($x); | |
$this->type = "jpg"; | |
return $l; | |
} | |
function getsize() { | |
$x = getimagesize($this->tmp_name); | |
return array($x[0], $x[1] ); | |
} | |
public function upload( $file ) { | |
if( @empty($_FILES[$file]) ){ | |
$this->error = "No file was selected."; | |
return false; | |
} | |
if( is_array($_FILES[$file]['name']) ) { | |
$this->error = "Cheatin', uh?!"; | |
return false; | |
} | |
if( isset($_FILES[$file]["error"]) && $_FILES[$file]["error"] != 0) { | |
$this->error = "¡Error! [{$_FILES[$file]['error']}]"; | |
return false; | |
} | |
$name = $_FILES[$file]['name']; | |
$size = $_FILES[ $file ][ "size" ]; | |
$this->tmp_name = $_FILES[ $file ][ "tmp_name"]; | |
if( ! in_array( $type = @exif_imagetype($this->tmp_name), array(IMAGETYPE_GIF, IMAGETYPE_PNG, IMAGETYPE_JPEG) ) || false === @getimagesize($this->tmp_name) ) { | |
$this->error = "Invalid image type!"; | |
return false; | |
} | |
if( ( ($size > 1024) / 1024) > 4 ) { | |
$this->error = "Image can't be greater than 5mb"; | |
return false; | |
} | |
$this->type = IMAGETYPE_GIF == $type ? 'gif' : IMAGETYPE_JPEG == $type ? 'jpg' : 'png'; | |
if( "png" == $this->type ) | |
$this->change_format($this->tmp_name); // ahora es JPG. | |
/** Todo listo! Imagen JPG o GIF. Lista para todo :) */ | |
return true; | |
} | |
public function move() { | |
$this->name = substr( md5(uniqid().rand(1,100) ), 0, 15); | |
$this->fullpath = $this->filepath . $this->name . "." . $this->type; | |
$r = move_uploaded_file($this->tmp_name, $this->fullpath); | |
chmod($this->fullpath, 0777); | |
return $r; | |
} | |
public function resize($array, $new = '') { | |
if( is_null($this->fullpath) ) | |
return false; | |
if( is_array($array) && 2 == count($array) ): | |
$width = $array[0]; | |
$height = $array[1]; | |
elseif( 'auto' == $array ): | |
$size = getimagesize($this->fullpath); | |
$height = round(500*$size[1]/$size[0]); | |
$width = 500; | |
endif; | |
$call = $this->type == "jpg" ? "imagecreatefromjpeg" : "imagecreatefromgif"; | |
$call2 = $this->type == "jpg" ? "imagejpeg" : "imagegif"; | |
$img_o = $call($this->fullpath); | |
$img_f = imagecreatetruecolor($width, $height); | |
$pX = imagesX($img_o); | |
$pY = imagesY($img_o); | |
imagecopyresampled($img_f, $img_o, 0, 0, 0, 0, $width+1, $height+1, $pX, $pY); | |
if( empty($new) ) | |
$call2($img_f, $this->fullpath); | |
else | |
$call2($img_f, $this->filepath . $new); | |
imagedestroy($img_o); | |
imagedestroy($img_f); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment