Skip to content

Instantly share code, notes, and snippets.

@autioch
Created November 7, 2017 19:02
Show Gist options
  • Save autioch/6e764dfc1eb8c84478065ea3b8e5aba7 to your computer and use it in GitHub Desktop.
Save autioch/6e764dfc1eb8c84478065ea3b8e5aba7 to your computer and use it in GitHub Desktop.
Create thumbnails of images in given directory
<?php
$imageFolder = '.';
$extensions = 'jpg,JPG,JPEG,jpeg,png,PNG'; //can be just 'all' as well.
$width = 150;
$height = 150;
/**
* Filters out thumbnails.
*
* @param [Array] $arr List of arrays
* @param [string] $prefix Prefix by which filename is detected as thumb
*
* @return [Array] Filtered list of files
*/
function filterThumbs($arr, $prefix)
{
$images = [];
foreach ($arr as $item) {
if (!preg_match('/^' . $prefix . '/', $item)) {
$images[] = $item;
}
}
return $images;
}
/**
* Creates a resized image.
*
* @param [String] $originalName Original filename
* @param [String] $thumbName Filename of the thumb
* @param [Integer] $width width of resized image
* @param [Integer] $height height of resized image
*
* @return [undefined] Nothing
*/
function createThumb($originalName, $thumbName, $width, $height)
{
$system = explode('.', $originalName);
if (preg_match('/jpg|jpeg/', $system[1])) {
$src_img = imagecreatefromjpeg($originalName);
}
if (preg_match('/png/', $system[1])) {
$src_img = imagecreatefrompng($originalName);
}
$oldWidth = imagesx($src_img);
$oldHeight = imagesy($src_img);
$thumbWidth = $width;
$thumbHeight = $height;
if ($oldWidth > $oldHeight) {
$thumbHeight = $oldHeight * ($height / $oldWidth);
} elseif ($oldWidth < $oldHeight) {
$thumbWidth = $oldWidth * ($width / $oldHeight);
}
$dst_img = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $oldWidth, $oldHeight);
if (preg_match('/png/', $system[1])) {
imagepng($dst_img, $thumbName);
} else {
imagejpeg($dst_img, $thumbName);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}
/**
* Reads the content of $directory. Takes the files that apply to $filter and returns an array of the filenames.
* findFiles(".","jpg,gif") gets all jpg and gif files in this directory.
* findFiles(".","all") gets all files.
*
* @param [string] $dir Directory path to read
* @param [string] $filters String all or extensions string separated by string
*
* @return [Array] List of files
*/
function findFiles($dir, $filters)
{
$handle = opendir($dir);
$files = [];
if ('all' == $filters) {
while (false !== ($file = readdir($handle))) {
$files[] = $file;
}
}
if ('all' != $filters) {
$filters = explode(',', $filters);
while (false !== ($file = readdir($handle))) {
for ($f = 0; $f < sizeof($filters); ++$f) {
$system = explode('.', $file);
if ($system[1] == $filters[$f]) {
$files[] = $file;
}
}
}
}
closedir($handle);
return $files;
}
$pics = filterThumbs(findFiles($imageFolder, $extensions), $prefix);
foreach ($pics as $p) {
createThumb($p, $prefix . $p, $width, $height);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment