image.module

Tracking 5.x-2.x branch
  1. drupal
    1. 5 contributions/image/image.module
    2. 6 contributions/image/image.module
    3. 7 drupal/modules/image/image.module
    4. 8 drupal/core/modules/image/image.module

Functions & methods

NameDescription
image_accessImplementation of hook_access
image_admin_settingsAdmin settings callback.
image_blockImplementation of hook_block.
image_create_node_fromFunction to other modules to use to create image nodes.
image_cronImplements hook_cron. (deletes old temp images)
image_deleteImplementation of hook_delete.
image_displayCreate an <img> tag for an image.
image_fetchFetches an image file, allows "shorthand" image urls such of the form: image/view/$nid/$label (e.g. image/view/25/thumbnail or image/view/14)
image_file_downloadImplementation of hook_file_download().
image_formImplementation of hook_form
image_form_add_thumbnail
image_get_derivative_sizesDetermine which sizes of derivative images need to be built for this image.
image_get_latestFetch the latest N image(s) - optionally from a given term.
image_get_randomFetch a random N image(s) - optionally from a given term.
image_get_sizesHelper function to return the defined sizes (or proper defaults).
image_helpImplementation of hook_help
image_insertImplementation of hook_insert
image_linkImplementation of hook_link.
image_loadImplementation of hook_load
image_menuImplementation of hook_menu
image_node_infoImplementation of hook_node_info
image_node_operationsImplementation of hook_node_operations().
image_operations_rebuild
image_panels_include_directoryImplementation of hook_panels_include_directory().
image_permImplementation of hook_perm
image_prepareImplementation of hook_prepare().
image_settings_sizes_submitMake changes to the settings before passing them off to system_settings_form_submit().
image_settings_sizes_validateCheck that the sizes provided have the required amount of information.
image_submit
image_updateImplementation of hook_update
image_validate
image_viewImplementation of hook_view
theme_image_block_latestTheme a latest block
theme_image_block_randomTheme a random block
theme_image_bodyTheme a body
theme_image_displayTheme an img tag for displaying the image.
theme_image_settings_sizes_form
theme_image_teaserTheme a teaser
_image_build_derivativesGenerate image derivatives.
_image_check_settingsVerify the image module and toolkit settings.
_image_filenameCreates an image filename.
_image_get_sizesHelper function to preserve backwards compatibility. This has been deprecated in favor of image_get_sizes().
_image_insertMoves temporary (working) images to the final directory and stores relevant information in the files table
_image_is_required_sizeIs a given size a built-in, required size?

Constants

NameDescription
IMAGE_LINK_HIDDEN
IMAGE_LINK_NEW
IMAGE_LINK_SHOWN
IMAGE_ORIGINAL
IMAGE_PREVIEW
IMAGE_THUMBNAIL

File

