2017-12-29 43 views
1

이것은 포스트 컨트롤러 기능입니다. 나는 항상 200x200 이하의 이미지를 업로드하고, 그 이미지는 '업로드' 폴더에 저장됩니다. 이미지를 업로드하면 ID 번호가 4546464.png과 같이 바뀝니다. 크기와 품질을 60x60x30으로 변경 한 후 업로드하고 저장할 때 이미지 크기를 60x60으로 변경하고 싶습니다. 이 코드는 크기는 좋지만 크기와 품질은 업로드하지 않습니다. 당신이업로드 할 때 이미지 크기를 조정하는 방법

$model->file->saveAs('upload/' . $model->image); 

크기를 조정하기 전에

public function actionCreate() { 
    $model = new Monitor(); 

    if ($model->load(Yii::$app->request->post())) { 
     if ($model->payment_processor) { 
      $model->payment_processor = implode(',', $model>payment_processor); 
     } 

     $model->file = UploadedFile::getInstance($model, 'file'); 

     if ($model->file != '') { 
      $model->image = time() . '.' . $model->file->extension; 
     } 

     $model->update_at = date('Y-m-d h:i:s'); 
     $model->save(); 

     if ($model->file != '') {    
      $model->file->saveAs('upload/' . $model->image);    
     } 
     return $this->redirect(['view', 'id' => $model->id]); 
    } else { 
     return $this->render('create', ['model' => $model]); 
    } 
} 
+0

시도해 보셨습니까? 이 코드가 우리가 어떻게 당신을 도울 수 있는지 작동한다면? 예를 들어, 당신은 몇 가지 PHP 함수를 사용할 수 있습니다, 구글에서 검색하려고하면 몇 가지 자습서를 찾을 수 있습니다. –

+0

Google을 검색했지만 yii2에 대해 잘 모릅니다. 내 코드로 완벽하게 작동 할 수있는 코드가 필요하다. 귀하의 라인 주셔서 감사합니다 @ 에릭 – Shubho

+0

내 대답을 확인해보십시오! 그것의 작품 100 % – kiamoz

답변

0

, 당신이 먼저 당신이 원하는 무엇 사이즈 크기를 조정하여 서버에있는 이미지를 업로드 할 수있는 또 다른 방법입니다,하지만 난 원래 저장 당신에게 추천합니다 파일로 복사 한 다음 사본으로 크기를 조정하십시오. 여기

센터에서 크기를 조정하는 기능과 작물의 이미지입니다 : 당신이 할 수있는

public static function resize_crop_image($max_width, $max_height, $source_file, $dst_dir, $quality = 100){ 
    $quality = 10; 
    $imgsize = getimagesize($source_file); 
    $width = $imgsize[0]; 
    $height = $imgsize[1]; 
    $mime = $imgsize['mime']; 

    switch($mime){ 
     case 'image/gif': 
      $image_create = "imagecreatefromgif"; 
      $image = "imagegif"; 
      break; 

     case 'image/png': 
      $image_create = "imagecreatefrompng"; 
      $image = "imagepng"; 
      $quality = 9; 
      break; 

     case 'image/jpeg': 
      $image_create = "imagecreatefromjpeg"; 
      $image = "imagejpeg"; 
      $quality = 100; 
      break; 



     default: 
      return false; 
      break; 
    } 

    $dst_img = imagecreatetruecolor($max_width, $max_height); 
    $src_img = $image_create($source_file); 

    $width_new = $height * $max_width/$max_height; 
    $height_new = $width * $max_height/$max_width; 
    //if the new width is greater than the actual width of the image, then the height is too large and the rest cut off, or vice versa 
    if($width_new > $width){ 
     //cut point by height 
     $h_point = (($height - $height_new)/2); 
     //copy image 
     imagecopyresampled($dst_img, $src_img, 0, 0, 0, $h_point, $max_width, $max_height, $width, $height_new); 
    }else{ 
     //cut point by width 
     $w_point = (($width - $width_new)/2); 
     imagecopyresampled($dst_img, $src_img, 0, 0, $w_point, 0, $max_width, $max_height, $width_new, $height); 
    } 

    $image($dst_img, $dst_dir, $quality); 

    if($dst_img)imagedestroy($dst_img); 
    if($src_img)imagedestroy($src_img); 
} 

이 기능을 사용하여 쉽게 :

yourModel::resize_crop_image(150,150, $path, 'upload/'.$name_you_want_or_random_string.'.jpg',$q); 

는 $ 경로가 업로드 한 원본 파일의 경로입니다. 웹 응용 프로그램 디렉토리에 업로드 폴더를 만들거나 원하는 위치로 대상을 변경해야합니다.

0

imageprocessor 확장자를 사용하고있어 매우 만족합니다. confgurations를 이미지 프로세서 구성 요소에 추가하고 구성 요소의 save 메서드를 호출하면 저장해야합니다. 구성한 크기의 새로운 이미지가 생성됩니다. 좋은 문서가 있습니다. 시도 해봐.