Finalizing MVP for image cache plugin. Closes #27.
This commit is contained in:
parent
0f302ec5ba
commit
710abf1237
@ -5,6 +5,7 @@
|
||||
*/
|
||||
|
||||
use System\Classes\PluginBase;
|
||||
use JasonWilliams\ImgCache\Classes\Image;
|
||||
|
||||
class Plugin extends PluginBase
|
||||
{
|
||||
@ -24,7 +25,8 @@ class Plugin extends PluginBase
|
||||
return [
|
||||
'filters' => [
|
||||
'imgcache' => function($imgurl) {
|
||||
return $imgurl;
|
||||
$image = new Image($imgurl);
|
||||
return $image;
|
||||
}
|
||||
]
|
||||
];
|
||||
|
BIN
www/plugins/jasonwilliams/imgcache/assets/images/notfound.png
Normal file
BIN
www/plugins/jasonwilliams/imgcache/assets/images/notfound.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 23 KiB |
52
www/plugins/jasonwilliams/imgcache/classes/Image.php
Normal file
52
www/plugins/jasonwilliams/imgcache/classes/Image.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?php namespace JasonWilliams\ImgCache\Classes;
|
||||
|
||||
/*
|
||||
Find a faster way of returning in the event of a 404
|
||||
*/
|
||||
|
||||
use October\Rain\Network\Http;
|
||||
use October\Rain\Filesystem\Filesystem;
|
||||
|
||||
class Image
|
||||
{
|
||||
protected $file;
|
||||
protected $folder;
|
||||
protected $urlbase;
|
||||
protected $imgurl;
|
||||
protected $filename;
|
||||
protected $found = false;
|
||||
|
||||
public function __construct($url)
|
||||
{
|
||||
$this->imgurl = $url;
|
||||
$this->folder = base_path(substr(config('cms.storage.media.path'), 1)).'/external/';
|
||||
$this->urlbase = config('cms.storage.media.path').'/external/';
|
||||
$this->filename = md5($url).'.jpg';
|
||||
|
||||
$fs = new Filesystem;
|
||||
|
||||
if($fs->existsInsensitive($this->folder.$this->filename))
|
||||
{
|
||||
$this->found = true;
|
||||
$this->imgurl = $this->urlbase.$this->filename;
|
||||
return;
|
||||
}
|
||||
|
||||
$req = new Http();
|
||||
$img = $req->get($url, [
|
||||
'CURLOPT_CONNECTTIMEOUT' => 1
|
||||
]);
|
||||
|
||||
if ($img->code !== 200) $this->imgurl = '/plugins/jasonwilliams/imgcache/assets/images/notfound.png';
|
||||
else {
|
||||
$this->found = true;
|
||||
$fs->put($this->folder.$this->filename, $img);
|
||||
$this->imgurl = $this->urlbase.$this->filename;
|
||||
}
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return $this->imgurl;
|
||||
}
|
||||
}
|
@ -13,7 +13,6 @@
|
||||
<div class="{{ feeditemclass }}" data-timestamp="{{ post.timestamp }}">
|
||||
<div class="card">
|
||||
{% if post.extra.img %}
|
||||
<!-- <img class="card-img-top" src="{{ post.extra.img }}"> -->
|
||||
<img class="card-img-top" src="{{ post.extra.img | imgcache }}">
|
||||
{% elseif post.extra.lat %}
|
||||
<img class="card-img-top" src="https://api.mapbox.com/styles/v1/jason32dev/ckgehopjc2o1y19o09aqs4x11/static/pin-l+B05C71({{ post.extra.lng }},{{ post.extra.lat }})/{{ post.extra.lng }},{{ post.extra.lat }},14,0/545x300@2x?access_token=pk.eyJ1IjoiamFzb24zMmRldiIsImEiOiJjazFsNHd5djcwMXptM2htbW8zM3MyZGxuIn0.-TtNNGFysQPQRfGR1P8DUA">
|
||||
@ -35,7 +34,7 @@
|
||||
<div class="feedcard-footer">
|
||||
<p class="float-right feedcard-itemtype"><span class="friendlytime">{{ post.friendlytime }}</span> <i class="{{ post.channel.icon }}" style="color: {{ post.channel.colour }};"></i></p>
|
||||
{% if post.channel.slug == 'twitter' and post.extra.opuser %}
|
||||
<p class="float-left feedcard-attribution"><i class="fas fa-retweet"></i> <a href="https://twitter.com/{{ post.extra.opuser }}">{{ post.extra.opname }}</a> <img src="{{ post.extra.opimg }}"></p>
|
||||
<p class="float-left feedcard-attribution"><i class="fas fa-retweet"></i> <a href="https://twitter.com/{{ post.extra.opuser }}">{{ post.extra.opname }}</a> <img src="{{ post.extra.opimg | imgcache }}"></p>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
|
Loading…
Reference in New Issue
Block a user