PHP and jquery implement custom screenshot pictures

Source: Internet
Author: User

Some SNS websites often have some custom avatar functions. Here we will introduce a method to implement custom avatar using PHP and jquery. The environment and prerequisites are as follows:

1. php requires GD 2.0.1 or a later version.
2. jquery Library
3. jquery image processing plug-in imgareaselect

The imgareaselect plug-in can refer to its official website: http://odyniec.net/projects/imgareaselect/, you can also browse the relevant content of this blog: jquery plug-in imgareaselect, Here describes how to set all parameters in the imgareaselect plug-in and its description.

File to be introduced in the HTML header:

<Head>
<LINK rel = "stylesheet" type = "text/CSS" href = "CSS/imgareaselect-default.css"/>
<SCRIPT type = "text/JavaScript" src = "scripts/jquery. Min. js"> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = "scripts/jquery. imgareaselect. Pack. js"> </SCRIPT>
<Head>
Among them, the CSS file is the CSS style that comes with the jquery plug-in imgareaselect. jquery. Min. JS is the jquery library file, and jquery. imgareaselect. Pack. JS is the file of the imgareaselect plug-in.

JavaScript code:

<SCRIPT type = "text/JavaScript">
Function Preview (IMG, selection ){
VaR scalex = 100/selection. width; // 100 indicates the width of the new graph.
VaR scaley = 100/selection. height; // 100 indicates the height of the new graph.
$ ('# Thumbnail img'hangzhou.css ({
Width: Math. Round (scalex * 800) + 'px ', // 800 indicates the width of the source image.
Height: Math. Round (scaley * 600) + 'px ', // 600 indicates the height of the source Image
Marginleft: '-' + math. Round (scalex * selection. X1) + 'px ',
Margintop: '-' + math. Round (scaley * selection. Y1) + 'px'
});
$ ('# X1'). Val (selection. X1 );
$ ('# Y1'). Val (selection. Y1 );
$ ('# X2'). Val (selection. x2 );
$ ('# Y2'). Val (selection. Y2 );
$ ('# W'). Val (selection. width );
$ ('# H'). Val (selection. Height );
}
$ (Document). Ready (function (){
$ ('Img # photo '). imgareaselect ({x1: 0, Y1: 0, X2: 50, Y2: 50, fadespeed: 10, aspectratio: '1: 1', handles: "corners", onselectchange: preview });
$ ('# Save_thumb'). Click (function (){
VaR X1 = $ ('# x1'). Val ();
VaR Y1 = $ ('# Y1'). Val ();
VaR X2 = $ ('# X2'). Val ();
VaR y2 = $ ('# Y2'). Val ();
VaR W = $ ('# W'). Val ();
VaR H = $ ('# H'). Val ();
If (x1 = "" | Y1 = "" | X2 = "" | y2 = "" | W = "" | H = "") {
Alert ("image is null ");
Return false;
} Else {
Return true;
}
});
});
</SCRIPT>
The preview function has a preview effect when selected.

HTML code:

<Div style = "float: Left;" class = "frame">

</Div>
<Div style = "float: Left; width: 100px; Height: 100px;" id = "thumbnail">
<Div style = "overflow: hidden; width: 100px; Height: 100px;" id = "preview">

</Div>
</Div>
<Div style = "margin-top: 10px;">
<Form name = "thumbnail" Action = "imgareaselect. php" method = "Post">
<Input type = "hidden" name = "X1" value = "" id = "X1"/>
<Input type = "hidden" name = "Y1" value = "" id = "Y1"/>
<Input type = "hidden" name = "X2" value = "" id = "X2"/>
<Input type = "hidden" name = "Y2" value = "" id = "Y2"/>
<Input type = "hidden" name = "W" value = "" id = "W"/>
<Input type = "hidden" name = "H" value = "" id = "H"/>
<Input type = "Submit" name = "upload_thumbnail" value = "Save thumb" id = "save_thumb"/>
</Form>
</Div>
PHP code:

$ Fn = "Water lilies.jpg ";
$ Ofn = "test_thumb.jpg ";
$ Ext = strtoupper (end (explode ('.', $ FN )));
If (is_file ($ FN) & ($ ext = "jpg" | $ ext = "Jpeg ")){
$ Source = imagecreatefromjpeg ($ FN );
} Elseif (is_file ($ FN) & $ ext = "PNG "){
$ Source = imagecreatefrompng ($ FN );
} Elseif (is_file ($ FN) & $ ext = "GIF "){
$ Source = imagecreatefromgif ($ FN );
}
$ Sourcewidth = imagesx ($ source );
$ Sourceheight = imagesy ($ source );
$ Thumbwidth = $ _ post ['W'];
$ Thumbheight = $ _ post ['H'];
$ Thumb = imagecreatetruecolor ($ thumbwidth, $ thumbheight );
$ X1 = $ _ post ['x1'];
$ Y1 = $ _ post ['y1 '];
$ X2 = $ _ post ['x2 '];
$ Y2 = $ _ post ['y2 '];
Imagecopyresampled ($ thumb,
$ Source,
0,
0,
$ X1,
$ Y1,
$ Thumbwidth,
$ Thumbheight,
$ Thumbwidth,
$ Thumbheight
);
Imagejpeg ($ thumb, $ ofn );
Here we will focus on the imagecopyresampled function materials,
(Imagecopyresampled re-sample and copy part of the image and adjust the size)

Prototype: bool imagecopyresampled (resource dst_image, resource src_image, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h) dst_image and src_image are the identifiers of the target image and source image respectively.

Others: imagecopyresampled (target image, source image, storage width, storage height, coordinate X, coordinate Y, Source width, and source height) returns true if the operation succeeds, and false if the operation fails. Imagecopyresampled () copies a Square area of an image to another image and smoothly inserts the pixel value. Therefore, in particular, the image size is reduced, while the image definition is maintained. If the width and height of the source and target are different, the image will be scaled and stretched accordingly. Coordinates refer to the upper left corner. This function can be used to copy the area inside the same image (if dst_image and src_image are the same), but the result is unpredictable if the area overlaps.

Note: There is a problem with the color palette image limit (255 + 1 color. The re-sampling or filtering of images usually requires more than 255 colors. An approximate value is used to calculate the new re-sampling pixels and their colors. When you try to assign a new color to the palette image, if it fails, we select the color with the closest calculation result (theoretically. This is not always the closest visual color. This may produce weird results, such as blank (or visually blank) images. To skip this issue, use a true color image as the target image, for example, created with imagecreatetruecolor.

Version requirements: This function requires GD 2.0.1 or later (2.0.28 and later versions are recommended ).

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.