开发者

How to optimize image processing in Symfony from model

Assume a Doctrine model Profile:

# This is example of my schema.yml
Profile:
 columns:
   avatar:
     type: string(255)
     notnull: true

My goal is to generate profile's avatar from uploaded file:

class Avatar extends BaseAvatar{
  public function postSave($e){
    if($this->getAvatar()){
      // resize/crop it to 100x100
      // and save
    }
}

That logic is fine for me now. But my Profile associated record is updated on every request with some extra info. And, as you can see, ava开发者_StackOverflowtar file is generated over and over in spite of the fact that avatar field may stay the same.

Question: How can framework determine whether the particular field is updated or not?

Note: I don't need updating in symfony's actions because of code repeating in several apps. Or maybe I need?


If you are using a Form to render the profile editing fields I would suggest moving your resizing code into there by overriding the saveFile method you inherit from sfFormDoctrine:

protected function saveFile($field, $filename = null, sfValidatedFile $file = null)
{
  $finalFilename = parent::saveFile($field, $filename, $file);
  if($field == 'avatar')
  {
    // generate thumbnail from $finalFilename
  }
}

That will get called when the save() method is called on your form. Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