Created
June 11, 2017 14:53
-
-
Save jerome-labidurie/abd26b31320e91735993c3f256a68daa to your computer and use it in GitHub Desktop.
small script for thumbnails creation/display
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
<html> | |
<head> | |
<title> static images </title> | |
</head> | |
<body> | |
<pre> | |
<?php | |
function make_thumb($src, $dest, $desired_width) { | |
/* read the source image */ | |
// $source_image = imagecreatefromjpeg($src); | |
$source_image = imagecreatefromstring ( file_get_contents($src) ); | |
$width = imagesx($source_image); | |
$height = imagesy($source_image); | |
/* find the "desired height" of this thumbnail, relative to the desired width */ | |
if ($height > $width) { | |
$desired_height = $desired_width; | |
$desired_width = floor($width * ($desired_height / $height)); | |
} else { | |
$desired_height = floor($height * ($desired_width / $width)); | |
} | |
/* create a new, "virtual" image */ | |
$virtual_image = imagecreatetruecolor($desired_width, $desired_height); | |
/* copy source image at a resized size */ | |
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height); | |
/* create the physical thumbnail image to its destination */ | |
imagejpeg($virtual_image, $dest); | |
} | |
/** display images */ | |
$IMG_PATTERN = "*.{jpg,JPG,jpeg,png,PNG,gif,GIF}"; | |
$W = 4; // page width | |
$H = 6; // page height | |
$PAGE_SIZE = $W * $H; | |
$THUMBS='thumbs'; // thumbnails directory | |
if (! is_dir($THUMBS)) { | |
mkdir($THUMBS); | |
} | |
if (isset( $_GET['page'] ) ) { | |
$start = ( ($_GET['page'] - 1) * $PAGE_SIZE ) + 1; // numéro d'image de départ | |
$page = $_GET['page']; // numéro de page | |
} else { | |
$start = 1; | |
$page = 1; | |
} | |
// navigation | |
if ($page > 1) { | |
echo "<a href='?page=" . ($page-1) ."'> prev </a>"; | |
} | |
echo "<a href='?page=" . ($page + 1) . "'> next </a>\n"; | |
// table d'images de la page | |
$c = 1; | |
echo "<table><tr>"; | |
foreach (glob($IMG_PATTERN, GLOB_BRACE) as $filename) { | |
if ($c >= $start) { | |
if ($c < $start + $PAGE_SIZE) { | |
// on est dans la page à afficher | |
// get thumbnail path | |
$filename_no_ext = reset(explode('.', $filename)); | |
$thumbnail = $THUMBS . '/' . $filename_no_ext . '.jpg'; | |
if (! is_file($thumbnail) ) { | |
// create thumbnail | |
make_thumb ($filename, $thumbnail, 150); | |
} | |
// affiche thum et lien image | |
echo "\t<td> <a href='$filename'><img src='$thumbnail'><br/>$filename</a> </td>\n"; | |
if (( ($c-$start+1) % $W) == 0) { | |
// fin de ligne | |
echo "</tr><tr>\n"; | |
} | |
} else { | |
// on a dépassé la page, stop | |
break; | |
} | |
} | |
$c += 1; | |
} | |
echo "</tr></table>\n"; | |
// navigation | |
if ($page > 1) { | |
echo "<a href='?page=" . ($page-1) ."'> prev </a>"; | |
} | |
echo "<a href='?page=" . ($page + 1) . "'> next </a>\n"; | |
?> | |
</pre> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment