PHP Gets the remote picture and adjusts the image size of the implementation code

Source: Internet
Author: User
Tags imagejpeg
  1. /**
  2. *
  3. * Function: Resize the picture or create a thumbnail image
  4. * Revision: 2013-2-15
  5. * Return: True/false
  6. Parameters
  7. * $Image need to adjust the picture (including the path)
  8. * $Dw =450 The maximum width when adjusting; absolute width when thumbnail
  9. * $Dh Maximum height for =450 adjustment, absolute height when thumbnail
  10. * $Type = 1 1, adjust the size; 2, generate thumbnail image
  11. * Site http://bbs.it-home.org
  12. */
  13. $phtypes =array (' img/gif ', ' img/jpg ', ' img/jpeg ', ' img/bmp ', ' img/pjpeg ', ' img/x-png ');
  14. function compressimg ($Image, $Dw, $Dh, $Type) {
  15. Echo $Image;
  16. IF (!file_exists ($Image)) {
  17. echo "no picture exists";
  18. return false;
  19. }
  20. echo "Existence picture";
  21. If you need to generate thumbnails, copy the original image to $image assignment (generate thumbnail action)
  22. When Type==1, the original image file is not copied, but the reduced image is regenerated on the original image file (resizing operation)
  23. IF ($Type!=1) {
  24. Copy ($Image, Str_replace (".", "_x.", $Image));
  25. $Image =str_replace (".", "_x.", $Image);
  26. }
  27. Get the type of file, build different objects according to different types
  28. $ImgInfo =getimagesize ($Image);
  29. Switch ($ImgInfo [2]) {
  30. Case 1:
  31. $IMG = @imagecreatefromgif ($Image);
  32. Break
  33. Case 2:
  34. $IMG = @imagecreatefromjpeg ($Image);
  35. break;
  36. Case 3:
  37. $IMG = @imagecreatefrompng ($Image);
  38. Break
  39. }
  40. If the object is not created successfully, the non-picture file
  41. IF (Empty ($IMG)) {
  42. If an error occurs when generating thumbnails, you need to delete the files that have already been copied
  43. IF ($Type!=1) {
  44. Unlink ($Image);
  45. }
  46. return false;
  47. }
  48. If you are performing a resize operation
  49. IF ($Type ==1) {
  50. $w =imagesx ($IMG);
  51. $h =imagesy ($IMG);
  52. $width = $w;
  53. $height = $h;
  54. IF ($width > $Dw) {
  55. $Par = $Dw/$width;
  56. $width = $Dw;
  57. $height = $height * $PAR;
  58. IF ($height > $Dh) {
  59. $Par = $Dh/$height;
  60. $height = $Dh;
  61. $width = $width * $PAR;
  62. }
  63. } ElseIF ($height > $Dh) {
  64. $Par = $Dh/$height;
  65. $height = $Dh;
  66. $width = $width * $PAR;
  67. IF ($width > $Dw) {
  68. $Par = $Dw/$width;
  69. $width = $Dw;
  70. $height = $height * $PAR;
  71. }
  72. } Else {
  73. $width = $width;
  74. $height = $height;
  75. }
  76. $NIMG =imagecreatetruecolor ($width, $height);//Create a new True color canvas
  77. Imagecopyresampled ($NIMG, $IMG, 0,0,0,0, $width, $height, $w, $h);//Resample copy part of the image and resize it
  78. Imagejpeg ($NIMG, $Image);//output images to a browser or file in JPEG format
  79. return true;
  80. } Else {//If you are performing a build thumbnail operation
  81. $w =imagesx ($IMG);
  82. $h =imagesy ($IMG);
  83. $width = $w;
  84. $height = $h;
  85. $NIMG =imagecreatetruecolor ($Dw, $Dh);
  86. IF ($h/$w > $Dh/$Dw) {//height ratio large
  87. $width = $Dw;
  88. $height = $h * $Dw/$w;
  89. $IntNH = $height-$Dh;
  90. Imagecopyresampled ($NIMG, $IMG, 0,-$IntNH/1.8, 0, 0, $Dw, $height, $w, $h);
  91. } Else {//width ratio is large
  92. $height = $Dh;
  93. $width = $w * $Dh/$h;
  94. $IntNW = $width-$Dw;
  95. Imagecopyresampled ($NIMG, $IMG,-$IntNW/1.8,0,0,0, $width, $Dh, $w, $h);
  96. }
  97. Imagejpeg ($NIMG, $Image);
  98. return true;
  99. }
  100. };
  101. ?>
Copy Code

2. Get remote pictures

  1. Network picture Path
  2. $imgPath = ' http://bbs.it-home.org/phone/compress-img/251139474ba926db3d7850.jpg ';
  3. $imgPath = "Http://bbs.it-home.org/userfiles/image/20111125/251139474ba926db3d7850.jpg";
  4. $tempPath = Str_replace (' http://bbs.it-home.org/', ' ', $imgPath);//Replace newline character
  5. $name = STRRCHR ($tempPath, "/");
  6. $path = Str_replace ($name, ", $tempPath);//Replace newline character
  7. /**
  8. * Set up a multi-level directory based on paths path
  9. * $dir The target directory $mode permissions, 0700 indicates the highest privilege
  10. */
  11. function MakeDir ($dir, $mode = "0700") {
  12. if (Strpos ($dir, "/")) {
  13. $dir _path = "";
  14. $dir _info = Explode ("/", $dir);
  15. foreach ($dir _info as $key = + $value) {
  16. $dir _path. = $value;
  17. if (!file_exists ($dir _path)) {
  18. @mkdir ($dir _path, $mode) or Die ("Failed to create folder");
  19. @chmod ($dir _path, $mode);
  20. } else {
  21. $dir _path. = "/";
  22. Continue;
  23. }
  24. $dir _path. = "/";
  25. }
  26. return $dir _path;
  27. } else {
  28. @mkdir ($dir, $mode) or Die ("Setup failed, check permissions");
  29. @chmod ($dir, $mode);
  30. return $dir;
  31. }
  32. }//end MakeDir
  33. MakeDir ($path);
  34. /**
  35. * Get pictures on the server based on URL
  36. * $url Server Slice path $filename file name
  37. */
  38. function Grabimage ($url, $filename = "") {
  39. if ($url = = "") return false;
  40. if ($filename = = "") {
  41. $ext =STRRCHR ($url, ".");
  42. if ($ext! = ". gif" && $ext! = ". jpg" && $ext! = ". png")
  43. return false;
  44. $filename =date ("Ymdhis"). $ext;
  45. }
  46. Ob_start ();
  47. ReadFile ($url);
  48. $img = Ob_get_contents ();
  49. Ob_end_clean ();
  50. $size = strlen ($img);
  51. $FP 2= @fopen ($filename, "a");
  52. Fwrite ($fp 2, $img);
  53. Fclose ($fp 2);
  54. return $filename;
  55. }
  56. ?>
Copy Code

3. Invoking the example

    1. The types of files that are allowed to be uploaded are:
    2. echo $path. "
      ";
    3. /**/
    4. $BIGIMG =grabimage ($imgPath, $tempPath);
    5. if ($BIGIMG) {
    6. Echo '
      ';
    7. } else {
    8. echo "false";
    9. }
    10. Compressimg ($BIGIMG, 100,80,1);
    11. ?>
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.