Пример #1
0
        private IFile MakeThumb(ManagerEngine man, IFile file)
        {
            ManagerConfig config = file.Config;
            IFile         thumbFile;
            int           thumbWidth, thumbHeight;
            int           configWidth, configHeight;
            double        scale;
            MediaInfo     thumbSize = new MediaInfo();

            // Is not enabled
            if (!config.GetBool("thumbnail.enabled", true))
            {
                return(file);
            }

            // Setup thumbnail path
            IFile thumbDir = man.GetFile(file.Parent, config.Get("thumbnail.folder", "mcith"));

            if (!thumbDir.Exists)
            {
                thumbDir.MkDir();
            }

            configWidth  = config.GetInt("thumbnail.width", 90);
            configHeight = config.GetInt("thumbnail.height", 90);

            // Make thumbnail
            thumbFile = man.GetFile(thumbDir.AbsolutePath, config.Get("thumbnail.prefix", "mcith") + file.Name);
            MediaInfo imageSize = new MediaInfo(file.AbsolutePath);

            // Need to scale?
            if (imageSize.Width < configWidth && imageSize.Height < configHeight)
            {
                return(file);
            }

            // To large
            if (imageSize.Width > config.GetInt("thumbnail.max_width", 65535) || imageSize.Height > config.GetInt("thumbnail.max_height", 65535))
            {
                return(file);
            }

            // Constrain proportions
            scale = Math.Min(configWidth / (double)imageSize.Width, configHeight / (double)imageSize.Height);

            if (config.Get("thumbnail.scale_mode", "percentage") == "percentage")
            {
                thumbWidth  = scale > 1 ? imageSize.Width : (int)Math.Floor(imageSize.Width * scale);
                thumbHeight = scale > 1 ? imageSize.Height : (int)Math.Floor(imageSize.Height * scale);
            }
            else
            {
                thumbWidth  = config.GetInt("thumbnail.width", 90);
                thumbHeight = config.GetInt("thumbnail.height", 90);
            }

            if (thumbFile.Exists)
            {
                thumbSize = new MediaInfo(thumbFile.AbsolutePath);
            }

            if (!thumbFile.Exists || thumbSize.Width != thumbWidth || thumbSize.Height != thumbHeight || file.LastModified != thumbFile.LastModified)
            {
                ImageUtils.MakeThumbnail(file.AbsolutePath, thumbFile.AbsolutePath, thumbWidth, thumbHeight, file.LastModified, config.GetInt("thumbnail.jpeg_quality", 75));
            }

            return(thumbFile);
        }