2016-04-07 239 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