gem
File uploading for Zend Framework more…
Introduction
Gem allows for easy file uploading and control of uploaded files. Gem works
as an plugin to Zend_Db_Table using the proposed Zend_Db_Table_Plugin.
Download
Usage
The Photo Model
class Photos extends Zend_Db_Table
{
protected $_attachment = array(
'column' => 'image',
'store_path' => /my/store/path/,
'manipulator' => 'ImageTransform',
'styles' => array(
'square' => array(
'geometry' => 'c75x75'),
'small' => array(
'geometry' => '200x200'),
'medium' => array(
'geometry' => '500x500'),
'large' => array(
'geometry' => '1000x1000'),
),
);
protected function _setupPlugins()
{
$attachment = new Gem_Db_Table_Plugin_Attachment($this->_attachment);
$this->addPlugin($attachment);
}
}
Upload and Save
Create a new photo from a form post (you probably want to use Zend_Form and
Zend_Form_Element_File instead).
$photos = new Photos();
$photo = $this->createRow();
$photo->image = new ArrayObject($_FILES['userfile']); // Or a real path to an existing file
$photo->save();
Once we call save the image is moved to the path specified in the model, and
the manipulator that is specified will do its manipulation. In this example
it will give us four different version of the uploaded file including the
original.
Views
Once saved all we need to to display the image so just retrieve it as usual and
pass it to your view.
$photos = new Photos();
$this->view->photo = $photos->fetchRow($photos->select()->where('id = ?', 1));
In the view all you need to do to display your image in the different versions is the
following.
echo $photo->image->small->url();
echo $photo->image->medium->url();
echo $photo->image->large->url();
Custom store path
As default gem is using the following store target, “:model/:id” for the example above it would mean “photos/1”. If you are not satisfied with this you could supply your own target. If you have a column named created_on you can also use the target parts “:year”, “:month” and “:day”.
public $attachment = array(
'column' => 'image',
'store_path' => '/my/store/path/',
'store_target' => ':model/:year/:month/:day/:id',
);
If this is not enough you can also create a method named “getAttachmentStorePath”. If this method exists a callback will be perfomed to this, it expects you to return a full string where to store the image. If this method is available there is no need to pass the “store_path” or “store_target” in the configuration.
/**
* @param Zend_Filter_Inflector $inflector
* @param Zend_Db_Table_Row $row
* @return string Full path of where to store the uploaded file
**/
public function getAttachmentStorePath(Zend_Filter_Inflector $inflector, Zend_Db_Table_Row $row)
Requirement
- Zend framework standard incubator in the include path
- PEAR package Image_Transform or PHP Thumbnailer
Todo
- Validation (file size, image size etc.)
- Configuration, add usage of Zend_Config
- More than images…
- Add support for custom Manipulators, should be a quick fix
Credits
Gem is inspired by PaperClip and UploadColumn that are available for ruby on rails.
Installation
Download necessary libs
$ mkdir libs && cd libs $ svn export http://framework.zend.com/svn/framework/standard/trunk zf-standard-trunk $ svn export http://framework.zend.com/svn/framework/standard/incubator zf-standard-incubator $ wget http://gitorious.org/projects/gem/repos/mainline/archive/master.tar.gz && \ tar -xzf master.tar.gz && \ rm master.tar.gzInstall your choosen image library
If you want the PEAR package Image_Transform
$ sudo pear install http://download.pear.php.net/package/Image_Transform-0.9.1.tgzOr if you prefer PHP Thumbnailer Class v2.0
$ mkdir thumbnailer && \ cd thumbnailer && \ wget http://www.gen-x-design.com/downloads/php5_thumbnail_v2.zip && \ unzip php5_thumbnail_v2.zip && \ rm php5_thumbnail_v2.zip && \ cd ..Setup your include paths to your project
The important thing here is that you put the path to the zf incubator
before the standard library.$includePath = array( '/path/to/libs/zf-standard-incubator/library', '/path/to/libs/zf-standard-trunk/library', '/path/to/libs/thumbnailer', '/path/to/libs/gem-mainline/library', get_include_path(), ); set_include_path(implode(PATH_SEPARATOR, $includePath));Done!
License
New BSD license, same as Zend Framework it self.
mainline
-
Cloning this repository:
git clone git://gitorious.org/gem/mainline.git mainline cd mainline
Add this repository as a remote to an existing local repository:
git remote add mainline git://gitorious.org/gem/mainline.git git fetch mainline git checkout -b my-local-tracking-branch mainline/master_or_other_branch
Activities 
-
Friday April 27 2012
-
Favorite
19:41
Arman Khalatyan started watching gem/mainline
-
-
Friday March 23 2012
-
Favorite
01:28
Evan Stafford started watching gem/mainline
-

