PHP combined with Jqueryjcrop implementation of the avatar image of the real example code

Source: Internet
Author: User

See some of the site has a picture cutting function, feel very dazzle, and later found a dedicated to the picture cutting plug-ins, jquery. Jcrop.min.js, with this plug-in can be conveniently implemented this function, the use of the mouse in the picture on the circle to select the selection, you can cut the picture into the selected section, is very suitable for the head of the cutting editing function. Front-End UI sharing

The demo is divided into two parts: HTML and PHP.

The first part, the HTML code:

. Code
  1. <! DOCTYPE HTML public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd ">
  2. "http://www.w3.org/1999/xhtml" >
  3. <title>jcrop Achieve picture clipping </title>
  4. <script src=". /jquery-1.6.2.min.js "></script>
  5. <script src=". /jquery. Jcrop.min.js "></script>
  6. <link rel="stylesheet" href= ". /jquery. Jcrop.min.css "type="text/css "/>
  7. <style type="Text/css" >
  8. #preview {width:100px;height:100px;border:1px solid #000;overflow:hidden;}
  9. #imghead {filter:progid:DXImageTransform.Microsoft.AlphaImageLoader (sizingmethod=image);}
  10. </style>
  11. <script language="Javascript" >
  12. JQuery (function () {
  13. JQuery (' #imghead '). Jcrop ({
  14. Aspectratio: 1,
  15. Onselect:updatecoords,//The corresponding callback function is executed when the area is selected
  16. Onchange:updatecoords,//The corresponding callback function is executed when the selection region changes
  17. });
  18. });
  19. function Updatecoords (c)
  20. {
  21. JQuery (' #x '). Val (c.x);//The upper-left corner of the selected area horizontal
  22. JQuery (' #y '). Val (c.y);//upper left corner of selected area ordinate
  23. JQuery ("#x2"). Val (c.x2);//The lower right corner of the selected area horizontal
  24. JQuery ("#y2"). Val (c.y2);//The lower-right corner of the selected area ordinate
  25. JQuery (' #w '). Val (C.W);//width of the selected area
  26. JQuery (' #h '). Val (c.h);//height of selected area
  27. };
  28. function Checkcoords ()
  29. {
  30. if (parseint (JQuery (' #w '). Val ()) >0) return true;
  31. Alert (' Please select the area of the picture you want to crop. ');
  32. return false;
  33. };
  34. </script>
  35. <body>
  36. <img id="imghead" border=0 src=' ... /image/b4.jpg '/>
  37. <form action="crop.php" method="post" onsubmit="return checkcoords ();" >
  38. <input type="text" id="x" Name="x"/>
  39. <input type="text" id="y" name="y"/>
  40. <input type="text" id="W" name="W"/>
  41. <input type="text" id="H" name="H"/>
  42. <input type="Submit" value="Submission" >
  43. </form>
  44. </body>

Part Two: The PHP Processing section: jquery sharing

. Code
  1. <?php
  2. if ($_server[' request_method '] = = ' POST ')
  3. {
  4. $targ _w = $targ _h = 150;
  5. $jpeg _quality = 90;
  6. $SRC = '.  /image/b4.jpg ';
  7. $img _r = Imagecreatefromjpeg ($SRC);
  8. $DST _r = Imagecreatetruecolor ($targ _w, $targ _h);
  9. Imagecopyresampled ($dst _r, $img _r,0,0,$_post[' x '],$_post[' y '],
  10. $targ _w, $targ _h,$_post[' W '],$_post[' h ']);
  11. Header (' content-type:image/jpeg ');
  12. Imagejpeg ($dst _r,null, $jpeg _quality);
  13. Exit
  14. }
  15. ?>

Please save the above two parts of the code separately as two files, the file name is self-prepared.

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.