add reverse image search links extension

This commit is contained in:
Joe 2023-08-19 15:12:11 -05:00 committed by Shish
parent 945c358ab6
commit 7fcb4078dd
9 changed files with 144 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 822 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 894 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View file

@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace Shimmie2;
class ReverseSearchLinksInfo extends ExtensionInfo
{
public const KEY = "reverse_search_links";
public string $key = self::KEY;
public string $name = "Reverse Search Links";
public array $authors = ['joe' => 'joe@thisisjoes.site'];
public string $license = self::LICENSE_GPLV2;
public string $description = "Provides reverse search links for images.";
public ?string $documentation = "Click on an icon in the 'Reverse Image Search' block to search for the image using the corresponding service. This may be useful to find the original source or author of an image.<br/>
Options for which services to show and the position and priority of the block are available for admins on the config page.";
}

View file

@ -0,0 +1,85 @@
<?php
declare(strict_types=1);
namespace Shimmie2;
abstract class ReverseSearchLinksConfig
{
public const PRIORITY = "ext_reverse_search_links_priority";
public const POSITION = "ext_reverse_search_links_position";
public const ENABLED_SERVICES = "ext_reverse_search_links_enabled_services";
}
class ReverseSearchLinks extends Extension
{
/** @var ReverseSearchLinksTheme */
protected Themelet $theme;
/**
* Show the extension block when viewing an image
*/
public function onDisplayingImage(DisplayingImageEvent $event)
{
global $page;
// only support image types
$supported_types = [MimeType::JPEG, MimeType::GIF, MimeType::PNG, MimeType::WEBP];
if(in_array($event->image->get_mime(), $supported_types)) {
$this->theme->reverse_search_block($page, $event->image);
}
}
/**
* Supported reverse search services
*/
protected array $SERVICES = [
'SauceNAO',
'IQDB',
'TinEye',
'trace.moe',
'ascii2d',
'Yandex'
];
private function get_options(): array
{
global $config;
$output = [];
$services = $this->SERVICES;
foreach ($services as $service) {
$output[$service] = $service;
}
return $output;
}
/**
* Generate the settings block
*/
public function onSetupBuilding(SetupBuildingEvent $event)
{
$sb = $event->panel->create_new_block("Reverse Search Links");
$sb->start_table();
$sb->add_int_option("ext_reverse_search_links_priority", "Priority:");
$sb->add_choice_option("ext_reverse_search_links_position", ["Main page" => "main", "In navigation bar" => "left"], "<br>Position: ");
$sb->add_multichoice_option("ext_reverse_search_links_enabled_services", $this->get_options(), "Enabled Services", true);
$sb->end_table();
}
/**
* Set default config values
*/
public function onInitExt(InitExtEvent $event)
{
global $config;
$config->set_default_int(ReverseSearchLinksConfig::PRIORITY, 20);
$config->set_default_string(ReverseSearchLinksConfig::POSITION, "main");
$config->set_default_array(
ReverseSearchLinksConfig::ENABLED_SERVICES,
['SauceNAO', 'IQDB', 'TinEye', 'trace.moe', 'ascii2d', 'Yandex']
);
}
}

View file

@ -0,0 +1,4 @@
#Reverse_Image_Searchmain a,
#Reverse_Image_Searchleft a {
padding: 0 5px;
}

View file

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace Shimmie2;
class ReverseSearchLinksTheme extends Themelet
{
public function reverse_search_block(Page $page, Image $image)
{
global $config;
$links = [
'SauceNAO' => 'https://saucenao.com/search.php?url=' . url_escape(make_http($image->get_thumb_link())),
'TinEye' => 'https://www.tineye.com/search/?url=' . url_escape(make_http($image->get_thumb_link())),
'trace.moe' => 'https://trace.moe/?auto&url=' . url_escape(make_http($image->get_thumb_link())),
'ascii2d' => 'https://ascii2d.net/search/url/' . url_escape(make_http($image->get_thumb_link())),
'Yandex' => 'https://yandex.com/images/search?rpt=imageview&url=' . url_escape(make_http($image->get_thumb_link()))
];
// only generate links for enabled reverse search services
$enabled_services = $config->get_array(ReverseSearchLinksConfig::ENABLED_SERVICES);
$html = "";
foreach($links as $name => $link) {
if (in_array($name, $enabled_services)) {
$icon_link = make_link("/ext/reverse_search_links/icons/" . strtolower($name) . ".ico");
$html .= "<a href='$link' rel='nofollow'><img title='Search with $name' src='$icon_link' alt='$name icon'></a>";
}
}
$position = $config->get_string(ReverseSearchLinksConfig::POSITION) ?? "main";
$priority = $config->get_int(ReverseSearchLinksConfig::PRIORITY) ?? 20;
$page->add_block(new Block("Reverse Image Search", $html, $position, $priority));
}
}