cncml手绘网

标题: PHP一般情况下生成的缩略图都比较不理想 [打印本页]

作者: admin    时间: 2018-7-7 23:52
标题: PHP一般情况下生成的缩略图都比较不理想
PHP用GD库生成高质量的缩略图片,PHP一般情况下生成的缩略图都比较不理想。今天试用PHP,GD库来生成缩略图。虽然并不100%完美。可是也应该可以满足缩略图的要求了。
  1. <?php ' M( l- @! y4 ^- I9 \/ }& t) j% F
  2. $FILENAME="image.thumb"; - P1 d9 v* C9 `5 v
  3. // 生成图片的宽度 , X$ L4 G! n$ H6 |
  4. $RESIZEWIDTH=400; . `. ]+ b/ _  f+ d* ~2 J. G
  5. // 生成图片的高度 1 {* J$ `* j- t" x8 p0 \* ]& S
  6. $RESIZEHEIGHT=400;& z7 e7 q( p; `2 \, p
  7. ! d2 ?/ i9 u5 H. K8 ]* z# ?( b9 n
  8. function ResizeImage($im,$maxwidth,$maxheight,$name){
    / i' U: [& b; m/ c4 L" A; H8 {1 U9 L
  9. $width = imagesx($im); ) K3 t8 k1 J  u- Z$ q' D; ~* P( F# A
  10. $height = imagesy($im); $ X/ o# b+ T' R2 b1 i! p
  11. if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){ # ]0 x! V: c1 Q8 Y
  12. if($maxwidth && $width > $maxwidth){ 5 l2 v, I( l6 m, I3 ?& p+ Q
  13. $widthratio = $maxwidth/$width; 9 M3 Y. E; w8 f# M3 q) w4 x
  14. $RESIZEWIDTH=true;
    , m: _5 W- a1 i' i' U
  15. }
    # l1 I% ~/ o* |4 u
  16. if($maxheight && $height > $maxheight){
    % c. q. L+ q3 z0 ?/ g' O( ~
  17. $heightratio = $maxheight/$height; ; _( s' j9 ^4 W$ C: D( }$ l
  18. $RESIZEHEIGHT=true; - d; [# _3 g: x: q1 D
  19. } , n+ {0 c1 P( t* }/ s. ?6 c
  20. if($RESIZEWIDTH && $RESIZEHEIGHT){
    1 ]; n# c, {  ^
  21. if($widthratio < $heightratio){ 8 e2 s- w% z. u! K& _
  22. $ratio = $widthratio; ( f  U! _6 ~0 l
  23. }else{
    % J/ |3 o) j: ?5 O% W2 X% }/ w3 u8 b
  24. $ratio = $heightratio;
    ) S& a3 `+ V: s, X' U
  25. } 9 Q6 v3 [) q% d- J; b, s( C
  26. }elseif($RESIZEWIDTH){
    4 o! U( o2 ~: f4 G. ?. N
  27. $ratio = $widthratio; - j: v( _! u( Y7 d! A* `! K
  28. }elseif($RESIZEHEIGHT){ . R8 Z7 @6 r0 P8 g' ^" d/ r
  29. $ratio = $heightratio;
    % \( _) {$ B, Z  k( E+ P
  30. } 2 t) O( u% t8 V4 F/ w+ {
  31. $newwidth = $width * $ratio;
    ' `" s, T, ~2 ^2 J8 E/ G3 H% J
  32. $newheight = $height * $ratio; . E1 S+ Y; z& J* T3 G' e3 c$ k
  33. if(function_exists("imagecopyresampled")){
    * h8 i. c6 f8 X. N* ~' z
  34. $newim = imagecreatetruecolor($newwidth, $newheight);
    9 m* H  o. I( _9 O; d7 L' c3 V
  35. imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    3 I' w/ m( {" t# r% @6 r* f
  36. }else{
    1 p' [2 H- t+ ?  x+ a% P# }
  37. $newim = imagecreate($newwidth, $newheight);
    % W0 Z0 P. J  p6 o3 ], E  S
  38. imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 5 W% N2 }1 N: @5 D
  39. } ( b7 v  q' d8 X6 O. i4 r
  40. ImageJpeg ($newim,$name . ".jpg"); $ r1 P0 i% b$ f3 O' O
  41. ImageDestroy ($newim); + N+ r7 {, ]. g) g& l5 L& N
  42. }else{ $ n9 S( {, M) U1 h5 J# U
  43. ImageJpeg ($im,$name . ".jpg");
    $ g) D. r- c3 `# O1 `5 z5 y
  44. }
    5 c6 u, X) z3 |* _) x& J" p
  45. }
    ( |1 c! S3 W* t. h  w" V  o+ A
  46. : @( R" _3 q4 T! J. Y; x
  47. if($_FILES['image']['size']){ 8 y* o9 D: Y  b  F% H
  48. if($_FILES['image']['type'] == "image/pjpeg"){ ) w4 F  a! ]/ U: D
  49. $im = imagecreatefromjpeg($_FILES['image']['tmp_name']); $ N. q4 G7 v  g# h6 w3 S
  50. }elseif($_FILES['image']['type'] == "image/x-png"){
    & Y, I: S) k$ c6 E: R% V
  51. $im = imagecreatefrompng($_FILES['image']['tmp_name']);
    5 z& D: f6 j* g9 O) `8 X) A
  52. }elseif($_FILES['image']['type'] == "image/gif"){ 9 J5 ]) D" Y! c& @' m
  53. $im = imagecreatefromgif($_FILES['image']['tmp_name']); . y+ @( `) E4 X% d3 C
  54. } 2881064151% _2 H! f2 D9 x- e
  55. if($im){ 0 i2 }; x% v3 c! N
  56. if(file_exists("$FILENAME.jpg")){
    % }+ k& `5 {2 F" D6 a
  57. unlink("$FILENAME.jpg"); 8 n% z0 W! T* A( V3 ~7 g  E
  58. }
    & O5 U: T, O9 f' I6 g
  59. ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME); , ]" L% n1 U& u2 l, h  {/ s
  60. ImageDestroy ($im);
    : z: L$ {5 \- q
  61. } . y) R8 D; Y/ ^2 X
  62. } 4 |, x" t3 D7 D
  63. ?>
复制代码
以下是测试代码(demo.php)
# o3 b4 C; O$ \; q6 g$ U* v4 x2 D+ W( L. t
代码如下:! G8 g, d' j, G) ~! R
8 R( \& e' H0 o
  1. <?php
    " q1 Y9 _& X1 y/ y
  2. include('ResizeImage.php');
    9 \0 ]# M- ]- k1 D9 k+ |
  3. if(!empty($_POST)){ , Q- j( O& A6 A$ h% d/ Q( X8 g" Z
  4. echo($FILENAME.".jpg?cache=".rand(0,999999));
    * I8 S' L2 I5 H' p
  5. }
    ; I$ J, N& M+ x. g( c/ p* w8 b4 Y
  6. ?> 3 s* E2 @0 x& M6 P* g
  7. <form name="test" action="?submit=true" enctype="multipart/form-data" method="post" >
    7 T! L2 s0 x6 T
  8. <input type="file" name="image" size="50" value="浏览"><p> 3 a7 {2 q' L3 h% z4 f- e
  9. <input type="submit" value="上传图片"> 1 I; u. Q4 N1 V/ v1 ~
  10. </form>
复制代码

8 n* Y+ M4 a8 w% c
4 ^  H' X3 g9 W& S% i. X. _: @




欢迎光临 cncml手绘网 (http://bbs.cncml.com/) Powered by Discuz! X3.2