Announcement

  •  » Engine
  •  » My solution to implement new image types for example ico, svg and webp

#1 2021-05-01 03:05:07

SourceCoder
Member
2021-04-30
5

My solution to implement new image types for example ico, svg and webp

Hello community, this is my first post.

I have a collection with some different file types like ico, svg and webp. Piwigo doesn't know how to handle these file types with autogenerated thumbnails. So this is my solution to handle these file types with autogenerated thumbnails in Piwigo 11.4.0.

Step 1: Generate a local config file under <Piwigo Directory>\local\config\ with the filename config.inc.php like this

Code:

<?php
// picture_ext : file extensions for picture file, must be a subset of
// file_ext
$conf['picture_ext'] = array( 'gif', 'jpg', 'jpeg', 'png');

// file_ext : file extensions (case sensitive) authorized
$conf['file_ext'] = array_merge(
    $conf['picture_ext'],
    array('ico', 'svg', 'tif', 'tiff', 'webp', 'asf', 'avi', 'divx', 'flv', 
    'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'ogg', 'ogv', 'rm', 'ts', 
    'webm', 'webmv', 'wmv', 'xvid')
  );
  
// in the upload form, let users upload only picture_exts or all file_exts?
// for some file types, Piwigo will try to generate a pwg_representative
// (TIFF, videos, PDF)
$conf['upload_form_all_types'] = true;

// 'png' or 'jpg': your uploaded TIF photos will have a representative in
// JPEG or PNG file format
$conf['tiff_representative_ext'] = 'jpg';

// 'png' or 'jpg': your uploaded file will have a representative in
// JPEG or PNG file format
$conf['file_representative_ext'] = 'jpg';

// If library used is external installation of ImageMagick ('ext_imagick'),
// you can define imagemagick directory.
$conf['ext_imagick_dir'] = 'E:/Program/ImageMagick/';

// If we try to generate a pwg_representative for a video we use ffmpeg. If
// "ffmpeg" is not visible by the web user, you can define the full path of
// the directory where "ffmpeg" executable is.
$conf['ffmpeg_dir'] = 'E:/Program/ffmpeg/';
?>

It is important to set the imagemagick directory with this parameter $conf['ext_imagick_dir']. Attention for the last slash. So Piwigo concatenate 'E:/Program/ImageMagick/' + 'convert.exe' to 'E:/Program/ImageMagick/convert.exe' from the the imagemagick directory.

Also it is important to set the ffmpeg directory with this parameter $conf['ffmpeg_dir']. Attention for the last slash too. So Piwigo concatenate 'E:/Program/ffmpeg/' + 'ffmpeg.exe' to 'E:/Program/ffmpeg/ffmpeg.exe' from the the ffmpeg directory.

Additionally I define a new parameter $conf['file_representative_ext'] for my new image types.

The parameters $conf['picture_ext'], $conf['file_ext'] and $conf['upload_form_all_types'] are confusing, but if you set this parameters like above all work fine.

Step 2: Change the file from Piwigo who controls the fileupload: <Piwigo Directory>\admin\include\functions_upload.inc.php

Attention: Create a backup file from <Piwigo Directory>\admin\include\functions_upload.inc.php before you change it!

For the first time i open this file i determine, that so parameters are not existing in the global configuration file under <Piwigo Directory>\include\config_default.inc.php. This parameters are missing in the global configuration file: $conf['pdf_jpg_quality'] and $conf['tiff_jpg_quality']

Here comes my changes for the file <Piwigo Directory>\admin\include\functions_upload.inc.php:


-Changes in: function upload_file_pdf($representative_ext, $file_path)

From

Code:

$jpg_quality = conf_get_param('pdf_jpg_quality', 90);

to

Code:

$jpg_quality = conf_get_param('pdf_jpg_quality', 95);

for better default quality preview image (quality only effective jpg) for pdf file.


-Changes in: function upload_file_tiff($representative_ext, $file_path)

