Techno

PHP random Facebook Cover - Tile Algorithm

Feb 1st, 2013
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.16 KB | None | 0 0
  1. <?php
  2.  
  3. function getfiles($dir){
  4.     $return = array('files' => array(), 'count' => 0);
  5.     $num = 0;
  6.     $allowed_extension = array('jpeg', 'jpg', 'png', 'gif');
  7.     if(is_dir($dir)){
  8.         $files = scandir($dir);
  9.         foreach($files as $file){
  10.             $file = $dir.DIRECTORY_SEPARATOR.$file;
  11.             if(is_file($file)){
  12.                 $extension = pathinfo($file, PATHINFO_EXTENSION);
  13.                 $extension = strtolower($extension);
  14.                 if(in_array($extension, $allowed_extension, true)){
  15.                     $return['files'][] = array('path' => realpath($file), 'extension' => $extension);
  16.                     $return['count']++;
  17.                 }
  18.             }
  19.         }
  20.     }
  21.     return $return;
  22. }
  23.  
  24. $height = 315;
  25. $width = 850;
  26.  
  27. // Fb covers are 850 x 315
  28.  
  29. $images = getfiles('img'); // scan img folder for tile images
  30. shuffle($images['files']);
  31. set_time_limit(0);
  32.  
  33. $columns = 4;
  34. $px = 0;
  35. $py = 0;
  36. $new_width = 0;
  37. $new_height = 0;
  38. $column_count = 0;
  39. $vary = floor($width/10);
  40.  
  41. $image = imagecreatetruecolor($width, $height);
  42. imagefilledrectangle($image, 0, 0, $width, $height, imagecolorallocate($image, 255, 255, 255));
  43.  
  44. for($i = 0; $i < $images['count']; $i++){
  45.     if($images['files'][$i]['extension'] == 'jpg' || $images['files'][$i]['extension'] == 'jpeg'){
  46.         $tmp_image = imagecreatefromjpeg($images['files'][$i]['path']);
  47.     }else if($images['files'][$i]['extension'] == 'png'){
  48.         $tmp_image = imagecreatefrompng($images['files'][$i]['path']);
  49.     }else if($images['files'][$i]['extension'] == 'gif'){
  50.         $tmp_image = imagecreatefromgif($images['files'][$i]['path']);
  51.     }
  52.     $tmp_width = imagesx($tmp_image);
  53.     $tmp_height = imagesy($tmp_image);
  54.     if($py == 0){
  55.         $new_width = rand(ceil($width/$columns) - $vary, ceil($width/$columns) + $vary);
  56.         $column_count++;
  57.     }
  58.     if($column_count == $columns){
  59.         $new_width = $width - $px;
  60.     }
  61.     $new_height = floor($new_width * ($tmp_height/$tmp_width));
  62.    
  63.     imagecopyresampled($image, $tmp_image, $px, $py, 0, 0, $new_width, $new_height, $tmp_width, $tmp_height);
  64.     $py += $new_height;
  65.     if($py >= $height){
  66.         $py = 0;
  67.         $px += $new_width;
  68.     }
  69.     //echo $px.','.$py.'<br />';
  70.     imagedestroy($tmp_image);
  71. }
  72.  
  73. header('Content-type: image/png');
  74. imagepng($image);
  75. imagepng($image, 'cover.png', 9);
  76. imagedestroy($image);
  77. ?>
Advertisement
Add Comment
Please, Sign In to add comment