简体   繁体   中英

How to stop SilverStripe 4 from autogenerating image thumbnails on file upload?

I'm building a website with a lot of large imagery that is to be served up responsively so that a mobile phone may see a small version of that image and a 4k monitor seeing a large version of that image. The intention was to allow for an author in SilverStripe to upload images that could be as big as 4000px x 3000px, or even larger, and then pass that off to a service like imgix to do the heavy lifting of image resizing and compression, for serving up the image responsively in the theme.

However, uploading images of that size causes PHP memory errors on most hosting environments, and it's because SilverStripe will automatically process the image and generate some thumbnails for the administrative interface. I would like to disable that thumbnail generation so that the image is uploaded - full size - and unmanipulated throughout to not risk PHP memory errors that image manipulation on the server can cause.

The closest thing I've seen within the functionality of SilverStripe to possibly disable image thumbnail generation is within the ImageManipulation trait, but I can't connect the dots of how I can leverage this from within my code:

<?php

namespace MyProject;

use SilverStripe\ORM\DataObject;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TabSet;
use SilverStripe\Assets\File;
use SilverStripe\AssetAdmin\Forms\UploadField;
use Page;

class Project extends DataObject
{
    private static $has_one = [
        'Photo' => File::class
    ];

    private static $owns = [
        'Photo'
    ];

    public function getCMSfields()
    {
        $fields = FieldList::create(TabSet::create('Root'));

        $fields->addFieldsToTab('Root.Presentation', [
            $photoUpload = UploadField::create('Photo', 'Photo')
        ]);

        $photoUpload->getValidator()->setAllowedExtensions(array(
            'png','jpeg','jpg'
        ));
        $photoUpload->setFolderName('photos');

        return $fields;
    }
}

I tried to use a File class, instead of an Image class for my Photo field, thinking SilverStripe may only try to generate thumbnails on Images and not Files, but that doesn't do the trick.

Any ideas?

You should be able to override which class is used for different extensions using class_for_file_extension in your yml. By default the File class does not generate any thumbnails, so by forcing these file extensions to use the File class you should bypass the thumbnail creation when uploading images.

Eg https://github.com/silverstripe/silverstripe-asset-admin/blob/1.0/code/Controller/AssetAdmin.php#L1159

---
Name: myproject
---
SilverStripe\Assets\File:
  class_for_file_extension:
    'jpg': SilverStripe\Assets\File
    'jpeg': SilverStripe\Assets\File
    'png': SilverStripe\Assets\File
    'gif': SilverStripe\Assets\File
    'bmp': SilverStripe\Assets\File
    'ico': SilverStripe\Assets\File

NOTE: Due to how SilverStripe handles the setting of these fields (merge), you will need to override all the default image types rather than just setting a wildcard.

Eg https://github.com/silverstripe/silverstripe-assets/blob/1.0/src/File.php#L210

In addition to overriding the class used for image file extensions as per Brett's answer, you also have to prevent the DBFile class from forcing a resample by editing your site yml file:

---
Name: myproject
---
SilverStripe\Assets\Storage\DBFile:
  force_resample: false

Setting the class for the file extension to file instead of image isn't enough, because DBFile does its own "is image" check ( https://github.com/silverstripe/silverstripe-assets/blob/1.0/src/Storage/DBFile.php#L90 ) which ignores what class you've set the file extension to.

So preventing resampling from being forced ( https://github.com/silverstripe/silverstripe-assets/blob/1.0/src/ImageManipulation.php#L282 ) will stop the resample even if DBFile has determined the underlying file is physically an image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM