Exemplo n.º 1
0
        async Task DoImageMagickCommandForGif(CommandContext ctx, byte[] buffer, ImageEditMode mode)
        {
            if (mode == ImageEditMode.Rescale)
            {
                await ctx.RespondAsync("이 모드는 속도가 느리고 이미지사이즈에 기반해서 느려지기 때문에 gif는 지원하지 않습니다.");

                return;
            }
            MagickImageCollection image;

            try
            {
                image = new MagickImageCollection(buffer);
            }
            catch (MagickMissingDelegateErrorException)
            {
                await ctx.RespondAsync("이미지 파일확장자를 알아볼 수 없다.");

                return;
            }
            int originalWidth = image[0].Width, originalHeight = image[0].Height;

            if (originalHeight * originalWidth > 1000000)
            {
                await ctx.RespondAsync($"Gif exceeds maximum size of 1000000 pixels (Actual size: {originalHeight * originalWidth})");

                return;
            }
            if (image.Count > 100)
            {
                await ctx.RespondAsync($"Gif exceeds maximum frame count of 100 pixels (Actual count: {image.Count})");

                return;
            }
            image.Coalesce();
            long rawLength;

            using (MemoryStream stream = new MemoryStream())
            {
                image.Write(stream);
                rawLength = stream.Length;
            }
            double exceed  = rawLength / 4194304d;
            double rescale = 1f;

            if (exceed > 1.0)
            {
                rescale = Math.Sqrt(exceed);
            }
            await ctx.TriggerTypingAsync();

            for (int i = 0; i < image.Count; i++)
            {
                IMagickImage frame = image[i];
                if (rescale > 1f)
                {
                    if (rescale > 2f)
                    {
                        frame.AdaptiveResize((int)(frame.Width / rescale), (int)(frame.Height / rescale));
                    }
                    else
                    {
                        frame.Resize((int)(frame.Width / rescale), (int)(frame.Height / rescale));
                    }
                }
                DoMagic(mode, frame, originalWidth, originalHeight);
            }
            await ctx.TriggerTypingAsync();

            image.OptimizeTransparency();
            using (Stream stream = new MemoryStream())
            {
                image.Write(stream);
                stream.Seek(0, SeekOrigin.Begin);
                await ctx.RespondWithFileAsync(stream, "magic.gif");
            }
        }