Skip to content

Instantly share code, notes, and snippets.

@niraj-shah
Created February 7, 2013 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niraj-shah/4730047 to your computer and use it in GitHub Desktop.
Save niraj-shah/4730047 to your computer and use it in GitHub Desktop.
Facebook API: Generating a Large Square Profile Image
<?php
function generate_ticket( $src, $l, $t, $r, $b ) {
// check to see if Image Functions Exist
if ( !function_exists ( "imagecreate" ) || !function_exists ( "imagecreatetruecolor" ) ) {
die( "No image create functions!" );
}
// get size of file
$size = @getimagesize( $src );
// calculate the cropped region - FB returns percentages instead of coordinates
$l_crop = $size[0] * $l;
$t_crop = $size[1] * $t;
$r_crop = $size[0] * $r;
$b_crop = $size[1] * $b;
// find the size of the cropped image
$newx = round( $r_crop - $l_crop, 4 );
$newy = round( $b_crop - $t_crop, 4 );
// fix errors in sizes - sometimes the crop isn't perfectly square
if ( $newx != $newy ) $newx = $newy = min( $newx, $newy );
// create the new image with the crop dimensions
$destimg = imagecreatetruecolor( $newx, $newy ) or die ();
// make a copy of the original image - Facbook uses JPEG
$sourceimg = imagecreatefromjpeg( $src );
// copy the Facebook image to the blank image, but using the cropped values instead
@imagecopyresampled ( $destimg, $sourceimg, 0, 0, $l_crop, $t_crop, $newx, $newy, $newx, $newy ) or die ( "Cannot use GD2 here! (Error 003)" );
// output the image to the screen
header( "content-type: image/png" );
imagepng( $destimg );
// clear memory
imagedestroy( $destimg );
imagedestroy( $sourceimg );
}
// check if we have a valid ID
if ( isset( $_GET['id'] ) && is_numeric( $_GET['id'] ) ) {
// get image data from Graph API using FQL
$data = file_get_contents( 'http://graph.facebook.com/fql?q=' . urlencode( 'select pic_crop from profile where id = ' . $_GET['id'] ) );
// fix the data
$data = json_decode( $data );
$pic = $data->data[0]->pic_crop;
// call the crop function
generate_ticket( $pic->uri, $pic->left, $pic->top, $pic->right, $pic->bottom );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment