PHP image processing image rotation and image Flip Example _php tips

Source: Internet
Author: User
Tags image flip imagecopy imagejpeg transparent color

The rotation and rollover of pictures is also a common feature in Web projects, but this is two different concepts, the rotation of the picture is a specific angle to rotate the picture, and the flip of the picture is the content of the picture in a specific direction of the swap. The picture flips needs to write the function to implement by oneself, but the rotation picture can use directly with the Imagerotate () function which provides in the GD library completes. The prototype of the function looks like this:

Copy Code code as follows:

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

This function rotates the src_im image with the given angle angle, bgd_color specifies the color of the part that is not covered after the rotation. The center of the rotation is the center of the image, and the rotated image is scaled down to fit the target image (the edges are not clipped). If the ignore_transpatrent is set to a value other than 0, the transparent color is ignored (otherwise it is retained). As an example of a picture in JPEG format, declare a function rotate () that can rotate the picture, as shown in the following code:

Copy Code code as follows:

<?php
Rotate the image with the given angle, take the JPEG image format as an example
function rotate ($filename, $degrees) {
To create an image resource, take the JPEG format as an example
$source = Imagecreatefromjpeg ($filename);
Use the Imagerotate () function to rotate at the specified angle
$rotate = Imagerotate ($source, $degrees, 0);
Rotated picture Save
$imagejpeg ($rotate, $filename);
}

Rotate a picture brophp.jpg 180 degrees
Rotate ("brophp", 180);
?>

The picture flips without specifying an angle, and can only be set in two directions: flip horizontally along the y-axis or flip vertically along the x-axis. If you flip along the y-axis, the original image is rotated from right to left (or right) to a pixel width, and the picture itself is rotated to the new resource, and the new resource you save is the picture that is flipped along the y-axis. For example, in a JPEG format picture, declare a picture function that can be flipped along the Y-axis turn_y () code looks like this:

Copy Code code as follows:

<?php
function trun_y ($filename) {
$back = Imagecreatefromjpeg ($filename);

$width = Imagesx ($back);
$height = Imagesy ($back);

Create a new picture resource to save the picture that is flipped along the y-axis
$new = Imagecreatetruecolor ($width, $height);
Flip along the y-axis to copy the original artwork from right to left, one pixel width to the new resource
for ($x =0; $x < $width; $x + +) {
Copy the picture itself height, 1 pixel width of the picture into the payroll resource
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")
?>

This example declares that the turn_y () function requires only one parameter, which is the URL of the picture 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 up), and the code looks like this:

Copy Code code as follows:

<?php
function trun_x ($filename) {
$back = Imagecreatefromjpeg ($filename);

$width = Imagesx ($back);
$height = Imagesy ($back);

Create a new picture resource to save the picture that is flipped along the y-axis
$new = Imagecreatetruecolor ($width, $height);
Flip along the y-axis to copy the original artwork from right to left, one pixel width to the new resource
for ($y =0; $y < $height; $y + +) {
Copy the picture itself height, 1 pixel width of the picture into the payroll resource
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")
?>

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.