示例#1
0
        static bool ImageCopyAndResize(PhpResource dst_im, PhpResource src_im,
                                       int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h,
                                       int src_w, int src_h, IResampler resampler)
        {
            var dst_img = PhpGdImageResource.ValidImage(dst_im);
            var src_img = PhpGdImageResource.ValidImage(src_im);

            if (dst_img == null || src_img == null)
            {
                return(false);
            }

            //if (src_w == 0 && src_h == 0)
            //    return true;

            if (dst_w == 0 || dst_h == 0)
            {
                return(true);
            }

            //if (dst_w < 0) dst_w = 0;
            //if (dst_h < 0) dst_h = 0;

            var src = src_img.Image
                      .Crop(new Rectangle(src_x, src_y, src_w, src_h))
                      .Resize(dst_w, dst_h, resampler);

            dst_img.Image.DrawImage(src, new Size(dst_w, dst_h), new Point(dst_x, dst_y), GraphicsOptions.Default);

            return(true);
        }
示例#2
0
        /// <summary>
        /// Internal image save.
        /// </summary>
        /// <param name="ctx">Runtime context.</param>
        /// <param name="im">Image resource.</param>
        /// <param name="to">Optional. Filename or stream. If not specified the functiona saves the image to output stream.</param>
        /// <param name="saveaction">Callback that actually save the image to given stream. Called when all checks pass.</param>
        /// <returns>True if save succeeded.</returns>
        static bool imagesave(Context ctx, PhpResource im, PhpValue to /* = null*/, Action <Image <Rgba32>, Stream> saveaction)
        {
            Debug.Assert(saveaction != null);

            // check the gd2 resource
            var img = PhpGdImageResource.ValidImage(im);

            if (img == null)
            {
                return(false);
            }

            try
            {
                // not specified stream or filename -> save to the output stream
                if (to.IsEmpty)
                {
                    saveaction(img.Image, ctx.OutputStream);
                    return(true);
                }

                // filename specified?
                var filename = to.ToStringOrNull();
                if (filename != null)
                {
                    using (var stream = File.OpenWrite(Path.Combine(ctx.WorkingDirectory, filename)))
                    {
                        saveaction(img.Image, stream);
                    }

                    return(true);
                }

                // to a PHP stream ?
                // validate the stream resource, outputs warning in case of invalid resource
                var phpstream = PhpStream.GetValid(to.AsObject() as PhpResource, FileAccess.Write);
                if (phpstream == null)
                {
                    return(false);
                }

                // save image to byte[] and pass it to php stream

                var ms = new MemoryStream();

                saveaction(img.Image, ms);

                phpstream.WriteBytes(ms.ToArray());
                phpstream.Flush();

                // stream is closed after the operation
                phpstream.Dispose();
            }
            catch
            {
                return(false);
            }

            return(true);
        }
示例#3
0
        /// <summary>
        /// Output JPEG image to browser or a file.
        /// </summary>
        public static bool imagejpeg(Context ctx, PhpResource im, string filename = null, int quality = 75)
        {
            var img = PhpGdImageResource.ValidImage(im);

            if (img == null)
            {
                return(false);
            }

            if (quality < 0)
            {
                quality = 75;
            }
            if (quality > 100)
            {
                quality = 100;
            }

            if (filename == null)
            {
                img.Image.SaveAsJpeg(ctx.OutputStream, quality);
            }
            else
            {
                using (var stream = File.OpenWrite(Path.Combine(ctx.WorkingDirectory, filename)))
                {
                    img.Image.SaveAsJpeg(stream, quality);
                }
            }

            return(true);
        }
示例#4
0
        static bool imagecopy(PhpResource dst_im, PhpResource src_im, int dst_x, int dst_y, int src_x, int src_y, int src_w, int src_h, float opacity = 1.0f)
        {
            var dst = PhpGdImageResource.ValidImage(dst_im);
            var src = PhpGdImageResource.ValidImage(src_im);

            if (src == null || dst == null)
            {
                return(false);
            }

            if (src_w <= 0 || src_h <= 0 || opacity <= 0)
            {
                // nothing to do
                return(true);
            }

            try
            {
                dst.Image.DrawImage(src.Image.Crop(new Rectangle(src_x, src_y, src_w, src_h)), opacity, new Size(src_w, src_h), new Point(dst_x, dst_y));
            }
            catch (Exception ex)
            {
                PhpException.Throw(PhpError.Warning, ex.Message);
                return(false);
            }

            return(true);
        }