From

Code:

function upload_file_tiff($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('tif', 'tiff')))
  {
    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 = $conf['tiff_representative_ext'];
  $representative_file_path.= $representative_ext;

  prepare_directory(dirname($representative_file_path));

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

  if ('jpg' == $conf['tiff_representative_ext'])
  {
    $exec .= ' -quality 98';
  }

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

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

  $exec .= ' 2>&1';
  @exec($exec, $returnarray);

  // sometimes ImageMagick creates file-0.jpg (full size) + file-1.jpg
  // (thumbnail). I don't know how to 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);
}

to

Code:

function upload_file_tiff($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('tif', 'tiff')))
  {
    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 = conf_get_param('tiff_representative_ext', 'jpg');
  $jpg_quality = conf_get_param('tiff_jpg_quality', 95);
  
  $representative_file_path.= $representative_ext;

  prepare_directory(dirname($representative_file_path));

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

  if ('jpg' == $representative_ext)
  {
    $exec.= ' -quality '.$jpg_quality;
  }

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

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

  $exec .= ' 2>&1';
  @exec($exec, $returnarray);

  // sometimes ImageMagick creates file-0.jpg (full size) + file-1.jpg
  // (thumbnail). I don't know how to 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);
}

for better default quality preview image (quality only effective jpg) for tiff file.


-New function: function upload_file_ico_svg_webp($representative_ext, $file_path)

Code:

add_event_handler('upload_file', 'upload_file_ico_svg_webp');
function upload_file_ico_svg_webp($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('ico', 'svg', 'webp')))
  {
    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 = conf_get_param('file_representative_ext', 'jpg');
  $jpg_quality = conf_get_param('file_jpg_quality', 95);
  
  $representative_file_path.= $representative_ext;

  prepare_directory(dirname($representative_file_path));

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

  if ('jpg' == $representative_ext)
  {
    $exec.= ' -quality '.$jpg_quality;
  }

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

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

  $exec .= ' 2>&1';
  @exec($exec, $returnarray);

  // sometimes ImageMagick creates file-0.jpg (full size) + file-1.jpg
  // (thumbnail). I don't know how to 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);
}

This new function allows users to upload ico, svg and webp file types and autogenerate thumbnails for these files.
I define two new parameters $conf['file_representative_ext'] for 'jpg' or 'png' and $conf['file_jpg_quality'] for better default quality preview image (quality only effective jpg) for the new image file types.


-Changes in: function upload_file_video($representative_ext, $file_path)

From

Code:

function upload_file_video($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;
  }

  $ffmpeg_video_exts = array( // extensions tested with FFmpeg
    'wmv','mov','mkv','mp4','mpg','flv','asf','xvid','divx','mpeg',
    'avi','rm', 'm4v', 'ogg', 'ogv', 'webm', 'webmv',
    );

  if (!in_array(strtolower(get_extension($file_path)), $ffmpeg_video_exts))
  {
    return $representative_ext;
  }

  $representative_file_path = dirname($file_path).'/pwg_representative/';
  $representative_file_path.= get_filename_wo_extension(basename($file_path)).'.';

  $representative_ext = 'jpg';
  $representative_file_path.= $representative_ext;

  prepare_directory(dirname($representative_file_path));

  $second = 1;

  $ffmpeg = $conf['ffmpeg_dir'].'ffmpeg';
  $ffmpeg.= ' -i "'.$file_path.'"';
  $ffmpeg.= ' -an -ss '.$second;
  $ffmpeg.= ' -t 1 -r 1 -y -vcodec mjpeg -f mjpeg';
  $ffmpeg.= ' "'.$representative_file_path.'"';

  @exec($ffmpeg);

  if (!file_exists($representative_file_path))
  {
    return null;
  }

  return $representative_ext;
}

to

Code:

