|
|
| (One intermediate revision by one other user not shown) |
| Line 1: |
Line 1: |
| [http://wc1.worldcrossing.com/WebX/.1de60a0b vigrx online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=90&forum=13 cheap cialis] [http://wc1.worldcrossing.com/WebX/.1de60a16 levitra] [http://news.engin.brown.edu/forums/thread-view.asp?tid=150 tramadol online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=165 cheap vicodin] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=156&forum=13 wellbutrin online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=133&forum=13 free qwest ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=130&forum=13 cheap propecia] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=129&forum=13 free polyphonic ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=221 free cool ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=213 polyphonic ringtones] [http://wc1.worldcrossing.com/WebX/.1de609f4 mono ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=106&forum=13 hydrocodone online] [http://wc1.worldcrossing.com/WebX/.1de609db but adipex] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=84&forum=13 free alltel ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=334 valium online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=146&forum=13 tenuate online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=128&forum=13 cheap phentermine] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=85&forum=13 alprazolam online] [http://wc1.worldcrossing.com/WebX/.1de60a1e order flexeril] [http://news.engin.brown.edu/forums/thread-view.asp?tid=196 hgh online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=114&forum=13 meridia online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=150&forum=13 cheap ultram] [http://news.engin.brown.edu/forums/thread-view.asp?tid=201 free ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=103&forum=13 funny ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=194 cheap diethylpropion] [http://wc1.worldcrossing.com/WebX/.1de609fe free qwest ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=207 real ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=186 lipitor online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=149&forum=13 cheap ultracet] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=148&forum=13 tramadol online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=159 cialis online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=142&forum=13 free sony ericsson ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=203 free funny ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a32 zoloft] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=120&forum=13 free music ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=336 carisoprodol online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=160&forum=13 zanaflex online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=354 free nokia ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=113&forum=13 lortab] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=362 free motorola ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=209 free samsung ringtones] [http://wc1.worldcrossing.com/WebX/.1de609dd alltel ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=222 free sharp ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a28 soma online] [http://wc1.worldcrossing.com/WebX/.1de60a1a free mtv ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=109&forum=13 levitra online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=127&forum=13 pharmacy online online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=361 free real ringtones] [http://wc1.worldcrossing.com/WebX/.1de609df cheap ambien] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=98&forum=13 cheap diethylpropion] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=158&forum=13 xanax online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=172 online lorazepam] [http://wc1.worldcrossing.com/WebX/.1de609ff real ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=220 punk ringtones] [http://wc1.worldcrossing.com/WebX/.1de609dc buy albuterol] [http://wc1.worldcrossing.com/WebX/.1de60a2e cheap xanax] [http://news.engin.brown.edu/forums/thread-view.asp?tid=156 fioricet] [http://wc1.worldcrossing.com/WebX/.1de60a10 didrex online] [http://wc1.worldcrossing.com/WebX/.1de60a35 free verizon ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=176 ultracet online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=356 free tracfone ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=92&forum=13 cheap clomid] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=344 ativan] [http://news.engin.brown.edu/forums/thread-view.asp?tid=155 xanax online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=178 buy nexium] [http://wc1.worldcrossing.com/WebX/.1de60a04 sonyericsson ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=182 clomid online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=82&forum=13 adipex online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=343 cialis online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=170 hydrocodone online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=169 levitra online] [http://wc1.worldcrossing.com/WebX/.1de609f3 midi ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=111&forum=13 lisinopril online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=224 free wwe ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=153 phentermine online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=147&forum=13 tracfone ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=181 sildenafil online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=212 verizon ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=223 free midi ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=335 phentermine online] [http://wc1.worldcrossing.com/WebX/.1de60a00 rivotril online] [http://wc1.worldcrossing.com/WebX/.1de60a0a vicodin] [http://news.engin.brown.edu/forums/thread-view.asp?tid=187 celexa online] [http://wc1.worldcrossing.com/WebX/.1de609eb free funny ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=197 cheap flexeril] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=105&forum=13 hoodia online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=188 tenuate online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=180 prozac online] [http://wc1.worldcrossing.com/WebX/.1de60a05 free sprint ringtones] [http://wc1.worldcrossing.com/WebX/.1de609f9 cheap norco] [http://wc1.worldcrossing.com/WebX/.1de609f2 meridia online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=145&forum=13 free sprint ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=96&forum=13 buy diazepam] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=338 online fioricet] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=358 free mp3 ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a21 buy paxil] [http://wc1.worldcrossing.com/WebX/.1de60a31 buy zanaflex] [http://wc1.worldcrossing.com/WebX/.1de609de cheap alprazolam] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=141&forum=13 cheap soma] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=342 cheap diazepam] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=157&forum=13 free wwe ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=161&forum=13 but zoloft] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=112&forum=13 buy lorazepam] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=124&forum=13 online norco] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=355 free ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=88&forum=13 carisoprodol online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=360 qwest ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=167 cheap clonazepam] [http://wc1.worldcrossing.com/WebX/.1de60a0e celexa online] [http://wc1.worldcrossing.com/WebX/.1de609e4 clonazepam online] [http://wc1.worldcrossing.com/WebX/.1de60a17 buy lipitor] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=123&forum=13 nokia ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=184 cheap lisinopril] [http://wc1.worldcrossing.com/WebX/.1de60a24 cheap propecia] [http://wc1.worldcrossing.com/WebX/.1de609f8 nokia ringtones] [http://wc1.worldcrossing.com/WebX/.1de609fd punk ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=154 carisoprodol online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=339 ultram online] [http://wc1.worldcrossing.com/WebX/.1de60a34 sony ericsson ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=202 tracfone ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=199 zyban online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=198 hoodia online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=332 tramadol online] [http://wc1.worldcrossing.com/WebX/.1de60a1d cingular ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=107&forum=13 free jazz ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=219 free alltel ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=101&forum=13 flexeril] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=359 free nextel ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a15 free kyocera ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=363 free samsung ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=117&forum=13 free motorola ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=346 meridia online] [http://wc1.worldcrossing.com/WebX/.1de60a30 xenical online] [http://wc1.worldcrossing.com/WebX/.1de60a03 sony ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=168 paxil online] [http://wc1.worldcrossing.com/WebX/.1de60a01 sagem ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=160 buy ativan] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=136&forum=13 cheap rivotril] [http://wc1.worldcrossing.com/WebX/.1de609fb ortho online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=89&forum=13 cheap celexa] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=139&forum=13 sharp ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=163 cheap adipex] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=125&forum=13 cheap ortho] [http://wc1.worldcrossing.com/WebX/.1de609ee but hydrocodone] [http://news.engin.brown.edu/forums/thread-view.asp?tid=177 cheap propecia] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=91&forum=13 free cingular ringtones] [http://wc1.worldcrossing.com/WebX/.1de609f7 nextel ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=215 free sagem ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=347 adipex online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=132&forum=13 punk ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=97&forum=13 buy didrex] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=144&forum=13 free sonyericsson ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=179 cheap didrex] [http://news.engin.brown.edu/forums/thread-view.asp?tid=158 diazepam online] [http://wc1.worldcrossing.com/WebX/.1de60a25 free samsung ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a26 free sharp ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=162 meridia online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=217 free mono ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=152 cheap valium] [http://wc1.worldcrossing.com/WebX/.1de609ec buy hgh] [http://news.engin.brown.edu/forums/thread-view.asp?tid=205 nextel ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=357 free funny ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=191 lortab online] [http://wc1.worldcrossing.com/WebX/.1de60a2b but ultracet] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=153&forum=13 viagra online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=183 albuterol] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=349 cheap vicodin] [http://wc1.worldcrossing.com/WebX/.1de60a18 free motorola ringtones] [http://wc1.worldcrossing.com/WebX/.1de609f0 cheap lorazepam] [http://wc1.worldcrossing.com/WebX/.1de60a12 ericsson ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a19 mp3 ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=216 free sonyericsson ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=161 viagra online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=95&forum=13 cheap cyclobenzaprine] [http://news.engin.brown.edu/forums/thread-view.asp?tid=192 cheap vigrx] [http://wc1.worldcrossing.com/WebX/.1de60a0d wwe ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=185 zoloft online] [http://wc1.worldcrossing.com/WebX/.1de609e3 clomid online] [http://wc1.worldcrossing.com/WebX/.1de60a13 fioricet online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=138&forum=13 samsung ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=83&forum=13 cheap albuterol] [http://wc1.worldcrossing.com/WebX/.1de60a29 buy tramadol] [http://news.engin.brown.edu/forums/thread-view.asp?tid=173 cheap xenical] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=350 online alprazolam] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=93&forum=13 clonazepam online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=204 mp3 ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=164 norco online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=337 online xanax] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=119&forum=13 free mtv ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=206 qwest ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=151&forum=13 valium] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=140&forum=13 sildenafil online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=162&forum=13 cheap zyban] [http://news.engin.brown.edu/forums/thread-view.asp?tid=166 cheap alprazolam] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=353 levitra online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=333 cheap soma] [http://news.engin.brown.edu/forums/thread-view.asp?tid=189 cheap ortho] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=159&forum=13 xenical online] [http://wc1.worldcrossing.com/WebX/.1de60a36 cheap zyban] {{howto|advanced|[[Ivo Jansch]] <ivo@achievo.org>}}
| | {{howto|advanced|[[Ivo Jansch]] <ivo@achievo.org>}} |
|
| |
|
| == Intro == | | == Intro == |
| Line 72: |
Line 72: |
| The atkFileAttribute contains a method called 'processFile'. By default this method does not do anything, but it can be implemented in custom attributes to do the processing. | | The atkFileAttribute contains a method called 'processFile'. By default this method does not do anything, but it can be implemented in custom attributes to do the processing. |
|
| |
|
| To make the image Sepia, I googled a bit and found a useful 'class.image.php' on http://php.amnuts.com/index.php?do=view | | To make the image Sepia, I googled a bit and found a useful 'class.image.php' on http://php.amnuts.com/index.php?do=view&id=16 (there's no reason to reinvent this wheel, right?). Let's download it and place it in the attriblib/ directory. According to the docs, all we need to do is call Image::sepia($img) and it converts the image to sepia. Below is the code that does all the image conversion. I'll let the comments speak for itself: |
| | |
| | <syntaxhighlight lang="php"> |
| | class sepiaAttribute extends atkFileAttribute |
| | { |
| | // By overriding this method, we can do processing on the uploaded image. |
| | // The path and filename of the image are passed as parameters. |
| | function processFile($path, $filename) |
| | { |
| | // Include the image class we downloaded. By using the 'moduleDir' method, we |
| | // ensure that our module works regardless of where it is installed. |
| | include_once(moduleDir("attriblib")."class.image.php"); |
| | |
| | // Read the image as a PHP image resource, and convert it to sepia: |
| | $img = imagecreatefrompng($path.$filename); |
| | Image::sepia($img); |
| | |
| | // Now we create a thumbnail with a fixed size. |
| | $img2 = imagecreate(220,120); |
| | |
| | // We've made the image a bit larger than what we're going to use |
| | // for the thumbnail, so the remaining space will be used as |
| | // backgroundcolor. By allocating the color, we create the background. |
| | // Let's choose 'read' for our border. |
| | $background_color = imagecolorallocate($img2, 255, 0, 0); |
| | |
| | // To resize the image, we need to know its original size, for which we |
| | // can use the 'getimagesize' function from PHP. |
| | list($width, $height, $type, $attr) = getimagesize($path.$filename); |
| | |
| | // Now create the thumbnail. |
| | imagecopyresized($img2, $img, 10, 10, 0, 0, 200, 100, $width, $height); |
| | |
| | // Make the thumbname file have the same name as the original, with prefix |
| | // "thumb_", and write it to the same directory as the original image. |
| | $thumb = "thumb_".$filename; |
| | imagepng($img2, $path.$thumb); |
| | } |
| | } |
| | </syntaxhighlight> |
| | |
| | You may notice that this attribute now only works with PNG files, because we used imagecreatefrompng and imagepng. This however is just for the sake of example. You could check the extension and make it work for other image formats easily. |
| | |
| | Now we have an attribute that generates a thumbnail with some effects. If you now give it a try you will notice that the thumbnail image is being generated when you upload a file; however, ATK still displays the larger original image. To remedy this, we have to tell the new attribute to use the thumbnail instead of the original image. This can be done by overriding the display() method in our custom attribute. Have a look at the following code sample (I left out the processFile method contents for brevity): |
| | |
| | <syntaxhighlight lang="php"> |
| | class sepiaAttribute extends atkFileAttribute |
| | { |
| | function processFile($path, $filename) |
| | { |
| | (...) |
| | } |
| | |
| | function display($record, $mode="") |
| | { |
| | // In recodlists, show the thumbnail instead of the larger image. |
| | if ($mode=="list") |
| | { |
| | // use the thumb_... version of the image |
| | return '<img src="'.$this->m_url. |
| | "thumb_". |
| | $record[$this->fieldName()]["orgfilename"].'">'; |
| | } |
| | return parent::display($record, $mode); |
| | } |
| | } |
| | </syntaxhighlight> |
| | |
| | You see it takes a little bit of knowledge about the original attribute. The m_url member variable contains the location of the uploaded file, and the 'orgfilename' part in the $record is the filename of the image that is used in the original display method. If you derive an attribute from another attribute, have a look at the original source to find out how the attribute works. |
| | |
| | In the above example, you see that we use the parent::display logic to handle the actual image display when not in list mode. This way, we don't have to reimplement this behaviour in our derived class. |
| | |
| | After adding this code, check the application in your browser. You'll now see that when you upload new images, the recordlist will show the manipulated thumbnail image. When you click on to the edit or view page of a record, you will see the larger original image. |
Intro
The atkFileAttribute can be used to upload images and other files into an ATK application. Sometimes however you will want to process files before they are uploaded.
In this howto we create a custom atkFileAttribute to handle this. As an example, we will do three things. First, we will make a thumbnail version of the uploaded image, and second, we make this thumbnail look Sepia, and finally, we want to add a small border around the generated thumbnail.
Requirements
All you need is to set up a node that contains at least an atkFileAttribute. Make sure you have a working sample before you start with this howto.
Your atkFileAttribute code may look something like the following:
$this->add(new atkFileAttribute("picture", atkconfig("atkroot")."pictures/"))
This example uses a field called 'picture' in the database to hold the file name, and the image itself will be uploaded to the pictures/ subdirectory of our application.
Creating a custom attribute
First, we create a custom attribute that derives from atkFileAttribute. If you need this attribute only once, you can place the code directly in the class.<yournode>.inc file of the node you want to use it in, but, assuming you want to reuse this attribute later, we store it as a separate attribute:
<bash>
[ivo@nikita workspace]$ cd <yourapplication>/modules/<yourmodule>
</bash>
You can in fact use any module you have; if you have a lot of custom attributes, you might want to consider creating an 'attriblib' module (as in 'library of attributes') or something along those lines to hold all the attributes that you create. For the rest of this howto, I'll assume that your application is called 'app' and your module is called 'attriblib'
After you decided what module to place the attribute in, create a subdirectory 'attributes':
<bash>
[ivo@nikita yourmodule]$ mkdir attributes/
</bash>
Now it's time to decide a name for the new attribute. For our sepia example, let's call it the 'sepiaAttribute'. Create a file called 'class.sepiaattribute.inc' in the attributes/ subdirectory with the following contents:
<?php
// Include the base class
useattrib("atkfileattribute");
class sepiaAttribute extends atkFileAttribute
{
}
?>
You now have a custom attribute which behaves identical to the atkFileAttribute, since we have not added any processing yet. But we can already add this attribute to our node.
Adding the attribute to the node
Open your node class, and add the following useattrib call:
useattrib("attriblib.sepiaattribute");
As you can see, you can use the modulename and the name of the attribute in the useattrib call. ATK will derive the actual location of the file from this identifier.
In your node, replace the atkFileAttribute with the sepiaAttribute like this:
$this->add(new sepiaAttribute("picture", atkconfig("atkroot")."pictures/"))
When you now browse your application, everything should appear unchanged. You've added a custom attribute, but it doesn't do anything custom yet, so it acts just like a regular atkFileAttribute.
Adding image processing
The atkFileAttribute contains a method called 'processFile'. By default this method does not do anything, but it can be implemented in custom attributes to do the processing.
To make the image Sepia, I googled a bit and found a useful 'class.image.php' on http://php.amnuts.com/index.php?do=view&id=16 (there's no reason to reinvent this wheel, right?). Let's download it and place it in the attriblib/ directory. According to the docs, all we need to do is call Image::sepia($img) and it converts the image to sepia. Below is the code that does all the image conversion. I'll let the comments speak for itself:
class sepiaAttribute extends atkFileAttribute
{
// By overriding this method, we can do processing on the uploaded image.
// The path and filename of the image are passed as parameters.
function processFile($path, $filename)
{
// Include the image class we downloaded. By using the 'moduleDir' method, we
// ensure that our module works regardless of where it is installed.
include_once(moduleDir("attriblib")."class.image.php");
// Read the image as a PHP image resource, and convert it to sepia:
$img = imagecreatefrompng($path.$filename);
Image::sepia($img);
// Now we create a thumbnail with a fixed size.
$img2 = imagecreate(220,120);
// We've made the image a bit larger than what we're going to use
// for the thumbnail, so the remaining space will be used as
// backgroundcolor. By allocating the color, we create the background.
// Let's choose 'read' for our border.
$background_color = imagecolorallocate($img2, 255, 0, 0);
// To resize the image, we need to know its original size, for which we
// can use the 'getimagesize' function from PHP.
list($width, $height, $type, $attr) = getimagesize($path.$filename);
// Now create the thumbnail.
imagecopyresized($img2, $img, 10, 10, 0, 0, 200, 100, $width, $height);
// Make the thumbname file have the same name as the original, with prefix
// "thumb_", and write it to the same directory as the original image.
$thumb = "thumb_".$filename;
imagepng($img2, $path.$thumb);
}
}
You may notice that this attribute now only works with PNG files, because we used imagecreatefrompng and imagepng. This however is just for the sake of example. You could check the extension and make it work for other image formats easily.
Now we have an attribute that generates a thumbnail with some effects. If you now give it a try you will notice that the thumbnail image is being generated when you upload a file; however, ATK still displays the larger original image. To remedy this, we have to tell the new attribute to use the thumbnail instead of the original image. This can be done by overriding the display() method in our custom attribute. Have a look at the following code sample (I left out the processFile method contents for brevity):
class sepiaAttribute extends atkFileAttribute
{
function processFile($path, $filename)
{
(...)
}
function display($record, $mode="")
{
// In recodlists, show the thumbnail instead of the larger image.
if ($mode=="list")
{
// use the thumb_... version of the image
return '<img src="'.$this->m_url.
"thumb_".
$record[$this->fieldName()]["orgfilename"].'">';
}
return parent::display($record, $mode);
}
}
You see it takes a little bit of knowledge about the original attribute. The m_url member variable contains the location of the uploaded file, and the 'orgfilename' part in the $record is the filename of the image that is used in the original display method. If you derive an attribute from another attribute, have a look at the original source to find out how the attribute works.
In the above example, you see that we use the parent::display logic to handle the actual image display when not in list mode. This way, we don't have to reimplement this behaviour in our derived class.
After adding this code, check the application in your browser. You'll now see that when you upload new images, the recordlist will show the manipulated thumbnail image. When you click on to the edit or view page of a record, you will see the larger original image.