What is the proper http code to respond to the content of an image?

1

Well, the question is quite straightforward.

I'm wanting to use PHP to respond to the content of an image. However, I would like to know if the http status type will have any impact on the client (such as cache images in the browser).

In this case, I'm using Laravel 3

$cachedImage = 'storage/images/cached.jpg';

$code = ... // Qual deles usar?

return Response::make(File::get($cachedImage), $code, ['content-type' => 'image/jpeg']);

Should I use the code 200 or 304 ? I noticed that in Google Chrome, it's returning 304 to images!

    
asked by anonymous 01.07.2015 / 16:06

2 answers

1

For this there are RFC , these are technical documents developed and maintained by the Internet Enginnering Task Force (IETF), an institution that specifies the standards that will be implemented and used throughout the internet.

According to RFC 2616 , section 10 :

  

10.3.5 304 Not Modified

     

If the client has performed a conditional GET request and access is allowed, but the document has not been modified, the SHOULD server responds with this status code. The 304 response MUST NOT contain a message-body, and thus is always terminated by the first empty line after the header fields.

     

The response MUST include the following header fields:

     
  • Date, unless its omission is required by section 14.18.1   If a clockless origin server obeys these rules, and proxies and clients add their own date to any response received without one (as already specified by [RFC 2068], section 14.19), caches will operate correctly.

      
  • ETag and / or Content-Location, if the header would have been sent       in 200 response to the same request

  •   
  • Expires, Cache-Control, and / or Vary, if the field-value might       differ from that in any previous response for the same       variant
  •   
If the conditional GET used a strong cache validator (see section 13.3.3), the SHOULD response does NOT include other entity-headers. Otherwise (i.e., the conditional GET used a weak validator), the response MUST NOT include other entity-headers; this prevents inconsistencies between cached entity-bodies and updated headers.

     

If a 304 response indicates an entity not currently cached, then the cache MUST disregard the response and repeat the request without the conditional.

     

If a cache uses a received 304 response to update the cache entry, the cache MUST update the entry to reflect any new field values given in the response.

Summarizing

For your answer, you can use 304 since the user's request will not change anything on the server and you're just returning the image.


Sources:

01.07.2015 / 16:22
0

In fact, for image, I have never seen use any! The code in this case is implicitly 200, so 200 would be redundant.

The most important is the header you are already entering.

    
01.07.2015 / 16:20