/** * Gets an image by id sized and rotated based on the passed params. If the file * doesn't exist, it is created using the imager class and stored in cache dir. Every * call after that will load the file directly. * * When sizing an image with the same width and height, it will be scaled down to the closest * width/height and then cropped. * * To size an image to a specific width, set the width value needed and the height value to 0. * This ensures the specified width is met, and the height is adjusted proportionally. The same * can be done by setting the height needed and the width to 0. * * @param $id int The id of image to get * @param $rotation int The degrees to rotate the image, can be null * @param $widthOrPercent int When by itself, represents percent to increase/decrease size. * When used with the height param, represents the image width. * @param $height int The height to size the image. * * @return string The relative path from ROOT to the file, if it exists. Boolean false otherwise. */ public function render($id, $rotation = null, $widthOrPercent = null, $height = null) { try { if ($image = Media::m('file')->find($id)) { $ext = pathinfo($image->name, PATHINFO_EXTENSION); $cachedFilename = str_replace('.' . $ext, sprintf('_%d.%s', $id . $rotation . $widthOrPercent . $height, $ext), $image->name); $cachedFullPath = ROOT . Media::getFilesPath() . Media::getCachePath() . $cachedFilename; if (!file_exists($cachedFullPath)) { $origFullPath = ROOT . Media::getFilesPath() . $image->path . $image->name; Media_Imager::open($origFullPath); if (!is_null($widthOrPercent) && is_null($height)) { Media_Imager::percent($widthOrPercent); } elseif (!is_null($widthOrPercent) && !is_null($height)) { $adaptive = $widthOrPercent == 0 || $height == 0 ? false : true; Media_Imager::resize($widthOrPercent, $height, $adaptive); } if (!is_null($rotation) && $rotation > 0) { Media_Imager::rotate($rotation); } Media_Imager::save($cachedFullPath); } return Media::getFilesPath() . Media::getCachePath() . $cachedFilename; } } catch (Exception $e) { Log::error('media', $e->getMessage()); } return false; }
/** * TODO Comments */ public static function open($path) { Load::asset('media', 'phpthumb/ThumbLib.inc.php'); self::$_thumb = PhpThumbFactory::create($path, array('resizeUp' => true)); }