You are here

function simple_image_rotate_rotate_image in Simple Image Rotate 7.2

Same name and namespace in other branches
  1. 8 simple_image_rotate.module \simple_image_rotate_rotate_image()
  2. 7 simple_image_rotate.module \simple_image_rotate_rotate_image()
  3. 2.1.x simple_image_rotate.module \simple_image_rotate_rotate_image()
  4. 1.0.x simple_image_rotate.module \simple_image_rotate_rotate_image()
  5. 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 116
Allows users to rotate images on node forms.

Code

function simple_image_rotate_rotate_image(&$entity, $type) {

  // For some reason sometime its called twice, so $entity->rotated will stop calling twice
  if (!isset($entity->rotated)) {
    $entity->rotated = TRUE;

    // Load all image fields.
    $image_fields = array_keys(field_read_fields(array(
      'type' => 'image',
    )));

    // Loop through all image fields.
    foreach ($image_fields as $field) {
      $field_language = field_language($type, $entity, $field);

      // Image field exists in the entity.
      if (isset($entity->{$field}) && is_array($entity->{$field}[$field_language])) {

        // Loop through all images of this field.
        foreach ($entity->{$field}[$field_language] as $delta => $value) {

          // If rotate angle is indicated.
          if (isset($value['rotate']) && $value['rotate']) {

            // Load image file.
            $file = file_load($value['fid']);

            // Get a new filename with '_r[counter]' suffix.
            if (preg_match('#_r([\\d]+)\\.[^.]+$#i', $file->uri, $matches)) {
              $file_counter = $matches[1];
              $new_uri = $file->uri;
            }
            else {
              $file_counter = 1;
              $pos = strrpos($file->uri, '.');
              $new_uri = substr_replace($file->uri, '_r' . $file_counter, $pos, 0);
            }

            // Increment filename counter if filename is occupied.
            while (file_exists($new_uri)) {
              $find = '_r' . $file_counter;
              $file_counter++;
              $replace = '_r' . $file_counter;
              $new_uri = str_replace($find, $replace, $new_uri);
            }

            // 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);
              try {
                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}[$field_language][$delta]['width'] = $image->info['width'];
                  $entity->{$field}[$field_language][$delta]['height'] = $image->info['height'];

                  // Update file.
                  $file->filesize = $image->info['file_size'];
                  file_save($file);
                }
              } catch (Exception $ex) {
                watchdog('simple_image_rotate', t('Problem rotating image: ') . $ex
                  ->getMessage());
              }
            }
          }
        }
      }
    }
  }
}