0

I used Form API to create an image upload form, where images get uploaded via managed_file. The upload process works just fine, but the uploaded files don't get listed in the admin/content/file overview that is provided by the File Entity Module. The overview only shows those files that were uploaded via file/add, File Entity's own upload form.

My code is:


/**
 * Create an image upload form
 */

function upload_form_image_form($form, &$form_state) {

  // Image file
  $form['image'] = array(
    '#type' => 'managed_file',
    '#name' => 'image',
    '#title' => t('Image file'),
    '#size' => 40,
    '#description' => t("Image should be in JPG format."),
    '#upload_location' => 'public://',
    '#required' => TRUE,
  );

  // Submit
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
  );

  return $form; 
}

/**
 * Submit the upload form and scale the image down to standard resolution
 */

function upload_form_image_form_submit($form, &$form_state) {
  if (isset($form_state['values']['image'])) {

    $image = file_load($form_state['values']['image']);

    $loadedimage = image_load($image->uri);
    list($width, $height) = getimagesize($image->uri);
    if ($width/$height >= 1) {
      image_scale($loadedimage, 900, null, $upscale = FALSE);
      }
    else {
      image_scale($loadedimage, null, 900, $upscale = FALSE);
      }

    image_save($loadedimage);
    $loadedimage->status = FILE_STATUS_PERMANENT;

  }
}
1

I think your problem is coming from calling image_save and not file_save. They are non synonymous so far as I can tell.

A managed_file is an uploaded file, and usually records an owner of the file with file_usage_add(). This is how drupal can manage whether the file is still needed by the system or can be unlinked or deleted when cron is run.

See this other drupal stackexchange answer on how to use the managed_file FAPI element. It should answer your question on how to get the (original) file saved. You could then create derivatives of the file with image processing as you've already successfully done; after the original file is saved.

I don't believe there is a streamlined solution for this in the file_entity module or in any image functions (I doubt it, as one thing is to upload and manage the file itself -- and another is to do proccessing on the image itself).

  • Okay, this is super weird, even with File Entity's own file upload form: 1) If I go to file/add (File Entity's own file upload form), choose an image file, click UPLOAD, then click SUBMIT, and on the edit form click SAVE... the image file gets listed in the admin/content/file overview. 2) But if I go to file/add, choose an image file, do NOT click upload but only submit, and on the edit form click save... the image file does NOT get listed in the admin/content/file overview! Could you check if you experience the same issue? – deinqwertz Oct 15 '12 at 22:18
  • Alright, so this behavior has been fixed in the latest File Entity version. – deinqwertz Oct 21 '12 at 16:51
  • Great, thank you so much! It works when I save the file via file_save(), load it again via image_load(), and then save it once more via image_save(). – deinqwertz Oct 21 '12 at 17:10

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.