/** * 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 bool */ function tp_im_cmdline_resize($input_name, $output_name, $maxwidth, $maxheight, $square = FALSE, $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0) { // Get the size information from the image $imgsizearray = getimagesize($input_name); if (!$imgsizearray) { return FALSE; } // Get width and height $orig_width = $imgsizearray[0]; $orig_height = $imgsizearray[1]; $params = tp_im_calc_resize_params($orig_width, $orig_height, $maxwidth, $maxheight, $square, $x1, $y1, $x2, $y2); if (!$params) { return FALSE; } $newwidth = $params['new_width']; $newheight = $params['new_height']; $accepted_formats = array('image/jpeg' => 'jpeg', 'image/pjpeg' => 'jpeg', 'image/png' => 'png', 'image/x-png' => 'png', 'image/gif' => 'gif'); // If it's a file we can manipulate ... if (!array_key_exists($imgsizearray['mime'], $accepted_formats)) { return FALSE; } $im_path = elgg_get_plugin_setting('im_path', 'tidypics'); if (!$im_path) { $im_path = "/usr/bin/"; } if (substr($im_path, strlen($im_path) - 1, 1) != "/") { $im_path .= "/"; } // see imagemagick web site for explanation of these parameters // the ^ in the resize means those are minimum width and height values $command = $im_path . "convert \"{$input_name}\" -resize " . $newwidth . "x" . $newheight . "^ -gravity center -extent " . $newwidth . "x" . $newheight . " \"{$output_name}\""; $output = array(); $ret = 0; exec($command, $output, $ret); if ($ret == 127) { trigger_error('Tidypics warning: Image Magick convert is not found', E_USER_WARNING); return FALSE; } else { if ($ret > 0) { trigger_error('Tidypics warning: Image Magick convert failed', E_USER_WARNING); return FALSE; } } return TRUE; }
/** * 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 bool */ function tp_im_cmdline_resize($input_name, $output_name, $maxwidth, $maxheight, $square = false, $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0) { // Get the size information from the image $imgsizearray = getimagesize($input_name); if (!$imgsizearray) { return false; } // Get width and height $orig_width = $imgsizearray[0]; $orig_height = $imgsizearray[1]; $params = tp_im_calc_resize_params($orig_width, $orig_height, $maxwidth, $maxheight, $square, $x1, $y1, $x2, $y2); if (!$params) { return false; } $newwidth = $params['new_width']; $newheight = $params['new_height']; $accepted_formats = array('image/jpeg' => 'jpeg', 'image/pjpeg' => 'jpeg', 'image/png' => 'png', 'image/x-png' => 'png', 'image/gif' => 'gif'); // If it's a file we can manipulate ... if (!array_key_exists($imgsizearray['mime'], $accepted_formats)) { return false; } $im_path = elgg_get_plugin_setting('im_path', 'tidypics'); if (!$im_path) { $im_path = "/usr/bin/"; } if (substr($im_path, strlen($im_path) - 1, 1) != "/") { $im_path .= "/"; } $gif_to_convert = $imgsizearray['mime'] == 'image/gif' ? '-coalesce' : ''; // see imagemagick web site for explanation of these parameters // the ^ in the resize means those are minimum width and height values $thumbnail_optimized = elgg_get_plugin_setting('thumbnail_optimization', 'tidypics'); switch ($thumbnail_optimized) { case "complex": $command = $im_path . "convert \"{$input_name}\" " . $gif_to_convert . " -filter Triangle -define filter:support=2 -thumbnail " . $newwidth . "x" . $newheight . "^ -gravity center -extent " . $newwidth . "x" . $newheight . " -unsharp 0.25x0.25+8+0.065 -dither None -posterize 136 -quality 82 -define jpeg:fancy-upsampling=off -define png:compression-filter=5 -define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude-chunk=all -interlace none -colorspace sRGB -strip \"{$output_name}\""; break; case "simple": $command = $im_path . "convert \"{$input_name}\" " . $gif_to_convert . " -quality 87 -filter Lanczos -thumbnail " . $newwidth . "x" . $newheight . "^ -gravity center -extent " . $newwidth . "x" . $newheight . " \"{$output_name}\""; break; default: $command = $im_path . "convert \"{$input_name}\" " . $gif_to_convert . " -resize " . $newwidth . "x" . $newheight . "^ -gravity center -extent " . $newwidth . "x" . $newheight . " \"{$output_name}\""; } $output = array(); $ret = 0; exec($command, $output, $ret); if ($ret == 127) { trigger_error('Tidypics warning: Image Magick convert is not found', E_USER_WARNING); return false; } else { if ($ret > 0) { trigger_error('Tidypics warning: Image Magick convert failed', E_USER_WARNING); return false; } } return true; }