Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Estou com um script para upload.. está funcionando corretamente, mas eu estava precisando que este sistema fizesse upload de várias imagens de uma vez
Vo posta o script pra ver se alguem pode me ajuda:
index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="[http://www.w3.org/1999/xhtml">](http://www.w3.org/1999/xhtml)
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Documento sem titulo</title>
</head>
<body>
<form action="thumb.php" enctype="multipart/form-data" method="post">
<input type="file" name="arquivo" /><br />
<input type="submit" value="Enviar" />
</form>
</body>
</html><?php
include "conexao.php";
class Upload
{
private $file;
private $file_uploaded;
private $image_size;
private $suggest_name;
private $errors = array();
/**
* Constructor
*
* @param array superglobal $_FILES
* @param string suggest name of generated image or false for automatic name
*/
function __construct( $superglobal, $suggest_name = false )
{
$this->file = $superglobal;
$this->image_size = getimagesize($superglobal['tmp_name']);
$this->suggest_name = ( !$suggest_name ) ? md5(mktime()) : $suggest_name;
}
/**
* Validation method for size, dimension and mime type
*
* @param array
* @return boolean
*/
public function validation( $arr_validation )
{
$array_keys = array_keys($arr_validation);
if( in_array('max_size', $array_keys) )
{
if( $this->file['size'] > $arr_validation['max_size'] )
{
array_push($this->errors, "Muito grande");
}
}
if( in_array('mime', $array_keys) )
{
if( !in_array($this->image_size['mime'], $arr_validation['mime']) )
{
array_push($this->errors, "Tipo inválido");
}
}
if( in_array('max_dimension', $array_keys) )
{
if( $this->image_size[0] > $arr_validation['max_dimension'] || $this->image_size[1] > $arr_validation['max_dimension'] )
{
array_push($this->errors, "Dimensões maiores");
}
}
if( in_array('min_dimension', $array_keys) )
{
if( $this->image_size[0] < $arr_validation['min_dimension'] || $this->image_size[1] < $arr_validation['min_dimension'] )
{
array_push($this->errors, "Dimensões menores");
}
}
if( count($this->errors) > 0 )return false;
}return true;
}
}
/**
* Upload method
*
* @param string folder of image destination
* @return void
*/
public function send( $dest )
{
$info = pathinfo($this->file['name']);
$name = $this->suggest_name . '.' . strtolower($info['extension']);
if( move_uploaded_file($this->file['tmp_name'], $dest . '/' . $name) )$this->setFileUploaded($dest . '/' . $name);
}$this->setFileUploaded(false);
}
}
/**
* Create and save square thumbnail
*
* @param integer max size of thumbnail
* @param string folder of thumbnail destination
* @param string name sufix for new image
* @param string image path of watermark or false for don't use watermark
* @return void
*/
public function createSquareThumb( $size, $dest = "", $sufix = '_square', $watermark = false )
{
$image_w = $this->image_size[0];
$image_h = $this->image_size[1];
$scale = $image_w / $image_h;
if( $scale < 1 )
{
$thumb_w = $size;
$thumb_h = round($size / $scale);
$centerX = 0;
$centerY = round( ($thumb_h - $thumb_w) / 2 );
}
else
{
$thumb_w = round($size * $scale);
$thumb_h = $size;
$centerX = round( ($thumb_w - $thumb_h) / 2 );
$centerY = 0;
}
$info = pathinfo($this->file['name']);
if( preg_match('/(jpg|jpeg)/i', $info['extension']) )
{
$image_orig = imagecreatefromjpeg($this->file['tmp_name']);
}$image_orig = imagecreatefromgif($this->file['tmp_name']);
}$image_orig = imagecreatefrompng($this->file['tmp_name']);
}
$ptX = imagesx($image_orig);
$ptY = imagesy($image_orig);
$image_fin = imagecreatetruecolor($size, $size);
imagecopyresampled($image_fin, $image_orig, -$centerX, -$centerY, 0, 0, $thumb_w, $thumb_h, $image_w, $image_h);
imageconvolution($image_fin, array(array(-1,-1,-1), array(-1,16,-1), array(-1,-1,-1)), 8, 0);
if( $watermark )$img_info = getimagesize($watermark);
$base = pathinfo($watermark);
if( preg_match('/(jpg|jpeg)/i', $base['extension']) )
{
$logo = imagecreatefromjpeg($watermark);
}$logo = imagecreatefromgif($watermark);
}$logo = imagecreatefrompng($watermark);
}
$size_x = imagesx($image_fin);
$size_y = imagesy($image_fin);
imagealphablending($image_fin, true);
imagecopymerge($image_fin, $logo, 0, ($size_y - $img_info[1]), 0, 0, $size_x, imagesy($logo), 60);
imagedestroy($logo);
}if( preg_match('/(jpg|jpeg)/i', $info['extension']) )
{
imagejpeg($image_fin, $dest . '/' . $name, 90);
}imagegif($image_fin, $dest . '/' . $name);
}imagepng($image_fin, $dest . '/' . $name, 90);
}
$this->setFileUploaded($dest . '/' . $name);echo "Tudo feito blza!<br>";
echo "Nome da Imagem: ".$name."<br>";
$sql = "INSERT INTO imagem VALUES ('', '$name')";
$resultado=mysql_query($sql);
echo "Dados inseridos com sucesso!!<br>";
$sql3="SELECT * FROM imagem ORDER BY id DESC LIMIT 0, 1";
$sql2=mysql_query($sql3);
$row = mysql_fetch_array($sql2);
$img = $row['imagem'];
echo "<img src='uploads/".$name."' />";
imagedestroy($image_orig);
imagedestroy($image_fin);
}
/**
* Create and save thumbnail
*
* @param integer max size of thumbnail
* @param string folder of thumbnail destination
* @param string name sufix for new image
* @param string image path of watermark or false for don't use watermark
* @return void
*/
public function createThumb( $size, $dest = "", $sufix = '_thumb', $watermark = false )
{
$image_w = $this->image_size[0];
$image_h = $this->image_size[1];
$scale = min($size/$image_w, $size/$image_h);
if( $scale < 1 )$thumb_w = round($scale * $image_w);
$thumb_h = round($scale * $image_h);
}$thumb_w = $image_w;
$thumb_h = $image_h;
}
$info = pathinfo($this->file['name']);
if( preg_match('/(jpg|jpeg)/i', $info['extension']) )
{
$image_orig = imagecreatefromjpeg($this->file['tmp_name']);
}$image_orig = imagecreatefromgif($this->file['tmp_name']);
}$image_orig = imagecreatefrompng($this->file['tmp_name']);
}
$ptX = imagesx($image_orig);
$ptY = imagesy($image_orig);
$image_fin = imagecreatetruecolor($thumb_w, $thumb_h);
imagecopyresampled($image_fin, $image_orig, 0, 0, 0, 0, $thumb_w, $thumb_h, $image_w, $image_h);
if( $watermark )$img_info = getimagesize($watermark);
$base = pathinfo($watermark);
if( preg_match('/(jpg|jpeg)/i', $base['extension']) )
{
$logo = imagecreatefromjpeg($watermark);
}$logo = imagecreatefromgif($watermark);
}$logo = imagecreatefrompng($watermark);
}
$size_x = imagesx($image_fin);
$size_y = imagesy($image_fin);
imagealphablending($image_fin, true);
imagecopymerge($image_fin, $logo, 0, ($size_y - $img_info[1]), 0, 0, $size_x, imagesy($logo), 60);
imagedestroy($logo);
}
$name = $this->suggest_name . $sufix . '.' . strtolower($info['extension']);
if( preg_match('/(jpg|jpeg)/i', $info['extension']) )
{
imagejpeg($image_fin, $dest . '/' . $name, 90);
}imagegif($image_fin, $dest . '/' . $name);
}imagepng($image_fin, $dest . '/' . $name);
}
$this->setFileUploaded($dest . '/' . $name);
imagedestroy($image_orig);
imagedestroy($image_fin);
}
/**
* Set method
*
* @param string name of generated image or false for upload error
* @return void
*/
public function setFileUploaded( $name )
{
$this->file_uploaded = $name;
}
/**
* Get method
*
* @return string / array
*/
public function getFileUploaded()
{
return $this->file_uploaded;
}
public function getArraySuperglobal()return $this->file;
}
public function getArrayImage()return $this->image_size;
}
public function getError()return $this->errors;
}
}
?><?php
include("upload.class.php");
// Instanciando a classe passando $_FILES e uma sugestão de nome das imagens geradas.
$rand = md5(uniqid(rand(), true));//nome do arquivo
$rand2 = md5(uniqid(rand(), true));//otra vez a criação do nome
$obj = new Upload($_FILES['arquivo'], "img".$rand."".$rand2."");
// Vetor de segurança, o qual será utilizado para fazer a verificação da imagem submetida.
// Estes são as quatro possibilidades de chave deste vetor. Nenhuma delas é obrigatória.
$arr_validation = array(
'min_dimension' => 480,
'max_dimension' => 8000,
'mime' => array('image/jpg', 'image/jpeg'),
'max_size' => 9104400
);
// Verificando se a imagem passará pelo método de validação.
if( !$obj->validation($arr_validation) )
{
echo "<pre>";
print_r($obj->getError());
echo "</pre>";
}// Criando uma miniatura proposrcionalmente menor.
/$obj->createThumb(640, "uploads", '_thumb', "marcadagua.png");/
// Mesma imagem submetida salva no diretório.
/$obj->send("uploads");/
}
?>
O interessante seria fazer isso com multi-threads, só que o php não é multi-thread, então você pode fazer desta maneira, criam um form com vários campos "file" e uma outra página php que recebe esse array de files e nesta página php você faz um loop chamando a sua classe de upload.
Tenta fazer ai e posta o resultado aqui.