PHP combined with JQueryJcrop to implement the code of the Avatar image cropping instance,

Source: Internet
Author: User

PHP combined with JQueryJcrop to implement the code of the Avatar image cropping instance,

I found some websites have the image cutting function, and it was quite dazzling. Later I found a plug-in dedicated to image cropping, jquery. jcrop. min. js. You can use this plug-in to conveniently implement this function. When using this function, you can circle the selected area on the image to cut the image into the selected part. This is very suitable for cropping and editing the Avatar. Frontend UI sharing

The demo consists of HTML and php:

The first part is 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. <Html xmlns = "http://www.w3.org/1999/xhtml">
  3. <Head>
  4. <Title> crop images using Jcrop </title>
  5. <Script src = "../jquery-1.6.2.min.js"> </script>
  6. <Script src = ".../jquery. Jcrop. min. js"> </script>
  7. <Link rel = "stylesheet" href = "../jquery.Jcrop.min.css" type = "text/css"/>
  8. <Style type = "text/css">
  9. # Preview {width: 100px; height: 100px; border: 1px solid #000; overflow: hidden ;}
  10. # Imghead {filter: progid: DXImageTransform. Microsoft. AlphaImageLoader (sizingMethod = image );}
  11. </Style>
  12. <Script language = "Javascript">
  13. JQuery (function (){
  14. JQuery ('# imghead'). Jcrop ({
  15. AspectRatio: 1,
  16. OnSelect: updateCoords, // execute the corresponding callback function when selecting the region
  17. OnChange: updateCoords, // execute the corresponding callback function when the selected region changes
  18. });
  19. });
  20. Function updateCoords (c)
  21. {
  22. JQuery ('# x'). val (c. x); // the upper left corner of the selected area
  23. JQuery ('# y'). val (c. y); // select the vertical coordinate in the upper left corner of the area
  24. // JQuery ("# x2"). val (c. x2); // the abscissa in the lower right corner of the selected area
  25. // JQuery ("# y2"). val (c. y2); // select the column coordinate in the lower right corner of the area
  26. JQuery ('# W'). val (c. w); // The width of the selected area
  27. JQuery ('# H'). val (c. h); // The height of the selected area
  28. };
  29. Function checkCoords ()
  30. {
  31. If (parseInt (jQuery ('# W'). val ()> 0) return true;
  32. Alert ('select the image area to crop .');
  33. Return false;
  34. };
  35. </Script>
  36. </Head>
  37. <Body>
  38. <Form action = "crop. php" method = "post" onsubmit = "return checkCoords ();">
  39. <Input type = "text" id = "x" name = "x"/>
  40. <Input type = "text" id = "y" name = "y"/>
  41. <Input type = "text" id = "w" name = "w"/>
  42. <Input type = "text" id = "h" name = "h"/>
  43. <Input type = "submit" value = "submit">
  44. </Form>
  45. </Body>
  46. </Html>

 

Part 2: PHP processing part: jquery sharing

. Code
  1. <? Php
  2. If ($ _ SERVER ['request _ method'] = 'post ')
  3. {
  4. $ Targ_w = $ targ_h = 150;
  5. $ Pai_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, $ pai_quality );
  13. Exit;
  14. }
  15. ?>

 

Please save the above two parts of code 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.