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