Redimension image with php [closed]

0

I'm trying to put a php script to resize images dynamically, until then I had no problem. I have a folder that gets saved the images that were edited by the script and the images arrive right, but I wanted the image name to be 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);
    }
    
asked by anonymous 07.01.2017 / 05:43

3 answers

1

Understanding your question better, we have that the collision index using rand() is too large. So to improve your code and lower that collision rate it would be interesting to use time() concatenating rand() and the file extension in the name of your image. In 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";
    }

}
    
07.01.2017 / 13:38
0

Your code does not make much sense, but to solve your problem, I recommend switching

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

By

$up = $linha['foto'];

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

    
07.01.2017 / 06:50
0

As the bad mood mentioned, although the function saved with the name rand, the return of it is boolean 1 in case you have managed to save and 0 in case you have made an error. $ up will always be 1 if it can save and 0 if it fails the save. If you want the name you generated to use in sql, save rand in a variable and pass the file name 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
}
    
07.01.2017 / 13:59