/** * @method POST */ function get() { // get token $token = Utilities::ValidateJWTToken(apache_request_headers()); // check if token is not null if ($token != NULL) { // get a reference to the site, user $site = Site::GetBySiteId($token->SiteId); parse_str($this->request->data, $request); // parse request $overwrite = NULL; $folder = 'files'; if (isset($_REQUEST['overwrite'])) { $overwrite = $_REQUEST['overwrite']; } if (isset($_REQUEST['folder'])) { $folder = $_REQUEST['folder']; } $arr = array(); // Get uploaded file info $filename = $_FILES['file']['name']; $file = $_FILES['file']['tmp_name']; $contentType = $_FILES['file']['type']; $size = intval($_FILES['file']['size'] / 1024); // overwrite if applicable if ($overwrite != NULL) { $filename = $overwrite; } $parts = explode(".", $filename); $ext = end($parts); // get extension $ext = strtolower($ext); // convert to lowercase $thumbnail = 't-' . $filename; // allowed filetypes $allowed = explode(',', ALLOWED_FILETYPES); // trim and lowercase all items in the aray $allowed = array_map('trim', $allowed); $allowed = array_map('strtolower', $allowed); // save image if ($ext == 'png' || $ext == 'jpg' || $ext == 'gif' || $ext == 'svg') { // upload image $arr = Image::SaveImageWithThumb($site, $filename, $file, $folder); // set local URL $url = $site['Domain']; // set URL if on S3 if (FILES_ON_S3 == true) { $url = str_replace('{{bucket}}', $site['Bucket'], S3_URL); $url = str_replace('{{site}}', $site['FriendlyId'], $url); } // create array $arr = array('filename' => $filename, 'fullUrl' => $url . '/' . $folder . '/' . $filename, 'thumbUrl' => $site['Domain'] . '/' . $folder . '/thumbs/' . $filename, 'extension' => $ext, 'isImage' => true, 'width' => $arr['width'], 'height' => $arr['height']); } else { if (in_array($ext, $allowed)) { // save file if it is allowed // save file to directory $directory = SITES_LOCATION . '/' . $site['FriendlyId'] . '/' . $folder . '/'; // set url $url = $site['Domain']; // set URL if on S3 if (FILES_ON_S3 == true) { // meta is blank for non-images $meta = array(); $type = $contentType; echo 'test'; echo $filename; // save file with meta-data S3::SaveContents($site, $type, $filename, $file, $meta, $folder); $url = str_replace('{{bucket}}', $site['Bucket'], S3_URL); $url = str_replace('{{site}}', $site['FriendlyId'], $url); } else { // upload file Utilities::SaveFile($directory, $filename, $file); } $arr = array('filename' => $filename, 'fullUrl' => $url . '/' . $folder . '/' . $filename, 'thumbUrl' => NULL, 'extension' => $ext, 'isImage' => false, 'width' => -1, 'height' => -1); } else { return new Tonic\Response(Tonic\Response::UNAUTHORIZED); } } // return a json response $response = new Tonic\Response(Tonic\Response::OK); $response->contentType = 'application/json'; $response->body = json_encode($arr); return $response; } else { // return an unauthorized exception (401) return new Tonic\Response(Tonic\Response::UNAUTHORIZED); } }
public static function CreateThumb($site, $image, $filename, $folder = 'files') { // directory $dir = SITES_LOCATION . '/' . $site['FriendlyId'] . '/' . $folder . '/'; // set thumb size $target_w = THUMB_MAX_WIDTH; $target_h = THUMB_MAX_HEIGHT; list($curr_w, $curr_h, $type, $attr) = Image::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; } // (for testing) die('curr_w='.$curr_w.' curr_h='.$curr_h.' x_start='.$x_start.' y_start='.$y_start.' target_w='.$target_w.' target_h='.$target_h.' up_w='.$up_w.' up_h='.$up_h); 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 . 'thumbs/' . $filename; if (!file_exists($dir)) { mkdir($dir, 0777, true); } switch ($ext) { case 'jpg': // save file on S3 if (FILES_ON_S3 == true) { // write image and copy buffer ob_start(); imagejpeg($dst_img, NULL, 100); $contents = ob_get_contents(); ob_end_clean(); $meta = array('width' => $target_w, 'height' => $target_h); S3::SaveContents($site, $type, 'thumbs/' . $filename, $contents, $meta, $folder); } else { // save file locally imagejpeg($dst_img, $full, 100); } break; case 'png': // save file on S3 if (FILES_ON_S3 == true) { // write image and copy buffer ob_start(); imagepng($dst_img, NULL); $contents = ob_get_contents(); ob_end_clean(); $meta = array('width' => $target_w, 'height' => $target_h); S3::SaveContents($site, $type, 'thumbs/' . $filename, $contents, $meta, $folder); } else { // save file locally imagepng($dst_img, $full); } break; case 'gif': // save file on S3 if (FILES_ON_S3 == true) { // write image and copy buffer ob_start(); imagegif($dst_img, null); $meta = array(); $contents = ob_get_contents(); ob_end_clean(); S3::SaveContents($site, $type, 'thumbs/' . $filename, $contents, $meta, $folder); } else { // save file locally imagegif($dst_img, $full); } break; case 'svg': // save file on S3 if (FILES_ON_S3 == true) { S3::SaveFile($site, $type, 'thumbs/' . $filename, $image, $folder); } else { // save file locally copy($image, $full); } break; default: return false; return true; } }