Skip to content

Instantly share code, notes, and snippets.

@JuanPotato
Created January 27, 2015 13:52
Show Gist options
  • Save JuanPotato/a71f868627f3094ac36d to your computer and use it in GitHub Desktop.
Save JuanPotato/a71f868627f3094ac36d to your computer and use it in GitHub Desktop.
<?PHP
$id = (isset($_GET['id']) && is_numeric($_GET['id'])) ? intval($_GET['id']) : 0;
if ($id != 0 && $id > 0){
header('Content-type: image/png');
function getLikesPosts($idd){
$html = file_get_contents("https://forums.oneplus.net/members/$idd/?card=1");
$pattern = '/\<dt\>Likes Received\:\<\/dt\>.\<dd\>(.*?)\<\/dd\>/';
preg_match($pattern, $html, $matches, PREG_OFFSET_CAPTURE);
$likes = preg_replace('/,/', '', $matches[1][0]);
$pattern = '/\<a href\=\"search\/member\?user_id\='.$idd.'\" class\=\"concealed\" rel\=\"nofollow\"\>(.*?)\<\/a\>/';
preg_match($pattern, $html, $matches, PREG_OFFSET_CAPTURE);
$posts = preg_replace('/,/', '', $matches[1][0]);
$pattern = "/\<h3 class=\"username\"\>\<a href=\"members\/(.*?)\.$idd\/\" class=\"username NoOverlay\" dir=\"auto\"\>(.*?)\<\/a\>\<\/h3\>/";
preg_match($pattern, $html, $matches, PREG_OFFSET_CAPTURE);
$user = trim($matches[2][0]);
$percentage = round( $likes/$posts * 100, 2, PHP_ROUND_HALF_EVEN) . "% like to post ratio";
$ratio = round( $likes/$posts, 2, PHP_ROUND_HALF_EVEN) . " likes per post";
return array($percentage,$ratio,$user);
}
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;
$width = 300;
$im = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($im, 200, 200, 200);
$black = imagecolorallocate($im, 0, 0, 0);
imagefill($im, 0, 0, $white);
$path = 'font.ttf';
$text = getLikesPosts($id);
$fontSize = 15;
$bbox0 = calculateTextBox($fontSize, 0, $path, $text[0]);
$bbox1 = calculateTextBox($fontSize, 0, $path, $text[1]);
$bbox2 = calculateTextBox(18, 0, $path, "Like to Post Ratio");
$bbox3 = calculateTextBox(13, 0, $path, $text[2]);
$centeredX0 = (($width - $bbox0['width']) / 2);
$centeredX1 = (($width - $bbox1['width']) / 2);
$centeredY0 = (($height - $bbox0['height']) / 2);
$centeredY1 = (($height - $bbox1['height']) / 2);
$centeredX2 = (($width - $bbox2['width']) / 2);
$centeredX3 = (($width - $bbox3['width']) / 2);
// here left coordinate is subtracted (+ negative value) from centeredX
imagettftext($im, $fontSize, 0, $centeredX0 + $bbox0['left'], $centeredY0 + $bbox0['top']+11, $black, $path, $text[0]);
imagettftext($im, $fontSize, 0, $centeredX1 + $bbox1['left'], $centeredY1 + $bbox1['top']+35, $black, $path, $text[1]);
imagettftext($im, 18, 0, $centeredX2 + $bbox2['left'], 22, $black, $path, "Like to Post Ratio");
imagettftext($im, 13, 0, $centeredX3 + $bbox2['left'], 43, $black, $path, $text[2]);
//imagettftext($im, 10, 0, 0, 25, $black, $path, $text);
imagepng($im);
imagedestroy($im);
}else{
echo "you fail so much -_-<br>put some 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