Skip to content

Instantly share code, notes, and snippets.

@JuanPotato
Created July 11, 2015 21:33
Show Gist options
  • Save JuanPotato/37116910af52fd485e68 to your computer and use it in GitHub Desktop.
Save JuanPotato/37116910af52fd485e68 to your computer and use it in GitHub Desktop.
<?PHP
$id = (isset($_GET['id']) && is_numeric($_GET['id'])) ? intval($_GET['id']) : 0;
//get the ?id=value from the url, if it doesn't exist, make it zero
//if $id is no 0 and it is greater than 0 (can't have a user 0 or negative)
if ($id != 0 && $id > 0){
header('Content-type: image/png');
//tell whatever is accessing the url that an image/png is going to show (not html)
//make a function that returns an array of info for the likes/post
function getLikesPosts($idd){
$html = file_get_contents("https://forums.oneplus.net/members/$idd/?card=1");
//get the html of the users page
$pattern = '/\<dt\>Likes Received\:\<\/dt\>.\<dd\>(.*?)\<\/dd\>/';
//make a regex to get the likes
preg_match($pattern, $html, $matches, PREG_OFFSET_CAPTURE);
//use the regex to get a match from $html to get the result and move it into $matches
$likes = preg_replace('/,/', '', $matches[1][0]);
//set $likes as the one of the results (I needed to print them all before I knew which one to pick)
$pattern = '/\<a href\=\"search\/member\?user_id\='.$idd.'\" class\=\"concealed\" rel\=\"nofollow\"\>(.*?)\<\/a\>/';
//make a regex to get the posts
preg_match($pattern, $html, $matches, PREG_OFFSET_CAPTURE);
//use the regex to get a match from $html to get the result and move it into $matches
$posts = preg_replace('/,/', '', $matches[1][0]);
//set $posts as the one of the results (I needed to print them all before I knew which one to pick)
$pattern = "/\<h3 class=\"username\"\>\<a href=\"members\/(.*?)\.$idd\/\" class=\"username NoOverlay\" dir=\"auto\"\>(.*?)\<\/a\>\<\/h3\>/";
//make a regex to get the user name
preg_match($pattern, $html, $matches, PREG_OFFSET_CAPTURE);
//use the regex to get a match from $html to get the result and move it into $matches
$user = trim($matches[2][0]);
//set $user as the one of the results (I needed to print them all before I knew which one to pick)
$percentage = "Percentage: ".round( $likes/$posts * 100, 2, PHP_ROUND_HALF_EVEN) . "%";
//format the percentage and round
$ratio = "Likes per post: ".round( $likes/$posts, 2, PHP_ROUND_HALF_EVEN);
//format the Likes per post and round
return array($percentage,$ratio,$user);
//return the $percentage, $ratio, $user in an array
}
//Find a neat little function off of the internet to help me center the text when I write it
//I do not know how they made this or how it works, the comments were there when I got it
function calculateTextBox($font_size, $font_angle, $font_file, $text) {
$box = imagettfbbox($font_size, $font_angle, $font_file, $text);
if( !$box )
return false;
$min_x = min( array($box[0], $box[2], $box[4], $box[6]) );
$max_x = max( array($box[0], $box[2], $box[4], $box[6]) );
$min_y = min( array($box[1], $box[3], $box[5], $box[7]) );
$max_y = max( array($box[1], $box[3], $box[5], $box[7]) );
$width = ( $max_x - $min_x );
$height = ( $max_y - $min_y );
$left = abs( $min_x ) + $width;
$top = abs( $min_y ) + $height;
// to calculate the exact bounding box i write the text in a large image
$img = @imagecreatetruecolor( $width << 2, $height << 2 );
$white = imagecolorallocate( $img, 255, 255, 255 );
$black = imagecolorallocate( $img, 0, 0, 0 );
imagefilledrectangle($img, 0, 0, imagesx($img), imagesy($img), $black);
// for sure the text is completely in the image!
imagettftext( $img, $font_size,
$font_angle, $left, $top,
$white, $font_file, $text);
// start scanning (0=> black => empty)
$rleft = $w4 = $width<<2;
$rright = 0;
$rbottom = 0;
$rtop = $h4 = $height<<2;
for( $x = 0; $x < $w4; $x++ )
for( $y = 0; $y < $h4; $y++ )
if( imagecolorat( $img, $x, $y ) ){
$rleft = min( $rleft, $x );
$rright = max( $rright, $x );
$rtop = min( $rtop, $y );
$rbottom = max( $rbottom, $y );
}
// destroy img and serve the result
imagedestroy( $img );
return array(
"left" => $left - $rleft,
"top" => $top - $rtop ,
"width" => $rright - $rleft + 1,
"height" => $rbottom - $rtop + 1 );
}
$height = 100;
//set the height of the image in a value
$width = 250;
//set the width of the image in a value
$im = imageCreateFromPng("likes.png");
//Create an image from the base image "likes.png"
imageAlphaBlending($im, true);
//set alpha blending as true for $im because without it, it ducks up in places where transparency is involved
imageSaveAlpha($im, true);
//save that setting we just did^
$white = imagecolorallocate($im, 255, 255, 255);
//set the value $white as the color white (255,255,255) and we are going to use it with $im
$red = imagecolorallocate($im, 235, 0, 41);
//same concept just different color, red
imagefill($im, 0, 0, $white);
//99% this fills the image as white, BUT WHAT, IT SHOULD ERASE THE BASE IMAGE o.O
//IGNORE THAT
$path = 'lato.ttf';
//set the font path that we are going to use when typing the text
$text = getLikesPosts($id);
//set $text as the value you get back from getLikesPosts($id) which was discussed up there
$fontSize = 15;
//set font size
$bbox0 = calculateTextBox($fontSize, 0, $path, $text[0]);
//calculate the actual size that the textbox would take up if it were using that fontsize, at an angle of 0, using that font, and with the text[0] which is the percentage
$bbox1 = calculateTextBox($fontSize, 0, $path, $text[1]);
//calculate the actual size that the textbox would take up if it were using that fontsize, at an angle of 0, using that font, and with the text[1] which is the ratio
$bbox2 = calculateTextBox($fontSize, 0, $path, $text[2]);
//calculate the actual size that the textbox would take up if it were using that fontsize, at an angle of 0, using that font, and with the text[2] which is the username
$centeredX0 = (($width - $bbox0['width']) / 2);
//get the centered x for the first textbox (percentage)
$centeredX1 = (($width - $bbox1['width']) / 2);
//get the centered x for the first textbox (ratio)
$centeredX2 = (($width - $bbox2['width']) / 2);
//get the centered x for the first textbox (username)
imagettftext($im, $fontSize, 0, $centeredX0 + $bbox0['left'], 71, $red, $path, $text[0]);
//write text on the image $im at an angle of 0 with the centered x + the length of how far the left of its edge is from the dge of the image. at height 71 using the color red, using that font, writing the percentage
imagettftext($im, $fontSize, 0, $centeredX1 + $bbox1['left'], 95, $red, $path, $text[1]);
//write text on the image $im at an angle of 0 with the centered x + the length of how far the left of its edge is from the dge of the image. at height 95 using the color red, using that font, writing the ratio
imagettftext($im, $fontSize, 0, $centeredX2 + $bbox2['left'], 48, $red, $path, $text[2]);
//write text on the image $im at an angle of 0 with the centered x + the length of how far the left of its edge is from the dge of the image. at height 48 using the color red, using that font, writing the username
imagepng($im);
//create a final png and present it from the $im
imagedestroy($im);
//destroy $im because it is already being showed, there is no reason to keep the variable
}else{
//Send minions after the failure because they entered 0, a negative number, or forgot to set ?id=66909 in the url
echo "you fail so much -_-<br>put some real value for id or I'll have my minions come after you";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment