Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Boa noite, amigos
Estou adaptando o script de Upload Uploadfy, me baseando em um script utilizado por um conhecido, porem o script que ele usa é muito cheio de lixo, pois ele usa aqueles componentes do DW para gerar querys e essas coisas... Minha idéia é otimizar o arquivo, removendo essas sujeiras e fazendo tudo a mão.
Boa parte está pronta, consegui fazer o Upload e salvar os nomes no banco MySQL sem problemas, porem não consigo criar as thumbnails e salvar nas respectivas pastas, estou rodando local usando Wamp server.
Seguem os arquivos:
index.php
<?php
//conexão
include_once('Conection/conn.php');
//verifica se foram preenchidos os campos nome e descrição para o album
//se foi, atribui eles as respectivas variáveis e faz o insert do album
if(isset($_POST['nome_album']) and isset($_POST['descricao'])){
$nome_album = mysql_real_escape_string($_POST['nome_album']);
$descricao_album = mysql_real_escape_string($_POST['descricao']);
mysql_query("INSERT INTO album VALUES (NULL, '$nome_album', NULL, '$descricao_album')") or die(mysql_error);
if(mysql_affected_rows() == 1){
$sucesso_insercao = "Album " . $nome_album . " criado com sucesso!";
}
}
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Albuns</title>
<script type="text/javascript">
<!--
function novaJanela(theURL,winName,features) { //v2.0
window.open(theURL,winName,features);
}
//-->
</script>
</head>
<body><form id="cad_album" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
<table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>Criar um novo album:</td>
<td width="10"> </td>
<td>Listar Album:</td>
</tr>
<tr>
<td>Nome do album:
<input type="texte" name="nome_album" id="nome_album" /></td>
<td width="10"></td>
<td>
<?php
//faz a busca e monta a linha que exibe os albuns
$query_Albuns = "SELECT * FROM album";
$consulta = mysql_query($query_Albuns) or die(mysql_error);
while($row_Albuns = mysql_fetch_array($consulta)){
echo '<a href="#' . $row_Albuns['id_album'] . '" onclick="novaJanela(\'app/index.php?id_album=' . $row_Albuns['id_album'] .'\', \'UPLOAD\', \'width=755,height=613\')">' . $row_Albuns['nome'] . '</a><br />';
}
?>
</td>
</tr>
<tr>
<td>Descrição <input type="text" name="descricao" id="descricao" /></td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><input name="btn_cad" type="submit" id="btn_cad" value="Cadastrar" /> </td>
<td width="10"> </td>
<td><?php echo $sucesso_insercao; ?></td>
</tr>
<tr>
<td> </td>
<td width="10"> </td>
<td> </td>
</tr>
</table></form>
</body>
</html>
fotos_albuns.php
<?php
include_once('Conection/conn.php');
/*
* @param define capa
* @param update
* verifica se a variavel $_POST['update'] foi enviada e contém o valor correto
* se estiver correto realiza a query e define a capa do album
*/
if((isset($_POST['update'])) && $_POST['update'] == "fm_capa"){
//Recebe o conteudo dos campos ocultos capa e id_album em variaveis
$capa = $_POST['capa'];
$id_album = $_POST['id_album'];
$updateCapa = "UPDATE album SET capa = '$capa' WHERE id_album = '$id_album'";//$id_album
$resultadoUpdateCapa = mysql_query($updateCapa) or die(mysql_error);
}
/*
* Verifico se o id_album está sendo passado via get
* se não estiver, assum que ocorreu uma atualização e associo a variavel $colname_IdAlbum ao $_POST['id_album']
*/
if(isset($_GET['id_album'])){
$colname_IdAlbum = $_GET['id_album'];//$_GET
} else {
$colname_IdAlbum = $_POST['id_album'];
}
/*
* @param id_album
* Realiza a consulta na tabela fotos usando o id_album como parametro para limitar quais fotos vai trazer
*/
$consulta_Fotos = "SELECT * FROM fotos_album WHERE id_album = '$colname_IdAlbum'";
$resultado_Fotos = mysql_query($consulta_Fotos) or die(mysql_error);
$num_rowsFotos = mysql_num_rows($resultado_Fotos);
/*
* Realiza a busca na tabela albuns
* verifica quantos albuns existem
* e traz informaçõoes sobre o albúm
*/
$consulta_Album = mysql_query("SELECT * FROM album WHERE id_album = '$colname_IdAlbum'") or die(mysql_error);
$row_Album = mysql_fetch_assoc($consulta_Album);
$num_rowsAlbum = mysql_num_rows($consulta_Album);
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
//verifica se existem fotos para o album se exitir exibi as mesmas
if($num_rowsFotos > 0) {
//Loop que exibe as fotos do albúm e o formulário para definir a foto de capa
while($row_Fotos = mysql_fetch_assoc($resultado_Fotos)){
?>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="0" class="sep">
<tr onmouseover="this.bgColor='#ffffff'" onmouseout="this.bgColor='#E8E8E8'">
<td width="100" height="75" align="center"><img src="fotos/100x75/<?php echo $row_Fotos['foto']; ?>" border="0" /></td>
<td width="10"> </td>
<td align="center" class="But1"><a href="#">Excluir</a><a href="#">
</a>
<a href="#"></a>
<?php
//verifico se a imagem é a imagem de capa do album, se for oculto o formulário e digo que é a imagem de capa.
if($row_Fotos['foto'] != $row_Album['capa']){
?>
<form id="fm_capa" name="fm_capa" method="POST" action="<?php echo $_SERVER['PHP_SELF'] //diz que o action é o próprio arquivo ?>">
<input type="submit" name="button" id="button" value="Marcar como Capa" />
<input name="id_album" type="hidden" id="id_album" value="<?php echo $row_Album['id_album']; ?>" />
<input name="capa" type="hidden" id="capa" value="<?php echo $row_Fotos['foto']; ?>" />
<input type="hidden" name="update" value="fm_capa" />
</form></td>
<?php
} else {
echo "Capa Atual";
}
?>
</tr>
</table>
<?php
}
} else {
echo "<center>Nenhuma Foto</center>";
}
?>
</body>
</html>
Dentro da pasta app temos:
index.php
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Carregar Imagens</title>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="jquery.uploadify.js"></script>
<link href="uploadify.css" rel="stylesheet" type="text/css" />
<style type="text/css">background-color: #333;
margin-left: 20px;
margin-top: 20px;
margin-right: 20px;
margin-bottom: 20px;
}</style>
</head>
<body>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="218px" valign="top">
<input type="file" name="fileInput" id="fileInput"/>
<script type="text/javascript">
$(document).ready(function(){
$('#fileInput').fileUpload({
'uploader' : 'uploader.swf',
'script' : 'upload.php',
'cancelImg' : 'cancel.png',
'width' : '300',
'auto' : true,
'multi' : true,
'folder' : '../fotos/Original/',
'scriptData': {'id_album' : '<?php echo $_GET['id_album']; ?>'},
'onAllComplete' : function() {
frames['lista'].window.location.reload();
}
});
});
</script>
</td>
<td width="5"> </td>
<td width="759" align="left"><iframe name="lista" id="lista" class="over" width="400px" height="600px" frameborder="1" src="../fotos_albuns.php?id_album=<?php echo $_GET['id_album']; ?>"></iframe></td>
</tr>
</table>
</body>
<div id="filesUploaded"></div>
</html>
upload.php
<?php
require_once("../Conection/conn.php");
$colname_rsAlbum = "-1";
if (isset($_GET['id_album'])) {
$colname_rsAlbum = $_GET['id_album'];
}
$query_rsAlbum = "SELECT * FROM album WHERE id_album = '$colname_rsAlbum' ";
$rsAlbum = mysql_query($query_rsAlbum) or die(mysql_error());
$row_rsAlbum = mysql_fetch_assoc($rsAlbum);
$totalRows_rsAlbum = mysql_num_rows($rsAlbum);
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_GET['folder'] . '/';
$ext = pathinfo($_FILES['Filedata']['name'], PATHINFO_EXTENSION); //figures out the extension
$newFileName = md5(uniqid(rand().time(), true)) . substr($_FILES['Filedata']['name'], strrpos($_FILES['Filedata']['name'], ".")); //generates random filename, then adds the file extension
$targetFile = str_replace('//','/',$targetPath) . $newFileName;
$id_p = $_GET['id_album'];//id_album
move_uploaded_file($tempFile,$targetFile);
mysql_query("INSERT INTO fotos_album SET foto ='${newFileName}', id_album ='${id_p}'");
mysql_query("UPDATE album SET foto ='${newFileName}', WHERE id_album = '${id_p}'");
//não funciona a partir daqui
}
$imgsize = getimagesize($targetFile);
switch(strtolower(substr($targetFile, -3))){
case "jpg":
$image = imagecreatefromjpeg($targetFile);
break;
case "png":
$image = imagecreatefrompng($targetFile);
break;
case "gif":
$image = imagecreatefromgif($targetFile);
break;
default:
exit;
break;
}
$width = 60; //New width of image
$height = $imgsize[1]/$imgsize[0]*$width; //This maintains proportions
$src_w = $imgsize[0];
$src_h = $imgsize[1];
$picture = imagecreatetruecolor($width, $height);
imagealphablending($picture, false);
imagesavealpha($picture, true);
$bool = imagecopyresampled($picture, $image, 0, 0, 0, 0, $width, $height, $src_w, $src_h);
if($bool){
switch(strtolower(substr($targetFile, -3))){
case "jpg":
header("Content-Type: image/jpeg");
$bool2 = imagejpeg($picture,$targetPath."../fotos/100x75" . $newFileName,100);
break;
case "png":
header("Content-Type: image/png");
imagepng($picture,$targetPath."../fotos/100x75". $newFileName);
break;
case "gif":
header("Content-Type: image/gif");
imagegif($picture,$targetPath."../fotos/100x75" . $newFileName);
break;
}
}
imagedestroy($picture);
imagedestroy($image);
//
$imgsize2 = getimagesize($targetFile);
switch(strtolower(substr($targetFile, -3))){
case "jpg":
$image2 = imagecreatefromjpeg($targetFile);
break;
case "png":
$image2 = imagecreatefrompng($targetFile);
break;
case "gif":
$image2 = imagecreatefromgif($targetFile);
break;
default:
exit;
break;
}
$width2 = 65; //New width of image
$height2 = $imgsize2[1]/$imgsize2[0]*$width2; //This maintains proportions
$src_w2 = $imgsize2[0];
$src_h2 = $imgsize2[1];
$picture2 = imagecreatetruecolor($width2, $height2);
imagealphablending($picture2, false);
imagesavealpha($picture2, true);
$bool2 = imagecopyresampled($picture2, $image2, 0, 0, 0, 0, $width2, $height2, $src_w2, $src_h2);
if($bool2){
switch(strtolower(substr($targetFile, -3))){
case "jpg":
header("Content-Type: image/jpeg");
$bool22 = imagejpeg($picture2,$targetPath."../fotos/Home" . $newFileName,100);
break;
case "png":
header("Content-Type: image/png");
imagepng($picture2,$targetPath."../fotos/Home" . $newFileName);
break;
case "gif":
header("Content-Type: image/gif");
imagegif($picture2,$targetPath."../fotos/Home" . $newFileName);
break;
}
}
imagedestroy($picture2);
imagedestroy($image2);
$imgsize3 = getimagesize($targetFile);
switch(strtolower(substr($targetFile, -3))){
case "jpg":
$image3 = imagecreatefromjpeg($targetFile);
break;
case "png":
$image3 = imagecreatefrompng($targetFile);
break;
case "gif":
$image3 = imagecreatefromgif($targetFile);
break;
default:
exit;
break;
}
$width3 = 139; //New width of image
$height3 = $imgsize3[1]/$imgsize3[0]*$width3; //This maintains proportions
$src_w3 = $imgsize3[0];
$src_h3 = $imgsize3[1];
$picture3 = imagecreatetruecolor($width3, $height3);
imagealphablending($picture3, false);
imagesavealpha($picture3, true);
$bool3 = imagecopyresampled($picture3, $image3, 0, 0, 0, 0, $width3, $height3, $src_w3, $src_h3);
if($bool3){
switch(strtolower(substr($targetFile, -3))){
case "jpg":
header("Content-Type: image/jpeg");
$bool33 = imagejpeg($picture3,$targetPath."../fotos/Media" . $newFileName,100);
break;
case "png":
header("Content-Type: image/png");
imagepng($picture3,$targetPath."../fotos/Media" . $newFileName);
break;
case "gif":
header("Content-Type: image/gif");
imagegif($picture3,$targetPath."../fotos/Media" . $newFileName);
break;
}
}
imagedestroy($picture3);
imagedestroy($image3);
$width9 = 567; //New width of image
$height9 = $imgsize9[1]/$imgsize9[0]*$width9; //This maintains proportions
$src_w9 = $imgsize9[0];
$src_h9 = $imgsize9[1];
$picture9 = imagecreatetruecolor($width9, $height9);
imagealphablending($picture9, false);
imagesavealpha($picture9, true);
$bool9 = imagecopyresampled($picture9, $image9, 0, 0, 0, 0, $width9, $height9, $src_w9, $src_h9);
if($bool9){
switch(strtolower(substr($targetFile, -9))){
case "jpg":
header("Content-Type: image/jpeg");
$bool99 = imagejpeg($picture9,$targetPath."../fotos/Media2" . $newFileName,100);
break;
case "png":
header("Content-Type: image/png");
imagepng($picture9,$targetPath."../fotos/Media2" . $newFileName);
break;
case "gif":
header("Content-Type: image/gif");
imagegif($picture9,$targetPath."../fotos/Media2" . $newFileName);
break;
}
}
imagedestroy($picture9);
imagedestroy($image9);
$imgsize4 = getimagesize($targetFile);
switch(strtolower(substr($targetFile, -3))){
case "jpg":
$image4 = imagecreatefromjpeg($targetFile);
break;
case "png":
$image4 = imagecreatefrompng($targetFile);
break;
case "gif":
$image4 = imagecreatefromgif($targetFile);
break;
default:
exit;
break;
}
$width4 = 70; //New width of image
$height4 = $imgsize4[1]/$imgsize4[0]*$width4; //This maintains proportions
$src_w4 = $imgsize4[0];
$src_h4 = $imgsize4[1];
$picture4 = imagecreatetruecolor($width4, $height4);
imagealphablending($picture4, false);
imagesavealpha($picture4, true);
$bool4 = imagecopyresampled($picture4, $image4, 0, 0, 0, 0, $width4, $height4, $src_w4, $src_h4);
if($bool4){
switch(strtolower(substr($targetFile, -3))){
case "jpg":
header("Content-Type: image/jpeg");
$bool44 = imagejpeg($picture4,$targetPath."../fotos/Miniatura" . $newFileName,100);
break;
case "png":
header("Content-Type: image/png");
imagepng($picture4,$targetPath."../fotos/Miniatura" . $newFileName);
break;
case "gif":
header("Content-Type: image/gif");
imagegif($picture4,$targetPath."../fotos/Miniatura" . $newFileName);
break;
}
}
imagedestroy($picture4);
imagedestroy($image4);
echo '1'; // Important so upload will work on OSX
mysql_free_result($rsAlbum);
?>
Já alterei as permissões mas não consigo encontrar o porque da criação de thumbnais não funcionar, se alguem puder me dar uma dica ficarei grato,.
Att
Carregando comentários...