示例#5
0
        /// <summary>
        /// Draw a filled rectangle
        /// </summary>
        public static bool imagefilledrectangle(PhpResource im, int x1, int y1, int x2, int y2, long col)
        {
            var img = PhpGdImageResource.ValidImage(im);

            if (img == null)
            {
                return(false);
            }

            var rect = new RectangleF(x1, y1, x2 - x1 + 1, y2 - y1 + 1);

            if (col == (int)ColorValues.TILED)
            {
                if (img.tiled != null)
                {
                    img.Image.Fill(img.tiled, rect);
                }
            }
            else
            {
                img.Image.Fill(FromRGBA(col), rect);
            }

            return(true);
        }
示例#6
0
        /// <summary>
        /// Allocate a color with an alpha level.  Works for true color and palette based images.
        /// </summary>
        public static int imagecolorallocatealpha(PhpResource im, int red, int green, int blue, int alpha)
        {
            var img = PhpGdImageResource.ValidImage(im);

            if (img == null)
            {
                return(-1);// TODO: false
            }
            //TODO: In non-truecolor images allocate the color
            return(RGBA(red, green, blue, alpha));
        }
示例#7
0
        /// <summary>
        /// return true if the image uses truecolor
        /// </summary>
        public static bool imageistruecolor(PhpResource im)
        {
            var img = PhpGdImageResource.ValidImage(im);

            if (img == null)
            {
                return(false);
            }

            return(!img.IsIndexed);
        }
示例#8
0
        public static int imagesy(PhpResource im)
        {
            var img = PhpGdImageResource.ValidImage(im);

            if (img == null)
            {
                return(-1);
            }

            return(img.Image.Height);
        }
示例#9
0
        public static PhpResource imagerotate(PhpResource im, double angle, int bgcolor, bool ignore_transparent = false)
        {
            var img = PhpGdImageResource.ValidImage(im);

            if (img == null)
            {
                return(null);
            }

            //
            return(new PhpGdImageResource(new Image <Rgba32>(img.Image).Rotate((float)(angle * (-Math.PI / 180.0)), true), img.Format));
        }
示例#10
0
        /// <summary>
        /// Include alpha channel to a saved image
        /// </summary>
        public static bool imagesavealpha(PhpResource im, bool on)
        {
            var img = PhpGdImageResource.ValidImage(im);

            if (img == null)
            {
                return(false);
            }

            img.SaveAlpha = on;

            return(true);
        }
示例#11
0
        /// <summary>
        /// Returns the index of the color of the pixel at the specified location in the image specified by image.
        /// </summary>
        public static long imagecolorat(PhpResource im, int x, int y)
        {
            var img = PhpGdImageResource.ValidImage(im);

            if (img == null)
            {
                return(-1);
            }

            var image = img.Image;

            return((long)image[x, y].Rgba);
        }
示例#12
0
        /// <summary>
        /// Turn alpha blending mode on or off for the given image
        /// </summary>
        public static bool imagealphablending(PhpResource im, bool blendmode)
        {
            var img = PhpGdImageResource.ValidImage(im);

            if (img == null || img.IsIndexed)
            {
                return(false);
            }

            // In PHP AlphaBlending is supported only in True color images
            img.AlphaBlending = blendmode;

            return(true);
        }
示例#13
0
        /// <summary>
        /// Destroy an image
        /// </summary>
        public static bool imagedestroy(PhpResource im)
        {
            var img = PhpGdImageResource.ValidImage(im);

            if (img == null)
            {
                return(false);
            }
            else
            {
                img.Dispose();
                return(true);
            }
        }
示例#14
0
        /// <summary>
        /// Draw a rectangle
        /// </summary>
        public static bool imagerectangle(PhpResource im, int x1, int y1, int x2, int y2, long col)
        {
            var img = PhpGdImageResource.ValidImage(im);

            if (img == null)
            {
                return(false);
            }

            var rect = new RectangleF(x1, y1, x2 - x1, y2 - y1);

            img.Image.Draw(FromRGBA(col), 1.0f, rect);

            return(true);
        }
示例#15
0
        /// <summary>
        /// Set the tile image to $tile when filling $image with the "IMG_COLOR_TILED" color
        /// </summary>
        public static bool imagesettile(PhpResource image, PhpResource tile)
        {
            var img = PhpGdImageResource.ValidImage(image);

            if (img == null)
            {
                return(false);
            }

            var imgTile = PhpGdImageResource.ValidImage(tile);

            if (imgTile == null)
            {
                return(false);
            }

            img.tiled = new ImageBrush <Rgba32>(imgTile.Image);

            return(false);
        }
