quarta-feira, 30 de maio de 2018

Upload de ficheiros com resize

<?php

if(isset($_POST['mover'])){
    $nome=addslashes($_POST['nome']);
    $email=addslashes($_POST['email']);
    $telemovel=addslashes($_POST['telemovel']);   
   
    require_once("ligacao.php");
   
    date_default_timezone_set('Europe/London');
    $data=date("ymdhis");   
    $ficheiro=$_FILES['ficheiro']['name'];
    $extensao=pathinfo($ficheiro,PATHINFO_EXTENSION);
    $extensao=".".$extensao;
    $ficheiro=base64_encode($ficheiro);
    $url=$data.$ficheiro.$extensao;
    $ficheiro_1200="imgs_1200/".$data.$ficheiro.$extensao;
    $ficheiro_800="imgs_800/".$data.$ficheiro.$extensao;
   
    $ficheiroTemporario=$_FILES['ficheiro']['tmp_name'];
   
    $sql="INSERT INTO fotografias (nome, email, telemovel, url) VALUES ('$nome', '$email', '$telemovel', '$url')";
    mysqli_query($ligar, $sql); 
     
// obtenho o nome do ficheiro
   
   
    $maxDim = 1200;
        list($width, $height, $type, $attr) = getimagesize($ficheiroTemporario);
        if ( $width > $maxDim || $height > $maxDim ) {
            $target_filename_1200 = $ficheiroTemporario;
            $fn_1200 = $ficheiroTemporario;
            $size = getimagesize( $fn_1200 );
            $ratio = $size[0]/$size[1]; // width/height
            if( $ratio > 1) {
                $width = $maxDim;
                $height = $maxDim/$ratio;
            } else {
                $width = $maxDim*$ratio;
                $height = $maxDim;
            }
            $src = imagecreatefromstring( file_get_contents( $fn_1200 ) );
            $dst = imagecreatetruecolor( $width, $height );
            imagecopyresampled( $dst, $src, 0, 0, 0, 0, $width, $height, $size[0], $size[1] );
            imagedestroy( $src );
            imagepng( $dst, $target_filename_1200 ); // adjust format as needed
            imagedestroy( $dst );
        }
   
    move_uploaded_file($target_filename_1200, $ficheiro_1200);
   
    copy($ficheiro_1200, $ficheiro_800);
   
    $maxDim = 800;
        list($width, $height, $type, $attr) = getimagesize($ficheiro_800);
        if ( $width > $maxDim || $height > $maxDim ) {
            $target_filename_800 = $ficheiro_800;
            $fn_800 = $ficheiro_800;
            $size = getimagesize( $fn_800 );
            $ratio = $size[0]/$size[1]; // width/height
            if( $ratio > 1) {
                $width = $maxDim;
                $height = $maxDim/$ratio;
            } else {
                $width = $maxDim*$ratio;
                $height = $maxDim;
            }
            $src = imagecreatefromstring( file_get_contents( $fn_800 ) );
            $dst = imagecreatetruecolor( $width, $height );
            imagecopyresampled( $dst, $src, 0, 0, 0, 0, $width, $height, $size[0], $size[1] );
            imagedestroy( $src );
            imagepng( $dst, $target_filename_800 ); // adjust format as needed
            imagedestroy( $dst );
        }   
   
         $caminho="index.php?submeteu=$totalFotos";
   
   
      header("Location:$caminho");
  }
?>

Sem comentários:

Enviar um comentário