Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function getfiles($dir){
- $return = array('files' => array(), 'count' => 0);
- $num = 0;
- $allowed_extension = array('jpeg', 'jpg', 'png', 'gif');
- if(is_dir($dir)){
- $files = scandir($dir);
- foreach($files as $file){
- $file = $dir.DIRECTORY_SEPARATOR.$file;
- if(is_file($file)){
- $extension = pathinfo($file, PATHINFO_EXTENSION);
- $extension = strtolower($extension);
- if(in_array($extension, $allowed_extension, true)){
- $return['files'][] = array('path' => realpath($file), 'extension' => $extension);
- $return['count']++;
- }
- }
- }
- }
- return $return;
- }
- $height = 315;
- $width = 850;
- // Fb covers are 850 x 315
- $images = getfiles('img'); // scan img folder for tile images
- shuffle($images['files']);
- set_time_limit(0);
- $columns = 4;
- $px = 0;
- $py = 0;
- $new_width = 0;
- $new_height = 0;
- $column_count = 0;
- $vary = floor($width/10);
- $image = imagecreatetruecolor($width, $height);
- imagefilledrectangle($image, 0, 0, $width, $height, imagecolorallocate($image, 255, 255, 255));
- for($i = 0; $i < $images['count']; $i++){
- if($images['files'][$i]['extension'] == 'jpg' || $images['files'][$i]['extension'] == 'jpeg'){
- $tmp_image = imagecreatefromjpeg($images['files'][$i]['path']);
- }else if($images['files'][$i]['extension'] == 'png'){
- $tmp_image = imagecreatefrompng($images['files'][$i]['path']);
- }else if($images['files'][$i]['extension'] == 'gif'){
- $tmp_image = imagecreatefromgif($images['files'][$i]['path']);
- }
- $tmp_width = imagesx($tmp_image);
- $tmp_height = imagesy($tmp_image);
- if($py == 0){
- $new_width = rand(ceil($width/$columns) - $vary, ceil($width/$columns) + $vary);
- $column_count++;
- }
- if($column_count == $columns){
- $new_width = $width - $px;
- }
- $new_height = floor($new_width * ($tmp_height/$tmp_width));
- imagecopyresampled($image, $tmp_image, $px, $py, 0, 0, $new_width, $new_height, $tmp_width, $tmp_height);
- $py += $new_height;
- if($py >= $height){
- $py = 0;
- $px += $new_width;
- }
- //echo $px.','.$py.'<br />';
- imagedestroy($tmp_image);
- }
- header('Content-type: image/png');
- imagepng($image);
- imagepng($image, 'cover.png', 9);
- imagedestroy($image);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment