Resize images on upload

我的未来我决定 提交于 2019-12-24 03:08:48

问题


How it would be possible to resize the images when uploading them?

There exists i.e. the function: $image->resizeByWidth(width)

Where it can be implemented for having a resizing of the images which are uploaded?

How could it be added to the following example of upload:

class GalleryPage extends Page {

    private static $many_many = array(
        'GalleryImages' => 'Image'
    );

    function getCMSFields() {

        $fields = parent::getCMSFields();

        $fields->addFieldToTab(
            'Root.Upload', 
            $uploadField = new UploadField(
                $name = 'GalleryImages',
                $title = 'Upload one or more images (max 10 in total)'
            )  
        );
        $uploadField->setAllowedMaxFileNumber(10);

        return $fields;        
    }   

Thank you


回答1:


It's done automatically by the CMS. When you go to use the image in your template, you tell it the dimensions, and it will be resized as soon as you save or publish (if it hasn't been already)




回答2:


If you're concerned about disk space from your CMS users uploading RAW files e.g. 5MB JPGs, then you can actually resample on upload as asked in your question and then further resize as needed within your templates.

If you're using SilverStripe 2.4+ then you could use this ResampleUpload extension. It resamples the image on upload and deletes the original.

If you're using SilverStripe 3+ then you'd need to change the above extension to extend DataExtension instead of DataObjectDecorator. I've not used this under SS3 myself so there may be other minor tweaks required but looking at it I can't see any changes needed as it's modifying the onAfterUpload and onAfterWrite methods which still exist on the Image class in SS 3.




回答3:


As stated before, no out of box functionality for this in SS 3. This mod offers the solution you're looking for though: https://github.com/heyday/silverstripe-optimisedimage

First Install the mod in root.

Then write the code below in your mysite/_config/config.yml to activate image resampling (resizing).

Image:
  extensions:
    - ResampleImage

In the same config.yml file, write the max resolution (x = width, y = height obviously). If user uploads a bigger res photo, it will be scaled down (maintaining aspect ratio) to fit within the given max res dimensions.

ResampleImage:
  max_x: 2000
  max_y: 2000


来源:https://stackoverflow.com/questions/19895497/resize-images-on-upload

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!