This extension intend to help to "attach" file(s) to a CActiveRecord, without the help of any database, by generating file names based on the concatenation of the primary key(s). File(s) can be processed in any way you want (look at Usage).
Here is the main idea:
For those who knows paperclip for rails, the purpose is nearly the same, but extended as you can create formats for any file by using processors (look at the example file provided in the archive).
Yii 1.1., the php gd or imagemagick extension for ImageARBehavior.
Download the archive
FileARBehavior : copy FileARBehavior in your components folder.
ImageARBehavior : copy FileARBehavior and ImageARBehavior in your components folder, then copy the image folder in your extension directory.
Look at the example file provided in the archive.
Very briefly, you have to configure your behaviors() method in models. An example to generate 3 formats of image:
public function behaviors() { return array( 'recipeImgBehavior' => array( 'class' => 'ImageARBehavior', 'attribute' => 'recipeImg', // this must exist 'extension' => 'png, gif, jpg', // possible extensions, comma separated 'prefix' => 'img_', 'relativeWebRootFolder' => 'images/recipes', // this folder must exist 'useImageMagick' => '/usr/bin', # I want to use imagemagick instead of GD, and // it is located in /usr/bin on my computer. // this will define formats for the image. // The format 'normal' always exist. This is the default format, by default no // suffix or no processing is enabled. 'formats' => array( // create a thumbnail grayscale format 'thumb' => array( 'suffix' => '_thumb', 'process' => array('resize' => array(60, 60), 'grayscale' => true), ), // create a large one (in fact, no resize is applied) 'large' => array( 'suffix' => '_large', ), // and override the default : 'normal' => array( 'process' => array('resize' => array(200, 200)), ), ), 'defaultName' => 'default', // when no file is associated, this one is used // defaultName need to exist in the relativeWebRootFolder path, and prefixed by prefix, // and with one of the possible extensions. if multiple formats are used, a default file must exist // for each format. Name is constructed like this : // {prefix}{name of the default file}{suffix}{one of the extension} ) );
The file (or files if you use Images with multiple formats) are generated like this :
relativWebFolder/{prefix} + join('_', primaryKeys) + {suffix} + {extension}
The suffix is only provided for images to handle multiple format.
For example, with a dish with one primary key equal to 1, you will have the following files (if you send a png image):
Think about it to avoid name clashes if you put multiples files (from different CActiveRecord) in the same directories and be sure in this case to define different prefix for each CActiveRecord.
version 0.7
version 0.5:
version 0.6:
The image extension used is modified to remove the need of the CArray file (and there is some new functions). You can use the default extension if you want by installing it instead of copying the image folder provided in my archive.
There is a forum to discuss about this extension.
Total 20 comments
Hi,
I'm using your extension. It's great. However, I believe its performance may be improved by storing directory file list in memory on the first access.
Consider the scenario of having a grid view with user photos. For 10 users, glob() function inside of your extension would be called for 10 times. Instead directory listing could be queried just once and then saved in memory.
Hi,
there is a warning because you don't check if
$fsis null before passing it to foreach.You should change it to this:
Hi,
It is possible to use image watermark for "large" format?
any chance you will put that on github? or should we do that? ;-)
hi, very nice extension. Problem: I inserted this ext. to Photo model. When I create Photo item it works fine, but when I create Photo item in another model where have relation with Photo model it create item but not manipulation with file(
When I change method afterSave in FileARBehavior() to:
it work fine. But how to set model dynamically?
How to sent file to Photo model?
I mode this problem so:
and when I create Photo Item in other model I do that:
If are you can made this problem with simple method please write here! )
Thank you for this nice extention. I encountered 2 little problems:
Uploading file with extention in capital letters (pic.JPG) does not work
Fix: in FileARBehavior insert strtolower in line 212 strpos(strtolower($this->extension), strtolower($file->extensionName))
The image magick driver does not work correctly when chaining filters, like first applying resize and then crop. The problem is, that you changed the original class so that you have only one call to "convert" When preparing that single operation then the crop operation works with the original dimensions of the image rather than with the result of the initial resize. The original image magick driver class calls image magick "convert" for every image operation. This might take longer but works correctly.
Fix: user original Kohona class, only applying minor changes to fit Yii
Please note that you should add quotes around the call to convert, so that it will also work if the path ($this->dir) has spaces in it, like this
if ($error = exec('"'.escapeshellcmd($this->dir.'convert'.$this->ext).'" '.$dir.' '.$this->cmd_image.' '.$this->cmd_image))
is it anyway for us to upload multiple files / images for 1 model.?
Component treats 'jpg' and 'JPG' differently.
I'm getting: Property "ImageARBehavior.useImageMagick" is not defined.
Btw, please use GitHub for development.... :)
Hello,
Very nice extention. I am heving some problems though with the create. Update works fine but wnen i create a new item I get and error when the field is required.
New version with new things:
See the changelog, and look at the example file !
Tell me what you think, report bugs and so on;
Thank you intel352 for all your ideas !
I love them all ;
I just created a repository on sourceforge so it is now possible to post issues/feature requests.
If you want, you can help me by sending me your code (I will send you a private message soon). Then I will make a new version 0.7 with the new things and mention your nickname in sources.
Tanks again
Nice job on this extension. Few questions & requests for you:
Works perfect.Very useful,I wanted a picture field for an Article class,and didn't want/was bored to mess up with the database...Thanks!
It is now possible to get the file(s) path(s), in order to test if a file exist or to delete it when you want.
In fact the name is generated by the primary key, or by the concatenation of keys separated by '_' in case of multiple primary keys. I'm not sure that a random generator is needed, because it is unique if you specify prefix and suffix correctly (or if you save into different directories).
But maybe you're right and can convince me ; we can discuss this on the dedicated forum newly created ; This way we won't annoy the followers.
Hi,
You can have a property to auto-generate the filename, example:
'recipeImgBehavior' => array( 'class' => 'ImageARBehavior', 'autoGenerateFileName' => true,
And the filename will be: /images/{prefix} + date('YmdHis') + {suffix} + {extension}
Instead of date('YmdHis') you can use another random generator.
It prevent image replace and can cause some problems if image is replaced.
Usually i use:
date('YmdHis') + '_' + {random numer 0 to 9} + {random numer 0 to 9} + {random numer 0 to 9} + {random numer 0 to 9} + {random numer 0 to 9}
And the final filename is (example):
20110101120000_01234
I'm sorry, I forgot this ! Here is the way to do this :
It's better each day.
If you want more image processing functions, you can view here: http://www.verot.net/php_class_upload_samples.htm
I have one extesion here in Yii with this class.
This class do everything with images.
I dont understand how the get the thumb, large and normal file, can you explain more?
There is now a new version of this extension (0.5).
It is a rewrite (in parts) of the code to let the user create multiple image formats and it is now better structured and I hope without errors or bugs.
The api has changed a bit. Check the example.
I recommend you to use this new version, as the api is now right for me.
Tell me if you find some bugs, and if you enjoy it too !
In the future, I may add some processing functions for images, by adding new function in the Image class. If you do this on your own, share it and I will report it.
Leave a comment
Please login to leave your comment.