Silverstripe 3.1 - resizing images on boot

I want to resize images on upload in order to maintain safety. I tried it like this

            $visual = new UploadField('Visual', _t('Dict.PREVIEW_IMAGE', 'Preview Image'));
        $visual->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif'));
        $visual->setFolderName('news/' .  $this->ID);
        $visual->resizeByHeight(10);

but the result was white in the backend.

Can I resize images at boot time? What am I doing wrong?

early

+1
source share
1 answer

, . , ?
, .
, , , . , , -, , .


$visual - UploadField, . resizeByHeight, , , , , , .

Image, , , _resampled.

. UploadField upload, , .

, :

class MyUploadField extends UploadField {
    protected function saveTemporaryFile($tmpFile, &$error = null) {
        $file = parent::saveTemporaryFile($tmpFile, $error);
        if ($file && is_a($file, 'Image')) {
            // only attempt to resize if it an image
            $filePath = Director::baseFolder() . "/" . $file->Filename;
            // create a backend (either GDBackend or ImagickBackend depending on your settings)
            $backend = Injector::inst()->createWithArgs(Image::get_backend(), array($filePath));
            if ($backend->hasImageResource() && $backend->getHeight() > 100) {
                // if it is a working image and is higher than 100px, resize it to 100px height
                $newBackend = $backend->resizeByHeight(100);
                if ($newBackend) {
                    // resize successful, overwrite the existing file
                    $newBackend->writeTo($filePath);
                }
            }
        }
        return $file;
    }
}
+5

Source: https://habr.com/ru/post/1606440/


All Articles