Disarankan untuk menggunakan hook_node_insert () dan hook_node_update () dan untuk memeriksa apakah turunan gambar yang dibutuhkan tidak dihasilkan, kemudian buat, jika tidak, jangan lakukan apa-apa.
/**
* 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);
}
}
Catatan: Jika bidang gambar Anda mengambil banyak gambar maka Anda harus mengulanginya seperti itu:
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']);
}
}
Generasi gaya gambar diambil dari sini