Skip to content

Instantly share code, notes, and snippets.

@jorisros
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save jorisros/1c28599f4b29e804a21c to your computer and use it in GitHub Desktop.

Select an option

Save jorisros/1c28599f4b29e804a21c to your computer and use it in GitHub Desktop.
Simple class to get a preview above the upload button if file is already uploaded
<?php
/**
* Required plugin
* https://github.com/aalbagarcia/sfThumbnailPlugin
*/
/**
* Class sfWidgetFormInputFileWithPreview
*
* Simple class to get a preview above the upload button if file is already uploaded
*
* @example
*
* $this->setWidget('image', new sfWidgetFormInputFileWithPreview());
* $this->setValidator('image', new sfValidatorFile(array(
* 'mime_types' => 'web_images',
* 'path' => sfConfig::get('sf_root_dir').'/data/images/',
* 'required'=>false,
* )));
*
*/
class sfWidgetFormInputFileWithPreview extends sfWidgetFormInputFile
{
public function render($name, $value = null, $attributes = array(), $errors = array())
{
$html = '';
if($value)
{
$fileName = explode('.', $value);
$thumbnail = new sfThumbnail(150, 150);
//@todo Create a more flexible way to save the paths
$thumbnail->loadFile(sfConfig::get('sf_root_dir').'/data/images/'.$value);
$thumbnail->save(sfConfig::get('sf_upload_dir').'/thumbnails/150x150_'.$fileName[0].'.png', 'image/png');
$html .= $this->renderTag('img', array('src'=>'/uploads/thumbnails/150x150_'.$fileName[0].'.png'));
}
$html .= $this->renderTag('input', array_merge(array('type' => $this->getOption('type'), 'name' => $name, 'value' => $value), $attributes));
return $html;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment