Announcement

#1 2021-04-29 17:07:47

plg
Piwigo Team
Nantes, France, Europe
2002-04-05
13791

support for additional file types (adobe...)

On Piwigo.com we have added support for a few file types, such as EPS, PSD (Adobe Photoshop), AI (Adobe Illustrator. The idea is not to keep it for Piwigo.com. I think I will create a plugin to provide this support on all Piwigo. For now, here are some piece of code you can use in a Personal plugin:

Code:

add_event_handler('upload_file', 'upload_file_psd');
function upload_file_psd($representative_ext, $file_path)
{
  global $logger, $conf;

  $logger->info(__FUNCTION__.', $file_path = '.$file_path.', $representative_ext = '.$representative_ext);

  if (isset($representative_ext))
  {
    return $representative_ext;
  }

  if (pwg_image::get_library() != 'ext_imagick')
  {
    return $representative_ext;
  }

  if (!in_array(strtolower(get_extension($file_path)), array('psd')))
  {
    return $representative_ext;
  }

  // move the uploaded file to pwg_representative sub-directory
  $representative_file_path = dirname($file_path).'/pwg_representative/';
  $representative_file_path.= get_filename_wo_extension(basename($file_path)).'.';

  $representative_ext = 'png';
  $representative_file_path.= $representative_ext;

  prepare_directory(dirname($representative_file_path));

  $exec = $conf['ext_imagick_dir'].'convert';

  $exec .= ' "'.realpath($file_path).'"';

  $dest = pathinfo($representative_file_path);
  $exec .= ' "'.realpath($dest['dirname']).'/'.$dest['basename'].'"';

  $exec .= ' 2>&1';
  $logger->info(__FUNCTION__.', exec = '.$exec);
  @exec($exec, $returnarray);

  // sometimes ImageMagick creates file-0.png + file-1.png + file-2.png...
  // It seems we can't avoid it.
  $representative_file_abspath = realpath($dest['dirname']).'/'.$dest['basename'];
  if (!file_exists($representative_file_abspath))
  {
    $first_file_abspath = preg_replace(
      '/\.'.$representative_ext.'$/',
      '-0.'.$representative_ext,
      $representative_file_abspath
      );

    if (file_exists($first_file_abspath))
    {
      rename($first_file_abspath, $representative_file_abspath);
    }
  }

  return get_extension($representative_file_abspath);
}

RAW files, based on JPEG extraction with exiftool.

Code:

add_event_handler('upload_file', 'pcom_upload_file_raw');
function pcom_upload_file_raw($representative_ext, $file_path)
{
  global $logger, $conf;

  $logger->info(__FUNCTION__.', $file_path = '.$file_path.', $representative_ext = '.$representative_ext);

  if (isset($representative_ext))
  {
    return $representative_ext;
  }

  // TODO check exiftool is available

  if (!in_array(strtolower(get_extension($file_path)), array('arw')))
  {
    return $representative_ext;
  }

  // move the uploaded file to pwg_representative sub-directory
  $representative_file_path = dirname($file_path).'/pwg_representative/';
  $representative_file_path.= get_filename_wo_extension(basename($file_path)).'.jpg';

  prepare_directory(dirname($representative_file_path));

  $exec = isset($conf['exiftool_path']) ? $conf['exiftool_path'] : 'exiftool';
  $exec.= ' -b -previewImage';
  $exec.= ' "'.realpath($file_path).'"';

  $dest = pathinfo($representative_file_path);
  $representative_file_abspath = realpath($dest['dirname']).'/'.$dest['basename'];

  $exec .= ' > "'.$representative_file_abspath.'"';

  $exec .= ' 2>&1';
  $logger->info(__FUNCTION__.', exec = '.$exec);
  @exec($exec, $returnarray);

  // let's check the extracted file
  list($width, $height, $type) = getimagesize($representative_file_abspath);
  if (IMAGETYPE_JPEG !== $type)
  {
    return $representative_ext;
  }

  return get_extension($representative_file_abspath);
}

better support for PDF files with pdftoppm command line tool

Code:

add_event_handler('upload_file', 'pcom_upload_file_pdf');
function pcom_upload_file_pdf($representative_ext, $file_path)
{
  global $logger, $conf;

  $logger->info(__FUNCTION__.', $file_path = '.$file_path.', $representative_ext = '.$representative_ext);

  if (isset($representative_ext))
  {
    return $representative_ext;
  }

  if (!isset($conf['use_pdftoppm']) or !$conf['use_pdftoppm'])
  {
    return $representative_ext;
  }

  if (!in_array(strtolower(get_extension($file_path)), array('pdf')))
  {
    return $representative_ext;
  }

  $ext = conf_get_param('pdf_representative_ext', 'jpg');

  // move the uploaded file to pwg_representative sub-directory
  $representative_file_path = original_to_representative($file_path, $ext);
  prepare_directory(dirname($representative_file_path));

  $representative_file_path_wo_ext = get_filename_wo_extension($representative_file_path);

  $exec = 'pdftoppm';
  $exec.= ' "'.realpath($file_path).'"';
  $exec.= ' "'.$representative_file_path_wo_ext.'"';

  if ('jpg' == $ext)
  {
    $exec.= ' -jpeg';
  }
  else
  {
    $exec.= ' -png';
  }

  $exec.= ' -f 1 -singlefile';
  $exec.= ' 2>&1';
  $logger->info(__FUNCTION__.', $exec = '.$exec);
  @exec($exec, $returnarray);

  // Return the extension (if successful) or false (if failed)
  if (file_exists($representative_file_path))
  {
    $representative_ext = $ext;
  }

  return $representative_ext;
}

Support for AI, based on the use of "gs" command line

Code:

add_event_handler('upload_file', 'pcom_upload_file_ai');
function pcom_upload_file_ai($representative_ext, $file_path)
{
  global $logger, $conf;

  $logger->info(__FUNCTION__.', $file_path = '.$file_path.', $representative_ext = '.$representative_ext);

  if (isset($representative_ext))
  {
    return $representative_ext;
  }

  if (!in_array(strtolower(get_extension($file_path)), array('ai')))
  {
    return $representative_ext;
  }

  $ext = 'png';
  $representative_file_path = original_to_representative($file_path, $ext);
  prepare_directory(dirname($representative_file_path));

  // gs -dNOPAUSE -dBATCH -sDEVICE=pngalpha -r300 -sOutputFile=out.png in.ai
  $exec = 'gs -dNOPAUSE -dBATCH -sDEVICE=pngalpha -r300 -sOutputFile=';
  $exec.= '"'.realpath($representative_file_path).'"';
  $exec.= ' "'.realpath($file_path).'"';
  $exec.= ' 2>&1';
  $logger->info(__FUNCTION__.', $exec = '.$exec);
  @exec($exec, $returnarray);

  // Return the extension (if successful) or false (if failed)
  if (file_exists($representative_file_path))
  {
    $representative_ext = $ext;
  }

  return $representative_ext;
}

EPS files

Code:

add_event_handler('upload_file', 'upload_file_eps');
function upload_file_eps($representative_ext, $file_path)
{
  global $logger, $conf;

  $logger->info(__FUNCTION__.', $file_path = '.$file_path.', $representative_ext = '.$representative_ext);

  if (isset($representative_ext))
  {
    return $representative_ext;
  }

  if (pwg_image::get_library() != 'ext_imagick')
  {
    return $representative_ext;
  }

  if (!in_array(strtolower(get_extension($file_path)), array('eps')))
  {
    return $representative_ext;
  }

  // if the representative is "jpg", the derivatives are ugly. With "png" it's fine.
  $ext = 'png';

  // move the uploaded file to pwg_representative sub-directory
  $representative_file_path = original_to_representative($file_path, $ext);
  prepare_directory(dirname($representative_file_path));

  // convert -density 300 image.eps -resize 2048x2048 image.png

  $exec = $conf['ext_imagick_dir'].'convert';
  $exec.= ' -density 300';
  $exec.= ' "'.realpath($file_path).'"';
  $exec.= ' -resize 2048x2048';
  $exec.= ' "'.$representative_file_path.'"';
  $exec.= ' 2>&1';
  @exec($exec, $returnarray);

  // Return the extension (if successful) or false (if failed)
  if (file_exists($representative_file_path))
  {
    $representative_ext = $ext;
  }

  return $representative_ext;
}

Offline

 

#2 2021-05-01 12:53:05

piwent
Member
2020-10-24
97

Re: support for additional file types (adobe...)

This is great! Thanks so much.

Any plans to include tif?

Offline

 

#3 2021-05-01 13:18:46

erAck
Only trying to help
2015-09-06
2029

Re: support for additional file types (adobe...)


Running Piwigo at https://erack.net/gallery/

Offline

 

Board footer

Powered by FluxBB

github twitter newsletter Donate Piwigo.org © 2002-2024 · Contact