Skip to content

Instantly share code, notes, and snippets.

@ftassi
Created March 8, 2012 11:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ftassi/2000596 to your computer and use it in GitHub Desktop.
Save ftassi/2000596 to your computer and use it in GitHub Desktop.
<?php
/*
* Helper per la gestione delle immagini
*/
/**
* Helper per il rendering delle immagini associate ad elementi del model
*
* <b>Options:</b>
* - 'absolute' - to output absolute file paths, useful for embedded images in emails
* - 'alt' - defaults to the file name part of the asset (capitalized and without the extension)
* - 'size' - Supplied as "XxY", so "30x45" becomes width="30" and height="45"
* - 'upload_dir_name' - default to "uploads"
*
* @see image_tag
* @param string $source
* @param array $options
* @return string
*/
function model_image_tag($source, $options = array())
{
if (!$source)
{
return '';
}
$options = _parse_attributes($options);
$absolute = false;
if (isset($options['absolute']))
{
unset($options['absolute']);
$absolute = true;
}
if (!isset($options['upload_dir_name']))
{
$uploadDirName = ltrim(str_replace(sfConfig::get('sf_web_dir'), '', sfConfig::get('sf_upload_dir')), '/');
}
else
{
$uploadDirName = $options['upload_dir_name'];
unset($options['upload_dir_name']);
}
if (!isset($options['raw_name']))
{
$options['src'] = _compute_public_path($source, $uploadDirName, 'png', $absolute);
}
else
{
$options['src'] = $source;
unset($options['raw_name']);
}
if (isset($options['alt_title']))
{
// set as alt and title but do not overwrite explicitly set
if (!isset($options['alt']))
{
$options['alt'] = $options['alt_title'];
}
if (!isset($options['title']))
{
$options['title'] = $options['alt_title'];
}
unset($options['alt_title']);
}
if (isset($options['size']))
{
list($options['width'], $options['height']) = explode('x', $options['size'], 2);
unset($options['size']);
}
return tag('img', $options);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment