2012-07-16 57 views
3

我有一个带有上传图像字段的表单。如何调整最近上传的图片到所需的大小? 我现在的形式是:如何在zend框架中调整上传的图像

<?php 
class Application_Form_User extends Zend_Form 
{ 
public function init() 
{ 
    $this->setAttrib('enctype', 'multipart/form-data'); 
    $this->setAction(""); 
    $this->setMethod("post"); 

    $element = new Zend_Form_Element_File('photo'); 
    $element->setLabel('Upload an image:') 
      ->setValueDisabled(true); 
    $this->addElement($element, 'photo'); 
    $element->addValidator('Count', false, 1); 
    // limit to 1000K 
    $element->addValidator('Size', false, 1024000); 
    // only JPEG, PNG, and GIFs 
    $element->addValidator('Extension', false, 'jpg,png,gif'); 
    $submit = $this->createElement('submit', 'submit'); 
    $submit->setLabel('Save'); 
    $this->addElement($submit); 
} 

}

而且我的控制器:

public function indexAction() 
{ 
    $form=new Application_Form_User(); 
    if ($this->getRequest()->isPost()) { 
     $formData = $this->getRequest()->getPost(); 
     if ($form->isValid($formData)) { 
      $file=pathinfo($form->photo->getFileName()); 
      $form->photo->addFilter('Rename', PUBLIC_PATH.'/images/'.uniqid().time().'.'.$file['extension']); 
      if ($form->photo->receive()) { 
       $this->view->photo=pathinfo($form->photo->getFileName()); 
      } 
     } 
    } 
    $this->view->form=$form; 
} 

有人可以为我提供一个例子吗?我怎样才能使用插件像PHP缩略图或类似的插件来调整上传的图像?

+1

给这个班一展身手 - https://gist.github.com/880506 – Phil 2012-07-16 07:21:13

+2

