首页  >  文章  >  后端开发  >  php获取远程图片url生成缩略图的方法

php获取远程图片url生成缩略图的方法

WBOY
WBOY原创
2016-07-25 08:52:31970浏览
  1. /**

  2. *
  3. *函数:调整图片尺寸或生成缩略图
  4. *返回:True/False
  5. *参数:
  6. * $Image 需要调整的图片(含路径)
  7. * $Dw=450 调整时最大宽度;缩略图时的绝对宽度
  8. * $Dh=450 调整时最大高度;缩略图时的绝对高度
  9. * $Type=1 1,调整尺寸; 2,生成缩略图
  10. */ bbs.it-home.org
  11. $phtypes=array('img/gif', 'img/jpg', 'img/jpeg', 'img/bmp', 'img/pjpeg', 'img/x-png');
  12. function compressImg($Image,$Dw,$Dh,$Type){

  13. IF(!file_exists($Image)){
  14. return false;
  15. }
  16. // 如果需要生成缩略图,则将原图拷贝一下重新给$Image赋值(生成缩略图操作)
  17. // 当Type==1的时候,将不拷贝原图像文件,而是在原来的图像文件上重新生成缩小后的图像(调整尺寸操作)
  18. IF($Type!=1){
  19. copy($Image,str_replace(".","_x.",$Image));
  20. $Image=str_replace(".","_x.",$Image);
  21. }
  22. // 取得文件的类型,根据不同的类型建立不同的对象
  23. $ImgInfo=getimagesize($Image);
  24. Switch($ImgInfo[2]){
  25. case 1:
  26. $Img =@imagecreatefromgif($Image);
  27. break;
  28. case 2:
  29. $Img =@imagecreatefromjpeg($Image);
  30. Break;
  31. case 3:
  32. $Img =@imagecreatefrompng($Image);
  33. break;
  34. }
  35. // 如果对象没有创建成功,则说明非图片文件
  36. IF(Empty($Img)){
  37. // 如果是生成缩略图的时候出错,则需要删掉已经复制的文件
  38. IF($Type!=1){
  39. unlink($Image);
  40. }
  41. return false;
  42. }
  43. // 如果是执行调整尺寸操作则
  44. IF($Type==1){
  45. $w=ImagesX($Img);
  46. $h=ImagesY($Img);
  47. $width = $w;
  48. $height = $h;
  49. IF($width>$Dw){
  50. $Par=$Dw/$width;
  51. $width=$Dw;
  52. $height=$height*$Par;
  53. IF($height>$Dh){
  54. $Par=$Dh/$height;
  55. $height=$Dh;
  56. $width=$width*$Par;
  57. }
  58. } ElseIF($height>$Dh) {
  59. $Par=$Dh/$height;
  60. $height=$Dh;
  61. $width=$width*$Par;
  62. IF($width>$Dw){
  63. $Par=$Dw/$width;
  64. $width=$Dw;
  65. $height=$height*$Par;
  66. }
  67. } Else {
  68. $width=$width;
  69. $height=$height;
  70. }
  71. $nImg =ImageCreateTrueColor($width,$height);// 新建一个真彩色画布
  72. ImageCopyReSampled($nImg,$Img,0,0,0,0,$width,$height,$w,$h);// 重采样拷贝部分图像并调整大小
  73. ImageJpeg($nImg,$Image);// 以JPEG格式将图像输出到浏览器或文件
  74. return true;
  75. } Else {// 如果是执行生成缩略图操作则
  76. $w=ImagesX($Img);
  77. $h=ImagesY($Img);
  78. $width = $w;
  79. $height = $h;
  80. $nImg =ImageCreateTrueColor($Dw,$Dh);
  81. IF($h/$w>$Dh/$Dw){// 高比较大
  82. $width=$Dw;
  83. $height=$h*$Dw/$w;
  84. $IntNH=$height-$Dh;
  85. ImageCopyReSampled($nImg, $Img, 0, -$IntNH/1.8, 0, 0, $Dw, $height, $w, $h);
  86. } Else {// 宽比较大
  87. $height=$Dh;
  88. $width=$w*$Dh/$h;
  89. $IntNW=$width-$Dw;
  90. ImageCopyReSampled($nImg, $Img,-$IntNW/1.8,0,0,0, $width, $Dh, $w, $h);
  91. }
  92. ImageJpeg($nImg,$Image);
  93. return true;
  94. }
  95. };
  96. /**
  97. *根据url获取服务器上的图片
  98. *$url服务器上图片路径 $filename文件名
  99. */
  100. function GrabImage($url,$filename="") {
  101. if($url=="") return false;
  102. if($filename=="") {
  103. $ext=strrchr($url,".");
  104. if($ext!=".gif" && $ext!=".jpg" && $ext!=".png")
  105. return false;
  106. $filename=date("YmdHis").$ext;
  107. }
  108. ob_start();
  109. readfile($url);
  110. $img = ob_get_contents();
  111. ob_end_clean();
  112. $size = strlen($img);
  113. $fp2=@fopen($filename, "a");
  114. fwrite($fp2,$img);
  115. fclose($fp2);
  116. return $filename;
  117. }
  118. ?>
复制代码

调用方法:

  1. //网络图片路径
  2. $imgPath = 'http://news.xxxx.cn/images/1382088444437.jpg';//远程URL 地址
  3. $tempPath = 'aa/bbs.jpg';//保存图片路径
  4. if(is_file($tempPath)){
  5. unlink($tempPath);
  6. }
  7. $bigImg=GrabImage($imgPath, $tempPath);
  8. compressImg($bigImg,70,70,1);
  9. ?>
复制代码


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn