PHP Image Operations Tutorial: 3D diagram, zoom, rotate, crop, add watermark

Source: Internet
Author: User
Tags 0xc0 rand resource

Picture operation in the application of the Web site is quite wide, especially now the internet is highly developed, a lot of content is shown by pictures, now we talk about using PHP to operate upload pictures, 3D picture drawing, picture scaling, picture rotation, picture cropping, image add watermark.

1, the use of PHP functions of the GD library to draw 3D sector statistics

code as follows  
<?php
Header ("Content-type", "Text/html;charset=utf-8");
* * Fan Statistics chart */
$image = imagecreatetruecolor (100, 100);/* Create canvas */
/* Set canvas required color */
$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 background color */
Imagefill ($image, 0, 0, $white);

/*3d */
for ($i = $i > $i-)
{
Imagefilledarc ($image, $i, MB, -160, $darknavy, IM G_arc_pie);
Imagefilledarc ($image, $i, M, $darkgray, IMg_arc_pie);
Imagefilledarc ($image, $i, M, $darkred, Img_arc_pie);
}

/* Draw an elliptical arc and populate */
Imagefilledarc ($image, -160, $darknavy, Img_arc_pie);
imagefilled Arc ($image, M, M, $darkgray, Img_arc_pie);
Imagefilledarc ($image, N, M, $darkred, Img_arc_pie);


/* Draw String */
Imagestring ($image, 3, MB, 30%, $white);
Imagestring ($image, 3,,, 60%, $whit e);
Imagestring ($image, 3, (), "10%", $white);

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

/* FREE resources */
Imagedestroy ($image);
?

2, scaling the picture

code as follows  
<DIV>
</div>

<? PHP
Header ("Content-type", "Text/html;charset=utf-8");

/*
* Picture Scaling
* @param string $filename The URL of a picture
* @param int $width Set the maximum width for picture scaling
* @param int $height set the maximum picture zoom Height
*/
Function thumb ($filename, $width =130, $height =130)
{
/* Get the size of the original */
list ($width _orig, $height _ ORIG) = getimagesize ($filename);

/* To convert the equal proportions of height and width according to the parameters $width and $height */
if ($width && ($width _orig < $height _orig)
{
$ width = ($height/$height _orig) * $width _orig;
}
Else
{
$height = ($width/$width _orig) * $height _orig;
}

/* Create canvas with new size */
$image _p = Imagecreatetruecolor ($width, $height);

/* Get Image resource */
$image = imagesCreatefrompng ($filename);

/* uses imagecopyresampled scaling */
imagecopyresampled ($image _p, $image, 0, 0, 0, 0, $width, $height, $width _orig, $h Eight_orig);

/* Save zoomed Picture and naming */

Imagepng ($image _p, ' test.png ');

/* FREE resources */

Imagedestroy ($image _p);
Imagedestroy ($image);
}

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

< Div>
</div>

3, PHP cropping of images

code as follows  
<DIV>
before </div>

<? PHP
Header ("Content-type", "Text/html;charset=utf-8"),
/*
* picture cropping
*@ param string $filename The URL of a picture
* @param int $width picture crop width
* @param int $height picture crop height
* @param int $x crop the position to the left of the picture br> * @param int $y Crop The position of the top edge of the picture
*/
Function cut ($filename, $x, $y, $width, $height)
{
/* Get the image resource, need to crop the picture */
$image = Imagecreatefrompng ($filename);

/* Creates a canvas with a new size, saves the cropped picture */

$image _p = Imagecreatetruecolor ($width, $height);

/* Use imagecopyresampled Scaling */
imagecopyresampled ($image _p, $image, 0, 0, $x, $y, $width, $height, $width, $height);
/* Save cropped pictures and named */
Imagepng ($image _p, ' test1.png ');
/* Free resources */
Imagedestroy ($image _p);
ImagedeStroy ($image);
}

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

< Div>
</div>

4, PHP add watermark for image

code as follows  
<DIV>
</div>

<? PHP
Header ("Content-type", "Text/html;charset=utf-8"),
/*
* Add a watermark to the background image, Background picture format PNG, watermark format gif
* @param string $filename a picture's URL
* @param string $water watermark Picture
*/
Function watermark ($filen Ame, $water)
{
/* Get the size of the original image */
List ($b _w, $b _h) = getimagesize ($filename),
/* Get watermark Picture size */
List ($w _w, $w _h ) = GetImageSize ($water);
/* The random start position of the picture in the background picture */
$posX =rand (0, ($b _w-$w _w)),
$posY =rand (0, ($b _h-$w _h),
/* Get the image resource, need to crop the picture */< br> $back = imagecreatefrompng ($filename);
$water = imagecreatefromgif ($water);
/* Use the Inagecopy function to copy the watermark picture to the specified location */
Imagecopy ($back, $water, $posX, $posY, 0, 0, $w _w, $w _h);
/* Save watermark Picture and name */
I Magepng ($back, ' TEST2.PNg ');
/* FREE resources */
Imagedestroy ($back);
Imagedestroy ($water);
}

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

< Div>
</div>
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.