Code snippets for symfony 1.x

Navigation

Snippets by user Daniel Santiago

sfFileImageValidator

Description:

This images validations is an extention for the sfFileValidator. You can use it for validate uploaded images maximum width and height, minimum width and height and if the images have square dimensions.

<?php
 
/**
 * sfFileImageValidator allows you to apply constraints to image file upload, it extend the sfFileValidator functions.
 *
 * <b>Optional parameters:</b>
 *
 * # <b>max_height</b>         - [none]                                - Maximum images height in pixels.
 * # <b>max_height_error</b>   - [The file height is too large]        - An error message to use when
 *                                                                       images height is too large.
 * # <b>max_width</b>          - [none]                                - Maximum images width in pixels.
 * # <b>max_width_error</b>    - [The file width is too large]         - An error message to use when
 *                                                                       images width is too large.
 * # <b>min_height</b>         - [none]                                - Minimum images height in pixels.
 * # <b>min_height_error</b>   - [The file height is too small]        - An error message to use when
 *                                                                       images height is too small.
 * # <b>min_width</b>          - [none]                                - Minimum images width in pixels.
 * # <b>min_width_error</b>    - [The file width is too small]         - An error message to use when
 *                                                                       images width is too small.
 * # <b>is_square</b>          - [false]                               - The image is a square
 * # <b>is_square_error</b>    - [The file is not a square]            - An error message to use when
 *                                                                       the images is not a square
 *                                                                       (The width size is not equal
 *                                                                       to the height size).
 * @package    symfony
 * @subpackage validator
 * @author     Daniel Santiago 
 */
 
class sfFileImageValidator extends sfFileValidator
{
  /**
   * Executes this validator.
   *
   * @param mixed A file or parameter value/array
   * @param error An error message reference
   *
   * @return bool true, if this validator executes successfully, otherwise false
   */
 
  public function execute(&$value, &$error)
  {
    if (parent::execute($value, $error))
    {
      list($width, $height) = @getimagesize($value['tmp_name']);
 
      // File is not a square
      $is_square = $this->getParameter('is_square');
      if ($is_square && $width != $height)
      {
        $error = $this->getParameter('is_square_error');
 
        return false;
      }
 
      // File height too large
      $max_height = $this->getParameter('max_height');
      if ($max_height !== null && $max_height < $height)
      {
        $error = $this->getParameter('max_height_error');
 
        return false;
      }
 
      // File width too large
      $max_width = $this->getParameter('max_width');
      if ($max_width !== null && $max_width < $width)
      {
        $error = $this->getParameter('max_width_error');
 
        return false;
      }
 
      // File height too small
      $min_height = $this->getParameter('min_height');
      if ($min_height !== null && $min_height > $height)
      {
        $error = $this->getParameter('min_height_error');
 
        return false;
      }
 
      // File width too small
      $min_width = $this->getParameter('min_width');
      if ($min_width !== null && $min_width > $width)
      {
        $error = $this->getParameter('min_width_error');
 
        return false;
      }
 
      return true;
    }
  }
 
  /**
   * Initializes this validator.
   *
   * @param sfContext The current application context
   * @param array   An associative array of initialization parameters
   *
   * @return bool true, if initialization completes successfully, otherwise false
   */
  public function initialize($context, $parameters = null)
  {
    // initialize parent
    parent::initialize($context, $parameters);
 
    // set defaults
    $this->getParameterHolder()->set('max_height',        null);
    $this->getParameterHolder()->set('max_height_error',  'The file height is too large');
    $this->getParameterHolder()->set('max_width',         null);
    $this->getParameterHolder()->set('max_width_error',   'The file width is too large');
    $this->getParameterHolder()->set('min_height',        null);
    $this->getParameterHolder()->set('min_height_error',  'The file height is too small');
    $this->getParameterHolder()->set('min_width',         null);
    $this->getParameterHolder()->set('min_width_error',   'The file width is too small');
    $this->getParameterHolder()->set('is_square',         false);
    $this->getParameterHolder()->set('is_square_error',   'The file is not a square');
 
    $this->getParameterHolder()->add($parameters);
 
    return true;
  }
}
 

How to use it?

In the YAML validation file put this:

  news{photo}:
    file:     yes
    sfFileImageValidator:
      min_height:       100
      min_height_error: 'The image height is too small, it must have minimum 100px'
      min_width:        120
      min_width_error: 'The image width is too small, it must have minimum 120px'
      max_height:       960
      max_height_error: 'The image height is too large, it must have maximum 960px'
      max_width:        450
      max_width_error: 'The image width is too large, it must have maximum 450px'
      is_square:        true
      is_square_error:  'The images must be a square (The height be equal to the width)'
 
      max_size:         256000
      max_size_error:   'The maximum images size is 250Kb'
      mime_types_error: 'We only accept GIF, PNG and JPEG.'
      mime_types:
        - 'image/jpeg'
        - 'image/png'
        - 'image/gif'
 
by Daniel Santiago on 2007-12-26, tagged image  images  validation  validator