Skip to content

Instantly share code, notes, and snippets.

@andersruneson
Last active July 21, 2018 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save andersruneson/d14979b1076318b3d2f8 to your computer and use it in GitHub Desktop.
Save andersruneson/d14979b1076318b3d2f8 to your computer and use it in GitHub Desktop.
<?php
/*
This script works much like gravatar
Put avatar files in $imagedir with the same name as the md5sum of the users emailaddress
*/
$imagedir = "/usr/share/mantis/www/avatar/images/";
$allOk = true;
$requestedfile = false;
$size = false;
/* Check for a size parameter */
if (isset($_GET["s"]) && ctype_alnum($_GET["s"]))
{
if ($_GET["s"] <= 1000)
{
$size = $_GET["s"];
}
}
/* Check for a id parameter */
if (isset($_GET["i"]) && ctype_alnum($_GET["i"]) && strlen($_GET["i"]) > 2 && preg_match("/^(\w{32})$/", $_GET["i"], $matches))
{
$requestedfile = $_GET["i"];
}
else if (array_key_exists('PATH_INFO', $_SERVER) === true)
{
/* Check for a id parameter */
if (preg_match("/^.(\w{32}).*/", $_SERVER['PATH_INFO'], $matches))
{
$requestedfile = $matches[1];
/* Found id parameter, check for a size parameter */
if (preg_match("/^.*s=(\d+).*/", $_SERVER['PATH_INFO'], $matches))
{
if ($matches[1] <= 1000)
{
$size = $matches[1];
}
}
}
else
{
$allOk = false;
}
}
else
{
$allOk = false;
}
/* Check if file with the same name as id is found */
if ($allOk && !is_readable($imagedir.$requestedfile))
{
$allOk = false;
}
if ($allOk)
{
/* Check that file type of avatar can be read */
$filetype = exif_imagetype($imagedir.$requestedfile);
if ($filetype === false)
{
$allOk = false;
}
}
if ($allOk)
{
/* Send the header */
header("Content-type: " . image_type_to_mime_type($filetype));
/* If there was a size parameter */
if ($size)
{
/* Resize avatar and crop if avatar is not rectangular */
$image = imagecreatefromstring(file_get_contents($imagedir.$requestedfile));
list($width, $height) = getimagesize($imagedir.$requestedfile);
$newimg = resize_image($image, $width, $height, $size, $size, true);
imagejpeg($newimg, "/tmp/".$requestedfile) or die("Error: Could not save\n");
/* Output avatar */
echo file_get_contents("/tmp/".$requestedfile);
}
else
{
/* Output avatar */
echo file_get_contents($imagedir.$requestedfile);
}
}
/* If something went wrong, load avatar from gravatar */
else if ($requestedfile != false)
{
if (ctype_alnum($_GET["d"]) && ctype_alnum($_GET["r"]) && ctype_alnum($_GET["s"]))
{
echo file_get_contents("https://secure.gravatar.com/avatar/" . $requestedfile . "?d=".$_GET["d"]."&r=".$_GET["r"]."&s=" . $size);
}
}
/* http://stackoverflow.com/questions/14649645/resize-image-in-php */
function resize_image($src, $width, $height, $w, $h, $crop=FALSE) {
$r = $width / $height;
$x = 0;
$y = 0;
if ($crop) {
if ($width > $height) {
$x = ($width - ceil($width-($width*abs($r-$w/$h))) )/2;
$width = ceil($width-($width*abs($r-$w/$h)));
} else {
$y = ( $height - ceil($height-($height*abs($r-$w/$h))) )/2;
$height = ceil($height-($height*abs($r-$w/$h)));
}
$newwidth = $w;
$newheight = $h;
} else {
if ($w/$h > $r) {
$newwidth = $h*$r;
$newheight = $h;
} else {
$newheight = $w/$r;
$newwidth = $w;
}
}
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, $x, $y, $newwidth, $newheight, $width, $height);
return $dst;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment