image.install 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the image module.
  5. */
  6. /**
  7. * Implements hook_install().
  8. */
  9. function image_install() {
  10. // Create the styles directory and ensure it's writable.
  11. $directory = file_default_scheme() . '://styles';
  12. file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
  13. }
  14. /**
  15. * Implements hook_uninstall().
  16. */
  17. function image_uninstall() {
  18. // Remove the styles directory and generated images.
  19. file_unmanaged_delete_recursive(file_default_scheme() . '://styles');
  20. }
  21. /**
  22. * Implements hook_schema().
  23. */
  24. function image_schema() {
  25. $schema = array();
  26. $schema['cache_image'] = drupal_get_schema_unprocessed('system', 'cache');
  27. $schema['cache_image']['description'] = 'Cache table used to store information about image manipulations that are in-progress.';
  28. $schema['image_styles'] = array(
  29. 'description' => 'Stores configuration options for image styles.',
  30. 'fields' => array(
  31. 'isid' => array(
  32. 'description' => 'The primary identifier for an image style.',
  33. 'type' => 'serial',
  34. 'unsigned' => TRUE,
  35. 'not null' => TRUE,
  36. ),
  37. 'name' => array(
  38. 'description' => 'The style machine name.',
  39. 'type' => 'varchar',
  40. 'length' => 255,
  41. 'not null' => TRUE,
  42. ),
  43. 'label' => array(
  44. 'description' => 'The style administrative name.',
  45. 'type' => 'varchar',
  46. 'length' => 255,
  47. 'not null' => TRUE,
  48. 'default' => '',
  49. ),
  50. ),
  51. 'primary key' => array('isid'),
  52. 'unique keys' => array(
  53. 'name' => array('name'),
  54. ),
  55. );
  56. $schema['image_effects'] = array(
  57. 'description' => 'Stores configuration options for image effects.',
  58. 'fields' => array(
  59. 'ieid' => array(
  60. 'description' => 'The primary identifier for an image effect.',
  61. 'type' => 'serial',
  62. 'unsigned' => TRUE,
  63. 'not null' => TRUE,
  64. ),
  65. 'isid' => array(
  66. 'description' => 'The {image_styles}.isid for an image style.',
  67. 'type' => 'int',
  68. 'unsigned' => TRUE,
  69. 'not null' => TRUE,
  70. 'default' => 0,
  71. ),
  72. 'weight' => array(
  73. 'description' => 'The weight of the effect in the style.',
  74. 'type' => 'int',
  75. 'unsigned' => FALSE,
  76. 'not null' => TRUE,
  77. 'default' => 0,
  78. ),
  79. 'name' => array(
  80. 'description' => 'The unique name of the effect to be executed.',
  81. 'type' => 'varchar',
  82. 'length' => 255,
  83. 'not null' => TRUE,
  84. ),
  85. 'data' => array(
  86. 'description' => 'The configuration data for the effect.',
  87. 'type' => 'blob',
  88. 'not null' => TRUE,
  89. 'size' => 'big',
  90. 'serialize' => TRUE,
  91. ),
  92. ),
  93. 'primary key' => array('ieid'),
  94. 'indexes' => array(
  95. 'isid' => array('isid'),
  96. 'weight' => array('weight'),
  97. ),
  98. 'foreign keys' => array(
  99. 'image_style' => array(
  100. 'table' => 'image_styles',
  101. 'columns' => array('isid' => 'isid'),
  102. ),
  103. ),
  104. );
  105. return $schema;
  106. }
  107. /**
  108. * Implements hook_field_schema().
  109. */
  110. function image_field_schema($field) {
  111. return array(
  112. 'columns' => array(
  113. 'fid' => array(
  114. 'description' => 'The {file_managed}.fid being referenced in this field.',
  115. 'type' => 'int',
  116. 'not null' => FALSE,
  117. 'unsigned' => TRUE,
  118. ),
  119. 'alt' => array(
  120. 'description' => "Alternative image text, for the image's 'alt' attribute.",
  121. 'type' => 'varchar',
  122. 'length' => 512,
  123. 'not null' => FALSE,
  124. ),
  125. 'title' => array(
  126. 'description' => "Image title text, for the image's 'title' attribute.",
  127. 'type' => 'varchar',
  128. 'length' => 1024,
  129. 'not null' => FALSE,
  130. ),
  131. 'width' => array(
  132. 'description' => 'The width of the image in pixels.',
  133. 'type' => 'int',
  134. 'unsigned' => TRUE,
  135. ),
  136. 'height' => array(
  137. 'description' => 'The height of the image in pixels.',
  138. 'type' => 'int',
  139. 'unsigned' => TRUE,
  140. ),
  141. ),
  142. 'indexes' => array(
  143. 'fid' => array('fid'),
  144. ),
  145. 'foreign keys' => array(
  146. 'fid' => array(
  147. 'table' => 'file_managed',
  148. 'columns' => array('fid' => 'fid'),
  149. ),
  150. ),
  151. );
  152. }
  153. /**
  154. * Implements hook_update_dependencies().
  155. */
  156. function image_update_dependencies() {
  157. $dependencies['image'][7002] = array(
  158. // Image update 7002 uses field API functions, so must run after
  159. // Field API has been enabled.
  160. 'system' => 7020,
  161. );
  162. return $dependencies;
  163. }
  164. /**
  165. * Install the schema for users upgrading from the contributed module.
  166. */
  167. function image_update_7000() {
  168. if (!db_table_exists('image_styles')) {
  169. $schema = array();
  170. $schema['cache_image'] = system_schema_cache_7054();
  171. $schema['cache_image']['description'] = 'Cache table used to store information about image manipulations that are in-progress.';
  172. $schema['image_styles'] = array(
  173. 'description' => 'Stores configuration options for image styles.',
  174. 'fields' => array(
  175. 'isid' => array(
  176. 'description' => 'The primary identifier for an image style.',
  177. 'type' => 'serial',
  178. 'unsigned' => TRUE,
  179. 'not null' => TRUE,
  180. ),
  181. 'name' => array(
  182. 'description' => 'The style name.',
  183. 'type' => 'varchar',
  184. 'length' => 255,
  185. 'not null' => TRUE,
  186. ),
  187. ),
  188. 'primary key' => array('isid'),
  189. 'unique keys' => array(
  190. 'name' => array('name'),
  191. ),
  192. );
  193. $schema['image_effects'] = array(
  194. 'description' => 'Stores configuration options for image effects.',
  195. 'fields' => array(
  196. 'ieid' => array(
  197. 'description' => 'The primary identifier for an image effect.',
  198. 'type' => 'serial',
  199. 'unsigned' => TRUE,
  200. 'not null' => TRUE,
  201. ),
  202. 'isid' => array(
  203. 'description' => 'The {image_styles}.isid for an image style.',
  204. 'type' => 'int',
  205. 'unsigned' => TRUE,
  206. 'not null' => TRUE,
  207. 'default' => 0,
  208. ),
  209. 'weight' => array(
  210. 'description' => 'The weight of the effect in the style.',
  211. 'type' => 'int',
  212. 'unsigned' => FALSE,
  213. 'not null' => TRUE,
  214. 'default' => 0,
  215. ),
  216. 'name' => array(
  217. 'description' => 'The unique name of the effect to be executed.',
  218. 'type' => 'varchar',
  219. 'length' => 255,
  220. 'not null' => TRUE,
  221. ),
  222. 'data' => array(
  223. 'description' => 'The configuration data for the effect.',
  224. 'type' => 'blob',
  225. 'not null' => TRUE,
  226. 'size' => 'big',
  227. 'serialize' => TRUE,
  228. ),
  229. ),
  230. 'primary key' => array('ieid'),
  231. 'indexes' => array(
  232. 'isid' => array('isid'),
  233. 'weight' => array('weight'),
  234. ),
  235. 'foreign keys' => array(
  236. 'image_style' => array(
  237. 'table' => 'image_styles',
  238. 'columns' => array('isid' => 'isid'),
  239. ),
  240. ),
  241. );
  242. db_create_table('cache_image', $schema['cache_image']);
  243. db_create_table('image_styles', $schema['image_styles']);
  244. db_create_table('image_effects', $schema['image_effects']);
  245. }
  246. }
  247. /**
  248. * @addtogroup updates-7.x-extra
  249. * @{
  250. */
  251. /**
  252. * Rename possibly misnamed {image_effect} table to {image_effects}.
  253. */
  254. function image_update_7001() {
  255. // Due to a bug in earlier versions of image_update_7000() it is possible
  256. // to end up with an {image_effect} table where there should be an
  257. // {image_effects} table.
  258. if (!db_table_exists('image_effects') && db_table_exists('image_effect')) {
  259. db_rename_table('image_effect', 'image_effects');
  260. }
  261. }
  262. /**
  263. * Add width and height columns to a specific table.
  264. *
  265. * @param $table
  266. * The name of the database table to be updated.
  267. * @param $columns
  268. * Keyed array of columns this table is supposed to have.
  269. */
  270. function _image_update_7002_add_columns($table, $field_name) {
  271. $spec = array(
  272. 'type' => 'int',
  273. 'unsigned' => TRUE,
  274. );
  275. $spec['description'] = 'The width of the image in pixels.';
  276. db_add_field($table, $field_name . '_width', $spec);
  277. $spec['description'] = 'The height of the image in pixels.';
  278. db_add_field($table, $field_name . '_height', $spec);
  279. }
  280. /**
  281. * Populate image dimensions in a specific table.
  282. *
  283. * @param $table
  284. * The name of the database table to be updated.
  285. * @param $columns
  286. * Keyed array of columns this table is supposed to have.
  287. * @param $last_fid
  288. * The fid of the last image to have been processed.
  289. *
  290. * @return
  291. * The number of images that were processed.
  292. */
  293. function _image_update_7002_populate_dimensions($table, $field_name, &$last_fid) {
  294. // Define how many images to process per pass.
  295. $images_per_pass = 100;
  296. // Query the database for fid / URI pairs.
  297. $query = db_select($table, NULL, array('fetch' => PDO::FETCH_ASSOC));
  298. $query->join('file_managed', NULL, $table . '.' . $field_name . '_fid = file_managed.fid');
  299. if ($last_fid) {
  300. $query->condition('file_managed.fid', $last_fid, '>');
  301. }
  302. $result = $query->fields('file_managed', array('fid', 'uri'))
  303. ->orderBy('file_managed.fid')
  304. ->range(0, $images_per_pass)
  305. ->execute();
  306. $count = 0;
  307. foreach ($result as $file) {
  308. $count++;
  309. $info = image_get_info($file['uri']);
  310. if (is_array($info)) {
  311. db_update($table)
  312. ->fields(array(
  313. $field_name . '_width' => $info['width'],
  314. $field_name . '_height' => $info['height'],
  315. ))
  316. ->condition($field_name . '_fid', $file['fid'])
  317. ->execute();
  318. }
  319. }
  320. // If less than the requested number of rows were returned then this table
  321. // has been fully processed.
  322. $last_fid = ($count < $images_per_pass) ? NULL : $file['fid'];
  323. return $count;
  324. }
  325. /**
  326. * Add width and height columns to image field schema and populate.
  327. */
  328. function image_update_7002(array &$sandbox) {
  329. if (empty($sandbox)) {
  330. // Setup the sandbox.
  331. $sandbox = array(
  332. 'tables' => array(),
  333. 'total' => 0,
  334. 'processed' => 0,
  335. 'last_fid' => NULL,
  336. );
  337. $fields = _update_7000_field_read_fields(array(
  338. 'module' => 'image',
  339. 'storage_type' => 'field_sql_storage',
  340. 'deleted' => 0,
  341. ));
  342. foreach ($fields as $field) {
  343. $tables = array(
  344. _field_sql_storage_tablename($field),
  345. _field_sql_storage_revision_tablename($field),
  346. );
  347. foreach ($tables as $table) {
  348. // Add the width and height columns to the table.
  349. _image_update_7002_add_columns($table, $field['field_name']);
  350. // How many rows need dimensions populated?
  351. $count = db_select($table)->countQuery()->execute()->fetchField();
  352. if (!$count) {
  353. continue;
  354. }
  355. $sandbox['total'] += $count;
  356. $sandbox['tables'][$table] = $field['field_name'];
  357. }
  358. }
  359. // If no tables need rows populated with dimensions then we are done.
  360. if (empty($sandbox['tables'])) {
  361. $sandbox = array();
  362. return;
  363. }
  364. }
  365. // Process the table at the top of the list.
  366. $keys = array_keys($sandbox['tables']);
  367. $table = reset($keys);
  368. $sandbox['processed'] += _image_update_7002_populate_dimensions($table, $sandbox['tables'][$table], $sandbox['last_fid']);
  369. // Has the table been fully processed?
  370. if (!$sandbox['last_fid']) {
  371. unset($sandbox['tables'][$table]);
  372. }
  373. $sandbox['#finished'] = count($sandbox['tables']) ? ($sandbox['processed'] / $sandbox['total']) : 1;
  374. }
  375. /**
  376. * Remove the variables that set alt and title length since they were not
  377. * used for database column size and could cause PDO exceptions.
  378. */
  379. function image_update_7003() {
  380. variable_del('image_alt_length');
  381. variable_del('image_title_length');
  382. }
  383. /**
  384. * Use a large setting (512 and 1024 characters) for the length of the image alt
  385. * and title fields.
  386. */
  387. function image_update_7004() {
  388. $alt_spec = array(
  389. 'type' => 'varchar',
  390. 'length' => 512,
  391. 'not null' => FALSE,
  392. );
  393. $title_spec = array(
  394. 'type' => 'varchar',
  395. 'length' => 1024,
  396. 'not null' => FALSE,
  397. );
  398. $fields = _update_7000_field_read_fields(array(
  399. 'module' => 'image',
  400. 'storage_type' => 'field_sql_storage',
  401. ));
  402. foreach ($fields as $field_name => $field) {
  403. $tables = array(
  404. _field_sql_storage_tablename($field),
  405. _field_sql_storage_revision_tablename($field),
  406. );
  407. $alt_column = $field['field_name'] . '_alt';
  408. $title_column = $field['field_name'] . '_title';
  409. foreach ($tables as $table) {
  410. db_change_field($table, $alt_column, $alt_column, $alt_spec);
  411. db_change_field($table, $title_column, $title_column, $title_spec);
  412. }
  413. }
  414. }
  415. /**
  416. * Add a column to the 'image_style' table to store administrative labels.
  417. */
  418. function image_update_7005() {
  419. $field = array(
  420. 'type' => 'varchar',
  421. 'length' => 255,
  422. 'not null' => TRUE,
  423. 'default' => '',
  424. 'description' => 'The style administrative name.',
  425. );
  426. db_add_field('image_styles', 'label', $field);
  427. // Do a direct query here, rather than calling image_styles(),
  428. // in case Image module is disabled.
  429. $styles = db_query('SELECT name FROM {image_styles}')->fetchCol();
  430. foreach ($styles as $style) {
  431. db_update('image_styles')
  432. ->fields(array('label' => $style))
  433. ->condition('name', $style)
  434. ->execute();
  435. }
  436. }
  437. /**
  438. * @} End of "addtogroup updates-7.x-extra".
  439. */
  440. /**
  441. * Implements hook_requirements() to check the PHP GD Library.
  442. *
  443. * @param $phase
  444. */
  445. function image_requirements($phase) {
  446. $requirements = array();
  447. if ($phase == 'runtime') {
  448. // Check for the PHP GD library.
  449. if (function_exists('imagegd2')) {
  450. $info = gd_info();
  451. $requirements['image_gd'] = array(
  452. 'value' => $info['GD Version'],
  453. );
  454. // Check for filter and rotate support.
  455. if (function_exists('imagefilter') && function_exists('imagerotate')) {
  456. $requirements['image_gd']['severity'] = REQUIREMENT_OK;
  457. }
  458. else {
  459. $requirements['image_gd']['severity'] = REQUIREMENT_WARNING;
  460. $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="http://www.php.net/manual/book.image.php">the PHP manual</a>.');
  461. }
  462. }
  463. else {
  464. $requirements['image_gd'] = array(
  465. 'value' => t('Not installed'),
  466. 'severity' => REQUIREMENT_ERROR,
  467. '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')),
  468. );
  469. }
  470. $requirements['image_gd']['title'] = t('GD library rotate and desaturate effects');
  471. }
  472. return $requirements;
  473. }