示例#16
0
        /// <summary>
        /// Draws a pixel at the specified coordinate.
        /// </summary>
        public static bool imagesetpixel(PhpResource im, int x, int y, long color)
        {
            var img = PhpGdImageResource.ValidImage(im);

            if (img == null)
            {
                return(false);
            }

            var image = img.Image;

            if (x < 0 || y < 0 || x >= image.Width || y >= image.Height)
            {
                return(false);
            }

            image[x, y] = FromRGBA(color);

            return(true);
        }
示例#17
0
        /// <summary>
        /// Applies a filter to an image.
        /// </summary>
        public static bool imagefilter(PhpResource image, FilterTypes filtertype, int arg1 = 0, int arg2 = 0, int arg3 = 0, int arg4 = 0)
        {
            var img = PhpGdImageResource.ValidImage(image);

            if (img != null)
            {
                switch (filtertype)
                {
                case FilterTypes.GRAYSCALE:
                    img.Image.Grayscale();
                    return(true);

                case FilterTypes.CONTRAST:
                    img.Image.Contrast(arg1);
                    return(true);

                case FilterTypes.BRIGHTNESS:
                    img.Image.Brightness(arg1);
                    return(true);

                case FilterTypes.NEGATE:
                    img.Image.Invert();
                    return(true);

                case FilterTypes.GAUSSIAN_BLUR:
                    img.Image.BoxBlur(arg1);
                    return(true);

                //case FilterTypes.COLORIZE:
                //case FilterTypes.SMOOTH:
                //    return false;

                default:
                    // argument exception
                    Debug.Fail("Not Implemented: imagefilter(" + filtertype.ToString() + ")");
                    break;
                }
            }

            return(false);
        }
示例#18
0
        public static PhpArray imagettftext(Context ctx, PhpResource im, double size, double angle, int x, int y, long color, string font_file, string text)
        {
            var img = PhpGdImageResource.ValidImage(im);

            if (img == null)
            {
                return(null);
            }

            if (string.IsNullOrEmpty(font_file))
            {
                PhpException.Throw(PhpError.Warning, Resources.filename_cannot_be_empty);
                return(null);
            }

            var font_stream = PhpStream.Open(ctx, font_file, "rb");

            if (font_stream == null)
            {
                PhpException.Throw(PhpError.Warning, Resources.invalid_font_filename, font_file);
                return(null);
            }

            // Font preparation
            FontFamily family;

            try
            {
                family = new FontCollection().Install(font_stream.RawStream); // TODO: perf: global font collection cache

                if (ReferenceEquals(family, null))
                {
                    throw new InvalidOperationException();
                }
            }
            catch
            {
                PhpException.Throw(PhpError.Warning, Resources.invalid_font_filename, font_file);
                return(null);
            }
            finally
            {
                font_stream.Dispose();
            }

            FontStyle style;

            if (family.IsStyleAvailible(FontStyle.Regular))
            {
                style = FontStyle.Regular;
            }
            else if (family.IsStyleAvailible(FontStyle.Bold))
            {
                style = FontStyle.Bold;
            }
            else if (family.IsStyleAvailible(FontStyle.Italic))
            {
                style = FontStyle.Italic;
            }
            else if (family.IsStyleAvailible(FontStyle.BoldItalic))
            {
                style = FontStyle.BoldItalic;
            }
            else
            {
                return(null);
            }

            var font     = new Font(family, (float)size, style);
            var textsize = TextMeasurer.Measure(text, new RendererOptions(font));

            // text transformation:
            var matrix = (angle == 0.0) ? Matrix3x2.Identity : Matrix3x2.CreateRotation((float)(angle * -2.0 * Math.PI / 360.0f));

            matrix.Translation = new Vector2(x, y);

            var path = new SixLabors.Shapes.PathBuilder(matrix).AddLine(0, 0, textsize.Width, 0).Build();

            // draw the text:
            // TODO: col < 0 => turn off antialiasing
            img.Image.DrawText(text, font, FromRGBA(Math.Abs(color)), path);

            // calculate drawen text boundaries:
            var pts = new Vector2[]
            {
                new Vector2(0, textsize.Height),              // lower left
                new Vector2(textsize.Width, textsize.Height), // lower right
                new Vector2(textsize.Width, 0),               // upper right
                new Vector2(0, 0),                            // upper left
            };

            for (int i = 0; i < pts.Length; i++)
            {
                pts[i] = Vector2.Transform(pts[i], matrix);
            }

            return(new PhpArray(8)
            {
                pts[0].X,
                pts[0].Y,

                pts[1].X,
                pts[1].Y,

                pts[2].X,
                pts[2].Y,

                pts[3].X,
                pts[3].Y,
            });
        }