Last active
April 6, 2022 23:46
-
-
Save serverwentdown/7653718 to your computer and use it in GitHub Desktop.
Code for thumb.uni.me
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// set_time_limit(3600); | |
$url = $_GET["url"]; | |
if ($url) { | |
$width = $_GET["w"]; | |
$allowed = array('jpg','gif','png'); | |
$pos = strrpos($url, "."); | |
$str = substr($url,($pos + 1)); | |
$ch = curl_init(); | |
$timeout = 0; | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); | |
// Getting binary data | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); | |
$image = curl_exec($ch); | |
curl_close($ch); | |
// output to browser | |
$im = @imagecreatefromstring($image); | |
$tw = @imagesx($im); | |
if(!$tw){ | |
// Font directory + font name | |
$font = 'LondrinaSolid.ttf'; | |
// Size of the font | |
$fontSize = 18; | |
// Height of the image | |
$height = 160; | |
// Width of the image | |
$width = 250; | |
// Text | |
$str = "omg. an error occurred\nthumbnail unavailable. "; | |
$img_handle = imagecreate ($width, $height) or die ("Cannot Create image"); | |
// Set the Background Color RGB | |
$backColor = imagecolorallocate($img_handle, 100, 100, 100); | |
// Set the Text Color RGB | |
$txtColor = imagecolorallocate($img_handle, 200, 200, 200); | |
$textbox = imagettfbbox($fontSize, 0, $font, $str) or die('Error in imagettfbbox function'); | |
$x = ($width - $textbox[4])/2; | |
$y = ($height - $textbox[5])/2 - 18; | |
imagettftext($img_handle, $fontSize, 0, $x, $y, $txtColor, $font , $str) or die('Error in imagettftext function'); | |
header('Content-Type: image/jpeg'); | |
imagejpeg($img_handle,NULL,100); | |
imagedestroy($img_handle); | |
} | |
else{ | |
if($str == 'jpg' || $str == 'jpeg') { | |
header("Content-type: image/jpeg"); | |
} | |
if($str == 'gif') { | |
header("Content-type: image/gif"); | |
} | |
if($str == 'png') { | |
header("Content-type: image/png"); | |
} | |
else { | |
$str = 'png'; | |
header("Content-type: image/png"); | |
} | |
$th = imagesy($im); | |
$thumbWidth = $width; | |
if($tw <= $thumbWidth){ | |
$thumbWidth = $tw; | |
} | |
$thumbHeight = $th * ($thumbWidth / $tw); | |
$thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight); | |
if($str == 'gif'){ | |
$colorTransparent = imagecolortransparent($im); | |
imagefill($thumbImg, 0, 0, $colorTransparent); | |
imagecolortransparent($thumbImg, $colorTransparent); | |
} | |
if($str == 'png'){ | |
imagealphablending($thumbImg, false); | |
imagesavealpha($thumbImg,true); | |
$transparent = imagecolorallocatealpha($thumbImg, 255, 255, 255, 127); | |
imagefilledrectangle($thumbImg, 0, 0, $thumbWidth, $thumbHeight, $transparent); | |
} | |
imagecopyresampled($thumbImg, $im, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $tw, $th); | |
if($str == 'jpg' || $str == 'jpeg'){ | |
imagejpeg($thumbImg, NULL, 55); | |
} | |
if($str == 'gif'){ | |
imagegif($thumbImg); | |
} | |
if($str == 'png'){ | |
imagealphablending($thumbImg,TRUE); | |
imagepng($thumbImg, NULL, 9, PNG_ALL_FILTERS); | |
} | |
imagedestroy($thumbImg); | |
} | |
} | |
else { | |
?> | |
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>thumb</title> | |
<style type="text/css"> | |
body { | |
padding: 100px; | |
font-family: "Helvetica Neue", "Helvetica", sans-serif; | |
font-weight: 300; | |
} | |
pre { | |
background-color: #eee; | |
padding: 5px; | |
margin: 10px; | |
} | |
h1, h2, h3 { | |
font-weight: 100; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Tiny thumbnail generator</h1> | |
<h2>API Usage</h2> | |
<p> | |
URL | |
<pre>http://thumb.uni.me/?w=<width>&url=<image url></pre> | |
</p> | |
<p> | |
For example, | |
<pre>http://thumb.uni.me/?w=100&url=http://www.google.com/images/srpr/logo11w.png</pre> | |
and | |
<pre>http://thumb.uni.me/?w=64&url=http://nodejs.org/images/logos/nodejs-1024x768.png</pre> | |
</p> | |
<h2>Warnings</h2> | |
<p> | |
This runs on 000webhost.com. Limited processing and bandwidth. | |
<br /> | |
<p> | |
Slow. Not for full-scale production code. | |
</p> | |
<h2><a href="https://gist.github.com/ambrosechua/7653718">Source code</a></h2> | |
<p> | |
Host your own copy! It would be great if you would link back to us. | |
</p> | |
<br /> | |
<p> | |
By Ambrose Chua (ambc). | |
</p> | |
</body> | |
</html> | |
<?php | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment