2014-09-19 59 views
0
Yii::$app->params['uploadPath'] = Yii::$app->basePath . '/uploads/'; 
if ($model->load(Yii::$app->request->post())) { 
    $image = UploadedFile::getInstance($model, 'photo'); 
    $model->filename = $image->name; 
    $ext = end((explode(".", $image->name))); 
    $model->photo = Security::generateRandomKey().".{$ext}"; 
    $path = Yii::$app->params['uploadPath'] . $model->photo; 
    if($model->save()){ 
     $image->saveAs($path); 
     return $this->redirect(['view', 'id' => $model->id]); 
    } 
} else { 
    return $this->render('create', [ 
     'model' => $model, 
    ]); 
} 

我想調整圖像大小的代碼。 那麼這段代碼中添加了哪些代碼?如何在此代碼中調整圖像大小

回答

2
Try this code: 
// add this method on your controller and then use resize 
function resize($imagePath, $destinationWidth, $destinationHeight, $destinationPath) 
    { 
     if (file_exists($imagePath)) { 
      $imageInfo = getimagesize($imagePath); 
      $sourceWidth = $imageInfo[0]; 
      $sourceHeight = $imageInfo[1]; 
      $source_aspect_ratio = $sourceWidth/$sourceHeight; 
      $thumbnail_aspect_ratio = $destinationWidth/$destinationHeight; 
      if ($sourceWidth <= $destinationWidth && $sourceHeight <= $destinationHeight) { 
       $thumbnail_image_width = $sourceWidth; 
       $thumbnail_image_height = $sourceHeight; 
      } elseif ($thumbnail_aspect_ratio > $source_aspect_ratio) { 
       $thumbnail_image_width = (int) ($destinationHeight * $source_aspect_ratio); 
       $thumbnail_image_height = $destinationHeight; 
      } else { 
       $thumbnail_image_width = $destinationWidth; 
       $thumbnail_image_height = (int) ($destinationWidth/$source_aspect_ratio); 
      } 
      $destinationWidth = $thumbnail_image_width; 
      $destinationHeight = $thumbnail_image_height; 
      $mimeType = $imageInfo['mime']; 
      $destinationWidth = $thumbnail_image_width; 
      $destinationHeight = $thumbnail_image_height; 
      $destination = imagecreatetruecolor($destinationWidth, $destinationHeight); 
      if ($mimeType == 'image/jpeg' || $mimeType == 'image/pjpeg') { 
       $source = imagecreatefromjpeg($imagePath); 
       imagecopyresampled($destination, $source, 0, 0, 0, 0, $destinationWidth, $destinationHeight, $sourceWidth, $sourceHeight); 
       $destinationPath = $destinationPath; 
       imagejpeg($destination, $destinationPath); 
      } else if ($mimeType == 'image/gif') { 
       $source = imagecreatefromgif($imagePath); 
       imagecopyresampled($destination, $source, 0, 0, 0, 0, $destinationWidth, $destinationHeight, $sourceWidth, $sourceHeight); 
       $destinationPath = $destinationPath; 
       imagegif($destination, $destinationPath); 
      } else if ($mimeType == 'image/png' || $mimeType == 'image/x-png') { 
       $source = imagecreatefrompng($imagePath); 
       imagecopyresampled($destination, $source, 0, 0, 0, 0, $destinationWidth, $destinationHeight, $sourceWidth, $sourceHeight); 
       $destinationPath = $destinationPath; 
       imagepng($destination, $destinationPath); 
      } else { 
       echo 'This image type is not supported.'; 
      } 
     } else { 
      echo 'The requested file does not exist.'; 
     } 
    } 
    // ========================================================== 
    $destinationPath = Yii::$app->params['uploadPath'] ."resize/". $model->photo;//note: create new directory (resize) in uploads directory 
     $this->resize($path, $destinationWidth=100, $destinationHeight=100, $destinationPath); 
+0

這個調整工作的比例是多少? – Kshitiz 2014-09-19 05:50:45