How to loop inside an image folder and resize them?

Asked

Viewed 654 times

3

I have this class in PHP that when running resizes the images contained in a folder. If you observe, the resizing is triggered by these instructions, one for each desired size.

resize_and_crop('banner.jpg', 'banner_100x100.jpg', 100, 100);
resize_and_crop('banner.jpg', 'banner_200x100.jpg', 200, 100);
resize_and_crop('banner.jpg', 'banner_200x300.jpg', 200, 300);

The complete function is in the Pastebin and it perfectly resizes in 3 copies the original image, each of a different size as determined in the use of the function resize_and_crop(). I’ll put the file to run inside the images folder, so you don’t need paths.

Note that the function awaits 4 parameters:

  • current image name
  • new image name
  • width ()
  • height (height)

Question: how to make a loop that returns all images from that folder and inside this one loop apply the function resize_and_crop() for each of the returned images?

// RESIZE AN IMAGE PROPORTIONALLY AND CROP TO THE CENTER
function resize_and_crop($original_image_url, $thumb_image_url, $thumb_w, $thumb_h, $quality = 100)
{
    // ACQUIRE THE ORIGINAL IMAGE: http://php.net/manual/en/function.imagecreatefromjpeg.php
    $original = imagecreatefromjpeg($original_image_url);
    if (!$original) return FALSE;

    // GET ORIGINAL IMAGE DIMENSIONS
    list($original_w, $original_h) = getimagesize($original_image_url);

    // RESIZE IMAGE AND PRESERVE PROPORTIONS
    $thumb_w_resize = $thumb_w;
    $thumb_h_resize = $thumb_h;
    if ($original_w > $original_h) ...

This is just a piece of Function that appears complete on the external link.

3 answers

4

To return all images from this folder, use the function glob, with the GLOB_BRACE, which allows you to expand search terms.

<?php

$formatos = array('png','jpg','jpeg','gif');

foreach(glob('*.{'.implode(',', $formatos).'}', GLOB_BRACE) as $imagem){
    # Aplicar resize_and_crop
    print "Imagem: " . $imagem . "<br/>";
    $thumb = explode('.',$imagem);
    print "Thumbnail: {$thumb[0]}_200x300.{$thumb[1]}<br/>";
}

?>

Within this loop, you can simply apply the function resize_and_crop as follows:

...
    resize_and_crop($imagem, "{$thumb[0]}_100x100.{$thumb[1]}", 100, 100);
    resize_and_crop($imagem, "{$thumb[0]}_200x100.{$thumb[1]}", 200, 100);
    resize_and_crop($imagem, "{$thumb[0]}_200x300.{$thumb[1]}", 200, 300);
...

It’s very likely that your function isn’t working properly because it doesn’t have support for formats other than jpeg, see this little example dealing with different formats.

3

You can use the function scandir() or the function glob()

Glob()

foreach (glob("*.jpg") as $arquivo) {
    $nameWithoutExtensionJPG = str_replace('.jpg', '', $arquivo);
    resize_and_crop($arquivo, $nameWithoutExtensionJPG.'_100x100.jpg', 100, 100);
    resize_and_crop($arquivo, $nameWithoutExtensionJPG.'_200x100.jpg', 200, 100);
    resize_and_crop($arquivo, $nameWithoutExtensionJPG.'_200x300.jpg', 200, 300);
}

Note: has better ways to take the file extension, I used this just to make it easy.

  • Because I am receiving this error: http://puu.sh/luW4K/3f707f1170.png

  • Looks like there’s some file .jpg that is not an image jpg

3

To take the images from a folder and loop it would be something like this:

$dir = "/var/www/img/*.jpg";
//pega todas as imagens do tipo jpg e coloca dentro de uma array com nome $images
$images = glob( $dir );

//Roda todas as imagens do array $images
foreach( $images as $image ):
    echo "<img src='" . $image . "' />"; // apenas um exemplo de código aqui dentro do loop
    //coloca a função resize_and_crop() aqui dentro.
endforeach;
  • Because I am receiving this error: http://puu.sh/luW4K/3f707f1170.png

Browser other questions tagged

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