1

I am new to php and I need help. I am trying to make a simple image gallery. I am done with uploading part of my gallery. Now I want to get those images from a folder, make their thumbnails and save them in an array so that I can display them later. This is what I am struggling with so far. The array remains null at the end.

$folder = 'images/';
$filetype = '*.*';
$files = glob($folder.$filetype);
$count = count($files);
$thumbArray = array();

 for($i=0; $i<$count; $i++){
 if(($img = @imagecreatefromstring($files[$i])) !== FALSE) {

    $width = imagesx($img);
    $height = imagesy($img);

    $boxSize = min($width,$height);
    $boxX = ($width / 2) - ($boxSize / 2);
    $boxY = ($height / 2) - ($boxSize / 2);
    $thumbArray[$i] = imagecreatetruecolor(100, 100);
    imagecopyresampled($thumbArray[$i], $img, 0, 0, $boxX, $boxY, 100, 100, $boxSize, $boxSize);
 }
} 

Thanks in advance.

1 Answer 1

1

The problem in your code is in this line:

if (($img = @imagecreatefromstring($files[$i])) !== FALSE) { ... }

It seems that statement was never been executed.

To obtain an image from a file using it's filename, you should use imagecreatefromjpeg function. So your code inside loop should look like:

$img = imagecreatefromjpeg($files[$i]);

$width = imagesx($img);
$height = imagesy($img);

$boxSize = min($width,$height);
$boxX = ($width / 2) - ($boxSize / 2);
$boxY = ($height / 2) - ($boxSize / 2);

$thumbArray[$i] = imagecreatetruecolor(100, 100);
imagecopyresampled($thumbArray[$i], $img, 0, 0, $boxX, $boxY, 100, 100, $boxSize, $boxSize);

At the end, you can use imagejpeg function to see the result in browser, or directly save it to a file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.