From 1e762d4d4614ba98ee26da3f1862c8bf0464eb49 Mon Sep 17 00:00:00 2001 From: brettp Date: Wed, 20 Jan 2010 20:08:11 +0000 Subject: Fixes #1445: get_resized_image_from_existing_file() has an optional parameter to allow upscaling if requested image is larger than original image. git-svn-id: http://code.elgg.org/elgg/trunk@3815 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/filestore.php | 90 +++++++++++++++++++++++++++++------------------- 1 file changed, 55 insertions(+), 35 deletions(-) (limited to 'engine/lib/filestore.php') diff --git a/engine/lib/filestore.php b/engine/lib/filestore.php index 5a199c944..9e114c262 100644 --- a/engine/lib/filestore.php +++ b/engine/lib/filestore.php @@ -324,10 +324,10 @@ class ElggDiskFilestore extends ElggFilestore { if (is_numeric($identifier)) { return $this->user_file_matrix($identifier); } - + return $this->deprecated_file_matrix($identifier); } - + /** * Construct the filename matrix with user info * @@ -345,12 +345,12 @@ class ElggDiskFilestore extends ElggFilestore { // only to be used for user directories return FALSE; } - + if (!$user->time_created) { // fall back to deprecated method return $this->deprecated_file_matrix($user->username); } - + $time_created = date('Y/m/d', $user->time_created); return "$time_created/$user->guid/"; } @@ -371,7 +371,7 @@ class ElggDiskFilestore extends ElggFilestore { $error = 'Deprecated use of ElggDiskFilestore::make_file_matrix. '; $error .= 'Username passed instead of guid.'; elgg_log($error, WARNING); - + $user = new ElggUser($filename); return $this->user_file_matrix($user->guid); } @@ -767,28 +767,29 @@ function get_resized_image_from_uploaded_file($input_name, $maxwidth, $maxheight * @param string $input_name The name of the file on the disk * @param int $maxwidth The desired width of the resized image * @param int $maxheight The desired height of the resized image - * @param true|false $square If set to true, takes the smallest of maxwidth and + * @param true|false $square If set to true, takes the smallest of maxwidth and * maxheight and use it to set the dimensions on the new image. If no * crop parameters are set, the largest square that fits in the image - * centered will be used for the resize. If square, the crop must be a + * centered will be used for the resize. If square, the crop must be a * square region. * @param int $x1 x coordinate for top, left corner * @param int $y1 y coordinate for top, left corner * @param int $x2 x coordinate for bottom, right corner * @param int $y2 y coordinate for bottom, right corner + * @param bool $upscale Resize images smaller than $maxwidth x $maxheight? * @return false|mixed The contents of the resized image, or false on failure */ -function get_resized_image_from_existing_file($input_name, $maxwidth, $maxheight, $square = FALSE, $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0) { +function get_resized_image_from_existing_file($input_name, $maxwidth, $maxheight, $square = FALSE, $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0, $upscale = FALSE) { // Get the size information from the image $imgsizearray = getimagesize($input_name); if ($imgsizearray == FALSE) { return FALSE; } - + // Get width and height $width = $imgsizearray[0]; $height = $imgsizearray[1]; - + // make sure we can read the image $accepted_formats = array( 'image/jpeg' => 'jpeg', @@ -797,7 +798,7 @@ function get_resized_image_from_existing_file($input_name, $maxwidth, $maxheight 'image/x-png' => 'png', 'image/gif' => 'gif' ); - + // make sure the function is available $load_function = "imagecreatefrom" . $accepted_formats[$imgsizearray['mime']]; if (!is_callable($load_function)) { @@ -809,7 +810,7 @@ function get_resized_image_from_existing_file($input_name, $maxwidth, $maxheight if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 == 0) { $crop = FALSE; } - + // how large a section of the image has been selected if ($crop) { $region_width = $x2 - $x1; @@ -819,28 +820,28 @@ function get_resized_image_from_existing_file($input_name, $maxwidth, $maxheight $region_width = $width; $region_height = $height; } - - // determine cropping offsets + + // determine cropping offsets if ($square) { // asking for a square image back - + // detect case where someone is passing crop parameters that are not for a square if ($crop == TRUE && $region_width != $region_height) { return FALSE; } - + // size of the new square image $new_width = $new_height = min($maxwidth, $maxheight); - + // find largest square that fits within the selected region $region_width = $region_height = min($region_width, $region_height); - + // set offsets for crop if ($crop) { $widthoffset = $x1; $heightoffset = $y1; $width = $x2 - $x1; - $height = $width; + $height = $width; } else { // place square region in the center $widthoffset = floor(($width - $region_width) / 2); @@ -851,25 +852,44 @@ function get_resized_image_from_existing_file($input_name, $maxwidth, $maxheight $new_width = $maxwidth; $new_height = $maxwidth; - + // maintain aspect ratio of original image/crop if (($region_height / (float)$new_height) > ($region_width / (float)$new_width)) { $new_width = floor($new_height * $region_width / (float)$region_height); } else { $new_height = floor($new_width * $region_height / (float)$region_width); } - + // by default, use entire image $widthoffset = 0; $heightoffset = 0; - + if ($crop) { $widthoffset = $x1; $heightoffset = $y1; } } - - + + // check for upscaling + // @todo This ignores squares, coordinates, and cropping. It's probably not the best idea. + // Size checking should be done in action code, but for backward compatibility + // this duplicates the previous behavior. + if (!$upscale && ($height < $new_height || $width < $new_width)) { + // determine if we can scale it down at all + // (ie, if only one dimension is too small) + // if not, just use original size. + if ($height < $new_height && $width < $new_width) { + $ratio = 1; + } elseif ($height < $new_height) { + $ratio = $new_width / $width; + } elseif ($width < $new_width) { + $ratio = $new_height / $height; + } + + $new_height = floor($height * $ratio); + $new_width = floor($width * $ratio); + } + // load original image $orig_image = $load_function($input_name); if (!$orig_image) { @@ -881,10 +901,10 @@ function get_resized_image_from_existing_file($input_name, $maxwidth, $maxheight if (!$newimage) { return FALSE; } - + // create the new image - $rtn_code = imagecopyresampled( $newimage, - $orig_image, + $rtn_code = imagecopyresampled( $newimage, + $orig_image, 0, 0, $widthoffset, @@ -896,17 +916,17 @@ function get_resized_image_from_existing_file($input_name, $maxwidth, $maxheight if (!$rtn_code) { return FALSE; } - + // grab contents for return ob_start(); imagejpeg($newimage, null, 90); $jpeg = ob_get_clean(); - + imagedestroy($newimage); imagedestroy($orig_image); - + return $jpeg; -} +} // putting these here for now @@ -1358,13 +1378,13 @@ function delete_directory($directory) { if (!$handle = opendir($directory)) { return FALSE; } - + // loop through all files while (($file = readdir($handle)) !== FALSE) { if (in_array($file, array('.', '..'))) { continue; } - + $path = "$directory/$file"; if (is_dir($path)) { // recurse down through directory @@ -1376,7 +1396,7 @@ function delete_directory($directory) { unlink($path); } } - + // remove empty directory closedir($handle); return rmdir($directory); @@ -1390,7 +1410,7 @@ function delete_directory($directory) { */ function clear_user_files($user) { global $CONFIG; - + $time_created = date('Y/m/d', $user->time_created); $file_path = "$CONFIG->dataroot$time_created/$user->guid"; if (file_exists($file_path)) { -- cgit v1.2.3