View source
  1. <?php
  2. define('IMAGE_ORIGINAL', '_original');
  3. define('IMAGE_PREVIEW', 'preview');
  4. define('IMAGE_THUMBNAIL', 'thumbnail');
  5. define('IMAGE_LINK_HIDDEN', 0);
  6. define('IMAGE_LINK_SHOWN', 1);
  7. define('IMAGE_LINK_NEW', 2);
  8. if (module_exists('views')) {
  9. include(drupal_get_path('module', 'image') .'/views.inc');
  10. include(drupal_get_path('module', 'image') .'/views_defaults.inc');
  11. }
  12. /**
  13. * Implementation of hook_help
  14. */
  15. function image_help($section) {
  16. switch ($section) {
  17. case 'admin/help#image':
  18. $output = '<p>'. t('The image module is used to create and administer images for your site. Each image is stored as a post, with thumbnails of the original generated automatically. There are two default thumbnail sizes, thumbnail and preview. The thumbnail size is shown as the preview for image posts and when browsing image galleries. The preview is the default size when first displaying an image node.') .'</p>';
  19. $output .= '<p>'. t('Image administration allows the image directory and the image sizes to be set.</p><p>
  20. Image galleries are used to organize and display images in galleries. The list tab allows users to edit existing image gallery names, descriptions, parents and relative position, known as a weight. The add galleries tab allows you to create a new image gallery defining name, description, parent and weight.') .'</p>';
  21. $output .= t('<p>You can</p>
  22. <ul>
  23. <li>view image handling messages in <a href="!admin-settings">administer &gt;&gt; settings</a>.</li>
  24. <li>configure image sizes and file directories at <a href="!admin-settings-image">administer &gt;&gt; settings &gt;&gt; image</a>.</li>
  25. <li>use the <a href="!external-http-drupal-org-project-img_assist">image assist module</a> to upload and insert images into posts.</li>
  26. ', array('!admin-settings' => url('admin/settings'), '!admin-image-galleries' => url('admin/image/galleries'), '!admin-settings-image' => url('admin/settings/image'), '!external-http-drupal-org-project-img_assist' => 'http://drupal.org/project/img_assist')) .'</ul>';
  27. $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="!image">Image page</a>.', array('!image' => 'http://www.drupal.org/handbook/modules/image/')) .'</p>';
  28. return $output;
  29. }
  30. }
  31. /**
  32. * Implementation of hook_node_info
  33. */
  34. function image_node_info() {
  35. return array(
  36. 'image' => array(
  37. 'name' => t('Image'),
  38. 'module' => 'image',
  39. 'description' => t('An image (with thumbnail). This is ideal for publishing photographs or screenshots.'),
  40. )
  41. );
  42. }
  43. /**
  44. * Implementation of hook_perm
  45. */
  46. function image_perm() {
  47. return array('create images', 'view original images', 'edit own images', 'edit images');
  48. }
  49. /**
  50. * Implementation of hook_access
  51. */
  52. function image_access($op, $node) {
  53. global $user;
  54. if ($op == 'create' && user_access('create images')) {
  55. return TRUE;
  56. }
  57. if ($op == 'update' || $op == 'delete') {
  58. if (user_access('edit images')) {
  59. return TRUE;
  60. }
  61. if (user_access('edit own images') && ($user->uid == $node->uid)) {
  62. return TRUE;
  63. }
  64. }
  65. }
  66. /**
  67. * Admin settings callback.
  68. */
  69. function image_admin_settings() {
  70. _image_check_settings();
  71. $form['#submit'] = array('image_settings_sizes_submit' => array());
  72. $form['image_updated'] = array(
  73. '#type' => 'hidden',
  74. '#value' => variable_get('image_updated', time()),
  75. );
  76. $form['paths'] = array(
  77. '#type' => 'fieldset',
  78. '#title' => t('File paths')
  79. );
  80. $form['paths']['image_default_path'] = array(
  81. '#type' => 'textfield',
  82. '#title' => t('Default image path'),
  83. '#default_value' => variable_get('image_default_path', 'images'),
  84. '#description' => t('Subdirectory in the directory %dir where pictures will be stored. Do not include trailing slash.', array('%dir' => file_directory_path())),
  85. );
  86. $form['image_max_upload_size'] = array(
  87. '#type' => 'textfield',
  88. '#title' => t('Maximum upload size'),
  89. '#default_value' => variable_get('image_max_upload_size', 800),
  90. '#field_suffix' => t('KB'),
  91. '#size' => 12,
  92. '#description' => t('Maximum file size for image uploads. When a maximum image dimensions is specified for original images the size is checked after resizing. '),
  93. );
  94. $form['image_sizes'] = array(
  95. '#type' => 'fieldset',
  96. '#title' => t('Image sizes'),
  97. '#tree' => TRUE,
  98. '#theme' => 'image_settings_sizes_form',
  99. '#description' => '<p>'. t("The <em>Scale image</em> operation resizes images so that they fit with in the given dimensions. If only one dimension is specified the other dimension will be computed based on the image's aspect ratio. The <em>Scale and crop image</em> operation resizes images to be exactly the given dimensions. If only one dimension is specified the image will not be cropped, making this is equivalent to <em>Scale image</em>.") .'</p>'
  100. .'<p>'. t("Note: 'Original' dimensions will only be used to resize images when they are first uploaded. Existing originals will not be modified.") .'</p>',
  101. '#validate' => array('image_settings_sizes_validate' => array()),
  102. );
  103. $link_options = array(
  104. IMAGE_LINK_HIDDEN => t('Hidden'),
  105. IMAGE_LINK_SHOWN => t('Same window'),
  106. IMAGE_LINK_NEW => t('New window'),
  107. );
  108. $sizes = image_get_sizes();
  109. // Add some empty rows for user defined sizes.
  110. $num_sizes = count($sizes);
  111. for ($i = $num_sizes; $i < ($num_sizes + 3); $i++) {
  112. $sizes['new'. $i] = array(
  113. 'label' => '',
  114. 'operation' => 'scale',
  115. 'width' => '',
  116. 'height' => '',
  117. 'link' => IMAGE_LINK_SHOWN,
  118. 'new' => TRUE,
  119. );
  120. }
  121. foreach ($sizes as $key => $size) {
  122. $form['image_sizes'][$key]['label'] = array(
  123. '#type' => 'textfield',
  124. '#default_value' => $size['label'],
  125. '#size' => 25,
  126. '#maxlength' => 32,
  127. );
  128. if (_image_is_required_size($key)) {
  129. $form['image_sizes'][$key]['label']['#attributes'] = array('disabled' => 'disabled');
  130. $form['image_sizes'][$key]['label']['#required'] = TRUE;
  131. }
  132. $form['image_sizes'][$key]['operation'] = array(
  133. '#type' => 'select',
  134. '#default_value' => $size['operation'],
  135. '#options' => array('scale' => t('Scale image'), 'scale_crop' => t('Scale and crop image')),
  136. );
  137. $form['image_sizes'][$key]['width'] = array(
  138. '#type' => 'textfield',
  139. '#default_value' => $size['width'],
  140. '#size' => 5,
  141. '#maxlength' => 5,
  142. );
  143. $form['image_sizes'][$key]['height'] = array(
  144. '#type' => 'textfield',
  145. '#default_value' => $size['height'],
  146. '#size' => 5,
  147. '#maxlength' => 5,
  148. );
  149. $form['image_sizes'][$key]['link'] = array(
  150. '#type' => 'select',
  151. '#default_value' => $size['link'],
  152. '#options' => $link_options,
  153. );
  154. }
  155. return system_settings_form($form);
  156. }
  157. /**
  158. * Check that the sizes provided have the required amount of information.
  159. */
  160. function image_settings_sizes_validate(&$form) {
  161. foreach (element_children($form) as $key) {
  162. // If there's a label they must provide at either a height or width.
  163. if ($key != IMAGE_ORIGINAL && !empty($form[$key]['label']['#value'])) {
  164. if (empty($form[$key]['width']['#value']) && empty($form[$key]['height']['#value'])) {
  165. form_set_error("image_sizes][$key][width", t('Must specify a width or height.'));
  166. }
  167. }
  168. }
  169. }
  170. /**
  171. * Make changes to the settings before passing them off to
  172. * system_settings_form_submit().
  173. *
  174. * Remove deleted sizes, and use the label as indexes for new sizes.
  175. */
  176. function image_settings_sizes_submit($form_id, &$form_values) {
  177. $old_sizes = image_get_sizes();
  178. // If the size's operation, or dimensions change we need to rebuild.
  179. $rebuild = FALSE;
  180. foreach ($form_values['image_sizes'] as $key => $size) {
  181. // Changed to the original setting only affect new images and they
  182. // shouldn't be able to add or remove it.
  183. if ($key == IMAGE_ORIGINAL) {
  184. continue;
  185. }
  186. // Remove sizes without labels.
  187. if (empty($size['label'])) {
  188. unset($form_values['image_sizes'][$key]);
  189. }
  190. // Check if only one is set, indicating an addition or removal.
  191. if (isset($form_values['image_sizes'][$key]) ^ isset($old_sizes[$key])) {
  192. $rebuild |= TRUE;
  193. // When adding a new size, we need to assign a key.
  194. if (isset($form_values['image_sizes'][$key])) {
  195. unset($form_values['image_sizes'][$key]);
  196. $new_key = drupal_strtolower(drupal_substr($size['label'], 0, 32));
  197. $form_values['image_sizes'][$new_key] = $size;
  198. }
  199. }
  200. // Check for changes.
  201. else if (isset($form_values['image_sizes'][$key]) && isset($old_sizes[$key])) {
  202. // Did the operation, height or width change?
  203. foreach (array('operation', 'height', 'width') as $field) {
  204. $rebuild |= ($form_values['image_sizes'][$key][$field] != $old_sizes[$key][$field]);
  205. }
  206. }
  207. }
  208. // If we've changed anything update the image_update variable so the
  209. // derivative images are rebuilt.
  210. if ($rebuild) {
  211. drupal_set_message(t('Changes to the images sizes mean that the derivative images will need to be regenerated.'));
  212. $form_values['image_updated'] = time();
  213. }
  214. return system_settings_form_submit($form_id, $form_values);
  215. }
  216. function theme_image_settings_sizes_form(&$form) {
  217. $header = array(t('Label'), t('Operation'), t('Width'), t('Height'), t('Link'));
  218. foreach (element_children($form) as $key) {
  219. $row = array();
  220. $row[] = drupal_render($form[$key]['label']);
  221. $row[] = drupal_render($form[$key]['operation']);
  222. $row[] = drupal_render($form[$key]['width']);
  223. $row[] = drupal_render($form[$key]['height']);
  224. $row[] = drupal_render($form[$key]['link']);
  225. $rows[] = $row;
  226. }
  227. $output .= theme('table', $header, $rows);
  228. $output .= drupal_render($form);
  229. return $output;
  230. }
  231. /**
  232. * Implementation of hook_menu
  233. */
  234. function image_menu($may_cache) {
  235. $items = array();
  236. if ($may_cache) {
  237. $items[] = array(
  238. 'path' => 'node/add/image',
  239. 'title' => t('Image'),
  240. 'access' => user_access('create images'),
  241. );
  242. $items[] = array(
  243. 'path' => 'image/view',
  244. 'title' => t('image'),
  245. 'access' => user_access('access content'),
  246. 'type' => MENU_CALLBACK,
  247. 'callback' => 'image_fetch',
  248. );
  249. $items[] = array(
  250. 'path' => 'admin/settings/image',
  251. 'title' => t('Images'),
  252. 'description' => t('Configure the location of image files and image sizes. Also, if enabled, configure image attachments and options for image galleries.'),
  253. 'callback' => 'drupal_get_form',
  254. 'callback arguments' => array('image_admin_settings'),
  255. 'access' => user_access('administer site configuration'),
  256. 'type' => MENU_NORMAL_ITEM,
  257. );
  258. $items[] = array(
  259. 'path' => 'admin/settings/image/nodes',
  260. 'title' => t('Files and sizes'),
  261. 'description' => t('Configure the location of image files and image sizes.'),
  262. 'access' => user_access('administer site configuration'),
  263. 'type' => MENU_DEFAULT_LOCAL_TASK,
  264. 'weight' => '-10',
  265. );
  266. }
  267. return $items;
  268. }
  269. /**
  270. * Implements hook_cron. (deletes old temp images)
  271. */
  272. function image_cron() {
  273. $path = file_directory_path() .'/'. variable_get('image_default_path', 'images') .'/temp';
  274. $files = file_scan_directory(file_create_path($path), '.*');
  275. foreach ($files as $file => $info) {
  276. if (time() - filemtime($file) > 60*60*6) {
  277. file_delete($file);
  278. }
  279. }
  280. }
  281. /**
  282. * Implementation of hook_node_operations().
  283. */
  284. function image_node_operations() {
  285. $operations = array(
  286. 'rebuild_thumbs' => array(
  287. 'label' => t('Rebuild derivative images'),
  288. 'callback' => 'image_operations_rebuild',
  289. ),
  290. );
  291. return $operations;
  292. }
  293. function image_operations_rebuild($nids) {
  294. foreach ($nids as $nid) {
  295. if ($node = node_load($nid)) {
  296. if ($node->type == 'image') {
  297. $node->rebuild_images = TRUE;
  298. image_update($node);
  299. }
  300. }
  301. }
  302. }
  303. /**
  304. * Implementation of hook_prepare().
  305. */
  306. function image_prepare(&$node, $field_name = NULL) {
  307. // We need to be aware that a user may try to edit multiple image nodes at
  308. // once. By using the $nid variable each node's files can be stored separately
  309. // in the session.
  310. $nid = ($node->nid) ? $node->nid : 'new_node';
  311. // When you enter the edit view the first time we need to clear our files in
  312. // session for this node. This is so if you upload a file, then decide you
  313. // don't want it and reload the form (without posting), the files will be
  314. // discarded.
  315. if (count($_POST) == 0) {
  316. unset($_SESSION['image_new_files'][$nid]);
  317. }
  318. if (is_null($field_name)) {
  319. $field_name = 'image';
  320. }
  321. if ($file = file_check_upload($field_name)) {
  322. // Ensure the file is an image.
  323. $image_info = image_get_info($file->filepath);
  324. if (!$image_info || empty($image_info['extension'])) {
  325. form_set_error($field_name, t('Uploaded file is not a valid image. Only JPG, PNG and GIF files are allowed.'));
  326. return;
  327. }
  328. // Save the file to the temp directory.
  329. $file = file_save_upload($field_name, _image_filename($file->filename, IMAGE_ORIGINAL, TRUE));
  330. if (!$file) {
  331. return;
  332. }
  333. // Resize the original.
  334. $aspect_ratio = $image_info['height'] / $image_info['width'];
  335. $original_size = image_get_sizes(IMAGE_ORIGINAL, $aspect_ratio);
  336. if (!empty($original_size['width']) && !empty($original_size['height'])) {
  337. $result = image_scale($file->filepath, $file->filepath, $original_size['width'], $original_size['height']);
  338. if ($result) {
  339. clearstatcache();
  340. $file->filesize = filesize($file->filepath);
  341. drupal_set_message(t('The original image was resized to fit within the maximum allowed resolution of %width x %height pixels.', array('%width' => $original_size['width'], '%height' => $original_size['height'])));
  342. }
  343. }
  344. // Check the file size limit.
  345. if ($file->filesize > variable_get('image_max_upload_size', 800) * 1024) {
  346. form_set_error($field_name, t('The image you uploaded was too big. You are only allowed upload files less than %max_size but your file was %file_size.', array('%max_size' => format_size(variable_get('image_max_upload_size', 800) * 1024), '%file_size' => format_size($file->filesize))));
  347. file_delete($file->filepath);
  348. return;
  349. }
  350. // We're good to go.
  351. $node->images[IMAGE_ORIGINAL] = $file->filepath;
  352. $node->rebuild_images = FALSE;
  353. $node->new_file = TRUE;
  354. // Call hook to allow other modules to modify the original image.
  355. module_invoke_all('image_alter', $node, $file->filepath, IMAGE_ORIGINAL);
  356. _image_build_derivatives($node, TRUE);
  357. // Store the new file into the session.
  358. $_SESSION['image_new_files'][$nid] = $node->images;
  359. }
  360. // Reload new files uploaded in a previous preview.
  361. else if (isset($_SESSION['image_new_files'][$nid])) {
  362. $node->images = $_SESSION['image_new_files'][$nid];
  363. }
  364. }
  365. /**
  366. * Implementation of hook_file_download().
  367. *
  368. * Note that in Drupal 5, the upload.module's hook_file_download() checks its
  369. * permissions for all files in the {files} table. We store our file
  370. * information in {files} if private files transfers are selected and the
  371. * upload.module is enabled, users will the 'view uploaded files'permission to
  372. * view images.
  373. */
  374. function image_file_download($filename) {
  375. $filepath = file_create_path($filename);
  376. $result = db_query("SELECT i.nid, f.filemime, f.filesize FROM {image} i INNER JOIN {files} f ON i.fid = f.fid WHERE f.filepath = '%s'", $filepath);
  377. if ($file = db_fetch_object($result)) {
  378. $node = node_load(array('type' => 'image', 'nid' => $file->nid));
  379. if ($node) {
  380. if (node_access('view', $node)) {
  381. // The user either needs to have 'view original images' permission or
  382. // the path must be listed for something other than the node's original
  383. // size. This will be the case when the orignal is smaller than a
  384. // derivative size.
  385. $images = (array) $node->images;
  386. unset($images[IMAGE_ORIGINAL]);
  387. if (user_access('view original images') || in_array($filepath, $images)) {
  388. return array(
  389. 'Content-Type: '. mime_header_encode($file->filemime),
  390. 'Content-Length: '. (int) $file->filesize,
  391. );
  392. }
  393. }
  394. return -1;
  395. }
  396. }
  397. }
  398. /**
  399. * Implementation of hook_link.
  400. */
  401. function image_link($type, $node, $main = 0) {
  402. $links = array();
  403. if ($type == 'node' && $node->type == 'image' && !$main) {
  404. $request = ($_GET['size']) ? $_GET['size'] : IMAGE_PREVIEW;
  405. foreach (image_get_sizes() as $key => $size) {
  406. if ($size['link']) {
  407. // For smaller images some derivative images may not have been created.
  408. // The thumbnail and preview images will be equal to the original images
  409. // but other sizes will not be set.
  410. if (isset($node->images[$key]) && $node->images[$key] != $node->images[$request]) {
  411. if ($size['link'] == IMAGE_LINK_NEW) {
  412. $links['image_size_'. $key] = array(
  413. 'title' => $size['label'],
  414. 'href' => "image/view/{$node->nid}/$key",
  415. 'attributes' => array('target' => '_blank'),
  416. );
  417. }
  418. else {
  419. $links['image_size_'. $key] = array(
  420. 'title' => $size['label'],
  421. 'href' => 'node/'. $node->nid,
  422. 'query' => 'size='. urlencode($key)
  423. );
  424. }
  425. }
  426. }
  427. }
  428. if (!user_access('view original images')) {
  429. unset($links['image_size_'. IMAGE_ORIGINAL]);
  430. }
  431. }
  432. return $links;
  433. }
  434. /**
  435. * Implementation of hook_block.
  436. *
  437. * Offers 2 blocks: latest image and random image. Skip if Views module is
  438. * enabled.
  439. */
  440. function image_block($op = 'list', $delta = 0, $edit = array()) {
  441. if (module_exists('views')) {
  442. return;
  443. }
  444. switch ($op) {
  445. case 'list':
  446. $block[0]['info'] = t('Latest image');
  447. $block[1]['info'] = t('Random image');
  448. return $block;
  449. case 'configure':
  450. $form['number_images'] = array(
  451. '#type' => 'select',
  452. '#title' => t('Number of images to display'),
  453. '#options' => drupal_map_assoc(range(1, 99)),
  454. '#default_value' => variable_get("image_block_{$delta}_number_images", 1),
  455. );
  456. return $form;
  457. case 'view':
  458. if (user_access('access content')) {
  459. switch ($delta) {
  460. case 0:
  461. $images = image_get_latest(variable_get("image_block_{$delta}_number_images", 1));
  462. $block['subject'] = t('Latest image');
  463. $block['content'] = theme('image_block_latest', $images, IMAGE_THUMBNAIL);
  464. break;
  465. case 1:
  466. $images = image_get_random(variable_get("image_block_{$delta}_number_images", 1));
  467. $block['subject'] = t('Random image');
  468. $block['content'] = theme('image_block_random', $images, IMAGE_THUMBNAIL);
  469. break;
  470. }
  471. }
  472. return $block;
  473. case 'save':
  474. variable_set("image_block_{$delta}_number_images", $edit['number_images']);
  475. break;
  476. }
  477. }
  478. function image_form_add_thumbnail($form, $form_values) {
  479. if ($form_values['images']['thumbnail']) {
  480. $node = (object)($form_values);
  481. $form['#title'] = t('Thumbnail');
  482. $form['#value'] = image_display($node, IMAGE_THUMBNAIL);
  483. }
  484. return $form;
  485. }
  486. /**
  487. * Implementation of hook_form
  488. */
  489. function image_form(&$node, &$param) {
  490. _image_check_settings();
  491. $type = node_get_types('type', $node);
  492. $form['title'] = array(
  493. '#type' => 'textfield',
  494. '#title' => check_plain($type->title_label),
  495. '#size' => 60,
  496. '#maxlength' => 128,
  497. '#required' => TRUE,
  498. '#default_value' => $node->title
  499. );
  500. $form['images']['#tree'] = TRUE;
  501. foreach (image_get_sizes() as $key => $size) {
  502. $form['images'][$key] = array(
  503. '#type' => 'value',
  504. '#value' => $node->images[$key]
  505. );
  506. }
  507. $form['thumbnail'] = array(
  508. '#type' => 'item',
  509. '#weight' => -10,
  510. '#after_build' => array('image_form_add_thumbnail'),
  511. );
  512. $form['rebuild_images'] = array(
  513. '#type' => 'checkbox',
  514. '#title' => t('Rebuild derivative images'),
  515. '#default_value' => FALSE,
  516. '#description' => t('Check this to rebuild the derivative images for this node.'),
  517. );
  518. $form['#attributes'] = array("enctype" => "multipart/form-data");
  519. $form['image'] = array(
  520. '#type' => 'file',
  521. '#title' => t('Image'),
  522. '#size' => 40,
  523. '#description' => t('Click "Browse..." to select an image to upload.'),
  524. '#weight' => -3,
  525. );
  526. if ($type->has_body) {
  527. $form['body_filter']['body'] = array(
  528. '#type' => 'textarea',
  529. '#title' => check_plain($type->body_label),
  530. '#required' => ($type->min_word_count > 0),
  531. '#default_value' => $node->body,
  532. );
  533. $form['body_filter']['format'] = filter_form($node->format);
  534. }
  535. return $form;
  536. }
  537. function image_validate($node) {
  538. $nid = ($node->nid) ? $node->nid : 'new_node';
  539. if (!isset($node->images[IMAGE_ORIGINAL]) && !isset($_SESSION['image_new_files'][$nid])) {
  540. form_set_error('image', t('You must upload an image.'));
  541. }
  542. }
  543. function image_submit($node) {
  544. $nid = ($node->nid) ? $node->nid : 'new_node';
  545. if (isset($_SESSION['image_new_files'][$nid])) {
  546. $node->new_file = TRUE;
  547. $node->rebuild_images = FALSE;
  548. $node->images = $_SESSION['image_new_files'][$nid];
  549. unset($_SESSION['image_new_files'][$nid]);
  550. }
  551. }
  552. /**
  553. * Implementation of hook_view
  554. */
  555. function image_view($node, $teaser = 0, $page = 0) {
  556. $sizes = image_get_sizes();
  557. $size = IMAGE_PREVIEW;
  558. if (isset($_GET['size'])) {
  559. // Invalid size specified.
  560. if (!isset($sizes[$_GET['size']])) {
  561. drupal_goto("node/$node->nid");
  562. }
  563. $size = $_GET['size'];
  564. // Not allowed to view the original.
  565. if ($size == IMAGE_ORIGINAL && !user_access('view original images')) {
  566. drupal_goto("node/$node->nid");
  567. }
  568. }
  569. $node = node_prepare($node, $teaser);
  570. $node->content['image'] = array(
  571. '#value' => theme($teaser ? 'image_teaser' : 'image_body', $node, $size),
  572. '#weight' => 0,
  573. );
  574. return $node;
  575. }
  576. /**
  577. * Implementation of hook_load
  578. */
  579. function image_load(&$node) {
  580. $result = db_query("SELECT i.image_size, f.filepath FROM {image} i INNER JOIN {files} f ON i.fid = f.fid WHERE i.nid = %d", $node->nid);
  581. $node->images = array();
  582. while ($file = db_fetch_object($result)) {
  583. $node->images[$file->image_size] = file_create_path($file->filepath);
  584. }
  585. $original_path = $node->images[IMAGE_ORIGINAL];
  586. if (empty($original_path)) {
  587. // There's no original image, we're in trouble...
  588. return;
  589. }
  590. $node->rebuild_images = FALSE;
  591. // Figure out which sizes should have been generated.
  592. $all_sizes = image_get_sizes();
  593. unset($all_sizes[IMAGE_ORIGINAL]);
  594. $needed_sizes = array_keys(image_get_derivative_sizes($original_path));
  595. $unneeded_sizes = array_diff(array_keys($all_sizes), $needed_sizes);
  596. // Derivative sizes that are larger than the original get set to the
  597. // original.
  598. foreach ($unneeded_sizes as $key) {
  599. if (empty($node->images[$key])) {
  600. $node->images[$key] = $original_path;
  601. }
  602. else {
  603. // Need to remove an extra derivative image in the database.
  604. $node->rebuild_images = TRUE;
  605. }
  606. }
  607. // Check that the derivative images are present and current.
  608. foreach ($needed_sizes as $key) {
  609. // If the file is missing or created after the last change to the sizes,
  610. // rebuild the derivatives.
  611. if (empty($node->images[$key]) || !file_exists($node->images[$key])) {
  612. $node->rebuild_images = TRUE;
  613. }
  614. // Derivative image had a timestamp that predates last changes to image
  615. // size settings, so it needs to be rebuilt.
  616. elseif (filemtime($node->images[$key]) < variable_get('image_updated', 0)) {
  617. $node->rebuild_images = TRUE;
  618. }
  619. }
  620. // Correct any problems with the derivative images.
  621. if ($node->rebuild_images) {
  622. image_update($node);
  623. watchdog('image', t('Derivative images were regenerated for %title.', array('%title' => $node->title)), WATCHDOG_NOTICE, l(t('view'), 'node/'. $node->nid));
  624. }
  625. }
  626. /**
  627. * Implementation of hook_insert
  628. */
  629. function image_insert($node) {
  630. // Derivative images that aren't needed are set to the original file. Make
  631. // note of the current path before calling _image_insert() because if it's
  632. // in the temp directory it'll be moved. We'll need it later to determine
  633. // which derivative images need to be saved with _image_insert().
  634. $original_path = $node->images[IMAGE_ORIGINAL];
  635. // Save the original first so that it if it's moved the derivatives are
  636. // placed in the correct directory.
  637. _image_insert($node, IMAGE_ORIGINAL, $original_path);
  638. $sizes = image_get_derivative_sizes($node->images[IMAGE_ORIGINAL]);
  639. foreach ($sizes as $key => $size_info) {
  640. if (!empty($node->images[$key]) && $node->images[$key] != $original_path) {
  641. _image_insert($node, $key, $node->images[$key]);
  642. }
  643. }
  644. }
  645. /**
  646. * Implementation of hook_update
  647. *
  648. * Take $node by reference so we can use this to save the node after
  649. * rebuilding derivatives.
  650. */
  651. function image_update(&$node) {
  652. if ($node->new_file || $node->rebuild_images) {
  653. // Derivative images that aren't needed are set to the original file. Make
  654. // note of the current path before calling _image_insert() because if it's
  655. // in the temp directory it'll be moved. We'll need it later to determine
  656. // which derivative images need to be saved with _image_insert().
  657. $original_path = $node->images[IMAGE_ORIGINAL];
  658. if ($node->new_file) {
  659. // The derivative images were built during image_prepare() or
  660. // image_create_node_from() so all we need to do is remove all the old,
  661. // existing images.
  662. // Remove all the existing images.
  663. $result = db_query("SELECT f.fid, f.filepath FROM {image} i INNER JOIN {files} f ON i.fid = f.fid WHERE i.nid = %d", $node->nid);
  664. while ($file = db_fetch_object($result)) {
  665. file_delete(file_create_path($file->filepath));
  666. db_query("DELETE FROM {files} WHERE fid = %d", $file->fid);
  667. }
  668. db_query("DELETE FROM {image} WHERE nid = %d", $node->nid);
  669. // Save the original first so that it if it's moved the derivatives are
  670. // placed in the correct directory.
  671. _image_insert($node, IMAGE_ORIGINAL, $original_path);
  672. }
  673. else if ($node->rebuild_images) {
  674. // Find the original image.
  675. $original_file = db_fetch_object(db_query("SELECT i.fid, f.filepath FROM {image} i INNER JOIN {files} f ON i.fid = f.fid WHERE i.nid = %d AND i.image_size = '%s'", $node->nid, IMAGE_ORIGINAL));
  676. // Delete all but the original image.
  677. $result = db_query("SELECT i.fid, f.filepath FROM {image} i INNER JOIN {files} f ON i.fid = f.fid WHERE i.nid = %d AND f.fid <> %d", $node->nid, $original_file->fid);
  678. while ($file = db_fetch_object($result)) {
  679. // Beware of derivative images that have the same path as the original.
  680. if ($file->filepath != $original_file->filepath) {
  681. file_delete(file_create_path($file->filepath));
  682. }
  683. db_query("DELETE FROM {files} WHERE fid = %d", $file->fid);
  684. db_query("DELETE FROM {image} WHERE fid = %d", $file->fid);
  685. }
  686. _image_build_derivatives($node, FALSE);
  687. // Prevent multiple rebuilds.
  688. $node->rebuild_images = FALSE;
  689. }
  690. $sizes = image_get_derivative_sizes($node->images[IMAGE_ORIGINAL]);
  691. foreach ($sizes as $key => $size_info) {
  692. if (!empty($node->images[$key]) && $node->images[$key] != $original_path) {
  693. _image_insert($node, $key, $node->images[$key]);
  694. }
  695. }
  696. }
  697. }
  698. /**
  699. * Implementation of hook_delete.
  700. */
  701. function image_delete($node) {
  702. $result = db_query('SELECT i.fid, f.filepath FROM {image} i INNER JOIN {files} f ON i.fid = f.fid WHERE i.nid = %d', $node->nid);
  703. while ($file = db_fetch_object($result)) {
  704. file_delete(file_create_path($file->filepath));
  705. db_query('DELETE FROM {files} WHERE fid = %d', $file->fid);
  706. }
  707. db_query("DELETE FROM {image} WHERE nid = %d", $node->nid);
  708. }
  709. /**
  710. * Create an <img> tag for an image.
  711. */
  712. function image_display(&$node, $label = IMAGE_PREVIEW, $attributes = array()) {
  713. if (empty($node->images[$label])) {
  714. return;
  715. }
  716. $image_info = image_get_info(file_create_path($node->images[$label]));
  717. $attributes['class'] = "image image-$label ". (isset($attributes['class']) ? $attributes['class'] : "");
  718. // Only output width/height attributes if image_get_info() was able to detect
  719. // the image dimensions, since certain browsers interpret an empty attribute
  720. // value as zero.
  721. if (!empty($image_info['width'])) {
  722. $attributes['width'] = $image_info['width'];
  723. }
  724. if (!empty($image_info['height'])) {
  725. $attributes['height'] = $image_info['height'];
  726. }
  727. return theme('image_display', $node, $label, file_create_url($node->images[$label]), $attributes);
  728. }
  729. /**
  730. * Fetches an image file, allows "shorthand" image urls such of the form:
  731. * image/view/$nid/$label
  732. * (e.g. image/view/25/thumbnail or image/view/14)
  733. */
  734. function image_fetch($nid = 0, $size = IMAGE_PREVIEW) {
  735. if ($size == IMAGE_ORIGINAL && !user_access('view original images')) {
  736. return drupal_access_denied();
  737. }
  738. if (isset($nid)) {
  739. $node = node_load(array('type' => 'image', 'nid' => $nid));
  740. if ($node) {
  741. if (!node_access('view', $node)) {
  742. return drupal_access_denied();
  743. }
  744. if (isset($node->images[$size])) {
  745. $file = $node->images[$size];
  746. $headers = image_file_download($file);
  747. if ($headers == -1) {
  748. return drupal_access_denied();
  749. }
  750. file_transfer($file, $headers);
  751. }
  752. }
  753. }
  754. return drupal_not_found();
  755. }
  756. /**
  757. * Theme a teaser
  758. */
  759. function theme_image_teaser($node) {
  760. return l(image_display($node, IMAGE_THUMBNAIL), 'node/'. $node->nid, array(), NULL, NULL, TRUE, TRUE);
  761. }
  762. /**
  763. * Theme a body
  764. */
  765. function theme_image_body($node, $size) {
  766. return image_display($node, $size);
  767. }
  768. /**
  769. * Theme an img tag for displaying the image.
  770. */
  771. function theme_image_display($node, $label, $url, $attributes) {
  772. return theme('image', $url, $node->title, $node->title, $attributes, FALSE);
  773. }
  774. /**
  775. * Theme a random block
  776. */
  777. function theme_image_block_random($images, $size) {
  778. $output = '';
  779. foreach ($images as $image) {
  780. $output .= l(image_display($image, $size), 'node/'. $image->nid, array(), NULL, NULL, FALSE, TRUE);
  781. }
  782. return $output;
  783. }
  784. /**
  785. * Theme a latest block
  786. */
  787. function theme_image_block_latest($images, $size) {
  788. $output = '';
  789. foreach ($images as $image) {
  790. $output .= l(image_display($image, $size), 'node/'. $image->nid, array(), NULL, NULL, FALSE, TRUE);
  791. }
  792. return $output;
  793. }
  794. /**
  795. * Fetch a random N image(s) - optionally from a given term.
  796. */
  797. function image_get_random($count = 1, $tid = 0) {
  798. if ($tid != 0) {
  799. $result = db_query_range(db_rewrite_sql("SELECT DISTINCT(n.nid), RAND() AS rand FROM {term_node} tn LEFT JOIN {node} n ON n.nid = tn.nid WHERE n.type='image' AND n.status = 1 AND tn.tid = %d ORDER BY rand"), $tid, 0, $count);
  800. }
  801. else {
  802. $result = db_query_range(db_rewrite_sql("SELECT DISTINCT(n.nid), RAND() AS rand FROM {node} n WHERE n.type = 'image' AND n.status = 1 ORDER BY rand"), 0, $count);
  803. }
  804. $output = array();
  805. while ($nid = db_fetch_object($result)) {
  806. $output[] = node_load(array('nid' => $nid->nid));
  807. }
  808. return $output;
  809. }
  810. /**
  811. * Fetch the latest N image(s) - optionally from a given term.
  812. */
  813. function image_get_latest($count = 1, $tid = 0) {
  814. if ($tid != 0) {
  815. $result = db_query_range(db_rewrite_sql("SELECT n.nid FROM {term_node} tn LEFT JOIN {node} n ON n.nid=tn.nid WHERE n.type='image' AND n.status=1 AND tn.tid=%d ORDER BY n.changed DESC"), $tid, 0, $count);
  816. }
  817. else {
  818. $result = db_query_range(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE n.type = 'image' AND n.status = 1 ORDER BY n.changed DESC"), 0, $count);
  819. }
  820. $output = array();
  821. while ($nid = db_fetch_object($result)) {
  822. $output[] = node_load(array('nid' => $nid->nid));
  823. }
  824. return $output;
  825. }
  826. /**
  827. * Verify the image module and toolkit settings.
  828. */
  829. function _image_check_settings() {
  830. // File paths
  831. $image_path = file_create_path(variable_get('image_default_path', 'images'));
  832. $temp_path = rtrim($image_path, '/') .'/temp';
  833. if (!file_check_directory($image_path, FILE_CREATE_DIRECTORY, 'image_default_path')) {
  834. return false;
  835. }
  836. if (!file_check_directory($temp_path, FILE_CREATE_DIRECTORY, 'image_default_path')) {
  837. return false;
  838. }
  839. // Sanity check : make sure we've got a working toolkit
  840. if (!image_get_toolkit()) {
  841. drupal_set_message(t('No image toolkit is currently enabled. Without one the image module will not be able to resize your images. You can select one from the <a href="!link">image toolkit settings page</a>.', array('!link' => url('admin/settings/image-toolkit'))), 'error');
  842. return false;
  843. }
  844. return true;
  845. }
  846. /**
  847. * Determine which sizes of derivative images need to be built for this image.
  848. *
  849. * @param $image_path
  850. * String file path to the image.
  851. * @return
  852. * Returns a subset of image_get_sizes()'s results depending on what
  853. * derivative images are needed.
  854. */
  855. function image_get_derivative_sizes($image_path) {
  856. $sizes = array();
  857. // Can't do much if we can't read the image.
  858. if (!$image_info = image_get_info($image_path)) {
  859. return $sizes;
  860. }
  861. $all_sizes = image_get_sizes(NULL, $image_info['height'] / $image_info['width']);
  862. foreach ($all_sizes as $key => $size) {
  863. // We don't want to include the original.
  864. if ($key == IMAGE_ORIGINAL) {
  865. continue;
  866. }
  867. // If the original isn't bigger than the requested size then there's no
  868. // need to resize it.
  869. if ($image_info['width'] > $size['width'] || $image_info['height'] > $size['height']) {
  870. $sizes[$key] = $size;
  871. }
  872. }
  873. return $sizes;
  874. }
  875. /**
  876. * Generate image derivatives.
  877. */
  878. function _image_build_derivatives(&$node, $temp = FALSE) {
  879. // sanity check:
  880. if (!_image_check_settings()) {
  881. return FALSE;
  882. }
  883. $original_path = file_create_path($node->images[IMAGE_ORIGINAL]);
  884. // Figure out which sizes we need to generate.
  885. $all_sizes = image_get_sizes();
  886. $needed_sizes = image_get_derivative_sizes($original_path);
  887. $unneeded_sizes = array_diff(array_keys($all_sizes), array_keys($needed_sizes));
  888. // Images that don't need a derivative image get set to the original.
  889. foreach ($unneeded_sizes as $key) {
  890. $node->images[$key] = $original_path;
  891. }
  892. // Resize for the necessary sizes.
  893. $image_info = image_get_info($original_path);
  894. foreach ($needed_sizes as $key => $size) {
  895. $destination = _image_filename($original_path, $key, $temp);
  896. $status = FALSE;
  897. switch ($size['operation']) {
  898. // Depending on the operation, the image will be scaled or resized & cropped
  899. case 'scale':
  900. $status = image_scale($original_path, $destination, $size['width'], $size['height']);
  901. break;
  902. case 'scale_crop':
  903. // This is based on Drupal 6's image_scale_and_crop(). Scales the
  904. // image so that the image's smaller dimension matches the
  905. // requested size and the crops any overlap on the image's larger
  906. // dimension.
  907. $scale = max($size['width'] / $image_info['width'], $size['height'] / $image_info['height']);
  908. $x = round(($image_info['width'] * $scale - $size['width']) / 2);
  909. $y = round(($image_info['height'] * $scale - $size['height']) / 2);
  910. if (image_resize($original_path, $destination, $image_info['width'] * $scale, $image_info['height'] * $scale)) {
  911. $status = image_crop($destination, $destination, $x, $y, $size['width'], $size['height']);
  912. }
  913. break;
  914. }
  915. if (!$status) {
  916. drupal_set_message(t('Unable to create scaled %label image', array('%label' => $size['label'])), 'error');
  917. return FALSE;
  918. }
  919. // Set standard file permissions for webserver-generated files
  920. @chmod($destination, 0664);
  921. $node->images[$key] = $destination;
  922. module_invoke_all('image_alter', $node, $destination, $key);
  923. }
  924. }
  925. /**
  926. * Creates an image filename.
  927. */
  928. function _image_filename($filename, $label = IMAGE_ORIGINAL, $temp = FALSE) {
  929. $path = file_directory_path() .'/'. variable_get('image_default_path', 'images');
  930. if ($temp) {
  931. $path .= '/temp';
  932. }
  933. $filename = basename($filename);
  934. // Insert the resized name in non-original images
  935. if ($label && ($label != IMAGE_ORIGINAL)) {
  936. $pos = strrpos($filename, '.');
  937. if ($pos === false) {
  938. // The file had no extension - which happens in really old image.module
  939. // versions, so figure out the extension.
  940. $image_info = image_get_info(file_create_path($path .'/'. $filename));
  941. $filename = $filename .'.'. $label .'.'. $image_info['extension'];
  942. }
  943. else {
  944. $filename = substr($filename, 0, $pos) .'.'. $label . substr($filename, $pos);
  945. }
  946. }
  947. return file_create_path($path .'/'. $filename);
  948. }
  949. /**
  950. * Helper function to return the defined sizes (or proper defaults).
  951. *
  952. * @param $size
  953. * An optional string to return only the image size with the specified key.
  954. * @param $aspect_ratio
  955. * Float value with the ratio of image height / width. If a size has only one
  956. * dimension provided this will be used to compute the other.
  957. * @return
  958. * An associative array with width, height, and label fields for the size.
  959. * If a $size parameter was specified and it cannot be found FALSE will be
  960. * returned.
  961. */
  962. function image_get_sizes($size = NULL, $aspect_ratio = NULL) {
  963. $defaults = array(
  964. IMAGE_ORIGINAL => array('width' => '', 'height' => '', 'label' => t('Original'), 'operation' => 'scale', 'link' => IMAGE_LINK_SHOWN),
  965. IMAGE_THUMBNAIL => array('width' => 100, 'height' => 100, 'label' => t('Thumbnail'), 'operation' => 'scale', 'link' => IMAGE_LINK_SHOWN),
  966. IMAGE_PREVIEW => array('width' => 640, 'height' => 640, 'label' => t('Preview'), 'operation' => 'scale', 'link' => IMAGE_LINK_SHOWN),
  967. );
  968. $sizes = array();
  969. foreach (variable_get('image_sizes', $defaults) as $key => $val) {
  970. // Only return sizes with a label.
  971. if (!empty($val['label'])) {
  972. // For a size with only one dimension specified, compute the other
  973. // dimension based on an aspect ratio.
  974. if ($aspect_ratio && (empty($val['width']) || empty($val['height']))) {
  975. if (empty($val['height']) && !empty($val['width'])) {
  976. $val['height'] = (int)round($val['width'] * $aspect_ratio);
  977. }
  978. elseif (empty($val['width']) && !empty($val['height'])) {
  979. $val['width'] = (int)round($val['height'] / $aspect_ratio);
  980. }
  981. }
  982. $sizes[$key] = $val;
  983. }
  984. }
  985. // If they requested a specific size return only that.
  986. if (isset($size)) {
  987. // Only return an array if it's available.
  988. return isset($sizes[$size]) ? $sizes[$size] : FALSE;
  989. }
  990. return $sizes;
  991. }
  992. /**
  993. * Helper function to preserve backwards compatibility. This has been
  994. * deprecated in favor of image_get_sizes().
  995. *
  996. * @TODO: Remove this in a future version.
  997. */
  998. function _image_get_sizes($size = NULL, $aspect_ratio = NULL) {
  999. return image_get_sizes($size, $aspect_ratio);
  1000. }
  1001. /**
  1002. * Is a given size a built-in, required size?
  1003. * @param $size
  1004. * One of the keys in the array returned by image_get_sizes().
  1005. * @return boolean
  1006. */
  1007. function _image_is_required_size($size) {
  1008. return in_array($size, array(IMAGE_THUMBNAIL, IMAGE_PREVIEW, IMAGE_ORIGINAL));
  1009. }
  1010. /**
  1011. * Moves temporary (working) images to the final directory and stores
  1012. * relevant information in the files table
  1013. */
  1014. function _image_insert(&$node, $size, $image_path) {
  1015. $original_path = $node->images[IMAGE_ORIGINAL];
  1016. if (file_move($image_path, _image_filename($original_path, $size))) {
  1017. // Update the node to reflect the actual filename, it may have been changed
  1018. // if a file of the same name already existed.
  1019. $node->images[$size] = $image_path;
  1020. $image_info = image_get_info($image_path);
  1021. $fid = db_next_id('{files}_fid');
  1022. db_query("INSERT INTO {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s', '%s', '%s', '%s')",
  1023. $fid, $node->nid, $size, $image_path, $image_info['mime_type'], $image_info['file_size']);
  1024. db_query("INSERT INTO {image} (nid, image_size, fid) VALUES (%d, '%s', %d)",
  1025. $node->nid, $size, $fid);
  1026. }
  1027. }
  1028. /**
  1029. * Function to other modules to use to create image nodes.
  1030. *
  1031. * @param $filepath
  1032. * String filepath of an image file. Note that this file will be moved into
  1033. * the image module's images directory.
  1034. * @param $title
  1035. * String to be used as the node's title. If this is ommitted the filename
  1036. * will be used.
  1037. * @param $body
  1038. * String to be used as the node's body.
  1039. * @param $taxonomy
  1040. * Taxonomy terms to assign to the node if the taxonomy.module is installed.
  1041. * @return
  1042. * A node object if the node is created successfully or FALSE on error.
  1043. */
  1044. function image_create_node_from($filepath, $title = NULL, $body = '', $taxonomy = NULL) {
  1045. global $user;
  1046. if (!user_access('create images')) {
  1047. drupal_access_denied();
  1048. }
  1049. // Ensure it's a valid image.
  1050. if (!$image_info = image_get_info($filepath)) {
  1051. return FALSE;
  1052. }
  1053. // Make sure we can copy the file into our temp directory.
  1054. $original_path = $filepath;
  1055. if (!file_copy($filepath, _image_filename($filepath, IMAGE_ORIGINAL, TRUE))) {
  1056. return FALSE;
  1057. }
  1058. // Resize the original image.
  1059. $aspect_ratio = $image_info['height'] / $image_info['width'];
  1060. $size = image_get_sizes(IMAGE_ORIGINAL, $aspect_ratio);
  1061. if (!empty($size['width']) && !empty($size['height'])) {
  1062. image_scale($filepath, $filepath, $size['width'], $size['height']);
  1063. }
  1064. // Build the node.
  1065. $node = new stdClass();
  1066. $node->type = 'image';
  1067. $node->uid = $user->uid;
  1068. $node->name = $user->name;
  1069. $node->title = isset($title) ? $title : basename($filepath);
  1070. $node->body = $body;
  1071. // Set the node's defaults... (copied this from node and comment.module)
  1072. $node_options = variable_get('node_options_'. $node->type, array('status', 'promote'));
  1073. $node->status = in_array('status', $node_options);
  1074. $node->promote = in_array('promote', $node_options);
  1075. if (module_exists('comment')) {
  1076. $node->comment = variable_get("comment_$node->type", COMMENT_NODE_READ_WRITE);
  1077. }
  1078. if (module_exists('taxonomy')) {
  1079. $node->taxonomy = $taxonomy;
  1080. }
  1081. $node->new_file = TRUE;
  1082. $node->images[IMAGE_ORIGINAL] = $filepath;
  1083. // Save the node.
  1084. $node = node_submit($node);
  1085. node_save($node);
  1086. // Remove the original image now that the import has completed.
  1087. file_delete($original_path);
  1088. return $node;
  1089. }
  1090. /**
  1091. * Implementation of hook_panels_include_directory().
  1092. */
  1093. function image_panels_include_directory($plugintype) {
  1094. switch ($plugintype) {
  1095. case 'content_types':
  1096. return $plugintype;
  1097. }
  1098. }