function upload_file_video($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;
  }

  // extensions tested with ffmpeg
  $ffmpeg_video_exts = array(
      'asf', 'avi', 'divx', 'flv', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 
    'ogg', 'ogv', 'rm', 'ts', 'webm', 'webmv', 'wmv', 'xvid'    
    );

  if (!in_array(strtolower(get_extension($file_path)), $ffmpeg_video_exts))
  {
    return $representative_ext;
  }

  $representative_file_path = dirname($file_path).'/pwg_representative/';
  $representative_file_path.= get_filename_wo_extension(basename($file_path)).'.';

  $representative_ext = conf_get_param('file_representative_ext', 'jpg');
  $representative_file_path.= $representative_ext;

  prepare_directory(dirname($representative_file_path));

  $second = 1;

  $ffmpeg = $conf['ffmpeg_dir'].'ffmpeg';
  $ffmpeg.= ' -i "'.$file_path.'"';
  $ffmpeg.= ' -an -ss '.$second;
  $ffmpeg.= ' -t 1 -r 1 -y';
  $ffmpeg.= ' "'.$representative_file_path.'"';

  @exec($ffmpeg);

  if (!file_exists($representative_file_path))
  {
    return null;
  }

  return $representative_ext;
}

Now users can upload more video file types like 'ts' and the autogenerate thumbnails for videos can be select by $conf['file_representative_ext'] either 'jpg' or 'png'.

Please let me know if this works for you or you can give me some improvements. May be this code can flow in one of the next versions of Piwigo.

So far.

Offline

 

#2 2021-05-01 13:17:18

erAck
Only trying to help
2015-09-06
1366

Re: My solution to implement new image types for example ico, svg and webp


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

Offline

 

#3 2021-05-01 15:11:32

SourceCoder
Member
2021-04-30
5

Re: My solution to implement new image types for example ico, svg and webp

@erAck:
Thank you very much for your answer and for the interesting php code snippets to implement further file types from user plg (https://piwigo.org/forum/viewtopic.php?id=31219). I'll try this to implement these snippets in <Piwigo Directory>\admin\include\functions_upload.inc.php too.

I think it is a good idea to exclude the control of any file types to a new plug-in in the style
of the plug-in Charlies' content with more setting possibilities (https://piwigo.org/ext/extension_view.php?eid=119).

Offline

 

#4 2021-09-23 18:43:42

Nigel-Aves
Member
2015-09-22
41

Re: My solution to implement new image types for example ico, svg and webp

erAck / SorceCoder,

I was looking into the webp issue this morning. I did try the plugin from OptiPic but all that gave me was a page of "broken image" icons. (I have written to their support.)

I looked at the solution posted by SourceCoder but that implementation was way above my pay grade as a non-programmer :)

I was wondering if there was any headway on implementing into the code? One thing I was unsure about in the write up is, will this method automatically create the .webp images from the original uploaded .jpgs?  (I need my models to be able to download the original high resolution JPG, and on a couple of my sites I have 1000's of photographs that would be very difficult to convert the .webp)

Anyways, what we do know is that we need a native way of doing this.

Many Thanks - Nigel Aves.

Offline

 

#5 2021-09-24 02:42:29

Zentalquabula
Member
2014-05-10
208

Re: My solution to implement new image types for example ico, svg and webp

Wordpress has gone webp now as well. Would probably be easy for the Piwigo team to implement, and hard to do with a plugin.

Offline

 

#6 2022-03-24 17:36:22

Nigel-Aves
Member
2015-09-22
41

Re: My solution to implement new image types for example ico, svg and webp

Has there been any movement on supporting the webp format?

Many Thanks - Nigel.

Offline

 

#7 2022-06-15 17:24:36

arseniy
Member
2015-12-08
11

Re: My solution to implement new image types for example ico, svg and webp

I am noob and have no time for web things. I wish PIWIGO had support for webp already

Offline

 
  •  » Engine
  •  » My solution to implement new image types for example ico, svg and webp

Board footer

Powered by FluxBB

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