Implement image resizing code on upload

Asked

Viewed 30 times

0

I have this script that uploads my images:

<?php
        if (isset($_POST['cadastrar'])) {

        $titulo = trim(strip_tags($_POST['titulo']));
        $descricao = trim(strip_tags($_POST['descricao']));

        //INFO IMAGEM
        $file = $_FILES['img'];
        $numFile = count(array_filter($file['name']));

        //PASTA
        $folder = '../assets/images/slider/';

        //REQUISITOS
        $permite = array('image/jpeg', 'image/png');
        $maxSize = 1024 * 1024 * 5;

        //MENSAGENS
        $msg = array();
        $errorMsg = array(
        1 => 'O arquivo no upload é maior do que o limite definido em upload_max_filesize no php.ini.',
        2 => 'O arquivo ultrapassa o limite de tamanho em MAX_FILE_SIZE que foi especificado no formulário HTML',
        3 => 'o upload do arquivo foi feito parcialmente',
        4 => 'Não foi feito o upload do arquivo'
        );

        if ($numFile <= 0) {
        echo '<div class="alert alert-danger">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                Selecione pelo menos 1 fotos!
            </div>';
        } else if ($numFile >= 2) {
        echo '<div class="alert alert-danger">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                Você ultrapassou o limite de upload. Selecione até 1 fotos e tente novamente!
            </div>';
        } else {
        for ($i = 0;
        $i < $numFile;
        $i++) {
        $name = $file['name'][$i];
        $type = $file['type'][$i];
        $size = $file['size'][$i];
        $error = $file['error'][$i];
        $tmp = $file['tmp_name'][$i];

        $extensao = @end(explode('.', $name));
        $nome_imagem = rand() . ".$extensao";

        if ($error != 0) {
        $msg[] = "<b>$name :</b> " . $errorMsg[$error];
        } else if (!in_array($type, $permite)) {
        $msg[] = "<b>$name :</b> Erro imagem não suportada!";
        } else if ($size > $maxSize) {
        $msg[] = "<b>$name :</b> Erro imagem ultrapassa o limite de 5MB";
        } else {
        if (move_uploaded_file($tmp, $folder . '/' . $nome_imagem)) {
        //$msg[] = "<b>$name :</b> Upload Realizado com Sucesso!";
        $sql = "INSERT INTO slides (titulo,descricao,imagem_slide) VALUES ('" . $titulo . "', '" . $descricao . "', '" . $nome_imagem . "')";
        $query = mysqli_query($conecta, $sql);
        }
        }
        }
        }

        }/* Fecha o isset */
        ?>

I have a while that brings my bank records into a array:

while ($linha = mysqli_fetch_array($query)) {

But I’m trying to insert an image resizing script in this upload, however, I can’t think of any logic.

$img_origem = ImageCreateFromJpeg('../assets/images/slider/' . $linha['imagem_slide']);/*Caminho da imagem*/
            $largura = imagesx($img_origem);
            $altura = imagesy($img_origem);
            $nova_largura = 200;
            $nova_altura = $altura * $nova_largura / $largura;
            $img_destino = imagecreatetruecolor($nova_largura, $nova_altura);
            imagecopyresampled($img_destino, $img_origem, 0, 0, 0, 0, $nova_largura, $nova_altura, $largura, $altura);

            // definindo o nome do arquivo
            $filenameRand = date('Ymdhms') . rand() . '.jpg';

            // salvando o arquivo na pasta específicada teste/
            $up = imageJPEG($img_destino, '../assets/images/slider/' . $filenameRand, 85);

            // verificação se a imagem foi salva corretamente
            if ($up)

            // aqui você esta inserindo no banco o nome do seu arquivo gerado com $filenameRand
            $sql = "UPDATE slides  SET (id_foto, titulo, descricao, imagem_slide) VALUES "
            . "('" . $linha['id_foto'] . "', '" . $linha['titulo'] . "', '" . $linha['descricao'] . "', '" . $filenameRand . "')";
            $query = mysqli_query($conecta, $sql);
            } else {
            echo "A imagem não foi salva";
            }
            ?>
  • You’re confused. What is this while has to do with the story?

  • while brings the name of the image I have registered in the bank.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.