jquery+php to achieve picture online cropping

Source: Internet
Author: User
Tags imagecopy imagejpeg

/ * Goofy 2011-11-29 * Image processing: According to the coordinates passed over the parameters, X,y,w,h, followed by the selected x-coordinate, y-coordinate, W-width, h-height through the imagecopy () Method copy the region to the blank image created in the first step , note that the Imagecreatetruecolor () True color is used when creating the image, otherwise the imagecreate () image will be distorted.

free picture cut no scale

  1. /**
  2. * Goofy 2011-11-29
  3. * Image processing: According to the coordinate parameters passed, x,y,w,h, followed by the selected x-coordinate, y-coordinate, W-width, h-height
  4. * Copy the area to a blank image created in the first step by using the Imagecopy () method
  5. * Note that when creating an image, use Imagecreatetruecolor () True Color, otherwise the imagecreate () image will be distorted
  6. */
  7. The proportion of pages passed over
  8. $scale =$_get[' scale ');
  9. The following attribute is multiplied by the corresponding scale
  10. $x =$_get[' x ']* $scale;
  11. $y =$_get[' y ']* $scale;
  12. $w =$_get[' W ']* $scale;
  13. $h =$_get[' h ']* $scale;
  14. Source Path
  15. $src =$_get[' src '];
  16. Do you want to continue? If you do not continue in this picture, the source picture will be deleted
  17. $again = "Off";
  18. if (!empty ($_get[' again ')) {
  19. $again =$_get[' again '];
  20. }
  21. The first step is to create a picture based on the wide, high parameters, and then just fill the area with the intercepted part.
  22. Header ("Content-type:image/jpeg");
  23. $target = @imagecreatetruecolor ($w, $h)
  24. Or Die ("Cannot Initialize new GD image Stream");
  25. The second step is to get the source image according to the path and create an image object with the source images.
  26. $source = Imagecreatefromjpeg ($SRC);
  27. The third step, according to the parameters, select a portion of the source image to fill in the image created in the first step
  28. Imagecopy ($target, $source, 0, 0, $x, $y, $w, $h);
  29. Fourth step, Save the image
  30. Intercept and organize new paths
  31. $pos _path= Strripos ($SRC, "/");
  32. $newPath =substr ($src, 0, $pos _path-strlen ($SRC)). " _new/";
  33. Intercept and organize new names
  34. $pos _name=strripos ($SRC, ".");
  35. $newName =substr ($src, 0, $pos _name);
  36. $pos _name_= Strripos ($newName, "/");
  37. There is temporarily no suffix ". jpg" to prevent duplicate files, if any, need to rename, add after Will inconvenient
  38. $newName =substr ($newName, $pos _name_-strlen ($newName) +1). " _";
  39. Create a picture with no suffix
  40. $file = $newPath. $newName;
  41. Append ASC Code Rename file, can also use random number, time and so on named, project requirements I don't use those naming methods.
  42. for ($i =0; $i <26; $i + +) {
  43. If the directory exists
  44. if (Is_dir ($newPath)) {
  45. If the file exists, continue looping until there are no duplicate files
  46. if (Is_file ($file. chr (97+ $i). ". JPG ")) {
  47. Continue
  48. }else{
  49. Create a file
  50. Imagejpeg ($target, $file. chr (97+ $i). ". JPG ", 100);
  51. File creation complete determines whether to delete source files, off to delete source files
  52. if ($again = = "Off") {
  53. Unlink ($SRC);
  54. unset ($_session[' url ');
  55. }
  56. Break
  57. }
  58. }else{
  59. Create a Directory
  60. mkdir ($newPath);
  61. Create a file
  62. Imagejpeg ($target, $file. chr (97+ $i). ". JPG ", 100);
  63. File creation complete determines whether to delete source files, off to delete source files
  64. if ($again = = "Off") {
  65. Unlink ($SRC);
  66. unset ($_session[' url ');
  67. }
  68. Break
  69. }
  70. }
  71. Jump to Homepage
  72. Header ("Location:index.php");
  73. ?>
Copy Code
JQuery, PHP
  • 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.