Beruflich Dokumente
Kultur Dokumente
php
class UploadComponent extends Object {
/**
* Private Vars
*/
var $_file;
var $_filepath;
var $_destination;
var $_name;
var $_short;
var $_rules;
var $_allowed;
var $minFile = 100;
var $maxFile = 40000000;
/**
* Public Vars
*/
var $errors;
function redirectSomewhere($value) {
// utilizing a controller method
}
//---------------------------------------------------
/**
* upload
* - handle uploads of any type
* @ file - a file (file to upload) $_FILES[FILE_NAME]
* @ path - string (where to upload to)
* @ name [optional] - override saved filename
* @ rules [optional] - how to handle file types
* - rules['type'] = string
('resize','resizemin','resizecrop','crop')
* - rules['size'] = array (x, y) or single number
* - rules['output'] = string ('gif','png','jpg')
* - rules['quality'] = integer (quality of output image)
* @ allowed [optional] - allowed filetypes
* - defaults to 'jpg','jpeg','gif','png'
* ex:
* $upload = new upload($_FILES['MyFile'], 'uploads');
*
*/
$this->result = false;
$this->error = false;
// -- save parameters
$this->_file = $file;
$this->_destination = $destination;
if (!is_null($rules)) $this->_rules = $rules;
// -- update name
$this->_name = $fileName;
}
else {
// -- it's been uploaded with php
if (is_uploaded_file($fileTmp)) {
} else {
$this->error("Possible file upload attack");
}
// -- format variables
$type = strtolower($type);
$output = strtolower($output);
if (is_array($size)) {
$maxW = intval($size[0]);
$maxH = intval($size[1]);
} else {
$maxScale = intval($size);
}
// -- check sizes
if (isset($maxScale)) {
if (!$maxScale) {
$this->error("Max scale must be set");
}
} else {
if (!$maxW || !$maxH) {
$this->error("Size width and height must be set");
return;
}
if ($type == 'resize') {
$this->error("Provide only one number for size");
}
}
// -- check output
if ($output != 'jpg' && $output != 'png' && $output != 'gif') {
$this->error("Cannot output file as " . strtoupper($output));
}
if (is_numeric($quality)) {
$quality = intval($quality);
if ($quality > 100 || $quality < 1) {
$quality = 75;
}
} else {
$quality = 75;
}
case 'resize':
# Maintains the aspect ration of the image and makes sure that
it fits
# within the maxW and maxH (thus some side will be smaller)
// -- determine new size
if ($uploadWidth > $maxScale || $uploadHeight > $maxScale) {
if ($uploadWidth > $uploadHeight) {
$newX = $maxScale;
$newY = ($uploadHeight*$newX)/$uploadWidth;
} else if ($uploadWidth < $uploadHeight) {
$newY = $maxScale;
$newX = ($newY*$uploadWidth)/$uploadHeight;
} else if ($uploadWidth == $uploadHeight) {
$newX = $newY = $maxScale;
}
} else {
$newX = $uploadWidth;
$newY = $uploadHeight;
}
break;
case 'resizemin':
# Maintains aspect ratio but resizes the image so that once
# one side meets its maxW or maxH condition, it stays at that
size
# (thus one side will be larger)
#get ratios
$ratioX = $maxW / $uploadWidth;
$ratioY = $maxH / $uploadHeight;
$dstImg = imagecreatetruecolor($newX,$newY);
imagecopyresampled($dstImg, $srcImg, 0, 0, 0, 0, $newX, $newY,
$uploadWidth, $uploadHeight);
break;
case 'resizecrop':
// -- resize to max, then crop to center
$ratioX = $maxW / $uploadWidth;
$ratioY = $maxH / $uploadHeight;
break;
case 'crop':
// -- a straight centered crop
$startY = ($uploadHeight - $maxH)/2;
$startX = ($uploadWidth - $maxW)/2;
break;
// -- try to write
switch ($output) {
case 'jpg':
$write = imagejpeg($dstImg, $this->_name, $quality);
break;
case 'png':
$write = imagepng($dstImg, $this->_name . ".png", $quality);
break;
case 'gif':
$write = imagegif($dstImg, $this->_name . ".gif", $quality);
break;
}
// -- clean up
imagedestroy($dstImg);
if ($write) {
$this->result = basename($this->_name);
} else {
$this->error("Could not write " . $this->_name . " to " . $this-
>_destination);
}
}
$file = preg_replace('/[^[:alnum:]_.-]/','',$parts['basename']);
$ext = $parts['extension'];
if ($ext) {
$ext = '.'.$ext;
$file = substr($file,0,-strlen($ext));
}
if(file_exists($dir.'/'.$file.$ext)):
$i = 1;
while (file_exists($dir.'/'.$file.$i.$ext)) $i++;
return $dir.'/'.$file.$i.$ext;
else:
return $dir.'/'.$file.$ext;
endif;
}
$xpos = 0;
$ypos = 0;
$scale = min($width / $this->info['width'], $height / $this-
>info['height']);
if ($scale == 1) {
return;
}
if($ratio!=false)
{
$new_width = (int)($this->info['width'] * $scale);
$new_height = (int)($this->info['height'] * $scale);
$xpos = (int)(($width - $new_width) / 2);
$ypos = (int)(($height - $new_height) / 2);
$image_old = $this->image;
$this->image = imagecreatetruecolor($width, $height);
imagealphablending($this->image, false);
imagesavealpha($this->image, true);
$background = imagecolorallocatealpha($this->image, $r, $g, $b,
127);
imagecolortransparent($this->image, $background);
} else {
$background = imagecolorallocate($this->image, $r, $g, $b);
}
$this->info['width'] = $width;
$this->info['height'] = $height;
}
else
{
$new_width = (int)($width);
$new_height = (int)($height);
$xpos = (int)(($width - $new_width) / 2);
$ypos = (int)(($height - $new_height) / 2);
$image_old = $this->image;
$this->image = imagecreatetruecolor($width, $height);
imagealphablending($this->image, false);
imagesavealpha($this->image, true);
$background = imagecolorallocatealpha($this->image, $r, $g, $b,
127);
imagecolortransparent($this->image, $background);
} else {
$background = imagecolorallocate($this->image, $r, $g, $b);
}
imagefilledrectangle($this->image, 0, 0, $width, $height, $background);
$this->info['width'] = $width;
$this->info['height'] = $height;
}
?>