How do I loop inside an image folder and resize it?

3

I have this class in PHP that when resized resizes the images contained in a folder. If you notice, 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 full function is in pastebin and it perfectly resizes the original image in 3 copies, each of a different size as determined by the use of the resize_and_crop() function. I'll put the file to run inside the images folder, so it does not need paths.

Note that function waits for 4 parameters:

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

Question: how to loop all images in this folder and within this loop apply the resize_and_crop() function to 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 the function that appears complete in the external link.

    
asked by anonymous 22.11.2015 / 20:19

3 answers

4

To return all images in this folder, use the glob function, with 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 resize_and_crop function 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 is not working as it should, because it does not have support for formats other than jpeg , see this small #

    
22.11.2015 / 21:20
3

You can use the scandir() function or the 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: there are better ways to take the file extension, I used it just to make it easier.

    
22.11.2015 / 20:28
3

To take pictures of 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;
    
22.11.2015 / 20:32