Examples of Image Rotation and Image Flip in PHP Image Processing

  • 2021-08-05 09:08:00
  • OfStack

Picture rotation and flip are also common functions in Web projects, but these are two different concepts. Picture rotation is to rotate the picture at a specific angle, while picture flip is to switch the contents of the picture in a specific direction. Picture flipping needs to be realized by writing your own functions, while rotating pictures can be completed directly with the help of imagerotate () function provided in GD library. The prototype of this function is as follows:


resource  imagerotate(resource src_im ,    float angle,    int bgd_color    [,int ignore_transpatrent])

This function rotates the src_im image at a given angle angle, and bgd_color specifies the color of the part that is not covered after rotation. The center of the rotation is the center of the image, and the rotated image is scaled down to fit the size of the target image (edges are not cut out). If ignore_transpatrent is set to a non-zero value, the transparent color is ignored (otherwise it is preserved). Let's take the picture in JPEG format as an example, and declare a function rotate () that can rotate the picture. The code is as follows:


<?php
    // Rotate the image at a given angle to jpeg Image format as an example
    function rotate($filename,$degrees){
        // Create an image resource to jpeg Format as an example
        $source = imagecreatefromjpeg($filename);
        // Use imagerotate() Function rotates at the specified angle
        $rotate = imagerotate($source, $degrees, 0);
        // Save the rotated picture
        $imagejpeg($rotate,$filename);
    }
 
    // Put 1 Image brophp.jpg Rotate 180 Degrees
    rotate("brophp", 180);
?>

Picture flip can't specify an angle at will, but can only be set in two directions: horizontal flip along Y axis or vertical flip along X axis. If you flip along the Y axis, you will copy the original image from right to left (or from doing to right) by one pixel width, and copy it to the new resource at the height of the image itself. The saved new resource is the image flipped along the Y axis. Taking the picture in JPEG format as an example, declare a picture function turn_y () that can be flipped along the Y axis as follows:


<?php
    function trun_y($filename){
        $back = imagecreatefromjpeg($filename);
 
        $width = imagesx($back);
        $height = imagesy($back);
 
        // Create 1 A new image resource is used to save the edge Y Picture after axis flipping
        $new = imagecreatetruecolor($width, $height);
        // Along y Axis flip is to press the original image from right to left 1 Pixel width is copied one by one into the new resource
        for($x=0 ;$x<$width; $x++){
            // Copy the height of the picture one by one, 1 Pixel-wide pictures into salary resources
            imagecopy($new, $back, $width-$x-1, 0, $x, 0, 1, $height);
        }
 
        // Save the flipped picture
        imagejpeg($new,$filename);
        imagedestroy($back);
        imagedestroy($new);
    }
 
    trun_y("brophp.jpg")
?>

The turn_y () function declared in this example requires only one argument, which is the image URL to be processed. This example calls the turn_y () function to flip the picture along the Y axis. If you flip along the X axis, you rotate the original image from top to bottom (or from bottom to top). The code is as follows:


<?php
    function trun_x($filename){
        $back = imagecreatefromjpeg($filename);
 
        $width = imagesx($back);
        $height = imagesy($back);
 
        // Create 1 A new image resource is used to save the edge Y Picture after axis flipping
        $new = imagecreatetruecolor($width, $height);
        // Along y Axis flip is to press the original image from right to left 1 Pixel width is copied one by one into the new resource
        for($y=0 ;$y<$height; $y++){
            // Copy the height of the picture one by one, 1 Pixel-wide pictures into salary resources
            imagecopy($new, $back,0, $height-$y-1, 0, $y, $width,1);
        }
 
        // Save the flipped picture
        imagejpeg($new,$filename);
        imagedestroy($back);
        imagedestroy($new);
    }
 
    trun_x("brophp.jpg")
?>


Related articles: