Gazelle/classes/class_image_tools.php

70 lines
1.8 KiB
PHP
Raw Normal View History

2012-06-18 08:00:14 +00:00
<?
/**
* This class determines the thumbnail equivalent of an image's url after being passed the original
*
**/
function to_thumbnail($url) {
$thumb = $url;
$extension = pathinfo($url, PATHINFO_EXTENSION);
2012-06-22 08:00:10 +00:00
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');
}
}
2012-06-18 08:00:14 +00:00
return $thumb;
}
function replace_extension($string, $extension) {
$string = preg_replace('/\.[^.]*$/', '', $string);
$string = $string . $extension;
return $string;
}
function contains($substring, $string) {
2012-06-22 08:00:10 +00:00
return $pos = strpos($string, $substring);
}
function hasWhatImgThumb($url) {
return !contains("_thumb", $url);
}
function cleanImgurUrl($url) {
$extension = pathinfo($url, PATHINFO_EXTENSION);
$path = preg_replace('/\.[^.]*$/', '', $url);
$last = $path[strlen($path)-1];
2012-06-18 08:00:14 +00:00
2012-06-22 08:00:10 +00:00
if($last == 'm' || $last == 'l' || $last == 's' || $last == 'h' || $last == 'b') {
$path = substr($path, 0, -1);
}
return $path . "." . $extension;
2012-06-18 08:00:14 +00:00
}
?>