Mudar o nome do arquivo após o Upload
Olá
Tenho um script de upload e crop de imagem. entretanto, não consigo adapta-lo para fazer um uploader de um arquivo e posteriormente renomear-lo para a pasta.
Segue o código.
<?php
include_once("../../includes/connect.php");
date_default_timezone_set('America/Sao_Paulo');
$date = date("d/m/Y h:i");
$small = "../media/images/thumbs/small/";
ini_set("memory_limit", "200000000");
// upload the file
if ((isset($_POST["submitted_form"])) && ($_POST["submitted_form"] == "image_upload_form")) {
// file needs to be jpg,gif,bmp,x-png and 4 MB max
if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/gif" || $_FILES["image_upload_box"]["type"] == "image/x-png") && ($_FILES["image_upload_box"]["size"] < 4000000))
{
$captureId = $_POST["idpost"];
// some settings
$max_upload_width = 250;
$max_upload_height = 250;
// if uploaded image was JPG/JPEG
if($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg"){
$image_source = imagecreatefromjpeg($_FILES["image_upload_box"]["tmp_name"]);
}
// if uploaded image was GIF
if($_FILES["image_upload_box"]["type"] == "image/gif"){
$image_source = imagecreatefromgif($_FILES["image_upload_box"]["tmp_name"]);
}
// BMP doesn't seem to be supported so remove it form above image type test (reject bmps)
// if uploaded image was BMP
if($_FILES["image_upload_box"]["type"] == "image/bmp"){
$image_source = imagecreatefromwbmp($_FILES["image_upload_box"]["tmp_name"]);
}
// if uploaded image was PNG
if($_FILES["image_upload_box"]["type"] == "image/x-png"){
$image_source = imagecreatefrompng($_FILES["image_upload_box"]["tmp_name"]);
}
$remote_file = "../../users/".$_FILES["image_upload_box"]["name"];
imagejpeg($image_source,$remote_file,70);
chmod($remote_file,0644);
// get width and height of original image
list($image_width, $image_height) = getimagesize($remote_file);
if($image_width>$max_upload_width || $image_height >$max_upload_height){
$proportions = $image_width/$image_height;
if($image_width>$image_height){
$new_width = $max_upload_width;
$new_height = round($max_upload_width/$proportions);
}
else{
$new_height = $max_upload_height;
$new_width = round($max_upload_height*$proportions);
}
$new_image = imagecreatetruecolor($new_width , $new_height);
$image_source = imagecreatefromjpeg($remote_file);
imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
imagejpeg($new_image,$remote_file,70);
imagedestroy($new_image);
}
imagedestroy($new_image);
$squery_pesquise=$conexaoDB->query("SELECT * FROM editores WHERE id_user='$captureId'");
$squery_cout=$squery_pesquise->fetch_assoc();
$veri = $squery_pesquise->num_rows;
$photoAtual = $squery_cout['perfil'];
$file = $_FILES["image_upload_box"]["name"];
if($photoAtual){
//Apagar arquivo da pasta do servidor
$patch_home_small ='../../users/';
$arquivo_small = $patch_home_small.$photoAtual;
unlink($arquivo_small);
}
if($veri >= 1){
$conexaoDB->query("UPDATE editores SET perfil='$file' WHERE id_user='$captureId'");
}else{
$post = $conexaoDB->query("INSERT INTO editores VALUES ('','".$captureId."','','',".$file."','','','')");
}
echo "<img src='../users/".$file."' style=\"width: 100%;\" class='uploadphotoClass circle' >";
}
else{
echo "405 - Erro no Upload";
}
}
/* if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST" and isset($_FILES["uploadpri"])){
$captureId = $_POST["idpost"];
$name = $_FILES['uploadpri']['name'];
$size = $_FILES['uploadpri']['size'];
//Pesquisar
$squery_pesquise=$conexaoDB->query("SELECT * FROM postagens WHERE id_post='$captureId'");
$squery_cout=$squery_pesquise->fetch_assoc();
$veri = $squery_pesquise->num_rows;
$photoAtual = $squery_cout['imagem'];
if(strlen($name)){
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats)){
if($size<(1024*1024)){
$tmp = $_FILES['uploadpri']['tmp_name'];
$typeIMG = $_FILES['uploadpri']['type'];
$largura_small = 481;
$altura_small = 321;
$largura_medium = 381;
$altura_medium = 221;
if($typeIMG == 'image/jpeg'){
$foto = imagecreatefromjpeg($tmp);
}elseif($typeIMG == 'image/png'){
$foto = imagecreatefrompng($tmp);
}
$x = imagesx($foto);
$y = imagesy($foto);
$novaimg_small = imagecreatetruecolor($largura_small, $altura_small);
imagecopyresampled($novaimg_small, $foto,0,0,0,0,$largura_small, $altura_small, $x, $y);
$novaimg_medium = imagecreatetruecolor($largura_medium, $altura_medium);
imagecopyresampled($novaimg_medium, $foto,0,0,0,0,$largura_medium, $altura_small, $x, $y);
//Muda o nome da imagem, colocando microsegundos e registrando largura e altura no nome da imagem. substitue espaços em branco e traços baixos;
$newimager = time().substr(str_replace(" ", "_", $x.''.$y.'.'.'img'.'-thumd'), 5).".".$ext;
//Caso a imagem tenha um type jpeg
if($typeIMG == 'image/jpeg'){
//move a imagem já cortada para a pasta
imagejpeg($novaimg_small, $small.$newimager,70);
imagedestroy($novaimg_small);
imagedestroy($foto);
//medium
imagejpeg($novaimg_medium, $medium.$newimager,70);
imagedestroy($novaimg_medium);
//ou png
}elseif($typeIMG == 'image/png'){
//move a imagem já cortada para a pasta
imagepng($novaimg_small, $small.$newimager,70);
imagedestroy($novaimg_small);
imagedestroy($foto);
//medium
imagepng($novaimg_medium, $medium.$newimager,70);
imagedestroy($novaimg_medium);
}
if($photoAtual>=1){
//Apagar arquivo da pasta do servidor
$patch_home_small ='../../media/images/thumbs/small/';
$arquivo_small = $patch_home_small.$photoAtual;
$patch_home_medium ='../../media/images/thumbs/medium/';
$arquivo_medium = $patch_home_medium.$photoAtual;
if (!unlink($arquivo_small)){}
if(!unlink($arquivo_medium)){}
}
if($veri >= 1){
$conexaoDB->query("UPDATE postagens SET imagem='$newimager' WHERE id_post='$captureId'");
}else{
$post = $conexaoDB->query("INSERT INTO postagens VALUES ('','".$captureId."','','rascunho','','".$date."','','','','','','".$newimager."','NO','','','','','')");
}
echo "<div class=\"boxer-text-into\">
<button class=\" btn-flat blue-text btn white waves-effect tooltipped modal-trigger\" data-position=\"bottom\" data-delay=\"10\" data-tooltip=\"Editar\" type=\"button\" data-target=\"modal4\">
<i class=\"material-icons\">system_update_alt</i>
</button></div><img src='../media/images/thumbs/small/".$newimager."' class='uploadphotoClass' >";
}else
echo "Máximo 1MB";
}else
echo "Aceitos JPG/PNG";
}else
echo "Selecione uma Imagem";
exit;
}*/
?>Discussão (0)
Carregando comentários...