Find undersized images with PHP before moving anything

Make a read-only report of small local images, skip invalid files and keep originals intact instead of moving everything that fails a dimension check.

When sorting an asset folder, the first useful result is a list of images that need attention—not a folder full of unexpectedly moved files. This version is read-only and does not need a local web server.

Produce a small-image report

Save as find-small-images.php outside the image folder. Run php find-small-images.php /absolute/path/to/images in a terminal. It inspects one directory, reports images where either dimension is below 400 pixels, and skips symlinks and non-image extensions.

<?php
declare(strict_types=1);

if (PHP_SAPI !== 'cli' || $argc !== 2) {
    throw new RuntimeException('Usage: php find-small-images.php /absolute/image/folder');
}
$directory = realpath($argv[1]);
if ($directory === false || !is_dir($directory)) {
    throw new RuntimeException('Choose an existing local image directory.');
}

$allowed = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
foreach (new FilesystemIterator($directory, FilesystemIterator::SKIP_DOTS) as $file) {
    if (!$file->isFile() || $file->isLink()
        || !in_array(strtolower($file->getExtension()), $allowed, true)) {
        continue;
    }
    if ($file->getSize() > 20 * 1024 * 1024) {
        continue; // Review unusually large inputs separately.
    }

    $size = @getimagesize($file->getPathname());
    if ($size === false) {
        fwrite(STDERR, "Skipped an unreadable or unsupported image.\n");
        continue;
    }
    [$width, $height] = $size;
    if ($width < 400 || $height < 400) {
        echo json_encode([
            'file' => $file->getFilename(),
            'width' => $width,
            'height' => $height,
        ], JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_SUBSTITUTE), "\n";
    }
}

JSON lines keep filenames with spaces or control characters from producing an ambiguous report. The file-size cap also prevents a small housekeeping script from casually inspecting enormous inputs.

Dimensions are not an upload-security check

getimagesize() reads image metadata; it does not establish that an arbitrary upload is safe. Use this for a trusted local collection. Portrait images, icons and intentional thumbnails may legitimately fail the threshold, so review the report before deciding what to remove.

If you subsequently move approved files, use a separate backed-up workflow with collision checks and an explicit destination. Do not rename directly over existing files or treat an unreadable image as zero pixels wide.

Reference: PHP getimagesize() behavior and limitations.

Original version10 December 2014

Kept here for reference and earlier links. The updated guide above is the recommended starting point; older code may depend on retired services or different software versions.

As well as PHP being a very easy code to use language on the web, PHP and localhost can be a great setup to do some basic file manipulation and sorting on your computer.

In this following codebyte the basic principle is we have a folder of images of varying sizes/dimensions, and we want to filter out all of the low-resolution images.

The PHP code loops over a folder of our choosing ($current_folder), using a foreach loop on each image/file, checking if either the $width or $height is under 400(px), if it is, then it moves it to the trash folder ($trash_folder) so that we then have one folder of images of low resolution.

<?php
// Change folder_name_here to the the folder containing your files/images you want to loop over. [Leave the trailing / alone]
$current_folder = "folder_name_here"."/";
// A folder to move the files that don't meet our rules. [Leave the trailing / alone]
$trash_folder = "trash"."/";
$files = scandir($current_folder);
foreach ($files as $file) {
  $image_info = list($width, $height) = getimagesize($current_folder.$file);
  if ($width < 400 || $height < 400) {
    rename($current_folder.$file, $trash_folder.$file);
  }
}
?>

Keep exploring

A couple more useful notes.