Last active
September 27, 2023 15:04
-
-
Save artlung/7bf6e15e08581f432d6207d09b9e125a to your computer and use it in GitHub Desktop.
Multiple Image Thumbnails - Command line PHP tool to generate image to represent multiple images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// By Joe Crawford joe@artlung.com | |
// September 2023 | |
// intended to be run from the command line | |
// php thumb.php | |
// place images in the same directory as this script and their names in the array below | |
// or place urls in the array below - filenames must be unique | |
// height and width of the final image, for now the math relies on everything being square | |
$height = 800; | |
$width = 800; | |
$im = imagecreatetruecolor($width, $height); | |
$color = imagecolorallocate($im, 255, 255, 255); | |
imagefilledrectangle($im, 0, 0, $width, $height, $color); | |
$images_to_place_on_base = [ | |
// place images of urls here or just filenames in the same directory as this script | |
]; | |
// we output a wide range of possible sizes for the images we place on the base so we can choose one we like | |
// can be edited but should be smaller than the base image, though it's not precluded that you might want things | |
// to run off the image | |
$childImageLengths = [200, 220, 240, 260, 280, 300, 320, 340, 360, 380, 400, 450, 480, 500, 520, 540, 560, 580, 600, 620, 640, 680, 700, 720, 740, 760]; | |
foreach ($childImageLengths as $iterationLength) { | |
$childImageWidth = $iterationLength; | |
$childImageHeight = $iterationLength; | |
// for ($index = 0; $index < count($images_to_place_on_base); $index++) { | |
for ($index = count($images_to_place_on_base) -1; $index >= 0; $index--) { | |
// if it's the first image, put it in the top left corner | |
$x = false; | |
$y = false; | |
$something_x = intval(($width - $childImageWidth) / (count($images_to_place_on_base) - 1)); | |
$something_y = intval(($height - $childImageHeight) / (count($images_to_place_on_base) - 1)); | |
$x = ($width - $childImageWidth) - ($something_x * $index); | |
$y = ($height - $childImageHeight) - ($something_y * $index); | |
$full_url = $images_to_place_on_base[$index]; | |
$target_file_name = basename($full_url); | |
if (file_exists($target_file_name)) { | |
// printf("File %s exists\n", $target_file_name); | |
} else { | |
printf("File %s does not exist\n", $target_file_name); | |
// download it | |
$image_data = file_get_contents($full_url); | |
file_put_contents($target_file_name, $image_data); | |
} | |
if ($x !== false && $y !== false) { | |
// if jpeg | |
if (strpos($target_file_name, '.jpeg') !== false || strpos($target_file_name, '.jpg') !== false) { | |
$image = imagecreatefromjpeg($target_file_name); | |
} | |
// if png | |
if (strpos($target_file_name, '.png') !== false) { | |
$image = imagecreatefrompng($target_file_name); | |
} | |
// if gif | |
if (strpos($target_file_name, '.gif') !== false) { | |
$image = imagecreatefromgif($target_file_name); | |
} | |
// if webp | |
if (strpos($target_file_name, '.webp') !== false) { | |
$image = imagecreatefromwebp($target_file_name); | |
} | |
// if none, exit | |
if (!$image) { | |
printf("No image for %s\n", $target_file_name); | |
exit; | |
} | |
$image_width = imagesx($image); | |
$image_height = imagesy($image); | |
$image_width_to_height_ratio = $image_width / $image_height; | |
$image_height_to_width_ratio = $image_height / $image_width; | |
$image_width = $childImageWidth; | |
$image_height = $childImageHeight; | |
$image_width = intval($image_width * $image_width_to_height_ratio); | |
$image_height = intval($image_height * $image_height_to_width_ratio); | |
$image = imagescale($image, $image_width, $image_height); | |
printf("Placing image %s at %s,%s\n", $color, $x, $y); | |
imagecopy($im, $image, $x, $y, 0, 0, $image_width, $image_height); | |
} | |
} | |
//output png to filesystem | |
imagepng($im, 'thumbnail_' . $childImageWidth . '.png'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment