Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rhand's avatar
Level 6

Store Images Gzipped on Digital Ocean Bucket

We need to adjust our image storage at Digital Ocean Spaces to the images are served gzipped compressed. This will allow better loading of the images by all browsers. So was thinking about using PHP gzencode. And so far I have come up with this code"

$gzippedImage = gzencode(file_get_contents($request->file->getRealPath()));
\Storage::disk('do')->put("uploads/{$this->currentProject->id}/" . $request->size . '_' . $request->name, $gzippedImage,
[
    'visibility'     => 'public',
    // 'ContentType'    => 'image/subtype',
    'ContentEncoding'=> 'gzip',
]);

The main thing I wonder about is the Content Type for each image type. Will a content type like image/png be sent along via the storage class as default meta data? Or do I need to send the ContentType along?

If not I guess I would have to check for mime type and then add a ContentType. If so how could I do that with the storage class?

0 likes
10 replies
martinbean's avatar

@rhand GZIP compression is usually configured at the server level, i.e. you would configure Apache or nginx to apply the correct headers and compress files before sending them to the end user.

1 like
rhand's avatar
Level 6

@martinbean True, but in this case all images are stored directly on Digital Ocean Spaces. And, like Amazon S3 apparently, they do not do gzip encoding. So I need to get them there compressed.

martinbean's avatar

@rhand You’d probably need to look at a CDN that does support compressing. So whilst say, S3 doesn’t support GZIP compression (because it’s just object storage and not a web server), CloudFront does.

1 like
Tray2's avatar

What format is the images in?

If it's jpg which are already compressed you will gain little if anything at all, they may even become larger.

1 like
rhand's avatar
Level 6

It is jpegs, pngs mainly images wise. And then the same for js and css. Will run some tests on the compression and size for images too.

martinbean's avatar

@rhand As @tray2 says, GZIP has little effect on binary file types like images. GZIP is best for text-based content (like JavaScript and CSS) because it looks for repeated text strings and replaces them with tokens.

1 like
rhand's avatar
Level 6

I see. Did not quite know that. Thanks @tray2 @martinbean for helping my out here. Will drop gzencode for images and only do it for the JavaScript and css files instead.

Will need to see how that works memory wise. This as we will be doing this for hundreds and in the future even thousands of files at the same time. When Nginx does it seems to gzip on demand only. But in the case of files being sent to Digital Ocean Spaces the gzipping will be done before they are stored and so before they are asked for by clients.

I thought about queues but once published these files need to be available right away from the CDN. Or I would publish them and then queue an overwrite not to overload the server... still thinking about this.

martinbean's avatar
Level 80

@rhand But that’s exactly what a CDN is for. Services like CloudFront, will request your files from an origin (your website), do any compressing, and then save that cached version of the file and serve the cached version on subsequent requests. Queues have nothing to do with it.

1 like

Please or to participate in this conversation.