Table of Contents
Have you read Symfony cookbook on How to upload a file at http://www.symfony-project.org/cookbook/trunk/upload ? You've got to understand that in order to understand the following.
Define the photo field in config/schema.yml:
photo: varchar(255)
In apps/my_app/modules/my_mod/config/generator.yml:
edit: fields: photo: name: Photo help: Please upload the photo type: admin_input_file_tag upload_dir: photos params: include_link=photos include_text="show photo" include_remove=true
That's it. Reload, your admin generator can now allow image uploading.
Automatically add thumbnails to uploaded photos. It will boost photo previews speed to be 10 times faster, and will ease on network bandwidth and server load as well.
Create file lib/imgHandling.class.php & lib/model/MyObj.php as follows:
Example 1. File lib/imgHandling.class.php
<?php class imgHandling { public static function genThumbnail($mx, $my, $upload_dir, $fileName, $fileNameNew="") { if (!$fileNameNew) $fileNameNew = $fileName; $thumbnail = new sfThumbnail($mx, $my); $thumbnail->loadFile($upload_dir. $fileName); $thumbnail->save(sfConfig::get('sf_upload_dir').'/thumbnails/'.$fileNameNew, 'image/png'); } }
Example 2. File lib/model/MyObj.php
<?php /** * Subclass for representing a row from the table. * * @package lib.model */ class MyObj extends BaseMyObj { // == Extend the model for business logic public function save($con = null) { // == pre save hook // fill prereq fields // == call the parent save() method parent::save($con); // == post save hook // fill extra fields $fileName = $this->getPhoto(); imgHandling::genThumbnail(100, 100, sfConfig::get('sf_upload_dir').'/photos/', $fileName); } }
Of course, the save() code has been simplified for illustration purpose. If you want to keep your original upload filename for the thumbnail, follow the next article.
Of course, to make above happens magically, you have to check and verify the following.
Install the plugin using the symfony command line:
$ symfony plugin-install http://plugins.symfony-project.org/sfThumbnailPlugin $ symfony cc
Make sure the GD library is activated, you might have to uncomment the related line in your php.ini and restart your web server to enable PHP image handling functions. If unsure, check:
GD Support enable and checking
http://www.plus2net.com/php_tutorial/gd-support.php