Photo Gallery in Miniature

Asked

Viewed 226 times

0

I have the code below, where it connects in the bank, takes the name of the image and saves in an Array:

while($row = $stm->fetch())  {

echo "<img  src=thumb.php?img=".$row['foto']."/>";

}

The file Thumb.php, is who does all the processing of the image to leave in gallery:

    $filename = $_GET['img'];
$percent = 0.10;

// Cabeçalho que ira definir a saida da pagina
header('Content-type: image/jpeg');

// pegando as dimensoes reais da imagem, largura e altura
list($width, $height) = getimagesize($filename);

//setando a largura da miniatura
$new_width = 120;
//setando a altura da miniatura
$new_height = 100;

//gerando a a miniatura da imagem
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

//o 3º argumento é a qualidade da imagem de 0 a 100
imagejpeg($image_p, null, 50);
imagedestroy($image_p);

The images are in the same directory where you have the index.php that connects in the database and shows the gallery and the file Thumb.php.

The problem that does not display the photo and does not generate any error. It brings back the photo comments that are in the database, but the image does not.

  • Here $Row['photo'] comes the full name of the photo on md5, example : 8d399117a184a7f243d236930d60eb0b.png . And in the same directory there is a photo with this name.

  • All files, both index.php, both Thumb.php and images are in the same directory. Anyway, I pointed out the current directory as your suggestion, however the same situation occurs. nothing appears.

  • I did this test and failed to open image file .png. I created another file with the same new however in . jpg and now opened the screen all encoded.

  • Now I’m back to the header and the thumbnail opened directly by Thumb.php

1 answer

0


The function imagecreatefromjpeg only reads JPEG type documents:

$image = imagecreatefromjpeg($filename);

Files in PNG format will cause failure as it expects a JPEG image.

You can use the getimagesize to detect the image type, but it is not as reliable as with fileinfo, then based on this other answer /a/73497/3635 adjust your code like this:

function mimeType($file)
{
    $mimetype = false;

    if (class_exists('finfo')) {//PHP5.4+
        $finfo     = finfo_open(FILEINFO_MIME_TYPE);
        $mimetype  = finfo_file($finfo, $file);
        finfo_close($finfo);
    } else if (function_exists('mime_content_type')) {//php5.3 ou inferiror
        $mimetype = mime_content_type($file);
    }

    return $mimetype;
}

$filename = $_GET['img'];
$percent = 0.10;

//Lê o formato do conteudo da imagem
$mime = mimeType($filename);

if (strpos($mime, 'image/') !== 0) {
    die('Este arquivo não é uma imagem');
}

//Remove o prefixo image/
$mime = substr($mime, 6);

//Remove `x` de x-jpeg, talvez ocorram em alguns servidores antigos
$mime = str_replace('x-', '', $mime);

switch ($mime) {
    case 'jpeg':
        $image = imagecreatefromjpeg($filename);
    break;
    case 'png':
        $image = imagecreatefrompng($filename);
    break;
    case 'gif':
        $image = imagecreatefromgif($filename);
    break;
    default:
        die('Formato não aceito');
}

// Cabeçalho que ira definir a saida da pagina
header('Content-type: image/jpeg');

// pegando as dimensoes reais da imagem, largura e altura
list($width, $height) = getimagesize($filename);

//setando a largura da miniatura
$new_width = 120;
//setando a altura da miniatura
$new_height = 100;

//gerando a a miniatura da imagem
$image_p = imagecreatetruecolor($new_width, $new_height);

imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

//o 3º argumento é a qualidade da imagem de 0 a 100
imagejpeg($image_p, null, 50);
imagedestroy($image_p);
  • adjust according to its orientation and return this error: Notice: Undefined variable: file in C: xampp htdocs Dashboard web web comprovantes Thumbs Thumbs Thumb.php on line 21 Warning: finfo_file(): Empty filename or path in C:xampp htdocs Dashboard web proof Thumbs Thumbs Thumb.php on line 8 This file is not an image

  • @user54154 I wrote the name of the wrong var, edited the answer and corrected, see: https://answall.com/posts/233633/revisions, can copy now.

  • Hello William, good night. Now he is falling into the default case ('Format not accepted'); I tried to find the bug but could not. I am new in Php.

  • @user54154 I will analyze here and I warn you

  • @user54154 was my mistake, fixed it now and tested it. It can copy again and test ;)

  • Now yes William. Thank you so much for the help. Big hug! You can indicate some course in php, book, something that can help me in my learning?

Show 1 more comment

Browser other questions tagged

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