可能[Resize image on server]的副本(http://stackoverflow.com/questions/7911293/resize-image-on-server) – Phil 2012-07-16 07:23:05

回答

0

Skoch_Filter_File_Resize有效。这是一个自定义过滤器:http://eliteinformatiker.de/2011/09/02/thumbnails-upload-and-resize-images-with-zend_form_element_file/

<?php 
// Skoch/Filter/File/Resize.php 
/** 
* Zend Framework addition by skoch 
* 
* @category Skoch 
* @package Skoch_Filter 
* @license http://opensource.org/licenses/gpl-license.php GNU Public License 
* @author  Stefan Koch <[email protected]> 
*/ 

/** 
* @see Zend_Filter_Interface 
*/ 
require_once 'Zend/Filter/Interface.php'; 

/** 
* Resizes a given file and saves the created file 
* 
* @category Skoch 
* @package Skoch_Filter 
*/ 
class Skoch_Filter_File_Resize implements Zend_Filter_Interface 
{ 
    protected $_width = null; 
    protected $_height = null; 
    protected $_keepRatio = true; 
    protected $_keepSmaller = true; 
    protected $_directory = null; 
    protected $_adapter = 'Skoch_Filter_File_Resize_Adapter_Gd'; 

    /** 
    * Create a new resize filter with the given options 
    * 
    * @param Zend_Config|array $options Some options. You may specify: width, 
    * height, keepRatio, keepSmaller (do not resize image if it is smaller than 
    * expected), directory (save thumbnail to another directory), 
    * adapter (the name or an instance of the desired adapter) 
    * @return Skoch_Filter_File_Resize An instance of this filter 
    */ 
    public function __construct($options = array()) 
    { 
     if ($options instanceof Zend_Config) { 
      $options = $options->toArray(); 
     } elseif (!is_array($options)) { 
      require_once 'Zend/Filter/Exception.php'; 
      throw new Zend_Filter_Exception('Invalid options argument provided to filter'); 
     } 

     if (!isset($options['width']) && !isset($options['height'])) { 
      require_once 'Zend/Filter/Exception.php'; 
      throw new Zend_Filter_Exception('At least one of width or height must be defined'); 
     } 

     if (isset($options['width'])) { 
      $this->_width = $options['width']; 
     } 
     if (isset($options['height'])) { 
      $this->_height = $options['height']; 
     } 
     if (isset($options['keepRatio'])) { 
      $this->_keepRatio = $options['keepRatio']; 
     } 
     if (isset($options['keepSmaller'])) { 
      $this->_keepSmaller = $options['keepSmaller']; 
     } 
     if (isset($options['directory'])) { 
      $this->_directory = $options['directory']; 
     } 
     if (isset($options['adapter'])) { 
      if ($options['adapter'] instanceof Skoch_Filter_File_Resize_Adapter_Abstract) { 
       $this->_adapter = $options['adapter']; 
      } else { 
       $name = $options['adapter']; 
       if (substr($name, 0, 33) != 'Skoch_Filter_File_Resize_Adapter_') { 
        $name = 'Skoch_Filter_File_Resize_Adapter_' . ucfirst(strtolower($name)); 
       } 
       $this->_adapter = $name; 
      } 
     } 

     $this->_prepareAdapter(); 
    } 

    /** 
    * Instantiate the adapter if it is not already an instance 
    * 
    * @return void 
    */ 
    protected function _prepareAdapter() 
    { 
     if ($this->_adapter instanceof Skoch_Filter_File_Resize_Adapter_Abstract) { 
      return; 
     } else { 
      $this->_adapter = new $this->_adapter(); 
     } 
    } 

    /** 
    * Defined by Zend_Filter_Interface 
    * 
    * Resizes the file $value according to the defined settings 
    * 
    * @param string $value Full path of file to change 
    * @return string The filename which has been set, or false when there were errors 
    */ 
    public function filter($value) 
    { 
     if ($this->_directory) { 
      $target = $this->_directory . '/' . basename($value); 
     } else { 
      $target = $value; 
     } 

     return $this->_adapter->resize($this->_width, $this->_height, 
      $this->_keepRatio, $value, $target, $this->_keepSmaller); 
    } 
} 

<?php 
// Skoch/Filter/File/Resize/Adapter/Abstract.php 
/** 
* Zend Framework addition by skoch 
* 
* @category Skoch 
* @package Skoch_Filter 
* @license http://opensource.org/licenses/gpl-license.php GNU Public License 
* @author  Stefan Koch <[email protected]> 
*/ 


/** 
* Resizes a given file and saves the created file 
* 
* @category Skoch 
* @package Skoch_Filter 
*/ 
abstract class Skoch_Filter_File_Resize_Adapter_Abstract 
{ 
    abstract public function resize($width, $height, $keepRatio, $file, $target, $keepSmaller = true); 

    protected function _calculateWidth($oldWidth, $oldHeight, $width, $height) 
    { 
     // now we need the resize factor 
     // use the bigger one of both and apply them on both 
     $factor = max(($oldWidth/$width), ($oldHeight/$height)); 
     return array($oldWidth/$factor, $oldHeight/$factor); 
    } 
} 

<?php 
// Skoch/Filter/File/Resize/Adapter/Gd.php 
/** 
* Zend Framework addition by skoch 
* 
* @category Skoch 
* @package Skoch_Filter 
* @license http://opensource.org/licenses/gpl-license.php GNU Public License 
* @author  Stefan Koch <[email protected]> 
*/ 

require_once 'Skoch/Filter/File/Resize/Adapter/Abstract.php'; 

/** 
* Resizes a given file with the gd adapter and saves the created file 
* 
* @category Skoch 
* @package Skoch_Filter 
*/ 
class Skoch_Filter_File_Resize_Adapter_Gd extends 
    Skoch_Filter_File_Resize_Adapter_Abstract 
{ 
    public function resize($width, $height, $keepRatio, $file, $target, $keepSmaller = true) 
    { 
     list($oldWidth, $oldHeight, $type) = getimagesize($file); 

     switch ($type) { 
      case IMAGETYPE_PNG: 
       $source = imagecreatefrompng($file); 
       break; 
      case IMAGETYPE_JPEG: 
       $source = imagecreatefromjpeg($file); 
       break; 
      case IMAGETYPE_GIF: 
       $source = imagecreatefromgif($file); 
       break; 
     } 

     if (!$keepSmaller || $oldWidth > $width || $oldHeight > $height) { 
      if ($keepRatio) { 
       list($width, $height) = $this->_calculateWidth($oldWidth, $oldHeight, $width, $height); 
      } 
     } else { 
      $width = $oldWidth; 
      $height = $oldHeight; 
     } 

     $thumb = imagecreatetruecolor($width, $height); 

     imagealphablending($thumb, false); 
     imagesavealpha($thumb, true); 

     imagecopyresampled($thumb, $source, 0, 0, 0, 0, $width, $height, $oldWidth, $oldHeight); 

     switch ($type) { 
      case IMAGETYPE_PNG: 
       imagepng($thumb, $target); 
       break; 
      case IMAGETYPE_JPEG: 
       imagejpeg($thumb, $target); 
       break; 
      case IMAGETYPE_GIF: 
       imagegif($thumb, $target); 
       break; 
     } 
     return $target; 
    } 
} 

$photo->addFilter(new Skoch_Filter_File_Resize(array(
    'width' => 200, 
    'height' => 300, 
    'keepRatio' => true, 
))); 
+0

嗨,我用这个库,它工作得很好。但上传的图像旋转到左侧。该怎么办?? – 2014-06-03 08:08:06