/wp-content/themes/method/lib/admin/classes/get-image.php

https://github.com/Canuckaholic/Pop-Digital · PHP · 152 lines · 114 code · 27 blank · 11 comment · 11 complexity · ee48974425c307d888fb0a5bc4db673b MD5 · raw file

  1. <?php
  2. /*
  3. -------------------------------------------------------------------------
  4. Credits: Bit Repository
  5. URL: http://www.bitrepository.com/web-programming/php/download-image.html
  6. -------------------------------------------------------------------------
  7. */
  8. class GetImage {
  9. var $source;
  10. var $save_to;
  11. var $set_extension;
  12. var $quality;
  13. function download($method = 'curl') // default method: cURL
  14. {
  15. $info = @GetImageSize($this->source);
  16. $mime = $info['mime'];
  17. if(!$mime) return false;
  18. //exit('Could not obtain mime-type information. Make sure that the remote file is actually a valid image.');
  19. // What sort of image?
  20. $type = substr(strrchr($mime, '/'), 1);
  21. switch ($type)
  22. {
  23. case 'jpeg':
  24. $image_create_func = 'ImageCreateFromJPEG';
  25. $image_save_func = 'ImageJPEG';
  26. $new_image_ext = 'jpg';
  27. // Best Quality: 100
  28. $quality = isSet($this->quality) ? $this->quality : 100;
  29. break;
  30. case 'png':
  31. $image_create_func = 'ImageCreateFromPNG';
  32. $image_save_func = 'ImagePNG';
  33. $new_image_ext = 'png';
  34. // Compression Level: from 0 (no compression) to 9
  35. $quality = isSet($this->quality) ? $this->quality : 0;
  36. break;
  37. case 'bmp':
  38. $image_create_func = 'ImageCreateFromBMP';
  39. $image_save_func = 'ImageBMP';
  40. $new_image_ext = 'bmp';
  41. break;
  42. case 'gif':
  43. $image_create_func = 'ImageCreateFromGIF';
  44. $image_save_func = 'ImageGIF';
  45. $new_image_ext = 'gif';
  46. break;
  47. case 'vnd.wap.wbmp':
  48. $image_create_func = 'ImageCreateFromWBMP';
  49. $image_save_func = 'ImageWBMP';
  50. $new_image_ext = 'bmp';
  51. break;
  52. case 'xbm':
  53. $image_create_func = 'ImageCreateFromXBM';
  54. $image_save_func = 'ImageXBM';
  55. $new_image_ext = 'xbm';
  56. break;
  57. default:
  58. $image_create_func = 'ImageCreateFromJPEG';
  59. $image_save_func = 'ImageJPEG';
  60. $new_image_ext = 'jpg';
  61. }
  62. if(isSet($this->set_extension))
  63. {
  64. $ext = strrchr($this->source, ".");
  65. $strlen = strlen($ext);
  66. $new_name = basename(substr($this->source, 0, -$strlen)).'.'.$new_image_ext;
  67. }
  68. else
  69. {
  70. $new_name = basename($this->source);
  71. }
  72. $save_to = $this->save_to.$new_name;
  73. if($method == 'curl')
  74. {
  75. $save_image = $this->LoadImageCURL($save_to);
  76. }
  77. elseif($method == 'gd')
  78. {
  79. $img = @$image_create_func($this->source);
  80. if(isSet($quality))
  81. {
  82. $save_image = $image_save_func($img, $save_to, $quality);
  83. }
  84. else
  85. {
  86. $save_image = $image_save_func($img, $save_to);
  87. }
  88. }
  89. elseif($method == 'fread')
  90. {
  91. $save_image = $this->LoadImageFREAD($save_to);
  92. }
  93. return $save_image;
  94. }
  95. function LoadImageCURL($save_to)
  96. {
  97. $ch = curl_init($this->source);
  98. $fp = fopen($save_to, "wb");
  99. // set URL and other appropriate options
  100. $options = array(CURLOPT_FILE => $fp,
  101. CURLOPT_HEADER => 0,
  102. CURLOPT_FOLLOWLOCATION => 1,
  103. CURLOPT_TIMEOUT => 60); // 1 minute timeout (should be enough)
  104. curl_setopt_array($ch, $options);
  105. $save = curl_exec($ch);
  106. curl_close($ch);
  107. fclose($fp);
  108. return $save;
  109. }
  110. function LoadImageFREAD($save_to)
  111. {
  112. $in = fopen($this->source, "rb");
  113. $out = fopen($save_to, "wb");
  114. while ( ( $chunk = fread( $in, 8192 ) ) != '' ) {
  115. fwrite( $out, $chunk, 8192 );
  116. }
  117. fclose($in);
  118. fclose($out);
  119. if( $chunk !== false )
  120. return true;
  121. else
  122. return false;
  123. }
  124. }
  125. ?>