PHP image operation Tutorial: 3D images, scaling, rotating, cropping, and adding watermarks-PHP source code

Source: Internet
Author: User
Tags 0xc0
Image operations are widely used on websites, especially when the Internet is highly developed. Many content is displayed in images. Now let's talk about using php to operate uploaded images, 3D image rendering, image scaling, image rotation, image cropping, and adding watermarks to images. Image operations are widely used on websites, especially when the Internet is highly developed. Many content is displayed in images. Now let's talk about using php to operate uploaded images, 3D image rendering, image scaling, image rotation, image cropping, and adding watermarks to images.

Script ec (2); script

1. Use functions of the php gd library to draw a 3D sector Statistical Chart

The Code is as follows:
Header ("content-type", "text/html; charset = UTF-8 ");
/* Sector statistical chart */
$ Image = imagecreatetruecolor (100,100);/* Create a canvas */
/* Set the color required for the canvas */
$ White = imagecolorallocate ($ image, 0xff, 0xff, 0xff );
$ Gray = imagecolorallocate ($ image, 0xc0, 0xc0, 0xc0 );
$ Darkgray = imagecolorallocate ($ image, 0x90, 0x90, 0x90 );
$ Navy = imagecolorallocate ($ image, 0x00, 0x00, 0x80 );
$ Darknavy = imagecolorallocate ($ image, 0x00, 0x00, 0x50 );
$ Red = imagecolorallocate ($ image, 0xff, 0x00, 0x00 );
$ Darkred = imagecolorallocate ($ image, 0x90, 0x00, 0x00 );

/* Fill in the background color */
Imagefill ($ image, 0, 0, $ white );

/* 3D production */
For ($ I = 60; $ I> 50; $ I --)
{
Imagefilledarc ($ image, 50, $ I, 100, 50,-160, 40, $ darknavy, IMG_ARC_PIE );
Imagefilledarc ($ image, 50, $ I, 100, 50, 40, 75, $ darkgray, IMG_ARC_PIE );
Imagefilledarc ($ image, 50, $ I, 100, 50, 75,200, $ darkred, IMG_ARC_PIE );
}

/* Draw an elliptical arc and fill it in */
Imagefilledarc ($ image, 50, 50,100, 50,-160, 40, $ darknavy, IMG_ARC_PIE );
Imagefilledarc ($ image, 50, 50,100, 50, 40, 75, $ darkgray, IMG_ARC_PIE );
Imagefilledarc ($ image, 50, 50,100, 50, 75,200, $ darkred, IMG_ARC_PIE );


/* Draw a string */
Imagestring ($ image, 3, 15, 55, "30%", $ white );
Imagestring ($ image, 3, 45, 35, "60%", $ white );
Imagestring ($ image, 3, 60, 60, "10%", $ white );

/* Output image */
Header ("content-type: image/png ");
Imagepng ($ image );

/* Release resources */
Imagedestroy ($ image );
?>


2. Scale the image

The Code is as follows:


Source image size



Header ("content-type", "text/html; charset = UTF-8 ");

/*
* Image Scaling
* @ Param string $ filename url of the image
* @ Param int $ width: sets the maximum width for image scaling.
* @ Param int $ height: sets the maximum zoom height of the image.
*/
Function thumb ($ filename, $ width = 130, $ height = 130)
{
/* Obtain the size of the source image */
List ($ width_orig, $ height_orig) = getimagesize ($ filename );

/* Calculate the height and width of the proportional ratio based on the $ width and $ height parameters */
If ($ width & ($ width_orig <$ height_orig ))
{
$ Width = ($ height/$ height_orig) * $ width_orig;
}
Else
{
$ Height = ($ width/$ width_orig) * $ height_orig;
}

/* Create a canvas with a new size */
$ Image_p = imagecreatetruecolor ($ width, $ height );

/* Obtain image resources */
$ Image = imagecreatefrompng ($ filename );

/* Use imagecopyresampled scaling */
Imagecopyresampled ($ image_p, $ image, 0, 0, 0, 0, $ width, $ height, $ width_orig, $ height_orig );

/* Save the scaled image and name */

Imagepng(%image_p,'test.png ');

/* Release resources */

Imagedestroy ($ image_p );
Imagedestroy ($ image );
}

/* Call a function */
Thumb('1.png ');
?>


Scaled size


3. PHP cropping Images

The Code is as follows:


Before Cropping



Header ("content-type", "text/html; charset = UTF-8 ");
/*
* Crop images
* @ Param string $ filename url of the image
* @ Param int $ width: crop the image width.
* @ Param int $ height: crop the Image height
* @ Param int $ x crop the start position on the left of the image
* @ Param int $ y specifies the starting position of the top edge of the cropped image.
*/
Function cut ($ filename, $ x, $ y, $ width, $ height)
{
/* Obtain image resources and images to be cropped */
$ Image = imagecreatefrompng ($ filename );

/* Create a canvas with a new size and save the cropped image */

$ Image_p = imagecreatetruecolor ($ width, $ height );

/* Use imagecopyresampled scaling */
Imagecopyresampled ($ image_p, $ image, 0, 0, $ x, $ y, $ width, $ height, $ width, $ height );
/* Save the cropped image and name */
Imagepng(%image_p,'test1.png ');
/* Release resources */
Imagedestroy ($ image_p );
Imagedestroy ($ image );
}

/* Call a function */
Cut('1.png ', 20, 20, 80, 80 );
?>


After Cropping

4. Add a watermark to the image using PHP

The Code is as follows:


No watermark



Header ("content-type", "text/html; charset = UTF-8 ");
/*
* Add a watermark to the background image. The format of the background image is png, and the format of the watermark is gif.
* @ Param string $ filename url of the image
* @ Param string $ water watermark image
*/
Function watermark ($ filename, $ water)
{
/* Obtain the size of the source image */
List ($ B _w, $ B _h) = getimagesize ($ filename );
/* Obtain the watermark image size */
List ($ w_w, $ w_h) = getimagesize ($ water );
/* Random start position of the watermark image in the background image */
$ PosX = rand (0, ($ B _w-$ w_w ));
$ PosY = rand (0, ($ B _h-$ w_h ));
/* Obtain image resources and images to be cropped */
$ Back = imagecreatefrompng ($ filename );
$ Water = imagecreatefromgif ($ water );
/* Use the Inagecopy function to copy the watermark image to the specified position */
Imagecopy ($ back, $ water, $ posX, $ posY, 0, 0, $ w_w, $ w_h );
/* Save the watermark image and name */
Imagepng(background back,'test2.png ');
/* Release resources */
Imagedestroy ($ back );
Imagedestroy ($ water );
}

/* Call a function */
Watermark('1.png', 'test.gif ');
?>


Add watermark

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.