Image Orientation in PHP
How to fixed Correct Orientation rotate image problem .
Solution:-
public function imageOrientaionUpload(Request $request) { $publicPath = public_path('uploads_directory/'); if($request->file('imgFile') == null) { echo 'No File Exist'; } $file = $request->file('imgFile'); // generating unique name for uploaded file $uniq = uniqid().time(); $uploadFile = $uniq.'.'.$file->getClientOriginalExtension(); // getting file path $filePath = $file->getPathName(); // image rotation code in php starts here $exif = @exif_read_data($filePath); if(isset($exif['Orientation'])) { $orientation = $exif['Orientation']; if (isset($orientation) && $orientation != 1){ switch ($orientation) { case 3: // 180 degree image rotation $deg = 180; break; case 6: // 270 degree image rotation $deg = 270; break; case 8: // 90 degree image rotation $deg = 90; break; } if ($deg) { // If uploaded image is php if ($file->getClientOriginalExtension() == "png") { $img_new = imagecreatefrompng($filePath); $img_new = imagerotate($img_new, $deg, 0); // Save rotated image imagepng($img_new,$filePath); } else { $img_new = imagecreatefromjpeg($filePath); $img_new = imagerotate($img_new, $deg, 0); // Save rotated image imagejpeg($img_new,$filePath,80); } } } } // image rotation code in php ends here // image compression start $destinationPath = $publicPath.$uploadFile; // checking file extenstion in php $getFileExtn = strtolower($file->getClientOriginalExtension()); $imageExtnArr = ['jpeg','jpg','png','bmp']; if(!in_array($getFileExtn, $imageExtnArr)){ echo 'Image File format not supported'; } // save roatate image $img->save($destinationPath,70); echo 'failed to upload image';}How to do a rotation of an image with PHP (Laravel)
Note:- Any Question any Time on the comment section
Your Answer:-Please comment on this.
Thanks.
Comments
Post a Comment