PHP picture processing picture rotation and picture flipping instances, _php tutorial

Source: Internet
Author: User
Tags imagecopy imagejpeg transparent color

PHP image processing image rotation and image rollover instances,


Picture rotation and flipping is also a common feature in Web projects, but this is two different concepts, the rotation of the picture is to rotate the picture at a specific angle, and the picture is flipped to the content of the picture in a specific direction. Picture flipping needs to write its own function to implement, and rotate the picture can be directly with the imagerotate () function provided in the GD library to complete. The prototype of the function is as follows:

Copy the 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 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 clipped). If Ignore_transpatrent is set to a value other than 0, the transparent color is ignored (otherwise it will be preserved). The following is an example of a JPEG-formatted image, declaring a function rotate () that can rotate a picture, as shown in the following code:

Copy the Code code as follows:
<?php
Rotate the image at a given angle, take the JPEG image format as an example
function rotate ($filename, $degrees) {
Create an image resource, for example in JPEG format
$source = Imagecreatefromjpeg ($filename);
Use the Imagerotate () function to rotate at a specified angle
$rotate = Imagerotate ($source, $degrees, 0);
Picture saved after rotation
$imagejpeg ($rotate, $filename);
}

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

Picture flipping does not arbitrarily specify an angle, only two directions: flip horizontally along the y-axis or vertically along the x-axis. If you flip along the y-axis, the original image is rotated from right to left (or right) by a pixel width, copied to the new resource at the height of the picture itself, and the new resource saved is the picture flipped along the y-axis. For example, in JPEG format, declare a picture function turn_y () code that can be flipped along the y-axis as shown below:

Copy the Code code as follows:
<?php
function trun_y ($filename) {
$back = Imagecreatefromjpeg ($filename);

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

Create a new picture resource to hold the picture after flipping along the y-axis
$new = Imagecreatetruecolor ($width, $height);
Flipping along the y-axis is copying the original image from right to left by one pixel width to the new resource
for ($x =0; $x < $width; $x + +) {
Copy the height of the picture itself, 1 pixel width of the picture into the salary resource
Imagecopy ($new, $back, $width-$x-1, 0, $x, 0, 1, $height);
}

Save the picture after flipping
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 image URL to be processed. This example calls the Turn_y () function to flip the picture along the y-axis. If you are flipping along the x-axis, you rotate the original image from top to bottom (or from the bottom up), as shown in the following code:

Copy the Code code as follows:
<?php
function trun_x ($filename) {
$back = Imagecreatefromjpeg ($filename);

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

Create a new picture resource to hold the picture after flipping along the y-axis
$new = Imagecreatetruecolor ($width, $height);
Flipping along the y-axis is copying the original image from right to left by one pixel width to the new resource
for ($y =0; $y < $height; $y + +) {
Copy the height of the picture itself, 1 pixel width of the picture into the salary resource
Imagecopy ($new, $back, 0, $height-$y-1, 0, $y, $width, 1);
}

Save the picture after flipping
Imagejpeg ($new, $filename);
Imagedestroy ($back);
Imagedestroy ($new);
}

Trun_x ("Brophp.jpg")
?>

http://www.bkjia.com/PHPjc/914056.html www.bkjia.com true http://www.bkjia.com/PHPjc/914056.html techarticle PHP image processing pictures rotation and image rollover instances, the rotation and rollover of the picture is also a common feature in Web projects, but this is two different concepts, the rotation of the picture is by the specific ...

  • 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.