Skip to content

Instantly share code, notes, and snippets.

@NTsvetkov
Created May 9, 2021 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NTsvetkov/9e15630ead60cb50c84eb54f337a17e8 to your computer and use it in GitHub Desktop.
Save NTsvetkov/9e15630ead60cb50c84eb54f337a17e8 to your computer and use it in GitHub Desktop.
Create thumbnails from the pictures of current folder
<?php
function createThumbnail($file)
{
$width = 220;
$height = 220;
$thumbPath = "thumbs/";
list($width_orig, $height_orig, $type) = getimagesize($file);
if (($width_orig > $width) || ($height_orig > $height)) {
if (($width_orig > $width) && ($height_orig > $height)) {
if ($width_orig > $height_orig) {
$k = ($width / $width_orig);
} else {
$k = ($height / $height_orig);
}
} else {
if ($width_orig > $width) {
$k = ($width / $width_orig);
} elseif ($height_orig > $height) {
$k = ($height / $height_orig);
} else {
$k = 1;
}
}
$width = round($width_orig * $k);
$height = round($height_orig * $k);
$small = imagecreatetruecolor($width, $height);
imagefilledrectangle($small, 0, 0, $width, $height, imagecolorallocate($small, 255, 255, 255));
switch ($type) {
case 1 :
$im = imageCreateFromGif($file);
break;
case 2 :
$im = imageCreateFromJpeg($file);
break;
case 3 :
$im = imageCreateFromPng($file);
break;
case 6 :
$im = imageCreateFromBmp($file);
break;
}
imagecopyresampled($small, $im, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagejpeg($small, $thumbPath . $file, 90);
imagedestroy($im);
imagedestroy($small);
} else {
copy($file, $thumbPath . $file);
}
}
if (!file_exists('thumbs')) {
mkdir('thumbs', 0777, true);
}
foreach (glob("*.{jpg,png,gif}", GLOB_BRACE) as $filename) {
createThumbnail($filename);
echo "$filename - ok\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment