cncml手绘网
标题:
PHP一般情况下生成的缩略图都比较不理想
[打印本页]
作者:
admin
时间:
2018-7-7 23:52
标题:
PHP一般情况下生成的缩略图都比较不理想
PHP用GD库生成高质量的缩略图片,PHP一般情况下生成的缩略图都比较不理想。今天试用PHP,GD库来生成缩略图。虽然并不100%完美。可是也应该可以满足缩略图的要求了。
<?php
' g2 O# K! j& ~ J: S" a: |9 N, H) X
$FILENAME="image.thumb";
9 ^5 b) l. l0 c5 J$ w5 r
// 生成图片的宽度
; t/ B& z G' T/ G, t
$RESIZEWIDTH=400;
4 U7 L; s T% C$ g6 w
// 生成图片的高度
/ D+ z8 d# | _/ w$ g9 h
$RESIZEHEIGHT=400;
' R3 W( j! s$ r& L9 G. [5 H
1 C& J j% ^# R, k* d- X( D# [$ i
function ResizeImage($im,$maxwidth,$maxheight,$name){
* z1 c2 k- K ^% h- u; [/ D
$width = imagesx($im);
1 \1 B) ]& G& N( m" Z+ ?
$height = imagesy($im);
; [/ \$ c: u9 \ j1 ~
if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
. @" s; @, y" u7 v
if($maxwidth && $width > $maxwidth){
2 f7 i' F/ s4 r5 W7 p6 Q7 \
$widthratio = $maxwidth/$width;
" A! J6 z0 q7 d5 y2 m3 k
$RESIZEWIDTH=true;
3 ~* d$ Z$ Z1 D
}
4 G: v D! ]9 T6 V
if($maxheight && $height > $maxheight){
/ K, p! K* E" f
$heightratio = $maxheight/$height;
' l7 k. L) x! P- b, }
$RESIZEHEIGHT=true;
# l+ |* e9 f5 Q( U+ X b$ k& S6 ^- d* m
}
5 ~, F u, u: p
if($RESIZEWIDTH && $RESIZEHEIGHT){
- F. e$ C$ U1 }2 F' }3 z
if($widthratio < $heightratio){
- `" y; h! Q8 d1 W
$ratio = $widthratio;
/ Q' [: a$ N/ }. j) F& @0 v, }
}else{
/ F% q2 W# Q' T8 r6 @* y% ]
$ratio = $heightratio;
+ i; J8 F9 X, E. J/ m x
}
9 b/ o+ q2 S. o
}elseif($RESIZEWIDTH){
, u& C/ B: R* F/ ]% l( S
$ratio = $widthratio;
8 s- H/ }* t5 \4 a( Q
}elseif($RESIZEHEIGHT){
' q- D1 z9 y, R$ ?" W1 }; {( u
$ratio = $heightratio;
2 b$ e, b4 G F# P4 x* a+ b
}
8 N# ?+ ~; }, j; ?3 j, a) Z! o- V
$newwidth = $width * $ratio;
) y; d0 w! b6 e% B1 l& J; }0 B* T
$newheight = $height * $ratio;
* k% S( W6 V3 d5 }- a) c' j) H
if(function_exists("imagecopyresampled")){
% q7 h* ]) Q+ }+ R1 \6 P
$newim = imagecreatetruecolor($newwidth, $newheight);
; V. W; U8 }" n/ F2 R$ t
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
W; Q, h. A* e
}else{
+ y: G. |, ?# `7 i+ u, z
$newim = imagecreate($newwidth, $newheight);
/ m- a1 Z4 a1 f" g6 J
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
" l' @: D8 ~ {, F
}
2 z% p y2 `2 q; i
ImageJpeg ($newim,$name . ".jpg");
$ {% I) L! t( d
ImageDestroy ($newim);
5 N7 w. X; ^; T+ T' {& P1 q+ }
}else{
" `" V$ P3 n' g9 b& w' u
ImageJpeg ($im,$name . ".jpg");
" M! i; L& f- z* Z( ~
}
. u, l a2 n9 K7 h4 n. ^
}
% i6 P* M% G4 w! i5 ^
4 w8 K1 l: F* _" ?) G" x6 w
if($_FILES['image']['size']){
6 ~. g* R/ f3 b
if($_FILES['image']['type'] == "image/pjpeg"){
5 {, H, N8 y" U; c7 {4 R' |' A
$im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
2 m7 N' N. T4 d4 G3 l9 r) k1 i! j$ V
}elseif($_FILES['image']['type'] == "image/x-png"){
5 Q0 c9 ~4 V! p0 M$ r8 r. {6 M7 L
$im = imagecreatefrompng($_FILES['image']['tmp_name']);
1 U+ S% n- ^; w% }' c% N
}elseif($_FILES['image']['type'] == "image/gif"){
. Z6 a) z9 f6 L
$im = imagecreatefromgif($_FILES['image']['tmp_name']);
2 |; X. o6 t5 }
} 2881064151
4 A! i" f5 B7 J# S0 L
if($im){
" \/ C0 ^ J! \) h5 C# E7 \0 V
if(file_exists("$FILENAME.jpg")){
# h4 y6 e) M6 d) P% R/ r
unlink("$FILENAME.jpg");
' L* n! h! Z8 ^% S5 j
}
" w! H1 z1 x6 v
ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME);
& I: r- ~- x+ S7 l8 v, b
ImageDestroy ($im);
. d/ T( f+ p0 |1 S8 q. i: x" o' d
}
& M6 K% M. ]2 I# t
}
F' Z$ t4 q- p( Q4 b2 `/ k% o
?>
复制代码
以下是测试代码(demo.php)
- ^6 }$ K# T: ], i1 E
( V: y& e' E: }# L( X
代码如下:
5 t# t1 S$ y# T; Q) L, g5 V9 V9 ^2 a
) i/ r$ M% }- c$ v
<?php
5 [( o; w( f% t) A
include('ResizeImage.php');
8 R- G6 R3 N3 T/ C1 E/ v
if(!empty($_POST)){
- d& Y5 }; s, }
echo($FILENAME.".jpg?cache=".rand(0,999999));
+ E' Y' v4 j) b( _7 l% T! Y( j# _- a
}
B M+ j% f0 v# l& X# h
?>
, A7 j' C% r. Z" X% C! Q
<form name="test" action="?submit=true" enctype="multipart/form-data" method="post" >
. _9 T, f2 S7 H1 J
<input type="file" name="image" size="50" value="浏览"><p>
' Q0 G6 |! ^' D9 Q" `
<input type="submit" value="上传图片">
) _+ Z, X6 W& q' L6 V _
</form>
复制代码
8 ^: A4 Q% U' c& _+ @6 J
# `7 p* k% C3 V
欢迎光临 cncml手绘网 (http://cncml.com/)
Powered by Discuz! X3.2