gpt4 book ai didi

zend-framework - 如何在 zend 框架中调整上传图像的大小

转载 作者:行者123 更新时间:2023-12-04 04:33:34 25 4
gpt4 key购买 nike

我有一个带有上传图片字段的表单。如何将最近上传的图片调整到想要的大小?我目前的表格是:

<?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);
}

还有我的 Controller :

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 thumbnailer 等插件或类似插件来调整上传图片的大小?

最佳答案

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 <cct@stefan-koch.name>
*/

/**
* @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 <cct@stefan-koch.name>
*/


/**
* 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 <cct@stefan-koch.name>
*/

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,
)));

关于zend-framework - 如何在 zend 框架中调整上传图像的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11499731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com