function simple_image_rotate_rotate_image in Simple Image Rotate 7
Same name and namespace in other branches
- 8 simple_image_rotate.module \simple_image_rotate_rotate_image()
- 7.2 simple_image_rotate.module \simple_image_rotate_rotate_image()
- 2.1.x simple_image_rotate.module \simple_image_rotate_rotate_image()
- 1.0.x simple_image_rotate.module \simple_image_rotate_rotate_image()
- 2.0.x simple_image_rotate.module \simple_image_rotate_rotate_image()
Rotates image.
2 calls to simple_image_rotate_rotate_image()
- simple_image_rotate_entity_presave in ./
simple_image_rotate.module - Implements hook_entity_presave().
- simple_image_rotate_profile2_presave in ./
simple_image_rotate.module - Implements hook_profile2_presave().
File
- ./
simple_image_rotate.module, line 104 - Allows users to rotate images on node forms.
Code
function simple_image_rotate_rotate_image($entity, $type) {
// Load all image fields.
$image_fields = array_keys(field_read_fields(array(
'type' => 'image',
)));
// Loop through all image fields.
foreach ($image_fields as $field) {
// Image field exists in the entity.
if (isset($entity->{$field})) {
// Loop through all images of this field.
foreach ($entity->{$field}[LANGUAGE_NONE] as $delta => $value) {
// If rotate angle is indicated.
if (isset($value['rotate']) && $value['rotate']) {
// Load image file.
$file = file_load($value['fid']);
// Get new filename for rotated image.
$new_uri = $file->uri;
$file_counter = 1;
if (preg_match('#_r([\\d]+)\\.([^.]+)$#i', $new_uri, $matches)) {
$file_counter = $matches[1] + 1;
$new_uri = str_replace($matches[0], '_r' . $file_counter . '.jpg', $new_uri);
}
// Increment filename counter if filename is occupied.
while (file_exists($new_uri)) {
$pos = strrpos($new_uri, '.');
$new_uri = substr_replace($new_uri, '_r' . $file_counter, $pos, 0);
}
// Move image file to a new location.
if (file_move($file, $new_uri, 'FILE_EXIST_ERROR')) {
// Update file object after moving file.
$file = file_load($value['fid']);
// Rotate image and save image object.
$image = image_load($file->uri);
image_rotate($image, $value['rotate']);
if (image_save($image)) {
// Reload image to get new width, height and filesize.
$image = image_load($file->uri);
// Update image width and height in entity.
$entity->{$field}[LANGUAGE_NONE][$delta]['width'] = $image->info['width'];
$entity->{$field}[LANGUAGE_NONE][$delta]['height'] = $image->info['height'];
// Update file.
$file->filesize = $image->info['file_size'];
file_save($file);
}
}
}
}
}
}
}