Warning, file /webapps/ocs-webserver/httpdocs/img/resize.php was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 <?php
0002 /**
0003  *  ocs-webserver
0004  *
0005  *  Copyright 2016 by pling GmbH.
0006  *
0007  *    This file is part of ocs-webserver.
0008  *
0009  *    This program is free software: you can redistribute it and/or modify
0010  *    it under the terms of the GNU Affero General Public License as
0011  *    published by the Free Software Foundation, either version 3 of the
0012  *    License, or (at your option) any later version.
0013  *
0014  *    This program is distributed in the hope that it will be useful,
0015  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017  *    GNU Affero General Public License for more details.
0018  *
0019  *    You should have received a copy of the GNU Affero General Public License
0020  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021  **/
0022 
0023 ini_set("memory_limit", "80M");
0024 
0025 # prevent creation of new directories
0026 $is_locked = false;
0027 
0028 
0029 # figure out requested path and actual physical file paths
0030 $orig_dir = dirname(__FILE__);
0031 $path = $_GET['dir'];
0032 $tokens = explode("/", $path);
0033 $file = "/" . implode('/', array_slice($tokens, 4));
0034 $orig_file = $orig_dir . $file;
0035 
0036 
0037 if (!file_exists($orig_file)) {
0038     header("Status: 404 Not Found");
0039     echo "Status: 404 Not Found";
0040     error_log("PATH={$path} ==> ORIGFILE={$orig_file}");
0041 
0042     return 0;
0043 }
0044 
0045 
0046 # check if new directory would need to be created
0047 $save_path = "$orig_dir/cache/$tokens[2]$file";
0048 $save_dir = dirname($save_path);
0049 
0050 if (!file_exists($save_dir) && $is_locked) {
0051     header("Status: 403 Forbidden");
0052     echo "Status: 403 Forbidden";
0053     error_log("Directory creation is forbidden. {$save_dir}");
0054 
0055     return 0;
0056 }
0057 
0058 # check for a valid image file
0059 if (!getimagesize($orig_file)) {
0060     header("Status: 404 Not Found");
0061     echo "Status: 404 Not Found";
0062     error_log("PATH={$path} ==> ORIGFILE={$orig_file}");
0063 
0064     return 0;
0065 }
0066 
0067 # parse out the requested image dimensions and resize mode
0068 $x_pos = strpos($tokens[2], 'x');
0069 $dash_pos = strpos($tokens[2], '-') ? strpos($tokens[2], '-') : strlen($tokens[2]);
0070 $target_width = substr($tokens[2], 0, $x_pos);
0071 $target_height = substr($tokens[2], $x_pos + 1, $dash_pos - $x_pos - 1);
0072 $mode = substr($tokens[2], $dash_pos + 1);
0073 
0074 $new_width = $target_width;
0075 $new_height = $target_height;
0076 
0077 try {
0078     $image = new Imagick($orig_file);
0079 } catch (ImagickException $e) {
0080     header("Status: 500 Internal Server Error");
0081     echo "Status: 500 Internal Server Error";
0082     error_log($e->getMessage());
0083 
0084 }
0085 list($orig_width, $orig_height, $type, $attr) = getimagesize($orig_file);
0086 
0087 # preserve aspect ratio, fitting image to specified box
0088 if ($mode == "0") {
0089     $new_height = $orig_height * $new_width / $orig_width;
0090     if ($new_height > $target_height) {
0091         $new_width = $orig_width * $target_height / $orig_height;
0092         $new_height = $target_height;
0093     }
0094 } # zoom and crop to exactly fit specified box
0095 else {
0096     if ($mode == "2") {
0097         // crop to get desired aspect ratio
0098         $desired_aspect = $target_width / $target_height;
0099         $orig_aspect = $orig_width / $orig_height;
0100 
0101         if ($desired_aspect > $orig_aspect) {
0102             $trim = $orig_height - ($orig_width / $desired_aspect);
0103             $image->cropImage($orig_width, $orig_height - $trim, 0, $trim / 2);
0104             error_log("HEIGHT TRIM $trim");
0105         } else {
0106             $trim = $orig_width - ($orig_height * $desired_aspect);
0107             $image->cropImage($orig_width - $trim, $orig_height, $trim / 2, 0);
0108         }
0109     }
0110 }
0111 
0112 # mode 3 (stretch to fit) is automatic fall-through as image will be blindly resized
0113 # in following code to specified box
0114 //bugfix: $new_width and $new_height have to be > 0
0115 if ($new_width == 0 && $new_height > 0) {
0116     $new_width = $new_height;
0117 }
0118 if ($new_height == 0 && $new_width > 0) {
0119     $new_height = $new_width;
0120 }
0121 
0122 $image->resizeImage($new_width, $new_height, imagick::FILTER_LANCZOS, 1);
0123 
0124 # save and return the resized image file
0125 if (!file_exists($save_dir)) {
0126     mkdir($save_dir, 0777, true);
0127 }
0128 
0129 $image->writeImage($save_path);
0130 
0131 
0132 // Parse Info / Get Extension
0133 $fsize = filesize($save_path);
0134 $path_parts = pathinfo($save_path);
0135 $ext = strtolower($path_parts["extension"]);
0136 
0137 // Determine Content Type
0138 switch ($ext) {
0139     case "pdf":
0140         $ctype = "application/pdf";
0141         break;
0142     case "exe":
0143         $ctype = "application/octet-stream";
0144         break;
0145     case "zip":
0146         $ctype = "application/zip";
0147         break;
0148     case "doc":
0149         $ctype = "application/msword";
0150         break;
0151     case "xls":
0152         $ctype = "application/vnd.ms-excel";
0153         break;
0154     case "ppt":
0155         $ctype = "application/vnd.ms-powerpoint";
0156         break;
0157     case "gif":
0158         $ctype = "image/gif";
0159         break;
0160     case "png":
0161         $ctype = "image/png";
0162         break;
0163     case "jpeg":
0164     case "jpg":
0165         $ctype = "image/jpg";
0166         break;
0167     default:
0168         $ctype = "text/html";
0169 }
0170 
0171 header("Content-Type: $ctype");
0172 ob_clean();
0173 flush();
0174 
0175 echo file_get_contents($save_path);
0176 
0177 return true;