forza la generazione dello stile di immagine sul nodo di salvataggio


Risposte:


12

Sì: è possibile implementare hook_node_insert()e hook_node_update()in un modulo personalizzato e creare lì le immagini utilizzando le funzioni dell'API delle immagini. Per esempio

function MYMODULE_node_insert($node) {
  // Get some field items from a field called 'field_photo.
  if ($image_items = field_get_items('node', $node, 'field_photo')) {
    $image_item = array_shift($image_items);

    // Load the associated file.
    $file = file_load($image_item['fid']);

    // An array of image styles to create.
    $image_styles = array('style_1', 'style_2');

    foreach ($image_styles as $style_name) {
      // Get the location of the new image.
      $derivative_uri = image_style_path($style_name, $file->uri);

      // Create the image.
      image_style_create_derivative($style_name, $file->uri, $derivative_uri);
    }
  }
}

sembra fantastico grazie! ma potresti sapere come ottenere il valore degli elementi della raccolta di campi relativi al mio nodetipo?
— sinini,


ho notato ora che le immagini generate non vengono ridimensionate, ma archiviate nella dimensione originale. ti è mai successo e potresti avere idea di come risolverlo?
— sinini,


La vita di D7 è limitata, ma nel caso qualcuno dovesse imbattersi in questo come ho fatto oggi, una correzione. image_style_create_derivative () prevede una matrice per il primo parametro, quindi quella riga dovrebbe essere: image_style_create_derivative (image_style_load ($ style_name), $ file-> uri, $ derivative_uri);
— Segna il

9

Le due risposte con blocchi di codice sono per lo più corrette, tranne che trascurano una cosa importante:

Il primo argomento di image_style_create_derivative dovrebbe essere un array di stili di immagine.

Quello che stanno passando è solo il nome dello stile. Nel foreach se aggiungi:

$style = image_style_load($style_name);

quindi cambia $ style_name in $ style nella funzione image_style_create_derivative, dovrebbe funzionare come previsto e generare l'immagine con stile.

image_style_create_derivative($style, $file->uri, $derivative_uri);

Spero che possa aiutare chiunque abbia questo problema.


4

Grazie per il tuo aiuto Clive, tutta la mia funzione per gli articoli della raccolta di campi: (un altro tuo utile post: Accesso a una raccolta di campi )

function channelportal_gallery_node_update($node) {

  //get all the id's from the field collection values
  $galleryimages = field_get_items('node', $node, 'field_gallery_and_caption');
  $fieldcollectionids = array();

  foreach ($galleryimages as $key => $value) {
    $fieldcollectionids[] = $value['value'];
  }

  // Load up the field collection items
  $items = field_collection_item_load_multiple($fieldcollectionids);

  foreach ($items as $item) {

    $image_item = field_get_items('field_collection_item', $item, 'field_gallery_image');

    // Load the associated file.
    $file = file_load($image_item[0]['fid']);

    // An array of image styles to create.
    $image_styles = array('gallery_big', 'gallery_thumbnail');

    foreach ($image_styles as $style_name) {
        // Get the location of the new image.
        $derivative_uri = image_style_path($style_name, $file->uri);

        // Create the image.
        image_style_create_derivative($style_name, $file->uri, $derivative_uri);
    }
  }
}


0

Si consiglia di utilizzare hook_node_insert () e hook_node_update () e di verificare se non vengono generati i derivati ​​dell'immagine necessari, quindi generarli, altrimenti non fare nulla.

/**
 * Implements hook_node_insert to generate derivative images for the new inserted node in
 * case they are not generated
 * @param object $node
 */
function YOUR_MODULE_node_insert($node) {
  //REPLACE field_YOUR_IMAGE_FIELD WITH YOUR FIELD IMAGE NAME
  if(isset($node->field_YOUR_IMAGE_FIELD['und'][0]['uri'])) {
    _generate_image_style($node->field_YOUR_IMAGE_FIELD['und'][0]['uri']);
  }
}

/**
 * Implements hook_node_update to generate derivative images for the new updated node in
 * case they are not generated
 * @param object $node
 */
function YOUR_MODULE_node_update($node) {
  //REPLACE field_YOUR_IMAGE_FIELD WITH YOUR FIELD IMAGE NAME
  if(isset($node->field_YOUR_IMAGE_FIELD['und'][0]['uri'])) {
    _generate_image_style($node->field_YOUR_IMAGE_FIELD['und'][0]['uri']);
  }
}

/**
 * Generates the needed image styles by the image uri if they are not already generated
 * @param string $image_uri
 */
function _generate_image_style($image_uri) {
  //This should be changed to your image styles names.
  $image_styles = array('image_style_name1', 'large_image', 'promo_image');
  foreach ($image_styles as $style) {
    $derivative_uri = image_style_path($style, $image_uri);
    file_exists($derivative_uri) || image_style_create_derivative(image_style_load($style), $image_uri, $derivative_uri);
  }
}

Nota: se il tuo campo immagine prende più immagini, dovresti attraversarle in questo modo:

if(isset($node->field_main_image['und']) && is_array($node->field_main_image['und'])) {
  foreach($node->field_main_image['und'] as $delta => $image_field) {
    _generate_image_style($node->field_YOUR_IMAGE_FIELD['und'][$delta]['uri']);
  }
}

La generazione degli stili di immagine è presa da qui

Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.