Symfony, Images And Uploading


Table of Contents

Image upload with the admin generator 
Prefix 
Changes 
Setup 
keep original filename when using admin generator upload 
problem with simple uploading of file 
problem with simple uploading of file 
problem with simple uploading of file 
problem with simple uploading of file 
problem with simple uploading of file 
problem with simple uploading of file 
problem with simple uploading of file 
Problem with Displaying Image using sfDatabaseStorage 
Problem with png image 
Image Upload Problem 
Image Upload Problem 
sfGallery2Plugin image paths 
Misc image related discusssions 

Image upload with the admin generator 

Prefix 

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.

Changes 

Enable image uploading within the admin generator 

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 adding thumbnails 

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.

Setup 

Of course, to make above happens magically, you have to check and verify the following.

Install the Thumbnails plugin 

Install the plugin using the symfony command line:

$ symfony plugin-install http://plugins.symfony-project.org/sfThumbnailPlugin
$ symfony cc

Enable GD library 

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

make the thumbnail directory 

Create the uploads/thumbnails/ directory beforehand:

mkdir web/uploads/thumbnails

documented on: 2007.08.22