Resize image with php

Asked

Viewed 68 times

0

I am trying to put a php script to resize images dynamically, until then I had no problem. I have a folder that is saved the images that were edited by the script and the images arrive right, only I wanted the name of the image was also saved in the database.

    $sql = "SELECT * FROM foto WHERE foto = '6671.jpg' OR foto = '6672.jpg'";
    $query = mysqli_query($conecta, $sql);    
    while ($linha = mysqli_fetch_array($query)) {     
    $img_origem = ImageCreateFromJpeg($linha['foto']);
    $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);
    $up = imageJPEG($img_destino,'teste/'. rand() . '.jpg', 85);
    $sql = "INSERT INTO foto (foto) VALUES ('" . $up . "')";
    $query = mysqli_query($conecta, $sql);
    }
  • 3

    I don’t understand what’s wrong with your script.

  • my problem is when inserting this resized image in the bank, that variable $up does not take the name of the image. the value assigned to it is always 1. but in my test folder the image is stored normally.

  • But the Imagejpeg function does not return the file name according to the documentation: http://php.net/manual/en/function.imagejpeg.php

  • in fact the Imagejpeg function returns the correct name, at least in the test folder it is saved correctly. but if I give an echo in the variable $up it has the value of 1

  • Bianca, vc is saving with name Rand(), but is inserting a Boolean in the bank of value 1. What the bad mood said is q in the documentation the return of the function is boolean.

3 answers

1


Better understanding your question, we have to use collision index rand() is very large. So to improve your code and decrease this collision rate it would be interesting to use time() concatenating with the rand() and the file extension in the name of your image. This way, the collision would be minimal. See:

$filenameRandon = date('Ymdhms').rand().'.jpg';

Now see below how your code would look:

$sql = "SELECT * FROM foto WHERE foto = '6671.jpg' OR foto = '6672.jpg'";
$query = mysqli_query($conecta, $sql);    

while ($linha = mysqli_fetch_array($query)) {     
    $img_origem = ImageCreateFromJpeg($linha['foto']);
    $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,'teste/'. $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 = "INSERT INTO foto (foto) VALUES ('" . $filenameRand . "')";
        $query = mysqli_query($conecta, $sql);

    } else {
        echo "A imagem naum foi salva";
    }

}
  • That’s exactly what it was, thank you very much

0

As the bad mood quoted, although the function saves with the name Rand, her return is Boolean 1 in case she was able to save and 0 in case she gave error. $up will always be 1 if you can save and 0 in case of error in the save. If you want the name you generated to use in sql, save Rand to a variable and pass to the file name and to sql.

Change:

$up = imageJPEG($img_destino,'teste/'. rand() . '.jpg', 85);
$sql = "INSERT INTO foto (foto) VALUES ('" . $up . "')";
$query = mysqli_query($conecta, $sql);

To:

$nome_rand = rand();
$salvouCerto = imageJPEG($img_destino,'teste/'. $nome_rand . '.jpg', 85);
if($salvouCerto)
{
    $sql = "INSERT INTO foto (foto) VALUES ('" . $nome_rand  . "')";
    $query = mysqli_query($conecta, $sql);
}else
{
//Exibe algum erro dizendo que não conseguiu salvar
}
  • Thanks for your help :D

0

Your code doesn’t make much sense, but to solve your problem, I recommend changing

$up = imageJPEG($img_destino,'teste/'. rand() . '.jpg', 85);

For

$up = $linha['foto'];

For the function imageJPEG returns a boolean whether the function has been successfully executed. According to documentation

  • Thanks for your help :D

Browser other questions tagged

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