使用CakePHP 2上传插件后创建缩略图

时间:2016-01-06 21:44:17

标签: php cakephp image-processing thumbnails cakephp-2.x

我使用CakePHP上传plugin已经有一段时间了,我对它非常满意:

public $actsAs = array(
    'Upload.Upload' => array(
        'image' => array(
            'fields' => array(
                'dir' => 'dir'
            ),
            'thumbnailSizes' => array(
                'xvga' => '1024x768',
                'vga' => '640x480',
                'thumb' => '300x300'
            ),
            'thumbnailMethod'  => 'php',
        )
    )
);

在一个新的用例中,我必须制作一些更好的缩略图,而不是将分辨率静态降级到640x640或300x300。我希望每个图像方向具有分辨率,或者说图像不应超过30kB。

插件作者Jose Gonzales明确表示上传插件不会创建缩略图:

此插件不会创建缩略图。您可以使用自定义Transformer创建文件上载的修改版本。

我想知道如何与现有的上传插件一起实现这一目标?

2 个答案:

答案 0 :(得分:1)

  

在一个新的用例中,我必须制作一些更好的缩略图,而不是将分辨率静态降级到640x640或300x300

什么阻止您在插件处理上传后进行自定义图像处理?您也可以扩展行为并添加所需内容。

您可以使用行为附带的Imagine plugin来轻松处理图像。或者将其与FileStorage结合使用,the underlying library Imagine使用Imagine在上传后自动处理图像。

或直接使用closing tkmessagebox after some time in python

  

我希望每个图像方向都有分辨率,或者说图像不应超过30kB。

使用验证规则。

答案 1 :(得分:0)

最后我使用 thephpleague 中的滑行https://github.com/thephpleague/glide

如文档页面所述,它允许"在飞行中"图像处理如:

  • 使用简单的基于HTTP的API调整,调整大小并为图像添加效果。
  • 自动缓存并提供操作的图像 远期到期标题。
  • 更改方向
  • 添加水印
  • 添加过滤器
  • 添加边框
  • 定义图像的最大尺寸

在CakePHP中实现非常简单(不需要插件)。在composer.json中添加库作为依赖项:

./composer.phar update

运行public function imageResize() { $server = League\Glide\ServerFactory::create([ 'source' => 'files', 'cache' => 'files/cache', 'watermarks' => 'files/watermarks' ]); $server->outputImage($path, $_GET); }

在控制器操作中,它可以像这样调用:

https://www.url.com/images/imageResize?w=480&mark=mark.png&markpos=right&markh=60&markpad=4

所以我可以简单地从应用程序中调用图像:

    public class WorkoutEditActivity extends ActionBarActivity{
        int whatShouldBeSaved = -1;

        Workout workout = null;
        ViewPager viewpager;
        ViewFactory factory = new ViewFactory(getSupportFragmentManager());;

        @Override
        public void onCreate(Bundle savedInstanceState) {
        Log.d("onCreate", "called");
        super.onCreate(savedInstanceState);

        setContentView(R.layout.workout_edit_activity);

        viewpager = (ViewPager) findViewById(R.id.viewPager);
        viewpager.setOffscreenPageLimit(0);
        viewpager.setAdapter(factory);

        viewpager.setOnPageChangeListener(
                new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                    }

                    @Override
                    public void onPageSelected(int position) {
                        if (position == 0) {
                            SettingsFragment.isActivated = true;
                        } else {
                            if (SettingsFragment.isActivated) {
                                SettingsFragment.isActivated = false;
                                InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
                                imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
                            }
                        }
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {

                    }
                }
        );

        if (savedInstanceState != null) {
            whatShouldBeSaved = savedInstanceState.getInt("test", -1);
            if (savedInstanceState.getSerializable("workout") != null){
                setWorkout((Workout) savedInstanceState.getSerializable("workout"));
            }
            Log.d("GOT SAVED DATA", "" + whatShouldBeSaved);
        }
        Log.d("CREATE", "created? " + workout);
        Log.d("CREATE", "created? " + whatShouldBeSaved);
        }

        public void setWorkout(Workout w){
        Log.d("WORKOUT", "had workout " + workout);
        workout = w;
        factory.setWorkout(workout);
        Log.d("WORKOUT", "has workout " + workout);
        }

        @Override
        public void onResume() {
        super.onResume();
        Log.d("RESUME", "resuming act");

        SettingsFragment.isActivated = true;
        //factory.setViewPager(viewpager);

        if (getIntent().getExtras() != null){
            if (getIntent().getExtras().containsKey("workout")){
                Log.d("RESUME", "resume has extra");
                setWorkout((Workout) getIntent().getSerializableExtra("workout"));
                whatShouldBeSaved = getIntent().getIntExtra("test", -1);
            }
        } else {
            Log.d("RESUME", "extras did not contain workout2");
        }

        Log.d("RESUME", "created? " + workout);
        Log.d("RESUME", "created? " + whatShouldBeSaved);
        }

        @Override
        public void onPause(){
        super.onPause();
        Log.d("ONPAUSE", "called");
        }


        @Override
        public void onStart(){
        super.onStart();
        Log.d("START", "starting act");
        }

        @Override
        public void onStop(){
        Log.d("STOP", "stopping act");
        super.onStop();
        }

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        Log.d("onACTRES", "called");
        }

        @Override
        public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        Log.d("SAVEINSTANCE", "called");
        savedInstanceState.putInt("test", whatShouldBeSaved);
        }

        @Override
        public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        // Restore UI state from the savedInstanceState.
        // This bundle has also been passed to onCreate.
        Log.d("RESTOREINSTANCE", "called");

        whatShouldBeSaved = savedInstanceState.getInt("test", 1);
        Log.d("GOT SAVE", ""+ whatShouldBeSaved);
        }

        @Override
        public void onRestoreInstanceState(Bundle savedInstanceState, PersistableBundle persistentState){
        super.onRestoreInstanceState(savedInstanceState, persistentState);
        Log.d("RESTOREINSTANCE2", "called");
        }

        @Override
        public void onDestroy(){
        super.onDestroy();
        Log.d("DESTROY", "onDestroy called");
        Log.d("DESTROY", "destroying " + workout);
        Log.d("DESTROY", "destroying " + whatShouldBeSaved);
        Log.d("DESTROY", "was destroyed");
        //first = true;
        }
    }

另一个有用的库是自适应图像http://adaptive-images.com

它可以检测访问者的屏幕大小,并自动创建,缓存和提供设备适当的重新缩放版本的网页嵌入式HTML图像。无需更改标记。它适用于响应式设计,并与流体图像技术结合使用。