2014-04-02 2 views
0

내 앱에 Miles Johnsons 우수한 업 로더 구성 요소를 구현하려고합니다. 그러나 여기에 문제가 있습니다 : 업로드 된 이미지의 크기에 따라 크기 변경 치수를 변경해야합니다. 내가 올바른 변환을 정확히 파악할 수 있어요,하지만 어떻게 이미지의 크기가 beforeTransform의 내부 변형 될 액세스합니까CakePHP 업 로더 : 이미지 크기에 따라 크기 변경 값을 변경하십시오.

public $actsAs = array(
     'Uploader.Attachment' => array(
      'image' => array(
       'nameCallback' => 'formatName', 
       'tempDir' => TMP, 
       'uploadDir' => UPLOADDIR, 
       'finalPath' => '/img/photos/', 
       'overwrite' => false, 
       'stopSave' => true, 
       'allowEmpty' => false, 
       'transforms' => array(
        array(
         'class' => 'exif', 
         'self' => true 
        ), 
        'image_sized' => array(
         'class' => 'resize', 
         'nameCallback' => 'transformSizedNameCallback', 
         'width' => 1680, 
         'height' => 980, 
         'aspect' => true 
        ) 
       ) 
      ) 
     ), 
     'Uploader.FileValidation' => array(
      'image' => array(
       'extension' => array(
        'value' => array('jpg', 'png', 'jpeg'), 
        'error' => 'Nicht unterstütztes Dateiformat - bitte JPG- oder PNG-Datei hochladen.' 
       ), 
       'minHeight' => array(
        'value' => 980, 
        'error' => 'Das Bild muss mindestens 980 Pixel hoch sein.' 
       ), 
       'minWidth' => array(
        'value' => 980, 
        'error' => 'Das Bild muss mindestens 980 Pixel breit sein.' 
       ), 
       'required' => array(
        'value' => true, 
        'error' => 'Bitte wählen Sie ein Bild aus.' 
       ) 
      ) 
     ) 
    ); 

    public function beforeTransform($options) { 
     if($options['dbColumn'] == 'image_sized') { 
      if($height > $width) { 
       $options['width'] = 980; 
       $options['height'] = 1680; 
      } 
     } 
     return $options; 
    } 

:

나는 콜백의 변환을 수정하려고? 어디에서 $width$height을 얻을 수 있습니까?

답변

1

나는 익숙하지 해요,하지만 코드보고에서이

$file = $this->data[$this->alias][$options['dbColumn']]; 
같은 단지 현재 처리 필드 데이터에 액세스 할 수 dbColumn 값을 사용하면 그 시점에서이 옵션 뭔가 것 같아

물론 이것은 dbColumn 값이 입력 필드 이름과 일치해야합니다! 그 경우가 아니라면 필드 이름을 보유하고 대신 그 필드 이름을 사용하는 추가 옵션이 필요합니다.

이제 $file은 원시 데이터 일뿐입니다. 대부분 file upload array입니다. 하나의 파일을 가정하거나 자신에 의해, 그 치수 tmp_name를 확인하거나 파일 업로드 배열을 처리하고 가능한 이미지의 크기를 입수하는 방법을 노출 할 수있는 Transite\File 클래스 활용 :

$transitFile = new File($file); 
$dimensions = $transitFile->dimensions(); 

https://github.com/milesj/transit/blob/1.5.1/src/Transit/File.php#L121

그래서 결국 당신이 이런 식으로 뭔가를 할 수 :

public function beforeTransform($options) { 
    if($options['dbColumn'] == 'image_sized') { 
     $file = $this->data[$this->alias][$options['dbColumn']]; 
     $transitFile = new \Transit\File($file); 
     $dimensions = $transitFile->dimensions(); 

     if($dimensions === null) { 
      // not an image or something else gone wrong, 
      // maybe throw an exception or wave your arms and call for help 
     } elseif($dimensions['height'] > $dimensions['width']) { 
      $options['width'] = 980; 
      $options['height'] = 1680; 
     } 
    } 
    return $options; 
} 

하시기 바랍니다없는이 모든 테스트되지 않은 예제 코드입니다.

관련 문제