290 lines
8.4 KiB
Plaintext
290 lines
8.4 KiB
Plaintext
<?php
|
|
|
|
/**
|
|
* @file
|
|
* Install, update and uninstall functions for the image module.
|
|
*/
|
|
|
|
/**
|
|
* Implements hook_install().
|
|
*/
|
|
function image_install() {
|
|
// Create the styles directory and ensure it's writable.
|
|
$directory = file_default_scheme() . '://styles';
|
|
$mode = isset($GLOBALS['install_state']['mode']) ? $GLOBALS['install_state']['mode'] : NULL;
|
|
file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS, $mode);
|
|
}
|
|
|
|
/**
|
|
* Implements hook_uninstall().
|
|
*/
|
|
function image_uninstall() {
|
|
// Remove the styles directory and generated images.
|
|
file_unmanaged_delete_recursive(file_default_scheme() . '://styles');
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
/**
|
|
* Implements hook_update_dependencies().
|
|
*/
|
|
function image_update_dependencies() {
|
|
// Convert image field and instance setting 'default_image' from integer to
|
|
// array only after fields and instances were converted to config.
|
|
$dependencies['image'][8003] = array('field' => 8006);
|
|
|
|
return $dependencies;
|
|
}
|
|
|
|
/**
|
|
* Loads all effects for an image style from backend.
|
|
*
|
|
* @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_backend_effects(array $style) {
|
|
$result = db_select('image_effects', NULL, array('fetch' => PDO::FETCH_ASSOC))
|
|
->fields('image_effects')
|
|
->condition('isid', $style['isid'])
|
|
->execute();
|
|
$effects = array();
|
|
foreach ($result as $effect) {
|
|
$effect['data'] = unserialize($effect['data']);
|
|
$effects[] = $effect;
|
|
}
|
|
|
|
return _image_update_convert_effects($effects);
|
|
}
|
|
|
|
/**
|
|
* Retuns the default image styles shipped with Drupal 7.
|
|
*
|
|
* @return array
|
|
* An associative array keyed by style id, having the style and associated
|
|
* effects as values. The array values are defined to be usable in
|
|
* image_update_8000().
|
|
*
|
|
* @see image_update_8000()
|
|
* @see https://api.drupal.org/api/drupal/modules%21image%21image.module/function/image_image_default_styles/7
|
|
*/
|
|
function _image_update_get_default_styles() {
|
|
// Clone from Drupal 7 image_image_default_styles().
|
|
$styles = array(
|
|
'thumbnail' => array(
|
|
'label' => 'Thumbnail (100x100)',
|
|
'effects' => array(
|
|
array(
|
|
'name' => 'image_scale',
|
|
'data' => array(
|
|
'width' => '100',
|
|
'height' => '100',
|
|
'upscale' => '1',
|
|
),
|
|
'weight' => '0',
|
|
),
|
|
),
|
|
),
|
|
'medium' => array(
|
|
'label' => 'Medium (220x220)',
|
|
'effects' => array(
|
|
array(
|
|
'name' => 'image_scale',
|
|
'data' => array(
|
|
'width' => '220',
|
|
'height' => '220',
|
|
'upscale' => '1',
|
|
),
|
|
'weight' => '0',
|
|
),
|
|
),
|
|
),
|
|
'large' => array(
|
|
'label' => 'Large (480x480)',
|
|
'effects' => array(
|
|
array(
|
|
'name' => 'image_scale',
|
|
'data' => array(
|
|
'width' => '480',
|
|
'height' => '480',
|
|
'upscale' => '0',
|
|
),
|
|
'weight' => '0',
|
|
),
|
|
),
|
|
),
|
|
);
|
|
|
|
// Add uuid, status, langcode and convert effects.
|
|
$langcode = language_default()->id;
|
|
$uuid = \Drupal::service('uuid');
|
|
foreach ($styles as $id => $style) {
|
|
$styles[$id]['name'] = $id;
|
|
$styles[$id]['uuid'] = $uuid->generate();
|
|
$styles[$id]['status'] = 1;
|
|
$styles[$id]['langcode'] = $langcode;
|
|
$styles[$id]['effects'] = _image_update_convert_effects($style['effects']);
|
|
}
|
|
|
|
return $styles;
|
|
}
|
|
|
|
/**
|
|
* Normalizes effects from Drupal 7 to Drupal 8 format.
|
|
*
|
|
* @param array $legacy_effects
|
|
* An array containing a list of effects obtained from database or from code.
|
|
*
|
|
* @return array
|
|
* A list of effects keyed by effect uuid and having the effect definition in
|
|
* a Drupal 8 configuration format.
|
|
*/
|
|
function _image_update_convert_effects(array $legacy_effects) {
|
|
$uuid = \Drupal::service('uuid');
|
|
$effects = array();
|
|
|
|
foreach ($legacy_effects as $effect) {
|
|
$effect['uuid'] = $uuid->generate();
|
|
|
|
// Use 'id' instead of 'name'.
|
|
$effect['id'] = $effect['name'];
|
|
|
|
// Clear out legacy keys, if case.
|
|
unset($effect['isid'], $effect['ieid'], $effect['name']);
|
|
|
|
$effects[$effect['uuid']] = $effect;
|
|
}
|
|
|
|
return $effects;
|
|
}
|
|
|
|
/**
|
|
* Convert existing image styles to the new config system.
|
|
*/
|
|
function image_update_8000() {
|
|
$langcode = language_default()->id;
|
|
$uuid = \Drupal::service('uuid');
|
|
|
|
$result = db_select('image_styles', NULL, array('fetch' => PDO::FETCH_ASSOC))
|
|
->fields('image_styles')
|
|
->execute();
|
|
|
|
// Prepare image styles from backend.
|
|
$styles = array();
|
|
foreach ($result as $style) {
|
|
$styles[$style['name']] = array(
|
|
'name' => $style['name'],
|
|
'label' => $style['label'],
|
|
'uuid' => $uuid->generate(),
|
|
'status' => 1,
|
|
'langcode' => $langcode,
|
|
'effects' => _image_update_get_backend_effects($style),
|
|
);
|
|
}
|
|
|
|
// Append Drupal 7 default image styles from code. Note that some of them may
|
|
// be overwritten in the backend. Using this array operator assures that we
|
|
// are migrating the overwritten version of the image style.
|
|
$styles += _image_update_get_default_styles();
|
|
|
|
// Save as Drupal 8 configuration.
|
|
foreach ($styles as $id => $style) {
|
|
\Drupal::config('image.style.' . $id)
|
|
->setData($style)
|
|
->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',
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Convert image field and instance setting 'default_image' from integer to
|
|
* array by adding alt, title, width, and height options.
|
|
*/
|
|
function image_update_8003() {
|
|
$image_factory = \Drupal::service('image.factory');
|
|
foreach (array('field', 'instance') as $type) {
|
|
$prefix = "field.$type";
|
|
foreach (config_get_storage_names_with_prefix($prefix) as $config_id) {
|
|
$config = \Drupal::config($config_id);
|
|
$is_image = ($type == 'field' && $config->get('type') == 'image') || ($type == 'instance' && $config->get('field_type') == 'image');
|
|
// Not dealing with an image field or image field instance?
|
|
if (!$is_image) {
|
|
continue;
|
|
}
|
|
$width = 0;
|
|
$height = 0;
|
|
if ($fid = (int) $config->get('settings.default_image')) {
|
|
$uri = db_query('SELECT fid FROM {file_managed} WHERE fid = :fid', array(':fid' => $fid))->fetchField();
|
|
if ($uri) {
|
|
$image = $image_factory->get($uri);
|
|
$width = $image->getWidth();
|
|
$height = $image->getHeight();
|
|
}
|
|
}
|
|
$default_image = array(
|
|
'fid' => $fid ?: NULL,
|
|
'alt' => '',
|
|
'title' => '',
|
|
'width' => $width ?: NULL,
|
|
'height' => $height ?: NULL,
|
|
);
|
|
$config
|
|
->set('settings.default_image', $default_image)
|
|
->save();
|
|
}
|
|
}
|
|
}
|