Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>We did this in our CMS (built on CakePHP 1.2; sorry if there are any significant discrepancies I'm not aware of) using a behaviour. That makes the controller logic very easy (in fact we use a baked controller without any modification at all).</p> <p>Unfortunately TIFF isn't a supported file format in GD (the default image manipulation library in PHP). You'll need to use ImageMagick or an equivalent tool to do the actual conversion itself, but the logic for implementing it in your CakePHP project won't be any different to what I describe here.</p> <p>The behaviour (in our case) was used to generate images as thumbnails as well as page resolution and to convert the uploaded file format into JPEG.</p> <p>In its <code>beforeSave()</code> method it checked that data was specified (and that there was no error), and then pulled the <code>tmp_name</code> value from the posted data (and removed the posted data object).</p> <p>In its <code>afterSave()</code> method, it actually performed the image conversion task itself (putting the generated images in the expected location on disk), then updated any foreign keys on extended models with the uploaded image's ID. We do this in the <code>afterSave()</code> operation so we have a database ID to use to name the files on disk.</p> <p>In its <code>afterDelete()</code> method we unlink the files on disk.</p> <p>Using the behaviour in the model is as simple as telling the model (where <code>ContentImage</code> is the name of the behaviour):</p> <pre><code>var $actsAs = array('ContentImage'); </code></pre> <p>Although we also use the model to define the output directory since we had a few models that implemented the behaviour, and it felt like the right thing to do, e.g. in the model:</p> <pre><code>function getThumbnailDir() { return WWW_ROOT.'img'.DS.'upload'.DS.'thumb'; } </code></pre> <p>and in the behaviour itself the output path becomes:</p> <pre><code>$Model-&gt;getThumbnailDir().DS.$Model-&gt;id.'.jpg' </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload