2016-04-07 242 views
0

在使用GD適配器和我的代碼示例是這樣的。它調整圖像的大小,但並不像預期的那樣。我想調整圖像,如果它的寬度超過458px,這種情況下它的工作正常,但如果是低於457px它不應該調整它應該保持它的原樣。但我的腳本總是調整任何大小的圖像什麼是錯的?請 !Phalcon圖像調整大小

if($this->request->hasFiles(true) == true) 
{ 
    foreach ($this->request->getUploadedFiles() as $file) 
    { 
     #Required enable extension=php_fileinfo.dll in php.ini 
     if ($this->imageCheck($file->getRealType())) 
     { 
      //echo $file->getName(), " ", $file->getSize(), "\n"; 
      $imgName = md5(uniqid(rand(), true)).strtolower(date('-dmy-').$file->getName()); 
      $file->moveTo('uploads/blogs/' . $imgName); 
      #Resize & Crop Image 
      $image = new GdAdapter('uploads/blogs/'.$imgName); 
      $image->resize(458,458)->crop(457,457)->save('uploads/blogs/'.$imgName); 
      $blog->bimage = $imgName; 
     } 
     else 
     { 
      $this->flashSession->error("File extension not allowed"); 
      return $this->response->redirect($this->router->getControllerName()); 
     } 
    } 
} 

回答

3

你必須添加一個關於圖像寬度的條件。例如:

$image = new GdAdapter('uploads/blogs/'.$imgName); 
    if ($image->getWidth() > 458) { 
     $image->resize(458,458)->crop(457,457)      
    } 
    $image->save('uploads/blogs/'.$imgName); 
+0

Thnx!它的工作......非常感謝你! – munaz