PHP image processing method, PHP image processing class

Source: Internet
Author: User
Previously written by a PHP online processing upload images, can be cropped. Add watermark map, watermark map More than the target image size, the watermark can automatically adapt to the target image and reduce, the watermark can be set with the background of the merging degree. Source: http://www.pooy.net/phpphp.html


  1. *exif_imagetype--Judging the type of an image
  2. * Source code Download: http://www.pooy.net/phpphp.html
  3. * Description: Function function is to cut an image into an image of any size, the image is not deformed
  4. * Parameter Description: Enter the file name that needs to process the picture, generate a new picture of the saved file name, create a new image width, create a new picture of the high
  5. */
  6. Get images of any size, stretch without distortion, leave no space
  7. function My_image_resize ($src _file, $dst _file, $new _width, $new _height) {
  8. $new _width= intval ($new _width);
  9. $new _height=intval ($new _width);
  10. if ($new _width <1 | | $new _height <1) {
  11. echo "Params width or height error!";
  12. Exit ();
  13. }
  14. if (!file_exists ($src _file)) {
  15. Echo $src _file. "is NOT exists!";
  16. Exit ();
  17. }
  18. Image type
  19. $type =exif_imagetype ($src _file);
  20. $support _type=array (Imagetype_jpeg, Imagetype_png, imagetype_gif);
  21. if (!in_array ($type, $support _type,true)) {
  22. echo "This type of image does not support! Only support JPG, GIF or PNG ";
  23. Exit ();
  24. }
  25. Load image
  26. Switch ($type) {
  27. Case IMAGETYPE_JPEG:
  28. $src _img=imagecreatefromjpeg ($src _file);
  29. Break
  30. Case Imagetype_png:
  31. $src _img=imagecreatefrompng ($src _file);
  32. Break
  33. Case Imagetype_gif:
  34. $src _img=imagecreatefromgif ($src _file);
  35. Break
  36. Default
  37. echo "Load image error!";
  38. Exit ();
  39. }
  40. $w =imagesx ($src _img);
  41. $h =imagesy ($src _img);
  42. $ratio _w=1.0 * $new _width/$w;
  43. $ratio _h=1.0 * $new _height/$h;
  44. $ratio = 1.0;
  45. The resulting image of the aspect ratio of the original is small, or all large, the principle is to take a large proportion of magnification, take a large proportion of the reduction (smaller proportion)
  46. if (($ratio _w < 1 && $ratio _h < 1) | | ($ratio _w > 1 && $ratio _h > 1)) {
  47. if ($ratio _w < $ratio _h) {
  48. $ratio = $ratio _h; Case one, the width of the ratio is smaller than the height direction, according to the height of the proportional standard to cut or enlarge
  49. }else {
  50. $ratio = $ratio _w;
  51. }
  52. Defines an intermediate temporary image that meets the target's aspect ratio exactly.
  53. $inter _w= (int) ($new _width/$ratio);
  54. $inter _h= (int) ($new _height/$ratio);
  55. $inter _img=imagecreatetruecolor ($inter _w, $inter _h);
  56. Var_dump ($inter _img);
  57. Imagecopy ($inter _img, $src _img, 0,0,0,0, $inter _w, $inter _h);
  58. Generates a temporary image that is the size of the maximum edge length, which is the $ratio scale of the target image
  59. To define a new image
  60. $new _img=imagecreatetruecolor ($new _width, $new _height);
  61. Var_dump ($new _img); exit ();
  62. Imagecopyresampled ($new _img, $inter _img,0,0,0,0, $new _width, $new _height, $inter _w, $inter _h);
  63. Switch ($type) {
  64. Case IMAGETYPE_JPEG:
  65. Imagejpeg ($new _img, $dst _file,100); Storing images
  66. Break
  67. Case Imagetype_png:
  68. Imagepng ($new _img, $dst _file,100);
  69. Break
  70. Case Imagetype_gif:
  71. Imagegif ($new _img, $dst _file,100);
  72. Break
  73. Default
  74. Break
  75. }
  76. }//End If 1
  77. 2 One edge of the target image is larger than the original, one edge is smaller than the original, the flat image is enlarged, then cropped
  78. =if (($ratio _w < 1 && $ratio _h > 1) | | ($ratio _w >1 && $ratio _h <1))
  79. else{
  80. $ratio = $ratio _h> $ratio _w? $ratio _h: $ratio _w; Take a large proportion of that value
  81. Defines an intermediate large image that is equal in height or width of the target image, and then zooms in on the original image
  82. $inter _w= (int) ($W * $ratio);
  83. $inter _h= (int) ($H * $ratio);
  84. $inter _img=imagecreatetruecolor ($inter _w, $inter _h);
  85. Crop after scaling the original
  86. Imagecopyresampled ($inter _img, $src _img,0,0,0,0, $inter _w, $inter _h, $w, $h);
  87. To define a new image
  88. $new _img=imagecreatetruecolor ($new _width, $new _height);
  89. Imagecopy ($new _img, $inter _img, 0,0,0,0, $new _width, $new _height);
  90. Switch ($type) {
  91. Case IMAGETYPE_JPEG:
  92. Imagejpeg ($new _img, $dst _file,100); Storing images
  93. Break
  94. Case Imagetype_png:
  95. Imagepng ($new _img, $dst _file,100);
  96. Break
  97. Case Imagetype_gif:
  98. Imagegif ($new _img, $dst _file,100);
  99. Break
  100. Default
  101. Break
  102. }
  103. }//IF3
  104. }//End Function
  105. My_image_resize (' test.gif ', ' 11111.gif ', ' 100px ', ' 100px ');
  106. SOURCE Download: http://www.pooy.net/phpphp.html
  107. ?>
Copy Code
  • 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.