Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reverse image search (minimal version) #963

Merged
merged 4 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added ext/reverse_search_links/icons/ascii2d.ico
Binary file not shown.
Binary file added ext/reverse_search_links/icons/saucenao.ico
Binary file not shown.
Binary file added ext/reverse_search_links/icons/tineye.ico
Binary file not shown.
Binary file added ext/reverse_search_links/icons/trace.moe.ico
Binary file not shown.
Binary file added ext/reverse_search_links/icons/yandex.ico
Binary file not shown.
18 changes: 18 additions & 0 deletions ext/reverse_search_links/info.php
Original file line number Diff line number Diff line change
@@ -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' => '[email protected]'];
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.";
}
67 changes: 67 additions & 0 deletions ext/reverse_search_links/main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Shimmie2;

abstract class ReverseSearchLinksConfig
{
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',
'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;
}

/**
* Set default config values
*/
public function onInitExt(InitExtEvent $event)
{
global $config;
$config->set_default_array(
ReverseSearchLinksConfig::ENABLED_SERVICES,
['SauceNAO', 'TinEye', 'trace.moe', 'ascii2d', 'Yandex']
);
}
}
3 changes: 3 additions & 0 deletions ext/reverse_search_links/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.reverse_image_link {
padding: 0 5px;
}
34 changes: 34 additions & 0 deletions ext/reverse_search_links/theme.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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' class='reverse_image_link' rel='nofollow'><img title='Search with $name' src='$icon_link' alt='$name icon'></a>";
}
}

$page->add_block(new Block("Reverse Image Search", $html, "main", 20));
}
}