1. <?php
  2. /**
  3. *
  4. *函数:调整图片尺寸或生成缩略图
  5. *修改:2013-2-15
  6. *返回:True/False
  7. *参数:
  8. *   $Image   需要调整的图片(含路径)
  9. *   $Dw=450   调整时最大宽度;缩略图时的绝对宽度
  10. *   $Dh=450   调整时最大高度;缩略图时的绝对高度
  11. *   $Type=1   1,调整尺寸; 2,生成缩略图
  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 "不存在图片";
  18. return false;
  19. }
  20. echo "存在图片";
  21. // 如果需要生成缩略图,则将原图拷贝一下重新给$Image赋值(生成缩略图操作)
  22. // 当Type==1的时候,将不拷贝原图像文件,而是在原来的图像文件上重新生成缩小后的图像(调整尺寸操作)
  23. IF($Type!=1){
  24. copy($Image,str_replace(".","_x.",$Image));
  25. $Image=str_replace(".","_x.",$Image);
  26. }
  27. // 取得文件的类型,根据不同的类型建立不同的对象
  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. // 如果对象没有创建成功,则说明非图片文件
  41. IF(Empty($Img)){
  42. // 如果是生成缩略图的时候出错,则需要删掉已经复制的文件
  43. IF($Type!=1){
  44. unlink($Image);
  45. }
  46. return false;
  47. }
  48. // 如果是执行调整尺寸操作则
  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);// 新建一个真彩色画布
  77. ImageCopyReSampled($nImg,$Img,0,0,0,0,$width,$height,$w,$h);// 重采样拷贝部分图像并调整大小
  78. ImageJpeg($nImg,$Image);// 以JPEG格式将图像输出到浏览器或文件
  79. return true;
  80. } Else {// 如果是执行生成缩略图操作则
  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){// 高比较大
  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 {// 宽比较大
  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. ?>
  102. <?php
  103. //网络图片路径
  104. $imgPath = 'http://www.lesohome.com/phone/compress-img/251139474ba926db3d7850.jpg';
  105. //$imgPath = "http://www.lesohome.com/userfiles/image/20111125/251139474ba926db3d7850.jpg";
  106. $tempPath = str_replace('http://www.lesohome.com/', '', $imgPath);//替换换行字符
  107. $name = strrchr($tempPath, "/");
  108. $path = str_replace($name, '', $tempPath);//替换换行字符
  109. /**
  110. *根据路径path建立多级目录
  111. *$dir目标目录 $mode权限,0700表示最高权限
  112. */
  113. function  makedir( $dir , $mode = "0700" ) {
  114. if(strpos($dir , "/" )){
  115. $dir_path = "" ;
  116. $dir_info = explode("/" , $dir );
  117. foreach($dir_info as $key => $value){
  118. $dir_path .= $value ;
  119. if (!file_exists($dir_path)){
  120. @mkdir($dir_path, $mode) or die ("建立文件夹时失败了" );
  121. @chmod($dir_path, $mode);
  122. } else {
  123. $dir_path .= "/" ;
  124. continue ;
  125. }
  126. $dir_path .= "/" ;
  127. }
  128. return $dir_path ;
  129. } else {
  130. @mkdir($dir, $mode) or die( "建立失败了,请检查权限" );
  131. @chmod($dir, $mode);
  132. return $dir ;
  133. }
  134. } //end makedir
  135. makedir($path);
  136. /**
  137. *根据url获取服务器上的图片
  138. *$url服务器上图片路径 $filename文件名
  139. */
  140. function GrabImage($url,$filename="") {
  141. if($url=="") return false;
  142. if($filename=="") {
  143. $ext=strrchr($url,".");
  144. if($ext!=".gif" && $ext!=".jpg" && $ext!=".png")
  145. return false;
  146. $filename=date("YmdHis").$ext;
  147. }
  148. ob_start();
  149. readfile($url);
  150. $img = ob_get_contents();
  151. ob_end_clean();
  152. $size = strlen($img);
  153. $fp2=@fopen($filename, "a");
  154. fwrite($fp2,$img);
  155. fclose($fp2);
  156. return $filename;
  157. }
  158. ?>
  159. <html>
  160. <body>
  161. 允许上传的文件类型为:<?=implode(', ',$phtypes)?><br/>
  162. <input type="button" id="getImg" name="getImg" size="10" value="获取图片并保存" onclick="alert('12333');" />
  163. <?php
  164. echo $path."<br>";
  165. /**/
  166. $bigImg=GrabImage($imgPath, $tempPath);
  167. if($bigImg){
  168. echo '<img src="'.$bigImg.'"><br>';
  169. } else {
  170. echo "false";
  171. }
  172. compressImg($bigImg,100,80,1);
  173. ?>
  174. </body>
  175. </html>

最新文章

  1. 第二次作业:Github的使用
  2. [转载]TFS源代码管理8大注意事项
  3. TCP连接状态与2MSL等待时间
  4. [Android] how to get facebook profile
  5. UIActionSheet 修改字体颜色
  6. Kubernetes 常用命令
  7. 狄利克雷卷积&amp;莫比乌斯反演总结
  8. Bootstrap 基础讲解2
  9. python3写入csv文件时中文为乱码
  10. 结合ajax 的表单验证
  11. C#调用C++库知识点
  12. Am335x SPI 驱动测试
  13. Ubuntu下环境变量该写进哪个文件里
  14. 一些 JS页面的 调用方式init()
  15. (转)全面认识一下.NET 4的缓存功能
  16. 剑指Offer面试题:1.实现单例模式
  17. Informatica 9.1.0 Domain地址变化修改
  18. Matlab freqs 函数
  19. LeetCode 017 4Sum
  20. 如何使用代码美化器Uncrustify (How to use code beautifier Uncrustify)

热门文章

  1. 【Tcpcopy】离线回放功能
  2. 第一个App“今日材料报价”上架,记录一下【原】
  3. JVM的GC机制及JVM的调优方法
  4. 使用asp.net MVC4中的Bundle遇到的问题及解决办法
  5. Ext.useShims=true
  6. Esper系列(十三)Splitting and Duplicating Streams
  7. HW6.24
  8. algorithm@ Strongly Connected Component
  9. ubuntu 运行android sdk 下的工具adb报bash: ./adb: No such file or directory
  10. rsync 的安装