4

I want to create a waveform image for every mp3 file I upload in a Drupal 7 website (as admin), and store it as a png for display in the frontend. I can use this script in order to do that.

I want to know what is the Drupal way of integrating that script, specifically:

  • I guess that functionality will go into a module; where should I hook? file upload (there will be more than one file types to upload, and I would just want to work on the mp3)?
  • how should I save the resulting waveform picture (where in the filesystem and how would I attach it as a field to my custom node type)?

As I'm new to Drupal (but experienced with PHP), I would appreciate a detailed response, but if you know of a module that already does that - I couldn't find it - that would be the shortest answer possible. :)

Thank you!

  • Which (major) version of Drupal are you using? – Madis Jan 19 '12 at 21:20
  • I edited my question and specified Drupal version (7), thanks Dooshta! – whonoes Jan 20 '12 at 7:39
2

BetaRide gave you some good links and generic suggestions already, here's some code to help you get a quicker start:

function mymodule_node_presave($node) {
  //drupal_set_message('<pre>'.var_export($node, TRUE).'</pre>');
  if (!isset($node->field_pic['und'][0]) && isset($node->field_file['und'][0]['fid'])) {
    $result = db_query("SELECT filename, uri, filemime FROM {file_managed} WHERE fid = :fid",
    array(':fid' => $node->field_file['und'][0]['fid']));
    $record = $result->fetchObject(); 
    if ($record->filemime == 'audio/mpeg' && pathinfo($record->filename, PATHINFO_EXTENSION) == 'mp3') {
      // TODO - create the picture, save in the temporary files folder and return the filename.
      $filename = 'created_image.png';
      $image = file_get_contents('sites/default/files/tmp/'.$filename);
      // public://pics/ -> sites/default/files/pics
      $file = file_save_data($image, 'public://pics/'.$filename, FILE_EXISTS_RENAME);
      drupal_unlink('sites/default/files/tmp/'.$filename);
      $node->field_pic['und'][0] = (array) $file;
    }
  }
}

This script should work when you have created a content type with 'field_file' and 'field_pic' that both allow 1 entry. First one should accept at least mp3 files and the second png images your linked script will create. The function/hook gets called before saving a node. If there is no image present in the image field and a mp3 file is present in the other you should use the script to create the image and return it's filename. After that a copy of the file from 'sites/default/files/tmp/' gets created into 'sites/default/files/pics' (possible name conflicts are taken care of). Then the temporary file gets deleted and the image info attached to the node. As you might have guessed 'sites/default/files' and it's subfolders should be used to store public files on Drupal sites. You should also make sure 'field_pic' file directory (set up via the GUI) matches with the one used in the code.

  • This makes so much sense now (and damn how elegant Drupal seems to be!), thanks Dooshta! – whonoes Jan 21 '12 at 0:19
3
  1. Create a content type with a file field to upload the mp3 file.
  2. Create a custom module and implement hook_node_presave. There you can do whatever you want with your node before it gets saved.
  3. For the file handling have a look at File interface.
  • Thank you for the pointers and for explaining the workflow! – whonoes Jan 21 '12 at 0:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.