You are here

function dynamic_background_save_image in Dynamic Background 7.2

Helper function thats used to save an uploaded image on the server and store it in the database.

Parameters

object $file_field: The file field was used to upload the image.

string $extension: The extension that the image should be locationed under.

mixed $data: Identifier defined by the extension that the image should be located under.

Return value

type

2 calls to dynamic_background_save_image()
dynamic_background_admin_images_validate in includes/backgrounds.admin.inc
The administration images form validator form. It handles uploading the image and deletion if the checkbox is flaged.
dynamic_background_user_upload_form_submit in ./dynamic_background.module
Submission callback for user uploaded image, which stores the image(s) uploaded and stores the last uploaded image in the $form_state values info array. So extension automatically can activate the last uploaded image.

File

./dynamic_background.module, line 885
This module enables administrators to upload images used as background on the site. The selected background image link is exposed as either $background in the page.tpl file or as /background.css.

Code

function dynamic_background_save_image($file_field, $extension, $data = -1) {

  // Load settings.
  $settings = variable_get('dynamic_background_setting', array());

  // Validate the file extension.
  $validators = array(
    'file_validate_extensions' => array(
      $settings['extensions'],
    ),
  );

  // Save the file temporally.
  if ($file = file_save_upload($file_field, $validators)) {
    $destination = file_default_scheme() . '://' . $settings['path'] . '/' . $file->filename;

    // Copy file and maybe rename it.
    if ($file = file_move($file, $destination, FILE_EXISTS_RENAME)) {

      // Mark file as permanent.
      $file->status |= FILE_STATUS_PERMANENT;
      file_save($file);

      // Save the image to the dynamic background tables.
      db_insert('dynamic_background_images')
        ->fields(array(
        'fid' => $file->fid,
        'data' => $data,
        'extension' => $extension,
      ))
        ->execute();

      // Return file id.
      return $file->fid;
    }
    else {
      throw new Exception(t("Failed to upload the image; the %directory directory doesn't exist or is not writable.", array(
        '%directory' => file_default_scheme() . '://' . $settings['path'],
      )));
    }
  }
  else {
    throw new Exception(t("Failed to upload the image; the image type should be jpeg, jpg or png."));
  }
}