Increase image size

Asked

Viewed 598 times

0

Good, I have this code that after filling 1 form uploading with an image, this code puts the image that was uploaded in a folder.

Code:

<?php
error_reporting(0);
include("config.php");
$idcarro = $_POST["idcarro"];
    if(isset($_POST['upload'])){

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

        //PASTA
        $folder     = 'imgcarros';

        //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 'Selecione uma Imagem!';
        }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));
                $novoNome = 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.'/'.$novoNome)){
                        $sql = mysqli_query($link, "INSERT INTO imgcarros (idcarro, img) VALUES ('$idcarro', '$folder/$novoNome')");
                        $verifica = mysqli_query($link, "SELECT * FROM carros where id='$idcarro'");
                        $array = mysqli_fetch_array($verifica);


?>

<script type="text/javascript">

window.alert("Foto enviada com Sucesso!");

</script>
<?php





                    }else{
                        $msg[] = "<b>$name :</b> Desculpe! Ocorreu um erro...";

                }

                foreach($msg as $pop){
                    echo $pop.'<br>';
            }
        }
    }
        }
    }

But I want to upload a photo with the dimensions: 2380x1422 pixels, but I can’t, but if I upload a photo with the sizes 264*261 pixels I can already.

How can I fix this, to put maximum sizes of 4000x4000 pixels?

Thank you

  • 1

    There’s nothing in your code that limits image resolution, so the problem of not being able to send high-resolution images is nowhere else. Maybe in the file size, you checked if you have 5Mb or less? Check the PHP upload limit in the php.ini file and on your web server. I don’t know about Apache, but Nginx has an upload limit too and you can increase / decrease in the conf file.

  • $maxSize = 1024 * 1024 * 5; Have you tried increasing this? Apparently that’s what you have in your code limiting file size. Now, why allow 4000x4000 upload? Are you sure that’s what you need?

  • Problem Solved!

1 answer

3


The code presented does not validate the dimensions (width, height).

Probably the problem may be in weight validation or to mime type.

Upload weight limit in PHP

In PHP settings, set the value in the directive upload_max_filesize.

If you don’t know how much is set, just do it:

echo ini_get('upload_max_filesize ');

Return the limit in bytes.

Input Hidden MAX_FILE_SIZE

Also check if the HTML form contains MAX_FILE_SIZE. Because it has priority over the upload_max_filesize PHP, if smaller. Note that more modern browsers ignore this parameter. As a precaution, for older browsers, it is good to specify. Finally, the choice depends on the target audience.

Size limit in code

In the code you posted there is also a specific limit defined in the variable $maxSize. The limit is 5mb.

Increase the limit if necessary.

Mime type and JPG type variations.

Another point that may be preventing an upload is the allowed file types, defined in the variable $permite

$permite = array('image/jpeg', 'image/png');

JPG types have variations. It is recommended to add such variations as well:

$permite = array(
    'image/jpeg',
    'image/jpg',
    'image/pjpg',
    'image/pjpeg',
    'image/png'
);
  • 2

    I voted positive, but here’s a detail: "Note that more modern browsers ignore this parameter." This parameter is actually browser-independent. MAX_FILE_SIZE is also PHP. The function of this parameter is to cut the upload faster, without having to accept the upload until the space is "popped". To avoid on the client side, you need JS and the file api.

  • Thank you, the problem was not the size of the image but the size of the file.

Browser other questions tagged

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