/** * Rotate an image. Valid options are degrees * * @param string $input_file * @param string $output_file * @param array $options */ static function rotate($input_file, $output_file, $options) { graphics::init_toolkit(); module::event("graphics_rotate", $input_file, $output_file, $options); // BEGIN mod to original function $image_info = getimagesize($input_file); // [0]=w, [1]=h, [2]=type (1=GIF, 2=JPG, 3=PNG) if (module::get_var("image_optimizer", "rotate_jpg") || $image_info[2] == 2) { // rotate_jpg enabled, the file is a jpg. get args $path = module::get_var("image_optimizer", "path_jpg"); $exec_args = " -rotate "; $exec_args .= $options["degrees"] > 0 ? $options["degrees"] : $options["degrees"] + 360; $exec_args .= " -copy all -optimize -outfile "; // run it - from input_file to tmp_file $tmp_file = image_optimizer::make_temp_name($output_file); exec(escapeshellcmd($path) . $exec_args . escapeshellarg($tmp_file) . " " . escapeshellarg($input_file), $exec_output, $exec_status); if ($exec_status || !filesize($tmp_file)) { // either a blank/nonexistant file or an error - log an error and pass to normal function Kohana_Log::add("error", "image_optimizer rotation failed on " . $output_file); unlink($tmp_file); } else { // worked - move temp to output rename($tmp_file, $output_file); $status = true; } } if (!$status) { // we got here if we weren't supposed to use jpegtran or if jpegtran failed // END mod to original function Image::factory($input_file)->quality(module::get_var("gallery", "image_quality"))->rotate($options["degrees"])->save($output_file); // BEGIN mod to original function } // END mod to original function module::event("graphics_rotate_completed", $input_file, $output_file, $options); }
/** * Crop the input image so that it matches the aspect ratio specified in the * rectangle_thumbs.aspect_ratio module setting. Focus on the center of the image and crop out * the biggest piece that we can. * * @param string $input_file * @param string $output_file * @param array $options */ static function crop_to_aspect_ratio($input_file, $output_file, $options) { graphics::init_toolkit(); if (@filesize($input_file) == 0) { throw new Exception("@todo EMPTY_INPUT_FILE"); } list($desired_width, $desired_height) = explode(":", module::get_var("rectangle_thumbs", "aspect_ratio")); $desired_ratio = $desired_width / $desired_height; // Crop the largest rectangular section we can out of the original image. Start with a // rectangular section that's guaranteed to be too large, then shrink it horizontally to just // barely fit. If it's still too tall vertically, shrink both dimensions proportionally until // the horizontal edge fits as well. $dims = getimagesize($input_file); if ($desired_ratio == 1) { $new_width = $new_height = min($dims[0], $dims[1]); } else { if ($desired_ratio < 1) { list($new_width, $new_height) = array($dims[0], $dims[0] / $desired_ratio); } else { list($new_width, $new_height) = array($dims[1] * $desired_ratio, $dims[1]); } } if ($new_width > $dims[0]) { // Too wide, scale it down list($new_width, $new_height) = array($dims[0], $dims[0] / $desired_ratio); } if ($new_height > $dims[1]) { // Too tall, scale it down some more $new_width = min($dims[0], $dims[1] * $desired_ratio); $new_height = $new_width / $desired_ratio; } $new_width = round($new_width); $new_height = round($new_height); Image::factory($input_file)->crop($new_width, $new_height)->quality(module::get_var("gallery", "image_quality"))->save($output_file); }
/** * Overlay an image on top of the input file. * * Valid options are: file, mime_type, position, transparency_percent, padding * * Valid positions: northwest, north, northeast, * west, center, east, * southwest, south, southeast * * padding is in pixels * * @param string $input_file * @param string $output_file * @param array $options */ static function composite($input_file, $output_file, $options) { try { graphics::init_toolkit(); module::event("graphics_composite", $input_file, $output_file, $options); list($width, $height) = getimagesize($input_file); list($w_width, $w_height) = getimagesize($options["file"]); $pad = isset($options["padding"]) ? $options["padding"] : 10; $top = $pad; $left = $pad; $y_center = max($height / 2 - $w_height / 2, $pad); $x_center = max($width / 2 - $w_width / 2, $pad); $bottom = max($height - $w_height - $pad, $pad); $right = max($width - $w_width - $pad, $pad); switch ($options["position"]) { case "northwest": $x = $left; $y = $top; break; case "north": $x = $x_center; $y = $top; break; case "northeast": $x = $right; $y = $top; break; case "west": $x = $left; $y = $y_center; break; case "center": $x = $x_center; $y = $y_center; break; case "east": $x = $right; $y = $y_center; break; case "southwest": $x = $left; $y = $bottom; break; case "south": $x = $x_center; $y = $bottom; break; case "southeast": $x = $right; $y = $bottom; break; } Image::factory($input_file)->composite($options["file"], $x, $y, $options["transparency"])->quality(module::get_var("gallery", "image_quality"))->save($output_file); module::event("graphics_composite_completed", $input_file, $output_file, $options); } catch (ErrorException $e) { Kohana::log("error", $e->get_message()); } }
/** * Crop the input image so that it's square. Focus on the center of the image. * * @param string $input_file * @param string $output_file * @param array $options */ static function crop_to_square($input_file, $output_file, $options) { graphics::init_toolkit(); if (@filesize($input_file) == 0) { throw new Exception("@todo EMPTY_INPUT_FILE"); } $size = module::get_var("gallery", "thumb_size"); $dims = getimagesize($input_file); Image::factory($input_file)->crop(min($dims[0], $dims[1]), min($dims[0], $dims[1]))->quality(module::get_var("gallery", "image_quality"))->save($output_file); }
/** * Overlay an image on top of the input file. * * Valid options are: file, position, transparency, padding * * Valid positions: northwest, north, northeast, * west, center, east, * southwest, south, southeast * * padding is in pixels * * @param string $input_file * @param string $output_file * @param array $options * @param Item_Model $item (optional) */ static function composite($input_file, $output_file, $options, $item = null) { try { graphics::init_toolkit(); $temp_file = system::temp_filename("composite_", pathinfo($output_file, PATHINFO_EXTENSION)); module::event("graphics_composite", $input_file, $temp_file, $options, $item); if (@filesize($temp_file) > 0) { // A graphics_composite event made an image - move it to output_file and use it. @rename($temp_file, $output_file); } else { // No events made an image - proceed with standard process. list($width, $height) = photo::get_file_metadata($input_file); list($w_width, $w_height) = photo::get_file_metadata($options["file"]); $pad = isset($options["padding"]) ? $options["padding"] : 10; $top = $pad; $left = $pad; $y_center = max($height / 2 - $w_height / 2, $pad); $x_center = max($width / 2 - $w_width / 2, $pad); $bottom = max($height - $w_height - $pad, $pad); $right = max($width - $w_width - $pad, $pad); switch ($options["position"]) { case "northwest": $x = $left; $y = $top; break; case "north": $x = $x_center; $y = $top; break; case "northeast": $x = $right; $y = $top; break; case "west": $x = $left; $y = $y_center; break; case "center": $x = $x_center; $y = $y_center; break; case "east": $x = $right; $y = $y_center; break; case "southwest": $x = $left; $y = $bottom; break; case "south": $x = $x_center; $y = $bottom; break; case "southeast": $x = $right; $y = $bottom; break; } Image::factory($input_file)->composite($options["file"], $x, $y, $options["transparency"])->quality(module::get_var("gallery", "image_quality"))->save($output_file); } module::event("graphics_composite_completed", $input_file, $output_file, $options, $item); } catch (ErrorException $e) { // Unlike rotate and resize, composite catches its exceptions here. This is because // composite is typically called for watermarks. If during thumb/resize generation // the watermark fails, we'd still like the image resized, just without its watermark. // If the exception isn't caught here, graphics::generate will replace it with a // placeholder. Kohana_Log::add("error", $e->getMessage()); } }