Пример #1
0
        public void ImportImage(QwantImage image)
        {
            // import image into avcms_wallpapers table
            logger.Info("- импорт картинки в CMS");

            int isFeatured = this.featuredCount > featuredThreshold ? 1 : 0;

            string[] keywords   = image.keywords.Split(' ');
            int      categoryId = this.DetectCategory(keywords);

            Random rand1           = new Random();
            int    total_downloads = rand1.Next(100);
            int    likes           = rand1.Next(100);
            int    dislikes        = rand1.Next(10);
            int    hits            = rand1.Next(1000);

            int      daysAdded = rand1.Next(90);
            DateTime foo       = DateTime.UtcNow.AddDays(0 - daysAdded);
            long     unixTime  = ((DateTimeOffset)foo).ToUnixTimeSeconds();

            // import image into db
            avcms_wallpapers avcmsWallpaper = new avcms_wallpapers()
            {
                creator_id      = 1,
                name            = image.keywords,
                description     = image.title,
                file            = image.file,
                category_id     = categoryId,
                date_added      = unixTime,
                published       = true,
                publish_date    = unixTime,
                slug            = image.keywords.Replace(" ", "-"),
                resize_type     = "crop",
                crop_position   = "center",
                original_width  = image.width,
                original_height = image.height,
                featured        = isFeatured,
                total_downloads = total_downloads,
                likes           = likes,
                dislikes        = dislikes,
                hits            = hits
            };

            bxCtx.avcms_wallpapers.Add(avcmsWallpaper);
            bxCtx.SaveChanges();

            // fill image tags
            this.processTags(avcmsWallpaper.id, keywords);

            this.featuredCount = this.featuredCount <= featuredThreshold ? this.featuredCount + 1 : 0;
        }
        public static string ImageProcessorRotateAutoCrop(string imageFullPath, float angle, QwantImage image)
        {
            Logger logger = LogManager.GetCurrentClassLogger();

            string imageDir       = Path.GetDirectoryName(imageFullPath);
            string resultFileName = $"{imageDir}\\{Path.GetFileNameWithoutExtension(imageFullPath)}_result.jpg";

            try
            {
                using (ImageFactory imageFactory = new ImageFactory(preserveExifData: false))
                {
                    imageFactory
                    .Load(imageFullPath)
                    .Flip();

                    int w = imageFactory.Image.Width;
                    int h = imageFactory.Image.Height;

                    logger.Info($"- пост-обработка изображения, входное разрешение {w}x{h}");

                    int h1 = Convert.ToInt32(Math.Round(w * Math.Sin((angle / 180D) * Math.PI)));
                    int w1 = Convert.ToInt32(Math.Round(h * Math.Sin((angle / 180D) * Math.PI)));

                    imageFactory.Rotate(angle);

                    int newW = imageFactory.Image.Width;
                    int newH = imageFactory.Image.Height;

                    ImageProcessor.Imaging.CropLayer cropLayer = new ImageProcessor.Imaging.CropLayer(
                        w1, h1, newW - 2 * w1, newH - 2 * h1, ImageProcessor.Imaging.CropMode.Pixels);

                    imageFactory
                    .Crop(cropLayer)
                    .Save(resultFileName);

                    image.width  = imageFactory.Image.Width;
                    image.height = imageFactory.Image.Height;
                    logger.Info($"- пост-обработка завершена, выходное разрешение {imageFactory.Image.Width}x{imageFactory.Image.Height}");

                    return(resultFileName);
                }
            }
            catch (Exception ex)
            {
                logger.Error($"- ошибка пост-обработки: {ex.Message}");
            }

            return(string.Empty);
        }