Jquery (Jcrop) + PHP cut images by coordinate

Source: Internet
Author: User
Tags imagecopy
Jquery (Jcrop) + PHP cut images by coordinate
/*** Goofy 2011-11-29 * Image Processing: based on the passed coordinate parameters, x, y, w, and h are the selected x coordinates, y coordinates, w width, h height * use the imagecopy () method to copy the area to the blank image created in step 1 * Note that the image must be in the true color of imagecreatetruecolor () when being created, otherwise use imagecreate () the image will be distorted */

  1. /**
  2. * Goofy 2011-11-29
  3. * Image Processing: the x, y, w, h coordinates, w width, and h are selected based on the passed coordinate parameters.
  4. * Use the imagecopy () method to copy the area to the blank image created in step 1.
  5. * Note: You must use the true color of imagecreatetruecolor () when creating an image. Otherwise, the image with imagecreate () will be distorted.
  6. */
  7. // Proportion of the page passed in
  8. $ Scale = $ _ GET ['scale'];
  9. // Multiply the following attributes by the corresponding proportions
  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. // Continue? If the source image is not deleted
  17. $ Again = "off ";
  18. If (! Empty ($ _ GET ['again ']) {
  19. $ Again = $ _ GET ['again '];
  20. }
  21. // The first step is to create an image based on the incoming width and high parameters, and then fill the captured part in this area.
  22. Header ("Content-type: image/jpeg ");
  23. $ Target = @ imagecreatetruecolor ($ w, $ h)
  24. Or die ("Cannot Initialize new GD image stream ");
  25. // Step 2: obtain the source image based on the path and create an image object with the source image
  26. $ Source = imagecreatefromjpeg ($ src );
  27. // Step 3: fill in part of the source image to the image created in step 1 based on the sent parameters
  28. Imagecopy ($ target, $ source, 0, 0, $ x, $ y, $ w, $ h );
  29. // Step 4: save the image
  30. // Truncate and organize a new path
  31. $ Pos_path = strripos ($ src ,"/");
  32. $ NewPath = substr ($ src, 0, $ pos_path-strlen ($ src). "_ new /";
  33. // Truncate and organize a new name
  34. $ Pos_name = strripos ($ src ,".");
  35. $ NewName = substr ($ src, 0, $ pos_name );
  36. $ Pos_name _ = strripos ($ newName ,"/");
  37. // Do not include the suffix ". jpg" to prevent duplicate files. If yes, you need to rename the files. it will be inconvenient to add them.
  38. $ NewName = substr ($ newName, $ pos_name _-strlen ($ newName) + 1 )."_";
  39. // Generate an image without a suffix
  40. $ File = $ newPath. $ newName;
  41. // Append the asc code to rename the file. you can also use a random number or time to name the file. I don't need to use the naming methods for the project.
  42. For ($ I = 0; $ I <26; $ I ++ ){
  43. // If the directory exists
  44. If (is_dir ($ newPath )){
  45. // If the file exists, continue the loop until the file with no duplicate names exists
  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. // After the file is created, determine whether to delete the source file. off is the source file to be deleted.
  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. // After the file is created, determine whether to delete the source file. off is the source file to be deleted.
  64. If ($ again = "off "){
  65. Unlink ($ src );
  66. Unset ($ _ SESSION ['URL']);
  67. }
  68. Break;
  69. }
  70. }
  71. // Jump to the homepage
  72. Header ("Location: index. php ");
  73. ?>

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.