Home > Backend Development > PHP Tutorial > How to rotate images and keep padding using PHP and OpenCV libraries

How to rotate images and keep padding using PHP and OpenCV libraries

WBOY
Release: 2023-07-17 19:06:02
Original
772 people have browsed it

How to rotate images and keep padding using PHP and OpenCV libraries

Abstract:
In image processing, image rotation is a common operation. This article will introduce how to use PHP and OpenCV libraries to achieve image rotation and keep padding. We will use PHP's image processing extension library GD and OpenCV's PHP extension library to implement this function. The article will provide detailed code examples to help readers understand and practice.

  1. Introduction
    Image processing has been widely used in computer vision, image recognition and other fields. Among them, image rotation is one of the basic operations of image processing. However, conventional image rotation often causes image distortion and cropping. In order to solve this problem, we can use the method of rotating the image and keeping the padding, so that the rotated image still remains intact.
  2. Preparation
    Before we begin, we need to ensure that we have installed the PHP image processing extension library GD and the OpenCV PHP extension library. If not installed, you can install them according to the following command:

    sudo apt-get install php7.4-gd
    sudo apt-get install php7.4-opencv
    Copy after login
  3. Image rotation code example
    Here is a sample code to implement image rotation and keep filling using PHP and OpenCV library:
<?php
// 加载OpenCV库
if (!extension_loaded('opencv'))
{
    dl('opencv.' . PHP_SHLIB_SUFFIX);
}

// 加载源图像
$srcImage = imagecreatefromjpeg('input.jpg');

// 源图像的宽度和高度
$srcWidth = imagesx($srcImage);
$srcHeight = imagesy($srcImage);

// 创建一个新的图像对象,大小为旋转后的图像大小
$newWidth = $srcHeight; // 宽度等于高度
$newHeight = $srcWidth; // 高度等于宽度
$dstImage = imagecreatetruecolor($newWidth, $newHeight);

// 旋转图像
$angle = 90; // 旋转角度
$bgColor = imagecolorallocate($dstImage, 255, 255, 255); // 填补颜色为白色
$dstImage = imagerotate($srcImage, $angle, $bgColor);

// 保存旋转后的图像
imagejpeg($dstImage, 'output.jpg');
?>
Copy after login

The above code will load an image from a source image named input.jpg and create a new image object with the size of the rotated image. The algorithm rotates the image 90 degrees and fills it with white. Finally, save the rotated image as output.jpg.

  1. Conclusion
    This article introduces how to use PHP and OpenCV libraries to achieve image rotation and keep padding. We can easily implement this function by using PHP's image processing extension library GD and OpenCV's PHP extension library. The article provides detailed code examples, readers can practice and further explore based on the code.

Through the study and practice of this article, readers will be able to better understand and apply PHP and OpenCV libraries to rotate images and maintain filling methods. I hope this article will be helpful to readers in their study and application in the field of image processing.

The above is the detailed content of How to rotate images and keep padding using PHP and OpenCV libraries. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template