function image_generate_nodeapi in Image 6
Implementation of hook_nodeapi().
File
- contrib/
image_generate/ image_generate.module, line 12 - image_generate.module
Code
function image_generate_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
if (isset($node->devel_generate)) {
if ($op == 'presave') {
if ($node->type == 'image') {
$size = image_get_sizes(IMAGE_ORIGINAL);
if (empty($size['width']) || empty($size['height'])) {
// If we don't have a max size, make one up.
$max_width = 800;
$max_height = 800;
}
else {
$max_width = $size['width'];
$max_height = $size['height'];
}
$width = rand((int) 100, (int) $max_width);
$height = rand((int) 100, (int) $max_height);
$temp_image = _image_generate_image('png', $width, $height);
$size = IMAGE_ORIGINAL;
// Add the tempfile to the node, which is what image does when an image
// is uploaded. This will be deleted when image generates derivatives.
$node->images[$size] = $temp_image;
}
}
if ($op == 'insert') {
if (module_exists('image_attach')) {
if (variable_get('image_attach_' . $node->type, 0)) {
// Load an array of images to attach. Unset the "None" option.
$images = _image_attach_get_image_nodes();
unset($images[0]);
if (count($images)) {
// Get a random number of attachments up to the maximum possible for
// this node type.
$attachments = rand(1, variable_get('image_attach_maximum_' . $node->type, 1));
$weight = 0;
// Insert a random image node id and increment the weight.
for ($i = 1; $i <= $attachments; $i++) {
db_query("INSERT INTO {image_attach} (nid, iid, weight) VALUES (%d, %d, %d)", $node->nid, array_rand($images), $weight++);
}
}
else {
drupal_set_message(t('No images were attached to generated nodes because there are no image nodes to attach.'));
}
}
}
}
}
}