/** * Lists all files for a site * * @return Response */ public function listFiles(Request $request) { // get request data $email = $request->input('auth-email'); $id = $request->input('auth-id'); // get a reference to the site $site = Site::getById($id); // list files $arr = File::listFiles($id); // set image extensions $image_exts = array('gif', 'png', 'jpg', 'svg'); $files = array(); foreach ($arr as $file) { $filename = str_replace('files/', '', $file); $path = app()->basePath() . '/public/sites/' . $id . '/files/' . $filename; // get extension $parts = explode(".", $filename); $ext = end($parts); // get extension $ext = strtolower($ext); // convert to lowercase // determine if it is an image $is_image = in_array($ext, $image_exts); // get the filesize $size = filesize($path); if ($is_image === TRUE) { $width = 0; $height = 0; try { list($width, $height, $type, $attr) = Utilities::getImageInfo($path); } catch (Exception $e) { } // set url, thumb $url = $thumb = 'sites/' . $site->id . '/files/' . $filename; // check for thumb if (file_exists(app()->basePath() . '/public/sites/' . $id . '/files/thumbs/' . $filename)) { $thumb = 'sites/' . $site->id . '/files/thumbs/' . $filename; } // push file to the array array_push($files, array('name' => $filename, 'url' => $url, 'thumb' => $thumb, 'extension' => $ext, 'isImage' => $is_image, 'width' => $width, 'height' => $height, 'size' => number_format($size / 1048576, 2))); } else { // push file to the array array_push($files, array('name' => $filename, 'url' => 'files/' . $filename, 'thumb' => '', 'extension' => $ext, 'isImage' => $is_image, 'width' => NULL, 'height' => NULL, 'size' => number_format($size / 1048576, 2))); } } return response()->json($files); }
/** * Creates a thumbnail * * @param {Site} $site * @param {string} $filename * @param {string} $image path to the image * @return {array} */ public static function createThumb($site, $image, $filename) { $dir = app()->basePath() . '/public/sites/' . $site->id . '/files/thumbs'; // set thumb size $target_w = env('THUMB_MAX_WIDTH'); $target_h = env('THUMB_MAX_HEIGHT'); list($curr_w, $curr_h, $type, $attr) = Utilities::getImageInfo($image); $ext = 'jpg'; switch ($type) { // create image case IMAGETYPE_JPEG: $ext = 'jpg'; break; case IMAGETYPE_PNG: $ext = 'png'; break; case IMAGETYPE_GIF: $ext = 'gif'; break; case 'image/svg+xml': $ext = 'svg'; break; default: return false; } $scale_h = $target_h / $curr_h; $scale_w = $target_w / $curr_w; $factor_x = $curr_w / $target_w; $factor_y = $curr_h / $target_h; if ($factor_x > $factor_y) { $factor = $factor_y; } else { $factor = $factor_x; } $up_w = ceil($target_w * $factor); $up_h = ceil($target_h * $factor); $x_start = ceil(($curr_w - $up_w) / 2); $y_start = ceil(($curr_h - $up_h) / 2); switch ($type) { // create image case IMAGETYPE_JPEG: $n_img = imagecreatefromjpeg($image); break; case IMAGETYPE_PNG: $n_img = imagecreatefrompng($image); break; case IMAGETYPE_GIF: $n_img = imagecreatefromgif($image); break; case 'image/svg+xml': break; default: return false; } $dst_img = ImageCreateTrueColor($target_w, $target_h); switch ($type) { // fix for transparency issues case IMAGETYPE_PNG: imagealphablending($dst_img, true); imagesavealpha($dst_img, true); $transparent_color = imagecolorallocatealpha($dst_img, 0, 0, 0, 127); imagefill($dst_img, 0, 0, $transparent_color); break; case IMAGETYPE_GIF: $transparency_index = imagecolortransparent($dst_img); if ($transparency_index >= 0) { $transparent_color = imagecolorsforindex($dst_img, $transparency_index); $transparency_index = imagecolorallocate($dst_img, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']); imagefill($dst_img, 0, 0, $transparency_index); imagecolortransparent($dst_img, $transparency_index); } break; default: break; } if ($type != 'image/svg+xml') { imagecopyresampled($dst_img, $n_img, 0, 0, $x_start, $y_start, $target_w, $target_h, $up_w, $up_h); } //return $dst_img; $full = $dir . '/' . $filename; if (!file_exists($dir)) { mkdir($dir, 0777, true); } switch ($ext) { case 'jpg': imagejpeg($dst_img, $full, 100); break; case 'png': // save file locally imagepng($dst_img, $full); break; case 'gif': // save file locally imagegif($dst_img, $full); break; case 'svg': // save file locally copy($image, $full); break; default: return false; return true; } }