in reply to CGI and image

That's not how HTTP works.

Your HTML and the image it references are two separate HTTP requests. Your script tries to answer both requests in one response. This approach will not work.

You need to wait for the image request and then send the image data. If you want/need to carry information from the page request to the image request, either use a cookie or pass the information in the image URL as a CGI parameter.

Replies are listed 'Best First'.
Re^2: CGI and image
by kschwab (Vicar) on Dec 13, 2016 at 17:39 UTC
    Technically, it could be one HTTP request. He could convert the image data to base64 and use a data uri. There's a URI::Data module that might be helpful in that regard.
      Technically, it could be one HTTP request. He could convert the image data to base64 and use a data uri.

      That won't work with all browsers. See http://caniuse.com/datauri. (Don't think that people have stopped using ancient IEs. And don't think that the browsers listed there are ALL browsers that exist.) Plus, it wastes a lot of bandwitdh, especially if the image is static. Using a classic HTTP(S) URL for the image allows the browser and all proxies between browser and server to cache the image, so it is transmitted only once, whereas the data URL "solution" sends the image for every request, with an additional overhead of 33% due to base64 encoding.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        >That won't work with all browsers.

        Well, for that matter, his choice of png images won't work on all browsers either. You have to draw a line somewhere :)

        >Plus, it wastes a lot of bandwitdh, especially if the image is static

        The code seems to indicate the image is a dynamic representation of current weather conditions that may not make sense to cache.

        But yes, I concede that data uris do have drawbacks.