How To Rotate An Image Using Php

geekerhub

Updated on:

rotate an image using php

Discover the straightforward process of rotating an image using PHP. With PHP’s powerful image manipulation capabilities, you can easily modify and transform images to fit your specific requirements. Whether you need to rotate an image using php for a web project, enhance its visual presentation, or align it correctly, PHP offers a range of functions to accomplish this task efficiently.

To rotate an image using PHP, you can leverage functions such as imagerotate() or imagedestroy(). These functions enable you to perform various transformations, including rotating images by a specified angle. By passing the image resource and rotation angle as parameters, you can effortlessly rotate the image.

Additionally, PHP provides options for rotating images in different directions, such as clockwise or counterclockwise, based on your project’s requirements. With PHP’s flexibility and image manipulation capabilities, you can easily incorporate image rotation functionality into your web applications, enhancing user experiences and ensuring visual precision.

By following a few simple steps, you can rotate images seamlessly and ensure they are displayed in the desired orientation.In order to rotate an image, we need to use different functions based on pathinfo extenstion of the image.

PHP functions used to rotate the image

Below is the simple code snippet that will be helpful to rotate the image by passing the path to the image and by what degree you need to rotate the image.

  function image_rotation($filename, $degrees = 90){
      //User this for jpg images
    	if (preg_match("/jpg|jpeg/", pathinfo($filename, PATHINFO_EXTENSION))){
          $image_source = imagecreatefromjpeg($filename);
          $rotate = imagerotate($image_source, $degrees, 0);
          imagejpeg($rotate, $filename);

      }

      //User this for png images
      if (preg_match("/png/", pathinfo($filename, PATHINFO_EXTENSION))){
         $image_source = imagecreatefrompng($filename);
         $rotate = imagerotate($image_source, $degrees, 0);
         imagepng($rotate, $filename);

      }
      //User this for gif images
      if (preg_match("/gif/", pathinfo($filename, PATHINFO_EXTENSION))){
         $image_source = imagecreatefromgif($filename);
         $rotate = imagerotate($image_source, $degrees, 0);
         imagepng($rotate, $filename);
      }

      imagedestroy($image_source); //free up the memory
      imagedestroy($rotate);  //free up the memory
  }
  image_rotation('/opt/lampp/htdocs/wp-momin/wp-content/plugins/lumise-customization/lumise/1649306773-lumise_print_front.png', 180);

Leave a Comment