Php + js allows you to upload, crop, preview, and submit images.

Source: Internet
Author: User

The first language used is php and the plug-in imgareaselect (). There are not many fancy styles. The index. php code is as follows:
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta content = "text/html; charsets = UTF-8" http-equiv = "Content-Type">
<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>
<Script type = "text/javascript">
Function preview (img, selection ){
$ ('# Selectbanner'). data ('x', selection. x1 );
$ ('# Selectbanner'). data ('y', selection. y1 );
$ ('# Selectbanner'). data ('w', selection. width );
$ ('# Selectbanner'). data ('h', selection. height );

Var scaleX = 100/(selection. width | 1 );
Var scale= 100/(selection. height | 1 );
$ ('# Ferret> img'hangzhou.css ({
Width: Math. round (scaleX * 512) + 'px ', // 512, 390 is the width and height of the image you upload
Height: Math. round (scaleY * 390) + 'px ',
MarginLeft: '-' + Math. round (scaleX * selection. x1) + 'px ',
MarginTop: '-' + Math. round (scaleY * selection. y1) + 'px'
});
}

// Insert a preview small image after the original image using jQuery syntax.
$ (Document). ready (function (){
$ ('<Div id = "ferret"> <div> 'salary .css ({
Float: 'left ',
Position: 'relative ',
Overflow: 'ddy ',
Width: '100px ',
Height: '100px'
})

. InsertAfter ($ ('# selectbanner '));

$ ('# Selectbanner'). imgAreaSelect ({
SelectionColor: 'Blue ', x1: 0, y1: 0, x2: 100, // initial position
MaxWidth: 500, y2: 100,
AspectRatio: '1: 1', // scaling ratio
SelectionOpacity: 0.2,
OnSelectEnd: preview // The function executed after cropping.
});
// Confirm the cropping
$ ("# SliceButton"). click (function (){
Var pic = $ ('# selectbanner'). attr ('src ');
// Alert (pic );
Var x, y, w, h;
$. Post (
"Cat. php", // (2) the code for this page will be attached.
{
X: $ ('# selectbanner'). data ('x '),
Y: $ ('# selectbanner'). data ('y '),
W: $ ('# selectbanner'). data ('W '),
H: $ ('# selectbanner'). data ('H '),
Pic: pic
},
Function (data ){
// Alert (data );
// Load the cropped image to # sure
If (data ){
$ ('# Sure'). attr ('src', data );
}
}
);
});
})
</Script>
<Title> crop and preview images </title>
</Head>
<Body>
<? Php
// Copy the image to the upload folder after uploading it
If ($ _ POST ){
$ Photo = $ _ FILES ['img '] ['name'];
$ Tmp_addr = $ _ FILES ['img '] ['tmp _ name'];

$ Path = 'upload /';
$ Type = array ("jpg", "gif", "jpeg", "png ");
$ Tool = substr (strrchr ($ photo, '.'), 1 );
If (! In_array (strtolower ($ tool), $ type )){
$ Text = implode ('.', $ type );
Echo "You can only upload the following types of files:", $ text, "<br> ";
} Else {
$ Filename = explode (".", $ photo); // you can specify the object name as an array.
$ Time = date ("m-d-H-I-s"); // obtain the current upload time
$ Filename [0] = $ time; // obtain the file name
$ Name = implode (".", $ filename); // name of the uploaded file
$ Uploadfile = $ path. $ name;
$ _ SESSION ['upfile'] = $ uploadfile; // address of the uploaded file name
Move_uploaded_file ($ tmp_addr, $ uploadfile );
}
// Echo $ uploadfile;
}
?>
<Div id = "s">
<! -- Upload an image -->
<Form action = "" method = "post" enctype = "multipart/form-data">
<Input type = "file" id = "img" name = "img" value = "" onclick = ""/>
<Input name = "submit" id = "submit" type = "submit" value = "submit" class = "submit"/>
</Form>
<! -- Display image -->
<? If (isset ($ _ SESSION ['upfile']) {?>
"Title =" mypic "/>
<? }?>
</Div>
<! -- Confirm cropping -->
<Div> <input type = "submit" id = "sliceButton" name = "sliceButton" value = "sliceButton"> </div>
<! -- Display the cropped image -->
<Div> </div>
</Body>
</Html>

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.