From 81cb128025f2b5527ac83498a97847aca8309267 Mon Sep 17 00:00:00 2001 From: Cash Costello Date: Sat, 6 Jun 2009 22:25:14 +0000 Subject: added PHP ImageMagick resizing - needs more testing and determining how much memory it needs --- actions/upload.php | 11 +- lib/resize.php | 301 ++++++++++++++++++++++++++++++--- views/default/tidypics/forms/admin.php | 4 +- 3 files changed, 288 insertions(+), 28 deletions(-) diff --git a/actions/upload.php b/actions/upload.php index 11290a1d8..8f84ea929 100644 --- a/actions/upload.php +++ b/actions/upload.php @@ -99,16 +99,15 @@ trigger_error('Tidypics warning: image memory size too large for resizing so rejecting', E_USER_WARNING); continue; } - } else if ($image_lib === 'ImageMagick') { // this will be for PHP ImageMagick -/* + } else if ($image_lib === 'ImageMagickPHP') { $mem_required = 5 * $imginfo[0] * $imginfo[1]; $mem_avail = $mem_avail - memory_get_peak_usage() - 4194304; // 4 MB buffer if ($mem_required > $mem_avail) { array_push($not_uploaded, $sent_file['name']); + array_push($error_msgs, elgg_echo('tidypics:image_pixels')); trigger_error('Tidypics warning: image memory size too large for resizing so rejecting', E_USER_WARNING); continue; } -*/ } //this will save to users folder in /image/ and organize by photo album @@ -141,12 +140,12 @@ trigger_error('Tidypics warning: failed to create thumbnails', E_USER_WARNING); } - } else if ($image_lib === 'ToDo:ImageMagick') { // ImageMagick PHP -/* + } else if ($image_lib === 'ImageMagickPHP') { // ImageMagick PHP + if (tp_create_imagick_thumbnails($file, $prefix, $filestorename) != true) { trigger_error('Tidypics warning: failed to create thumbnails', E_USER_WARNING); } -*/ + } else { // ImageMagick command line $thumbs = tp_create_imagick_cmdline_thumbnails($file, $prefix, $filestorename); diff --git a/lib/resize.php b/lib/resize.php index 186900f7d..19af7b451 100644 --- a/lib/resize.php +++ b/lib/resize.php @@ -27,10 +27,10 @@ $image_sizes = unserialize($image_sizes); // Generate thumbnails - $thumbnail = get_resized_image_from_existing_file( $file->getFilenameOnFilestore(), - $image_sizes['thumb_image_width'], - $image_sizes['thumb_image_height'], - true); + $thumbnail = tp_gd_resize( $file->getFilenameOnFilestore(), + $image_sizes['thumb_image_width'], + $image_sizes['thumb_image_height'], + true); if ($thumbnail) { $thumb = new ElggFile(); @@ -47,10 +47,10 @@ } unset($thumbnail); - $thumbsmall = get_resized_image_from_existing_file( $file->getFilenameOnFilestore(), - $image_sizes['small_image_width'], - $image_sizes['small_image_height'], - true); + $thumbsmall = tp_gd_resize( $file->getFilenameOnFilestore(), + $image_sizes['small_image_width'], + $image_sizes['small_image_height'], + true); if ($thumbsmall) { @@ -68,10 +68,10 @@ } unset($thumbsmall); - $thumblarge = get_resized_image_from_existing_file( $file->getFilenameOnFilestore(), - $image_sizes['large_image_width'], - $image_sizes['large_image_height'], - false); + $thumblarge = tp_gd_resize( $file->getFilenameOnFilestore(), + $image_sizes['large_image_width'], + $image_sizes['large_image_height'], + false); if ($thumblarge) { $thumb = new ElggFile(); @@ -91,6 +91,108 @@ return true; } + /** + * Gets the jpeg contents of the resized version of an already uploaded image - original from Elgg filestore.php + * (Returns false if the uploaded file was not an image) + * + * @param string $input_name The name of the file input field on the submission form + * @param int $maxwidth The maximum width of the resized image + * @param int $maxheight The maximum height of the resized image + * @param true|false $square If set to true, will take the smallest of maxwidth and maxheight and use it to set the dimensions on all size; the image will be cropped. + * @return false|mixed The contents of the resized image, or false on failure + */ + function tp_gd_resize($input_name, $maxwidth, $maxheight, $square = false, $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0) { + + // Get the size information from the image + if ($imgsizearray = getimagesize($input_name)) { + + // Get width and height + $width = $imgsizearray[0]; + $height = $imgsizearray[1]; + $newwidth = $width; + $newheight = $height; + + // Square the image dimensions if we're wanting a square image + if ($square) { + if ($width < $height) { + $height = $width; + } else { + $width = $height; + } + + $newwidth = $width; + $newheight = $height; + + } + + if ($width > $maxwidth) { + $newheight = floor($height * ($maxwidth / $width)); + $newwidth = $maxwidth; + } + if ($newheight > $maxheight) { + $newwidth = floor($newwidth * ($maxheight / $newheight)); + $newheight = $maxheight; + } + + $accepted_formats = array( + 'image/jpeg' => 'jpeg', + 'image/png' => 'png', + 'image/gif' => 'gif' + ); + + // If it's a file we can manipulate ... + if (array_key_exists($imgsizearray['mime'],$accepted_formats)) { + + $function = "imagecreatefrom" . $accepted_formats[$imgsizearray['mime']]; + $newimage = imagecreatetruecolor($newwidth,$newheight); + + if (is_callable($function) && $oldimage = $function($input_name)) { + + // Crop the image if we need a square + if ($square) { + if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 ==0) { + $widthoffset = floor(($imgsizearray[0] - $width) / 2); + $heightoffset = floor(($imgsizearray[1] - $height) / 2); + } else { + $widthoffset = $x1; + $heightoffset = $y1; + $width = ($x2 - $x1); + $height = $width; + } + } else { + if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 ==0) { + $widthoffset = 0; + $heightoffset = 0; + } else { + $widthoffset = $x1; + $heightoffset = $y1; + $width = ($x2 - $x1); + $height = ($y2 - $y1); + } + } + + if ($square) { + $newheight = $maxheight; + $newwidth = $maxwidth; + } + + imagecopyresampled($newimage, $oldimage, 0,0,$widthoffset,$heightoffset,$newwidth,$newheight,$width,$height); + + ob_start(); + imagejpeg($newimage, null, 90); + $jpeg = ob_get_clean(); + return $jpeg; + + } + + } + + } + + return false; + } + + /** * Create thumbnails using PHP ImageMagick Library * @@ -101,13 +203,172 @@ */ function tp_create_imagick_thumbnails($file, $prefix, $filestorename) { - global $CONFIG; + $image_sizes = get_plugin_setting('image_sizes', 'tidypics'); + if (!$image_sizes) { + register_error(elgg_echo('tidypics:nosettings')); + return false; + } + $image_sizes = unserialize($image_sizes); + + $thumb = new ElggFile(); + + + // tiny thumbnail + $thumb->setFilename($prefix."thumb".$filestorename); + $thumbname = $thumb->getFilenameOnFilestore(); + $rtn_code = tp_imagick_resize( $file->getFilenameOnFilestore(), + $thumbname, + $image_sizes['thumb_image_width'], + $image_sizes['thumb_image_height'], + true); + if (!$rtn_code) + return false; + $file->thumbnail = $prefix."thumb".$filestorename; + + + // album thumbnail + $thumb->setFilename($prefix."smallthumb".$filestorename); + $thumbname = $thumb->getFilenameOnFilestore(); + $rtn_code = tp_imagick_resize( $file->getFilenameOnFilestore(), + $thumbname, + $image_sizes['small_image_width'], + $image_sizes['small_image_height'], + true); + if (!$rtn_code) + return false; + $file->smallthumb = $prefix."smallthumb".$filestorename; + + + // main image + $thumb->setFilename($prefix."largethumb".$filestorename); + $thumbname = $thumb->getFilenameOnFilestore(); + $rtn_code = tp_imagick_resize( $file->getFilenameOnFilestore(), + $thumbname, + $image_sizes['large_image_width'], + $image_sizes['large_image_height'], + true); + if (!$rtn_code) + return false; + $file->largethumb = $prefix."largethumb".$filestorename; + + unset($thumb); + + return true; + } + + + /** + * Resize using PHP ImageMagick Library + * + * Gets the jpeg contents of the resized version of an already uploaded image + * (Returns false if the uploaded file was not an image) + * + * @param string $input_name The name of the file input field on the submission form + * @param string $output_name The name of the file to be written + * @param int $maxwidth The maximum width of the resized image + * @param int $maxheight The maximum height of the resized image + * @param true|false $square If set to true, will take the smallest of maxwidth and maxheight and use it to set the dimensions on all size; the image will be cropped. + * @return false|mixed The contents of the resized image, or false on failure + */ + function tp_imagick_resize($input_name, $output_name, $maxwidth, $maxheight, $square = false, $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0) { - $mime = $file->getMimeType(); + // Get the size information from the image + $imgsizearray = getimagesize($input_name); + if (!$imgsizearray) + return false; - return false; // not implemented yet - } + $accepted_formats = array( + 'image/jpeg' => 'jpeg', + 'image/png' => 'png', + 'image/gif' => 'gif' + ); + + // test if it's a file we can manipulate ... + if (!array_key_exists($imgsizearray['mime'],$accepted_formats)) + return false; + // Get width and height + $width = $imgsizearray[0]; + $height = $imgsizearray[1]; + $newwidth = $width; + $newheight = $height; + + // initial guess at final dimensions for new image (doesn't check for squareness yet + if ($newwidth > $maxwidth) { + $newheight = floor($newheight * ($maxwidth / $newwidth)); + $newwidth = $maxwidth; + } + if ($newheight > $maxheight) { + $newwidth = floor($newwidth * ($maxheight / $newheight)); + $newheight = $maxheight; + } + + // Handle squareness for both original and new image + if ($square) { + if ($width < $height) { + $height = $width; + } else { + $width = $height; + } + + // if input arguments = square, no need to use above calculations (which can have round-off errors) + if ($maxheight == $maxwidth) { + $newwidth = $maxwidth; + $newheight = $maxheight; + } else { + if ($newwidth < $newheight) { + $newheight = $newwidth; + } else { + $newwidth = $newheight; + } + } + } + + + // Crop the original image - this needs to be checked over + if ($square) { + if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 ==0) { + $xoffset = floor(($imgsizearray[0] - $width) / 2); + $yoffset = floor(($imgsizearray[1] - $height) / 2); + } else { // assume we're being passed good croping coordinates + $xoffset = $x1; + $yoffset = $y1; + $width = ($x2 - $x1); + $height = $width; + } + } else { + if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 ==0) { + $xoffset = 0; + $yoffset = 0; + } else { + $xoffset = $x1; + $yoffset = $y1; + $width = ($x2 - $x1); + $height = ($y2 - $y1); + } + } + + + try { + $img = new Imagick($input_name); + } catch (ImagickException $e) { + return false; + } + + $img->cropImage($width, $height, $xoffset, $yoffset); + + // use the default IM filter (windowing filter), I think 1 means default blurring or number of lobes + $img->resizeImage($newwidth, $newheight, imagick::FILTER_LANCZOS, 1); + + if ($img->writeImage($output_name) != true) { + $img->destroy(); + return false; + } + + $img->destroy(); + + return true; + } /** * Create thumbnails using ImageMagick executables @@ -130,19 +391,19 @@ } $image_sizes = unserialize($image_sizes); - $thumblarge = tp_imagick_resize($file->getFilenameOnFilestore(), + $thumblarge = tp_imagick_cmdline_resize($file->getFilenameOnFilestore(), "largethumb", $image_sizes['large_image_width'], $image_sizes['large_image_height'], false); - $thumbsmall = tp_imagick_resize($file->getFilenameOnFilestore(), + $thumbsmall = tp_imagick_cmdline_resize($file->getFilenameOnFilestore(), "smallthumb", $image_sizes['small_image_width'], $image_sizes['small_image_height'], true); - $thumbnail = tp_imagick_resize($file->getFilenameOnFilestore(), + $thumbnail = tp_imagick_cmdline_resize($file->getFilenameOnFilestore(), "thumb", $image_sizes['thumb_image_width'], $image_sizes['thumb_image_height'], @@ -185,7 +446,7 @@ * @param true|false $square If set to true, will take the smallest of maxwidth and maxheight and use it to set the dimensions on all size; the image will be cropped. * @return false|mixed The contents of the resized image, or false on failure */ - function tp_imagick_resize($input_name, $prefix, $maxwidth, $maxheight, $square = false, $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0) { + function tp_imagick_cmdline_resize($input_name, $prefix, $maxwidth, $maxheight, $square = false, $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0) { $params = array( "input_name"=>$input_name, diff --git a/views/default/tidypics/forms/admin.php b/views/default/tidypics/forms/admin.php index d4c9e1b5b..af84def00 100644 --- a/views/default/tidypics/forms/admin.php +++ b/views/default/tidypics/forms/admin.php @@ -22,8 +22,8 @@ 'internalname' => 'params[image_lib]', 'options_values' => array( 'GD' => 'GD', - 'ImageMagick' => 'ImageMagick2', - 'ImageMagick Cmdline' => 'ImageMagick', + 'ImageMagickPHP' => 'ImageMagick', + 'ImageMagick' => 'ImageMagick Cmdline', ), 'value' => $image_lib )); -- cgit v1.2.3