PHP code for obtaining remote images and adjusting the image size

Source: Internet
Author: User
PHP code for obtaining remote images and adjusting the image size

  1. /**
  2. *
  3. * Function: adjust the image size or generate a thumbnail.
  4. * Modification:
  5. * Return value: True/False
  6. * Parameters:
  7. * $ Image the Image to be adjusted (including the path)
  8. * $ Dw = 450 max width when adjusted; absolute width when thumbnails
  9. * $ Dh = 450 max. height when adjusted; absolute height when thumbnails
  10. * $ Type = 1 1, adjust the size; 2, generate a thumbnail
  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 "image does not exist ";
  18. Return false;
  19. }
  20. Echo "images exist ";
  21. // If you need to generate a thumbnail, copy the source Image and assign a value to $ Image (create a thumbnail)
  22. // When Type = 1, the original image file will not be copied, but the reduced image will be re-generated on the original image file (resize operation)
  23. IF ($ Type! = 1 ){
  24. Copy ($ Image, str_replace (".", "_ x.", $ Image ));
  25. $ Image = str_replace (".", "_ x.", $ Image );
  26. }
  27. // Obtain the object type and create different objects based on 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 object is not an image file.
  41. IF (Empty ($ Img )){
  42. // If an error occurs when a thumbnail is generated, delete the copied file.
  43. IF ($ Type! = 1 ){
  44. Unlink ($ Image );
  45. }
  46. Return false;
  47. }
  48. // If you want to adjust the size
  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 true color canvas
  77. ImageCopyReSampled ($ nImg, $ Img, $ width, $ height, $ w, $ h); // Copy part of the image and resize it.
  78. ImageJpeg ($ nImg, $ Image); // output the Image to a browser or file in JPEG format
  79. Return true;
  80. } Else {// if a thumbnail is generated
  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) {// relatively high
  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 {// relatively wide
  92. $ Height = $ Dh;
  93. $ Width = $ w * $ Dh/$ h;
  94. $ IntNW = $ width-$ Dw;
  95. ImageCopyReSampled ($ nImg, $ Img,-$ IntNW/1.8, 0, 0, $ width, $ Dh, $ w, $ h );
  96. }
  97. ImageJpeg ($ nImg, $ Image );
  98. Return true;
  99. }
  100. };
  101. ?>

2. obtain remote images

  1. // Network Image 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 line feed characters
  5. $ Name = strrchr ($ tempPath ,"/");
  6. $ Path = str_replace ($ name, '', $ tempPath); // replace line feed characters
  7. /**
  8. * Create a multi-level directory based on the path
  9. * $ Dir target directory $ mode permission. 0700 indicates the highest permission.
  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 ("failed to establish, please check the permission ");
  29. @ Chmod ($ dir, $ mode );
  30. Return $ dir;
  31. }
  32. } // End makedir
  33. Makedir ($ path );
  34. /**
  35. * Retrieve images on the server based on the 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. $ Fp2 = @ fopen ($ filename, "");
  52. Fwrite ($ fp2, $ img );
  53. Fclose ($ fp2 );
  54. Return $ filename;
  55. }
  56. ?>

3. call example

  1. The file types that can 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. ?>

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.