Thanks again V@no for your feedback.
Some changes made in line with your comments and script now able to rotate clockwise or anti-clockwise by clicking appropriate link. Script now also uses a more standard approach to coding.
Add a new script /includes/rotate.php
<?
// change to suit your needs eg: define (JPG_QUALITY, '75'); gives a more compressed
// image file, but a poorer quality, when re-writing the jpeg after rotation.
define('JPG_QUALITY', '100');
//define variables and includes (should be no need to change these)
define('ROOT_PATH', './../');
include(ROOT_PATH.'global.php');
require(ROOT_PATH.'includes/sessions.php');
$user_access = get_permission();
//is there an image_id and does the user have permission to delete the image? If not, send them back to the index page!
if (!$image_id || ($config['user_delete_image'] != 1)) {
header("Location: ".$site_sess->url(ROOT_PATH."index.php", "&"));
exit;
}
//if ok so far, read the image info from the database
$sql = "SELECT image_id, cat_id, user_id, image_name, image_media_file, image_thumb_file
FROM ".IMAGES_TABLE."
WHERE image_id = $image_id";
$image_row = $site_db->query_firstrow($sql);
//is the user a registered user and the 'owner' of the image? If not, send them back to the index page!
if (!$image_row || $image_row['user_id'] <= USER_AWAITING || ($user_info['user_id'] != $image_row['user_id'])) {
header("Location: ".$site_sess->url(ROOT_PATH."index.php", "&"));
exit;
}
//so far so good, more variables need defining...
$ok=false;
$file=$image_row[image_media_file];
$cat_id=$image_row[cat_id];
$path_to_media = ROOT_PATH.MEDIA_DIR."/".$cat_id."/";
$path_to_thumbs = ROOT_PATH.THUMB_DIR."/".$cat_id."/";
$ext = get_file_extension($file);
//clockwise or anticlockwise?
$degrees=0;
if ($wize=="pos"){$degrees = 270;}
if ($wize=="neg"){$degrees = 90;}
// Load existing images (if a 'jpg')
if (($ext == "jpg") || ($ext == "jpeg"))
{
$source_image = imagecreatefromjpeg($path_to_media.$file);
$source_thumb = imagecreatefromjpeg($path_to_thumbs.$file);
$ok=true;
}
// Load existing images (if a 'gif')
if ($ext == "gif")
{
$source_image = imagecreatefromgif($path_to_media.$file);
$source_thumb = imagecreatefromgif($path_to_thumbs.$file);
$ok=true;
}
if ($ok) //is it ok to process?
{
// Rotate
$rotate_image = imagerotate($source_image, $degrees, 0);
$rotate_thumb = imagerotate($source_thumb, $degrees, 0);
//Create new file names using timestamp as 'random' prefix to force browser to reload
$new_file_name=time().".".$ext;
$new_image = $path_to_media.$new_file_name;
$new_thumb = $path_to_thumbs.$new_file_name;
//Output 'new' images
if (($ext == "jpg") || ($ext == "jpeg"))
{
imagejpeg($rotate_image,$new_image,JPG_QUALITY);
imagejpeg($rotate_thumb,$new_thumb,JPG_QUALITY);
}
if ($ext == "gif")
{
imagegif($rotate_image,$new_image);
imagegif($rotate_thumb,$new_thumb);
}
//Delete 'old' files
unlink($path_to_media.$file);
unlink($path_to_thumbs.$file);
imagedestroy($rotate_image);
imagedestroy($rotate_thumb);
imagedestroy($source_image);
imagedestroy($source_thumb);
//write 'new' filenames to database
$sql=" UPDATE `4images_images`
SET `image_media_file` = '$new_file_name',`image_thumb_file` = '$new_file_name'
WHERE `image_id` = '$image_id'
LIMIT 1";
$result = $site_db->query($sql);
} //end of processing
//display new rotated image
header("Location: ".$site_sess->url(ROOT_PATH."details.php?&".URL_IMAGE_ID."=".$image_id, "&"));
?>
Go to ./details.php.
Find:
elseif ($is_image_owner) {
$admin_links .= ($config['user_edit_image'] != 1) ? "" : "<a href=\"".$site_sess->url(ROOT_PATH."member.php?action=editimage&".URL_IMAGE_ID."=".$image_id)."\">".$lang['edit']."</a> ";
$admin_links .= ($config['user_delete_image'] != 1) ? "" : "<a href=\"".$site_sess->url(ROOT_PATH."member.php?action=removeimage&".URL_IMAGE_ID."=".$image_id)."\">".$lang['delete']."</a>";
}
Replace with:
elseif ($is_image_owner) {
$admin_links .= ($config['user_edit_image'] != 1) ? "" : "<a href=\"".$site_sess->url(ROOT_PATH."member.php?action=editimage&".URL_IMAGE_ID."=".$image_id)."\">".$lang['edit']."</a> ";
$admin_links .= ($config['user_delete_image'] != 1) ? "" : "<a href=\"".$site_sess->url(ROOT_PATH."member.php?action=removeimage&".URL_IMAGE_ID."=".$image_id)."\">".$lang['delete']."</a> ";
$admin_links .= ($config['user_delete_image'] != 1) ? "" : "<a href=\"".$site_sess->url(ROOT_PATH."includes/rotate.php?".URL_IMAGE_ID."=".$image_id."&wize=neg")."\">".$lang['rotate_neg']."</a> ";
$admin_links .= ($config['user_delete_image'] != 1) ? "" : "<a href=\"".$site_sess->url(ROOT_PATH."includes/rotate.php?".URL_IMAGE_ID."=".$image_id."&wize=pos")."\">".$lang['rotate_pos']."</a>";
}
Go to ./lang/{your language}/main.php
Find:
$lang['edit'] = "[Edit]";
$lang['delete'] = "[Delete]";
Add below:
$lang['rotate_neg'] = "[Rotate-Anti Clockwise]";
$lang['rotate_pos'] = "[Rotate-Clockwise]";