X
    Categories: LaravelPHP

How to rotate image and save in php

How to rotate image and save in php

One of the reader requested to explain about rotate the image and save using php. We are calling rotated image as Exif images.
PHP have library function to rotate an image, imagerotate is the php function which is used to rotate the image in php.
imagejpeg function will save the rotated image to your folder.

<?php

//header('Content-type: image/jpeg');
   if(isset($_FILES['image'])){
      $errors= array();
      $file_name = $_FILES['image']['name'];
      $file_size =$_FILES['image']['size'];
      $file_tmp =$_FILES['image']['tmp_name'];
      $file_type=$_FILES['image']['type'];
      $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
 
      $expensions= array("jpeg","jpg","png");
      
      if(in_array($file_ext,$expensions)=== false){
         $errors[]="extension not allowed, please choose a JPEG or PNG file.";
      }
      
      if($file_size > 2097152){
         $errors[]='File size must be excately 2 MB';
      }
      
      if(empty($errors)==true){
         move_uploaded_file($file_tmp, $file_name);
         echo "Success";
      }else{
         print_r($errors);
      }

      $filename = $file_name;  //this is the original file
      //For laravel you can use public_path($filename)
      $exif = exif_read_data($filename);
      if(!empty($exif['Orientation'])) {
         switch($exif['Orientation']) {
                    case 8:
                        $degree = 90;
                        break;
                    case 3:
                        $degree = 180;
                        break;
                    case 6:
                        $degree = -90;
                        break;
                }
      $source = imagecreatefromjpeg($filename) or notfound();
      $rotate = imagerotate($source,$degree,0);

      imagejpeg($rotate,$filename); //save the new image

      imagedestroy($source); //free up the memory
      imagedestroy($rotate);  //free up the memory
      }

      echo '<img src="'.$file_name.'">';
   }
?>
<html>
   <body>
      
      <form action="" method="POST" enctype="multipart/form-data">
         <input type="file" name="image" />
         <input type="submit"/>
      </form>
      
   </body>
</html>

 

imagecreatefromjpeg

Create a new image from file or URL.


imagerotate

Rotates the image image using the given angle in degrees.
The center of rotation is the center of the image, and the rotated image may have different dimensions than the original image.

imagejpeg

imagejpeg() creates a JPEG file from the given image.
bool imagejpeg ( resource $image [, string $filename [, int $quality ]] )

image
An image resource, returned by one of the image creation functions, such as imagecreatetruecolor().

filename
The path to save the file to. If not set or NULL, the raw image stream will be outputted directly.

To skip this argument in order to provide the quality parameter, use NULL.

quality
quality is optional, and ranges from 0 (worst quality, smaller file) to 100 (best quality, biggest file). The default is the default IJG quality value (about 75).

If you like this article please dont’t forget to share and comment.

Thanks!.

Marimuthu: