View Raw SPL
/*****************************************************************************
*                                                                            *
*   WEBREADIMAGE.SPL Copyright (C) 2020 DSP Development Corporation          *
*                               All Rights Reserved                          *
*                                                                            *
*   Author:      Randy Race                                                  *
*                                                                            *
*   Synopsis:    Returns an image from a URL                                 *
*                                                                            *
*   Revisions:   20 Aug 2020  RRR  Creation                                  *
*                                                                            *
*****************************************************************************/


#if @HELP_WEBREADIMAGE

    WEBREADIMAGE

    Purpose: Returns an image from a URL.

    Syntax:  WEBREADIMAGE("url", "header")

              "url"     - A string, the URL string.

              "header"  - A string, the HTML header string. Defaults to empty,
                          no header.

    Returns: A series, the binary image for the URL.

    Example:
             webreadimage("www.dadisp.com/grafx/HomePageHeader00.jpg")

             Places the DADiSP home page image in the current window.
             The aspect ratio of the window is adjusted to accommodate
             the returned image.

    Example:
             dspimg = webreadimage("www.dadisp.com/grafx/HomePageHeader00.jpg")

             Same as above except the returned image is assigned to the
             variable DSPIMG.

    Remarks:
             WEBREADIMAGE returns binary image for the input URL.
             If no output is provided, the image is placed in the current
             window and the aspect ratio is adjusted to match the image.

    See Also:
             Geturl
             Webreadhtml
             Webreadjson
             Webreadimage
#endif


/* return image for a URL */
webreadimage(url = {}, header = "")
{
        local img, status, err, p, fname, ext;

        if (not(isstring(url)))
        {
                error(sprintf("%s - input URL required", __FUNC__));
        }

        header = webformatheader(header);

        /* get image as binary stream */
        (img, status, err) = geturl(url, "", header, "binary");

        if (status == 1)
        {
                /* save to temp file and load as image */
                (p, fname, ext) = fileparts(url);

                imfile = getmiscpath(1, 1) + "temp." + webreadext(ext);

                writeb(imfile, ubyte, 1, img);

                img = readimage(imfile);

                if (outargc == 0)
                {
                        setaspect(w0, -1);
                }

                return(img);
        }
        else
        {
                error(sprintf("%s - %s", __FUNC__, err));
        }
}