PHP image processing-image rotation and Image flip instances

Source: Internet
Author: User
Tags image flip imagecopy imagejpeg transparent color
This article mainly introduces PHP Image Processing's image rotation and Image flip instances. This article uses the imagerotate function to implement and customize multiple functions to meet functional requirements, you can refer to the rotation and flip of images as a common feature in Web projects. However, there are two different concepts. the rotation of images is based on a specific angle, the image flip function is to adjust the image content in a specific direction. You need to write a function to implement Image flip, while rotating images can be done directly using the imagerotate () function provided in the GD Library. The function is prototype as follows:

The code is as follows:


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

This function can rotate the src_im image with the given angle. bgd_color specifies the color of the part not covered after rotation. The center of rotation is the center of the image, and the rotated image scales down proportionally to fit the size of the target image (the edge is not cut off ). If ignore_transpatrent is set to a non-zero value, the transparent color is ignored (otherwise it will be retained ). The following uses an image in JPEG format as an example to declare a rotate () function that can rotate the image. the code is as follows:

The code is as follows:


<? Php
// Rotate the image from a given angle. take the jpeg image format as an example.
Function rotate ($ filename, $ degrees ){
// Create image resources. the jpeg format is used as an example.
$ Source = imagecreatefromjpeg ($ filename );
// Use the imagerotate () function to rotate at the specified angle
$ Rotate = imagerotate ($ source, $ degrees, 0 );
// Save the rotated image
$ Imagejpeg ($ rotate, $ filename );
}

// Rotate the image brophp.jpg at 180 degrees.
Rotate ("brophp", 180 );
?>

You cannot specify an angle for image flip. you can set only two directions: horizontal flip along the y axis or vertical flip along the x axis. If the image is flipped along the y axis, the source image is rotated from right to left (or from right to right) by a pixel width and copied to the new resource in a cycle at the height of the image, the saved new resource is the image that is flipped along the y axis. Take an image in JPEG format as an example. the code for declaring an image function turn_y () that can be flipped along the y axis is as follows:

The code is as follows:


<? Php
Function trun_y ($ filename ){
$ Back = imagecreatefromjpeg ($ filename );

$ Width = imagesx ($ back );
$ Height = imagesy ($ back );

// Create a new image resource to save the image that is flipped along the y axis
$ New = imagecreatetruecolor ($ width, $ height );
// Flip along the y axis to copy the source image one by one from the right to the left to the new resource.
For ($ x = 0; $ x <$ width; $ x ++ ){
// Copy the image height one by one, with one pixel width to the salary source
Imagecopy ($ new, $ back, $ width-$ X-1, 0, $ x, 0, 1, $ height );
}

// Save the flipped image
Imagejpeg ($ new, $ filename );
Imagedestroy ($ back );
Imagedestroy ($ new );
}

Trun_y ("brophp.jpg ")
?>

The turn_y () function declared in this example only needs one parameter, that is, the image URL to be processed. In this example, the turn_y () function is called to flip the image along the y axis. If it is flipped along the x axis, it is to rotate the source image from top down (or from bottom up), the code is as follows:

The code is as follows:


<? Php
Function trun_x ($ filename ){
$ Back = imagecreatefromjpeg ($ filename );

$ Width = imagesx ($ back );
$ Height = imagesy ($ back );

// Create a new image resource to save the image that is flipped along the y axis
$ New = imagecreatetruecolor ($ width, $ height );
// Flip along the y axis to copy the source image one by one from the right to the left to the new resource.
For ($ y = 0; $ y <$ height; $ y ++ ){
// Copy the image height one by one, with one pixel width to the salary source
Imagecopy ($ new, $ back, 0, $ height-$ Y-1, 0, $ y, $ width, 1 );
}

// Save the flipped image
Imagejpeg ($ new, $ filename );
Imagedestroy ($ back );
Imagedestroy ($ new );
}

Trun_x ("brophp.jpg ")
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.