Viewing file: FileUpload.php (11.02 KB) -rwxr-xr-x Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
//header("Content-type: image/jpeg");
if(!class_exists('Util'))
require_once(APP_PATH."/util/Util.php");
/**
* Description of Upload
*
* @author Cledson
*/
class FileUpload {
public $targetUpload = "../../uploads/";
public $type = "image"; // file - image
public $width = 400;
public $height = 300;
public $resize = false;
/**
* Retorna o tipo do arquivo
*
* @return String
*/
public function getType() {
return $this->type;
}
/**
* Seta o tipo do arquivo
* @param String $type Tipo do arquivo
*
* @return void
*/
public function setType($type) {
$this->type = $type;
}
/**
* Efetua o upload do arquivo<br>
* Caso seja uma imagem, verifica se esta deve ser redimensionada
*
* @param String $folder Pasta de destino do arquivo
* @param Integer $id Identificador do registro
* @param String $filename Nome do arquivo
* @param ByteArray $bytes Array de bytes do arquivo vindo do flex
*
* @return boolean
* */
public function upload($file, $folder, $id, $filename) {
$util = new Util();
if (!is_dir($this->targetUpload . "/")){
mkdir($this->targetUpload . "/", 0777);
}
if (!is_dir($this->targetUpload . $folder . "/")){
mkdir($this->targetUpload . $folder . "/", 0777);
}
if (!is_dir($this->targetUpload . $folder . "/" . $id . "/")){
mkdir($this->targetUpload . $folder . "/" . $id . "/", 0777);
}
if(move_uploaded_file($file, $this->targetUpload.$folder."/".$id."/".$util->nameForWeb($filename))){
if ($this->type == "image" && $this->resize){
$this->imageResize($util->nameForWeb($filename), $folder."/".$id);
}
return true;
}
else
return false;
}
/**
* Upload files!
*
* @param FileVO $file
* @return string
* */
public function deleteFolder($folder, $id) {
@$OpenDir = opendir($this->targetUpload . $folder . "/" . $id);
while (false !== ($file = @readdir($OpenDir))) {
if (($file != ".") && ($file != "..") && ($file != ""))
@unlink($this->targetUpload . $folder . "/" . $id . "/" . $file);
}
@closedir($OpenDir);
@rmdir($this->targetUpload . $folder . "/" . $id);
return true;
}
/**
* Redimensiona a imagem de acordo com o tamanho pré estabelecido
*
* @param String $image Imagem a ser redimensionada
* @param String $folder Pasta de destino do arquivo
*
* @return void
* */
public function imageResize($image, $folder, $prefix="", $gallery=false) {
$originalExtension = strrchr($image, ".");
$extension = strtolower($originalExtension);
$imageTmp = $this->targetUpload . $folder . $image;
if($prefix == "" && !$gallery){
$prefix = str_replace($originalExtension, "", $image);
}
if($gallery){
$prefix = $prefix.str_replace($originalExtension, "", $image);
}
if ($extension == ".jpg" || $extension == ".jpeg") {
$img = imagecreatefromjpeg($imageTmp);
$x = imagesx($img);
$y = imagesy($img);
if($x < $this->width){
$this->width = $x;
}
if($y < $this->height){
$this->height = $y;
}
$lowerSide = ($this->width < $this->height) ? $this->width : $this->height;
$biggerSide = ($this->width > $this->height) ? $this->width : $this->height;
$widthJPG = $x < $y ? $lowerSide : $biggerSide;
$heightJPG = $x > $y ? $lowerSide : $biggerSide;
$finalImage = imagecreatetruecolor($widthJPG, $heightJPG);
imagecopyresampled($finalImage, $img, 0, 0, 0, 0, $widthJPG, $heightJPG, $x, $y);
imagejpeg($finalImage, $this->targetUpload.$folder."/".$prefix.$extension, 90);
}
if ($extension == ".gif") {
$img = imagecreatefromgif($imageTmp);
$widthGIF = $this->width;
$heightGIF = $this->height;
$x = imagesx($img);
$y = imagesy($img);
if($x < $this->width){
$this->width = $x;
}
if($y < $this->height){
$this->height = $y;
}
$lowerSide = ($this->width < $this->height) ? $this->width : $this->height;
$biggerSide = ($this->width > $this->height) ? $this->width : $this->height;
$widthGIF = $x < $y ? $lowerSide : $biggerSide;
$heightGIF = $x > $y ? $lowerSide : $biggerSide;
$finalImage = imagecreatetruecolor($widthGIF, $heightGIF);
imagecopyresampled($finalImage, $img, 0, 0, 0, 0, $widthGIF, $heightGIF, $x, $y);
imagegif($finalImage, $this->targetUpload.$folder."/".$prefix.$extension, 90);
}
if ($extension == ".png") {
$img = imageCreateFromPng($imageTmp);
$x = imagesx($img);
$y = imagesy($img);
if($x < $this->width){
$this->width = $x;
}
if($y < $this->height){
$this->height = $y;
}
$lowerSide = ($this->width < $this->height) ? $this->width : $this->height;
$biggerSide = ($this->width > $this->height) ? $this->width : $this->height;
$widthPNG = $x < $y ? $lowerSide : $biggerSide;
$heightPNG = $x > $y ? $lowerSide : $biggerSide;
$finalImage = imagecreatetruecolor($widthPNG, $heightPNG);
imagecopyresampled($finalImage, $img, 0, 0, 0, 0, $widthPNG, $heightPNG, $x, $y);
imagepng($finalImage, $this->targetUpload.$folder."/".$prefix.$extension, 9);
}
}
/**
* Recorta a imagem de acordo com o tamanho e a posição pré estabelecida
*
* @param String $image Imagem a ser redimensionada
* @param String $folder Pasta de destino do arquivo
*
* @return void
* */
public function imageCrop($image, $folder, $width, $height, $posX, $posY, $prefix) {
$imageTmp = $this->targetUpload . $folder . $image;
$extension = strtolower(strrchr($image, "."));
if ($extension == ".jpg" || $extension == ".jpeg") {
$imgTemp = imagecreatefromjpeg($imageTmp);
$baseTemp = imagecreatetruecolor($width, $height);
imagecopyresampled($baseTemp, $imgTemp, 0, 0, $posX, $posY, $width, $height, $width, $height);
imagejpeg($baseTemp, $this->targetUpload . $folder.$prefix.$extension, 100);
}
if ($extension == ".gif") {
$imgTemp = imagecreatefromgif($imageTmp);
$baseTemp = imagecreatetruecolor($width, $height);
imagecopyresampled($baseTemp, $imgTemp, 0, 0, $posX, $posY, $width, $height, $width, $height);
imagegif($baseTemp, $this->targetUpload . $folder.$prefix.$extension, 100);
}
if ($extension == ".png") {
$imgTemp = imageCreateFromPng($imageTmp);
$baseTemp = imagecreatetruecolor($width, $height);
imagecopyresampled($baseTemp, $imgTemp, 0, 0, $posX, $posY, $width, $height, $width, $height);
imagepng($baseTemp, $this->targetUpload . $folder.$prefix.$extension, 9);
}
}
/**
* Une 2 imagens com a opção de adicionar um texto á marca d'agua
*
* @param String $image Imagem a ser redimensionada
* @param String $folder Pasta de destino do arquivo
*
* @return void
* */
public function imageMerge($image, $folder, $text="") {
$extension = strtolower(strrchr($image, "."));
$imageTmp = $this->targetUpload . $folder . "/" . $image;
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
if ($extension == ".jpg") {
$imgJPG = imagecreatefromjpeg($imageTmp);
$x = imagesx($imgJPG);
$y = imagesy($imgJPG);
if($x < $this->width){
$this->width = $x;
}
if($y < $this->height){
$this->height = $y;
}
$lowerSide = ($this->width < $this->height) ? $this->width : $this->height;
$biggerSide = ($this->width > $this->height) ? $this->width : $this->height;
$widthJPG = $x < $y ? $lowerSide : $biggerSide;
$heightJPG = $x > $y ? $lowerSide : $biggerSide;
$finalImage = imagecreatetruecolor($widthJPG, $heightJPG);
imagecopyresampled($finalImage, $imgJPG, 0, 0, 0, 0, $widthJPG, $heightJPG, $x, $y);
imagejpeg($finalImage, $this->targetUpload.$folder."/".$prefix.str_replace("temp_", "", $image), 90);
}
if ($extension == ".gif") {
$imgGIF = imagecreatefromgif($imagemTmp);
$widthGIF = $this->width;
$heightGIF = $this->height;
$x = imagesx($imgGIF);
$y = imagesy($imgGIF);
if($x < $this->width){
$this->width = $x;
}
if($y < $this->height){
$this->height = $y;
}
$lowerSide = ($this->width < $this->height) ? $this->width : $this->height;
$biggerSide = ($this->width > $this->height) ? $this->width : $this->height;
$widthGIF = $x < $y ? $lowerSide : $biggerSide;
$heightGIF = $x > $y ? $lowerSide : $biggerSide;
$finalImage = imagecreatetruecolor($widthGIF, $heightGIF);
imagecopyresampled($finalImage, $imgGIF, 0, 0, 0, 0, $widthGIF, $heightGIF, $x, $y);
imagegif($finalImage, $this->targetUpload.$folder."/".$prefix.$image, 90);
}
if ($extension == ".png") {
$imgPNG = imagecreatefrompng($imagemTmp);
$widthPNG = $this->width;
$heightPNG = $this->height;
$x = imagesx($imgPNG);
$y = imagesy($imgPNG);
if($x < $this->width){
$this->width = $x;
}
if($y < $this->height){
$this->height = $y;
}
$lowerSide = ($this->width < $this->height) ? $this->width : $this->height;
$biggerSide = ($this->width > $this->height) ? $this->width : $this->height;
$widthPNG = $x < $y ? $lowerSide : $biggerSide;
$heightPNG = $x > $y ? $lowerSide : $biggerSide;
$finalImage = imagecreatetruecolor($widthPNG, $heightPNG);
imagecopyresampled($finalImage, $imgPNG, 0, 0, 0, 0, $widthPNG, $heightPNG, $x, $y);
imagepng($finalImage, $this->targetUpload.$folder."/".$prefix.$image, 90);
}
}
}
?>
|