/** * Rotated/Flip an image based on the orientation flag or a definded angle * * @param int|object $image * @param string (optional) $dir, CW (clockwise)or CCW (counter clockwise), if set to false, the exif flag will be used * @param string (optional) $flip, could be either false | V (flip vertical) | H (flip horizontal) * @return string result code */ function rotate_image($image, $dir = false, $flip = false) { global $ngg; if (!class_exists('ngg_Thumbnail')) { require_once nggGallery::graphic_library(); } if (is_numeric($image)) { $image = nggdb::find_image($image); } if (!is_object($image)) { return __('Object didn\'t contain correct data', 'nggallery'); } if (!is_writable($image->imagePath)) { return ' <strong>' . $image->filename . __(' is not writeable', 'nggallery') . '</strong>'; } // if you didn't define a rotation, we look for the orientation flag in EXIF if ($dir === false) { $meta = new nggMeta($image->pid); $exif = $meta->get_EXIF(); if (isset($exif['Orientation'])) { switch ($exif['Orientation']) { case 5: // vertical flip + 90 rotate right $flip = 'V'; case 6: // 90 rotate right $dir = 'CW'; break; case 7: // horizontal flip + 90 rotate right $flip = 'H'; case 8: // 90 rotate left $dir = 'CCW'; break; case 4: // vertical flip $flip = 'V'; break; case 3: // 180 rotate left $dir = 180; break; case 2: // horizontal flip $flip = 'H'; break; case 1: // no action in the case it doesn't need a rotation // no action in the case it doesn't need a rotation default: return '0'; break; } } else { return '0'; } } $file = new ngg_Thumbnail($image->imagePath, TRUE); // skip if file is not there if (!$file->error) { // If required save a backup copy of the file if ($ngg->options['imgBackup'] == 1 && !file_exists($image->imagePath . '_backup')) { @copy($image->imagePath, $image->imagePath . '_backup'); } // before we start we import the meta data to database (required for uploads before V1.4.X) nggAdmin::maybe_import_meta($image->pid); if ($dir !== 0) { $file->rotateImage($dir); } if ($dir === 180) { $file->rotateImage('CCW'); } // very special case, we rotate the image two times if ($flip == 'H') { $file->flipImage(true, false); } if ($flip == 'V') { $file->flipImage(false, true); } $file->save($image->imagePath, $ngg->options['imgQuality']); // read the new sizes $size = @getimagesize($image->imagePath); // add them to the database nggdb::update_image_meta($image->pid, array('width' => $size[0], 'height' => $size[1])); } $file->destruct(); if (!empty($file->errmsg)) { return ' <strong>' . $image->filename . ' (Error : ' . $file->errmsg . ')</strong>'; } return '1'; }