Rotate image before upload feature allows the user to fix the photo orientation when uploading images to the server. With this feature, the user can preview the image and correct the orientation before file upload. Image rotation functionality is very useful when you want to preview the image orientation and rotate image before upload.
Preview image before upload and rotate image element functionality can be easily implemented using jQuery. You can rotate image in a specific angle and upload image to server using PHP. In this tutorial, we will show you how to preview image using jQuery and rotate image before upload to the server using PHP.
The following functionality will be integrated into the example image rotate script.
Create an HTML form with a file input field (for select image file), hidden input (for specifying the rotation degrees) and a submit button.
<form method="post" action="upload.php" enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<input type="hidden" name="rotation" id="rotation" value="0"/>
<input type="submit" name="submit" value="Upload"/>
</form>
Define an HTML element to preview the image.
<div class="img-preview" style="display: none;">
<button id="rleft">Left</button>
<button id="rright">Right</button>
<div id="imgPreview"></div>
</div>
The jQuery is used to add, remove, and rotate image element, so include the jQuery library first.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
The filePreview()
is a custom JavaScript function that generates a preview of the image.
function filePreview(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#imgPreview + img').remove();
$('#imgPreview').after('<img src="'+e.target.result+'" class="pic-view" width="450" height="300"/>');
};
reader.readAsDataURL(input.files[0]);
$('.img-preview').show();
}else{
$('#imgPreview + img').remove();
$('.img-preview').hide();
}
}
Once an image file is selected, the preview appears under the specified element using filePreview() function.
filePreview()
function is triggered using the jQuery change() method.filePreview()
function displays a preview of the selected image on the web page.$("#file").change(function (){
// Image preview
filePreview(this);
});
Note that: You can use the File Type (extension) Validation using JavaScript to restrict the user to select only the image files.
The following code rotates the image by changing the transform property of the element using jQuery.
$(function() {
var rotation = 0;
$("#rright").click(function() {
rotation = (rotation -90) % 360;
$(".pic-view").css({'transform': 'rotate('+rotation+'deg)'});
if(rotation != 0){
$(".pic-view").css({'width': '300px', 'height': '300px'});
}else{
$(".pic-view").css({'width': '100%', 'height': '300px'});
}
$('#rotation').val(rotation);
});
$("#rleft").click(function() {
rotation = (rotation + 90) % 360;
$(".pic-view").css({'transform': 'rotate('+rotation+'deg)'});
if(rotation != 0){
$(".pic-view").css({'width': '300px', 'height': '300px'});
}else{
$(".pic-view").css({'width': '100%', 'height': '300px'});
}
$('#rotation').val(rotation);
});
});
After the form submission, the selected file is sent to the server-side script (upload.php
) to proceed with the upload process using PHP.
<?php
$uploadPath = 'uploads/';
$statusMsg = '';
$upload = 0;
if(isset($_POST['submit'])){
if(!empty($_FILES['file']['name'])){
$fileName = $_FILES['file']['name'];
$fileType = $_FILES['file']['type'];
$fileTemp = $_FILES['file']['tmp_name'];
$filePath = $uploadPath.basename($fileName);
// Allow certain file formats
$allowTypes = array('image/png','image/jpg','image/jpeg','image/gif');
if(in_array($fileType, $allowTypes)){
$rotation = $_POST['rotation'];
if($rotation == -90 || $rotation == 270){
$rotation = 90;
}elseif($rotation == -180 || $rotation == 180){
$rotation = 180;
}elseif($rotation == -270 || $rotation == 90){
$rotation = 270;
}
if(!empty($rotation)){
switch($fileType){
case 'image/png':
$source = imagecreatefrompng($fileTemp);
break;
case 'image/gif':
$source = imagecreatefromgif($fileTemp);
break;
default:
$source = imagecreatefromjpeg($fileTemp);
}
$imageRotate = imagerotate($source, $rotation, 0);
switch($fileType){
case 'image/png':
$upload = imagepng($imageRotate, $filePath);
break;
case 'image/gif':
$upload = imagegif($imageRotate, $filePath);
break;
default:
$upload = imagejpeg($imageRotate, $filePath);
}
}elseif(move_uploaded_file($fileTemp, $filePath)){
$upload = 1;
}else{
$statusMsg = 'File upload failed, please try again.';
}
}else{
$statusMsg = 'Sorry, only JPG/JPEG/PNG/GIF files are allowed to upload.';
}
}else{
$statusMsg = 'Please select a file to upload.';
}
}
// Display status
if($upload == 1){
echo '<h4>File has been uploaded successfully</h4>';
echo '<img src="'.$filePath.'" width="450" height="300" />';
}else{
echo '<h4>'.$statusMsg.'</h4>';
}
?>
Upload File without Page Refresh using jQuery, Ajax, and PHP
Our example code will help you to rotate an image before upload it on the server using PHP. The image preview feature makes the image upload process user-friendly. The image orientation change option adds an extra value to the image upload functionality. Our example code provides an easy way to rotate the image in the client-side using CSS property and upload a rotated image to the server using PHP. You can easily enhance the functionality of Preview and Rotate Image Before Upload script as per your needs.
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request