返回列表 回复 发帖

生成高质量的缩略图片

  1. <?
  2. &#36;FILENAME="image_name";
  3. // 生成图片的宽度
  4. &#36;RESIZEWIDTH=400;
  5. // 生成图片的高度
  6. &#36;RESIZEHEIGHT=400;
  7. function ResizeImage(&#36;im,&#36;maxwidth,&#36;maxheight,&#36;name){
  8. &#36;width = imagesx(&#36;im);
  9. &#36;height = imagesy(&#36;im);
  10. if((&#36;maxwidth && &#36;width > &#36;maxwidth) || (&#36;maxheight && &#36;height > &#36;maxheight)){
  11. if(&#36;maxwidth && &#36;width > &#36;maxwidth){
  12. &#36;widthratio = &#36;maxwidth/&#36;width;
  13. &#36;RESIZEWIDTH=true;
  14. }
  15. if(&#36;maxheight && &#36;height > &#36;maxheight){
  16. &#36;heightratio = &#36;maxheight/&#36;height;
  17. &#36;RESIZEHEIGHT=true;
  18. }
  19. if(&#36;RESIZEWIDTH && &#36;RESIZEHEIGHT){
  20. if(&#36;widthratio < &#36;heightratio){
  21. &#36;ratio = &#36;widthratio;
  22. }else{
  23. &#36;ratio = &#36;heightratio;
  24. }
  25. }elseif(&#36;RESIZEWIDTH){
  26. &#36;ratio = &#36;widthratio;
  27. }elseif(&#36;RESIZEHEIGHT){
  28. &#36;ratio = &#36;heightratio;
  29. }
  30. &#36;newwidth = &#36;width * &#36;ratio;
  31. &#36;newheight = &#36;height * &#36;ratio;
  32. if(function_exists("imagecopyresampled")){
  33. &#36;newim = imagecreatetruecolor(&#36;newwidth, &#36;newheight);
  34. imagecopyresampled(&#36;newim, &#36;im, 0, 0, 0, 0, &#36;newwidth, &#36;newheight, &#36;width, &#36;height);
  35. }else{
  36. &#36;newim = imagecreate(&#36;newwidth, &#36;newheight);
  37. imagecopyresized(&#36;newim, &#36;im, 0, 0, 0, 0, &#36;newwidth, &#36;newheight, &#36;width, &#36;height);
  38. }
  39. ImageJpeg (&#36;newim,&#36;name . ".jpg");
  40. ImageDestroy (&#36;newim);
  41. }else{
  42. ImageJpeg (&#36;im,&#36;name . ".jpg");
  43. }
  44. }
  45. if(&#36;_FILES[';image';][';size';]){
  46. if(&#36;_FILES[';image';][';type';] == "image/pjpeg"){
  47. &#36;im = imagecreatefromjpeg(&#36;_FILES[';image';][';tmp_name';]);
  48. }elseif(&#36;_FILES[';image';][';type';] == "image/x-png"){
  49. &#36;im = imagecreatefrompng(&#36;_FILES[';image';][';tmp_name';]);
  50. }elseif(&#36;_FILES[';image';][';type';] == "image/gif"){
  51. &#36;im = imagecreatefromgif(&#36;_FILES[';image';][';tmp_name';]);
  52. }
  53. if(&#36;im){
  54. if(file_exists("&#36;FILENAME.jpg")){
  55. unlink("&#36;FILENAME.jpg");
  56. }
  57. ResizeImage(&#36;im,&#36;RESIZEWIDTH,&#36;RESIZEHEIGHT,&#36;FILENAME);
  58. ImageDestroy (&#36;im);
  59. }
  60. }
  61. ?>
  62. <img src="<? echo(&#36;FILENAME.".jpg?reload=".rand(0,999999)); ?>"><br><br>
  63. <form enctype="multipart/form-data" method="post">
  64. <br>
  65. <input type="file" name="image" size="50" value="浏览"><p>
  66. <input type="submit" value="上传图片">
  67. </form>
  68. </body>
  69. </html>
复制代码
哈哈哈!!!!你的IP是不是?我都知道了!!!
返回列表