Gazelle/classes/class_image_tools.php

101 lines
2.7 KiB
PHP
Raw Normal View History

2012-06-18 08:00:14 +00:00
<?
/**
2012-07-03 08:00:15 +00:00
* This class determines the thumbnail equivalent of an image's url after being passed the original
2012-06-18 08:00:14 +00:00
*
2012-07-03 08:00:15 +00:00
**/
2012-06-18 08:00:14 +00:00
2012-07-11 08:00:16 +00:00
$blacklist = array("tinypic", "dsimg");
2013-02-22 08:00:24 +00:00
2012-07-11 08:00:16 +00:00
/**
* Checks if image host is good, otherwise displays an error.
*/
function check_imagehost($url) {
global $blacklist;
2013-02-22 08:00:24 +00:00
foreach ($blacklist as &$value) {
2012-07-16 08:00:07 +00:00
if(strpos(strtolower($url), $value)) {
2012-07-11 08:00:16 +00:00
$parsed_url = parse_url($url);
error($parsed_url['host'] . " is not an allowed imagehost. Please use a different imagehost.");
break;
}
}
}
2013-02-22 08:00:24 +00:00
2012-07-03 08:00:15 +00:00
/**
* The main function, called to get the thumbnail url.
*/
2012-06-18 08:00:14 +00:00
function to_thumbnail($url) {
2012-07-03 08:00:15 +00:00
$thumb = $url;
$extension = pathinfo($url, PATHINFO_EXTENSION);
if (contains('whatimg', $url)) {
if (hasWhatImgThumb($url)) {
if ($extension == 'jpeg') {
$thumb = replace_extension($url, '_thumb.jpeg');
}
if ($extension == 'jpg') {
$thumb = replace_extension($url, '_thumb.jpg');
}
if ($extension == 'png') {
$thumb = replace_extension($url, '_thumb.png');
}
if ($extension == 'gif') {
$thumb = replace_extension($url, '_thumb.gif');
}
}
} elseif (contains('imgur', $url)) {
$url = cleanImgurUrl($url);
if ($extension == 'jpeg') {
$thumb = replace_extension($url, 'm.jpeg');
}
if ($extension == 'jpg') {
$thumb = replace_extension($url, 'm.jpg');
}
if ($extension == 'png') {
$thumb = replace_extension($url, 'm.png');
}
if ($extension == 'gif') {
$thumb = replace_extension($url, 'm.gif');
}
}
return $thumb;
2012-06-18 08:00:14 +00:00
}
2012-07-03 08:00:15 +00:00
/**
* Replaces the extension.
*/
2012-06-18 08:00:14 +00:00
function replace_extension($string, $extension) {
2012-07-03 08:00:15 +00:00
$string = preg_replace('/\.[^.]*$/', '', $string);
$string = $string . $extension;
return $string;
2012-06-18 08:00:14 +00:00
}
function contains($substring, $string) {
2012-07-03 08:00:15 +00:00
return $pos = strpos($string, $substring);
2012-06-22 08:00:10 +00:00
}
2012-07-03 08:00:15 +00:00
/**
* Checks if url points to a whatimg thumbnail.
*/
function hasWhatImgThumb($url) {
return !contains("_thumb", $url);
2012-06-22 08:00:10 +00:00
}
2012-07-03 08:00:15 +00:00
/**
* Cleans up imgur url if it already has a modifier attached to the end of it.
*/
2012-06-22 08:00:10 +00:00
function cleanImgurUrl($url) {
2012-07-03 08:00:15 +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;
2012-06-18 08:00:14 +00:00
}
?>