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

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

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

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

Loading…
Cancel
Save