Gazelle/classes/imagetools.class.php

215 lines
5.3 KiB
PHP
Raw Normal View History

2013-02-25 21:16:55 +00:00
<?php
2012-06-18 08:00:14 +00:00
/**
2013-02-25 21:16:55 +00:00
* ImageTools Class
* Thumbnail aide, mostly
*/
class ImageTools {
/**
* Store processed links to avoid repetition
* @var array 'URL' => 'Parsed URL'
*/
2013-04-30 18:18:07 +00:00
private static $Storage = array();
2012-06-18 08:00:14 +00:00
2013-02-25 21:16:55 +00:00
/**
* We use true as an extra property to make the domain an array key
* @var array $Hosts Array of image hosts
*/
2013-04-30 18:18:07 +00:00
private static $Hosts = array(
2013-02-25 21:16:55 +00:00
'whatimg.com' => true,
'imgur.com' => true
);
2013-02-22 08:00:24 +00:00
2013-02-25 21:16:55 +00:00
/**
* Blacklisted sites
* @var array $Blacklist Array of blacklisted hosts
*/
2013-04-30 18:18:07 +00:00
private static $Blacklist = array(
2013-02-25 21:16:55 +00:00
'tinypic.com'
);
2013-02-22 08:00:24 +00:00
2013-02-25 21:16:55 +00:00
/**
* Array of image hosts that provide thumbnailing
* @var array $Thumbs
*/
2013-04-30 18:18:07 +00:00
private static $Thumbs = array(
2013-02-25 21:16:55 +00:00
'i.imgur.com' => true,
'whatimg.com' => true
);
2013-02-22 08:00:24 +00:00
2013-02-25 21:16:55 +00:00
/**
* Array of extensions
* @var array $Extensions
*/
2013-04-30 18:18:07 +00:00
private static $Extensions = array(
2013-02-25 21:16:55 +00:00
'jpg' => true,
'jpeg' => true,
'png' => true,
'gif' => true
);
/**
* Checks from our list of valid hosts
* @param string $Host Domain/host to check
* @return boolean
*/
public static function valid_host($Host) {
return !empty(self::$Hosts[$Host]) && self::$Hosts[$Host] === true;
}
/**
* Checks if a link's host is (not) good, otherwise displays an error.
* @param string $Url Link to an image
* @return boolean
*/
2013-05-27 08:00:58 +00:00
public static function blacklisted($Url, $ShowError = true) {
2013-02-25 21:16:55 +00:00
foreach (self::$Blacklist as &$Value) {
2013-06-24 08:00:28 +00:00
$Blacklisted = stripos($Url, $Value);
if ($Blacklisted !== false) {
2013-02-25 21:16:55 +00:00
$ParsedUrl = parse_url($Url);
2013-05-30 08:00:30 +00:00
if ($ShowError) {
2013-05-27 08:00:58 +00:00
error($ParsedUrl['host'] . ' is not an allowed image host. Please use a different host.');
}
2013-02-25 21:16:55 +00:00
return true;
}
}
return false;
}
/**
* Checks to see if a link has a thumbnail
* @param string $Url Link to an image
* @return string|false Matched host or false
*/
2013-04-30 18:18:07 +00:00
private static function thumbnailable($Url) {
2013-02-25 21:16:55 +00:00
$ParsedUrl = parse_url($Url);
return !empty(self::$Thumbs[$ParsedUrl['host']]);
}
/**
* Checks an extension
* @param string $Ext Extension to check
* @return boolean
*/
2013-04-30 18:18:07 +00:00
private static function valid_extension($Ext) {
2013-02-25 21:16:55 +00:00
// return @self::$Extensions[$Ext] === true;
2013-06-24 08:00:28 +00:00
return !empty(self::$Extensions[$Ext]) && (self::$Extensions[$Ext] === true);
2013-02-25 21:16:55 +00:00
}
/**
* Stores a link with a (thumbnail) link
* @param type $Link
* @param type $Processed
*/
2013-04-30 18:18:07 +00:00
private static function store($Link, $Processed) {
2013-02-25 21:16:55 +00:00
self::$Storage[$Link] = $Processed;
}
/**
* Retrieves an entry from our storage
* @param type $Link
* @return boolean|string Returns false if no match
*/
2013-04-30 18:18:07 +00:00
private static function get_stored($Link) {
2013-02-25 21:16:55 +00:00
if (isset(self::$Storage[$Link])) {
return self::$Storage[$Link];
}
return false;
}
/**
2013-04-30 18:18:07 +00:00
* Checks if URL points to a whatimg thumbnail.
2013-02-25 21:16:55 +00:00
*/
2013-04-30 18:18:07 +00:00
private static function has_whatimg_thumb($Url) {
return (strpos($Url, '_thumb') !== false);
2013-02-25 21:16:55 +00:00
}
/**
2013-04-30 18:18:07 +00:00
* Cleans up imgur URL if it already has a modifier attached to the end of it.
*/
private static function clean_imgur_url($Url) {
2013-06-13 08:01:05 +00:00
$Extension = pathinfo($Url, PATHINFO_EXTENSION);
$Full = preg_replace('/\.[^.]*$/', '', $Url);
$Base = substr($Full, 0, strrpos($Full, '/'));
$Path = substr($Full, strrpos($Full, '/') + 1);
if (strlen($Path) == 6) {
$Last = $Path[strlen($Path) - 1];
if ($Last == 'm' || $Last == 'l' || $Last == 's' || $Last == 'h' || $Last == 'b') {
$Path = substr($Path, 0, -1);
}
}
return "$Base/$Path.$Extension";
2013-02-25 21:16:55 +00:00
}
/**
2013-04-30 18:18:07 +00:00
* Replaces the extension.
2013-02-25 21:16:55 +00:00
*/
2013-04-30 18:18:07 +00:00
private static function replace_extension($String, $Extension) {
2013-06-13 08:01:05 +00:00
return preg_replace('/\.[^.]*$/', $Extension, $String);
2013-02-25 21:16:55 +00:00
}
/**
* Create image proxy URL
2013-03-05 08:00:26 +00:00
* @param string $Url image URL
* @return image proxy URL
2013-02-25 21:16:55 +00:00
*/
public static function proxy_url($Url) {
global $SSL;
2013-04-30 18:18:07 +00:00
return ($SSL ? 'https' : 'http') . '://' . SITE_URL . '/image.php?c=1&amp;i=' . urlencode($Url);
2013-02-25 21:16:55 +00:00
}
2013-04-30 18:18:07 +00:00
/**
* Determine the image URL. This takes care of the image proxy and thumbnailing.
* @param string $Url
* @param bool $Thumb
* @return string
*/
public static function process($Url, $Thumb = false) {
if (empty($Url)) {
return '';
}
if (($Found = self::get_stored($Url . ($Thumb ? '_thumb' : '')))) {
return $Found;
}
2013-02-25 21:16:55 +00:00
2013-04-30 18:18:07 +00:00
$ProcessedUrl = $Url;
if ($Thumb) {
$Extension = pathinfo($Url, PATHINFO_EXTENSION);
if (self::thumbnailable($Url) && self::valid_extension($Extension)) {
if (strpos($Url, 'whatimg') !== false && !self::has_whatimg_thumb($Url)) {
$ProcessedUrl = self::replace_extension($Url, '_thumb.' . $Extension);
} elseif (strpos($Url, 'imgur') !== false) {
$ProcessedUrl = self::replace_extension(self::clean_imgur_url($Url), 'm.' . $Extension);
}
}
}
2012-06-18 08:00:14 +00:00
2013-05-01 08:00:16 +00:00
if (check_perms('site_proxy_images')) {
2013-09-30 08:00:55 +00:00
$ProcessedUrl = self::proxy_url($ProcessedUrl);
2013-04-30 18:18:07 +00:00
}
2013-05-01 08:00:16 +00:00
self::store($Url . ($Thumb ? '_thumb' : ''), $ProcessedUrl);
2013-04-30 18:18:07 +00:00
return $ProcessedUrl;
}
2012-06-22 08:00:10 +00:00
2013-04-30 18:18:07 +00:00
/**
* Cover art thumbnail in browse, on artist pages etc.
* @global array $CategoryIcons
* @param string $Url
* @param int $CategoryID
*/
public static function cover_thumb($Url, $CategoryID) {
global $CategoryIcons;
if ($Url) {
$Src = self::process($Url, true);
$Lightbox = self::process($Url);
} else {
$Src = STATIC_SERVER . 'common/noartwork/' . $CategoryIcons[$CategoryID - 1];
$Lightbox = $Src;
2013-04-18 08:00:54 +00:00
}
2013-04-30 18:18:07 +00:00
?>
<img src="<?=$Src?>" width="90" height="90" alt="Cover" onclick="lightbox.init('<?=$Lightbox?>', 90)" />
<?
2013-04-18 08:00:54 +00:00
}
2012-06-18 08:00:14 +00:00
}