Use SHA1_ prefix, attempt to make thumbnails more reliable

master
Skylar Ittner 5 years ago
parent 70ec2caa93
commit ae2ae1ffbf

@ -69,10 +69,10 @@ class Thumbnail {
* @param type $height
* @return type
*/
static function addThumbnailToCache(string $url, int $width = 150, $height = true) {
static function addThumbnailToCache(string $url, int $width = 150, $height = true, $forcesha1 = false) {
global $database;
$encodedfilename = Base64::encode($url);
if (strlen("$encodedfilename.$width.jpg") > 250) {
if ($forcesha1 || strlen("$encodedfilename.$width.jpg") > 250) {
// We're too long for common filesystems
$encodedfilename = "SHA1_" . sha1($url);
if (!$database->has("imagecache", ["url" => $url])) {
@ -82,6 +82,7 @@ class Thumbnail {
$path = "cache/thumb/$encodedfilename.$width.jpg";
$image = self::getThumbnailFromUrl($url, $width, $height);
file_put_contents(__DIR__ . "/../$path", $image);
echo $path;
return $image;
}

@ -15,6 +15,7 @@ define("PAGES", [
],
"scripts" => [
"static/Shuffle/dist/shuffle.min.js",
"static/js/newsgrid.js",
"static/js/home.js"
]
],
@ -24,10 +25,11 @@ define("PAGES", [
"icon" => "fas fa-newspaper",
"scripts" => [
"static/Shuffle/dist/shuffle.min.js",
"static/js/newsgrid.js",
"static/js/news.js"
]
],
"404" => [
"title" => "404 error"
]
]);
]);

@ -77,13 +77,14 @@ foreach ($newsitems as $item) {
<div class="card mb-2">
<?php if (!empty($item->getImage())) { ?>
<a href="<?php echo $item->getURL(); ?>" target="_BLANK">
<img src="<?php
<?php
if (strpos($item->getImage(), "preview.redd.it") !== false) {
echo $item->getImage();
$imgurl = $item->getImage();
} else {
echo Thumbnail::getThumbnailCacheURL($item->getImage(), 500);
$imgurl = Thumbnail::getThumbnailCacheURL($item->getImage(), 500);
}
?>" class="card-img-top" alt="">
?>
<img src="<?php echo $imgurl; ?>" class="card-img-top newscard-img" alt="">
</a>
<?php } ?>
<div class="card-body">

@ -41,7 +41,7 @@ $newsitems = News::getItems();
} else {
echo Thumbnail::getThumbnailCacheURL($item->getImage(), 500);
}
?>" class="card-img-top" alt="">
?>" class="card-img-top newscard-img" alt="">
</a>
<?php } ?>
<div class="card-body">

Loading…
Cancel
Save