image.install

Install, update and uninstall functions for the image module.

File

drupal/core/modules/image/image.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the image module.
 */
use Drupal\Component\Uuid\Uuid;

/**
 * Implements hook_install().
 */
function image_install() {

  // Create the styles directory and ensure it's writable.
  $directory = file_default_scheme() . '://styles';
  file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
}

/**
 * Implements hook_uninstall().
 */
function image_uninstall() {

  // Remove the styles directory and generated images.
  file_unmanaged_delete_recursive(file_default_scheme() . '://styles');
}

/**
 * Implements hook_field_schema().
 */
function image_field_schema($field) {
  return array(
    'columns' => array(
      'fid' => array(
        'description' => 'The {file_managed}.fid being referenced in this field.',
        'type' => 'int',
        'not null' => FALSE,
        'unsigned' => TRUE,
      ),
      'alt' => array(
        'description' => "Alternative image text, for the image's 'alt' attribute.",
        'type' => 'varchar',
        'length' => 512,
        'not null' => FALSE,
      ),
      'title' => array(
        'description' => "Image title text, for the image's 'title' attribute.",
        'type' => 'varchar',
        'length' => 1024,
        'not null' => FALSE,
      ),
      'width' => array(
        'description' => 'The width of the image in pixels.',
        'type' => 'int',
        'unsigned' => TRUE,
      ),
      'height' => array(
        'description' => 'The height of the image in pixels.',
        'type' => 'int',
        'unsigned' => TRUE,
      ),
    ),
    'indexes' => array(
      'fid' => array(
        'fid',
      ),
    ),
    'foreign keys' => array(
      'fid' => array(
        'table' => 'file_managed',
        'columns' => array(
          'fid' => 'fid',
        ),
      ),
    ),
  );
}

/**
 * Implements hook_requirements() to check the PHP GD Library.
 *
 * @param $phase
 */
function image_requirements($phase) {
  $requirements = array();
  if ($phase == 'runtime') {

    // Check for the PHP GD library.
    if (function_exists('imagegd2')) {
      $info = gd_info();
      $requirements['image_gd'] = array(
        'value' => $info['GD Version'],
      );

      // Check for filter and rotate support.
      if (!function_exists('imagefilter') || !function_exists('imagerotate')) {
        $requirements['image_gd']['severity'] = REQUIREMENT_WARNING;
        $requirements['image_gd']['description'] = t('The GD Library for PHP is enabled, but was compiled without support for functions used by the rotate and desaturate effects. It was probably compiled using the official GD libraries from http://www.libgd.org instead of the GD library bundled with PHP. You should recompile PHP --with-gd using the bundled GD library. See <a href="@url">the PHP manual</a>.', array(
          '@url' => 'http://www.php.net/manual/book.image.php',
        ));
      }
    }
    else {
      $requirements['image_gd'] = array(
        'value' => t('Not installed'),
        'severity' => REQUIREMENT_ERROR,
        'description' => t('The GD library for PHP is missing or outdated. Check the <a href="@url">PHP image documentation</a> for information on how to correct this.', array(
          '@url' => 'http://www.php.net/manual/book.image.php',
        )),
      );
    }
    $requirements['image_gd']['title'] = t('GD library rotate and desaturate effects');
  }
  return $requirements;
}

/**
 * Loads all effects for an image style.
 *
 * @param array $style
 *   The image style (array) to retrieve effects for.
 *
 * @return array
 *   An array of effects keyed by UUIDs.
 *
 * @see image_update_8000()
 */
function _image_update_get_style_with_effects(array $style) {

  // Retrieve image effects.
  $effects = array();
  $result = db_select('image_effects', NULL, array(
    'fetch' => PDO::FETCH_ASSOC,
  ))
    ->fields('image_effects')
    ->condition('isid', $style['isid'])
    ->execute();
  foreach ($result as $effect) {
    unset($effect['isid']);
    $effect['data'] = unserialize($effect['data']);

    // Generate a unique image effect ID for the effect.
    $uuid = new Uuid();
    $effect['ieid'] = $uuid
      ->generate();
    $effects[$effect['ieid']] = $effect;
  }
  return $effects;
}

/**
 * Convert existing image styles to the new config system.
 */
function image_update_8000() {
  $styles = array();
  $result = db_select('image_styles', NULL, array(
    'fetch' => PDO::FETCH_ASSOC,
  ))
    ->fields('image_styles')
    ->execute()
    ->fetchAllAssoc('name', PDO::FETCH_ASSOC);
  foreach ($result as $style_name => $style) {
    $style['effects'] = _image_update_get_style_with_effects($style);
    $styles[$style_name] = $style;
  }

  // Convert each style into a configuration object.
  foreach ($styles as $name => $style) {
    $config = config('image.style.' . $name);
    $config
      ->set('name', $name);
    $config
      ->set('effects', $style['effects']);
    $config
      ->save();
  }
}

/**
 * Empty update. See http://drupal.org/node/1980058.
 */
function image_update_8001() {
}

/**
 * Moves image module settings from variable to config.
 *
 * @ingroup config_upgrade
 */
function image_update_8002() {
  update_variables_to_config('image.settings', array(
    'image_style_preview_image' => 'preview_image',
  ));
}

Functions

Namesort descending Description
image_field_schema Implements hook_field_schema().
image_install Implements hook_install().
image_requirements Implements hook_requirements() to check the PHP GD Library.
image_uninstall Implements hook_uninstall().
image_update_8000 Convert existing image styles to the new config system.
image_update_8001 Empty update. See http://drupal.org/node/1980058.
image_update_8002 Moves image module settings from variable to config.
_image_update_get_style_with_effects Loads all effects for an image style.