php生成缩略图的三种模式

原文地址:开源中国社区 luckme 跳转地址

1、把大图缩略到缩略图指定的范围内,可能有留白(原图细节不丢失)

 1 thumb_stand.php
 2 <?php
 3 // http://localhost/exa5/thumb_image/thumb_stand.php?w=200&h=200
 4 // 把大图缩略到缩略图指定的范围内,可能有留白(原图细节不丢失)
 5 $w = $_GET['w']?$_GET['w']:200;
 6 $h = $_GET['h']?$_GET['h']:200;
 7 $filename = "stand_test_".$w."_".$h.".jpg";
 8 image_resize( 'test.jpg',$filename, $w, $h);
 9 header("content-type:image/png");//设定生成图片格式
10 echo file_get_contents($filename);
11  
12 function image_resize($f, $t, $tw, $th){
13 // 按指定大小生成缩略图,而且不变形,缩略图函数
14         $temp = array(1=>'gif', 2=>'jpeg', 3=>'png');
15  
16         list($fw, $fh, $tmp) = getimagesize($f);
17  
18         if(!$temp[$tmp]){
19                 return false;
20         }
21         $tmp = $temp[$tmp];
22         $infunc = "imagecreatefrom$tmp";
23         $outfunc = "image$tmp";
24  
25         $fimg = $infunc($f);
26  
27         // 使缩略后的图片不变形,并且限制在 缩略图宽高范围内
28         if($fw/$tw > $fh/$th){
29             $th = $tw*($fh/$fw);
30         }else{
31             $tw = $th*($fw/$fh);
32         }
33  
34         $timg = imagecreatetruecolor($tw, $th);
35         imagecopyresampled($timg, $fimg, 0,0, 0,0, $tw,$th, $fw,$fh);
36         if($outfunc($timg, $t)){
37                 return true;
38         }else{
39                 return false;
40         }
41 }
42 ?>
缩略图1

2、把大图缩略到缩略图指定的范围内,不留白(原图会居中缩放,把超出的部分裁剪掉)

 1 thumb_cut.php
 2 <?php
 3 // http://localhost/exa5/thumb_image/thumb_cut.php?w=200&h=200
 4 // 把大图缩略到缩略图指定的范围内,不留白(原图会居中缩放,把超出的部分裁剪掉)
 5 $w = $_GET['w']?$_GET['w']:200;
 6 $h = $_GET['h']?$_GET['h']:200;
 7 $filename = "cut_test_".$w."_".$h.".jpg";
 8 image_resize( 'test.jpg',$filename, $w, $h);
 9 header("content-type:image/png");//设定生成图片格式
10 echo file_get_contents($filename);
11  
12 // 按指定大小生成缩略图,而且不变形,缩略图函数
13 function image_resize($f, $t, $tw, $th){
14         $temp = array(1=>'gif', 2=>'jpeg', 3=>'png');
15         list($fw, $fh, $tmp) = getimagesize($f);
16         if(!$temp[$tmp]){
17                 return false;
18         }
19         $tmp = $temp[$tmp];
20         $infunc = "imagecreatefrom$tmp";
21         $outfunc = "image$tmp";
22  
23         $fimg = $infunc($f);
24 //      $fw = 10;
25 //      $fh = 4;
26 //      $tw = 4;
27 //      $th = 2;
28         // 把图片铺满要缩放的区域
29         if($fw/$tw > $fh/$th){
30             $zh = $th;
31             $zw = $zh*($fw/$fh);
32             $_zw = ($zw-$tw)/2;
33         }else{
34             $zw = $tw;
35             $zh = $zw*($fh/$fw);
36             $_zh = ($zh-$th)/2;
37         }
38 //        echo $zw."<br>";   
39 //        echo $zh."<br>";   
40 //        echo $_zw."<br>";   
41 //        echo $_zh."<br>";   
42 //        exit;
43         $zimg = imagecreatetruecolor($zw, $zh);
44         // 先把图像放满区域
45         imagecopyresampled($zimg, $fimg, 0,0, 0,0, $zw,$zh, $fw,$fh);
46  
47         // 再截取到指定的宽高度
48         $timg = imagecreatetruecolor($tw, $th);
49         imagecopyresampled($timg, $zimg, 0,0, 0+$_zw,0+$_zh, $tw,$th, $zw-$_zw*2,$zh-$_zh*2);
50 //        
51         if($outfunc($timg, $t)){
52                 return true;
53         }else{
54                 return false;
55         }
56 }
57  
58 ?>
缩略图2

3、把大图缩略到缩略图指定的范围内,不留白(原图会剪切掉不符合比例的右边和下边)

 1 thumb_strict.php
 2 <?php
 3 // http://localhost/exa5/thumb_image/thumb_strict.php?w=200&h=200
 4 // 把大图缩略到缩略图指定的范围内,不留白(原图会剪切掉不符合比例的右边和下边)
 5 $w = $_GET['w']?$_GET['w']:200;
 6 $h = $_GET['h']?$_GET['h']:200;
 7 $filename = "strict_test_".$w."_".$h.".jpg";
 8 image_resize( 'test.jpg',$filename, $w, $h);
 9 header("content-type:image/png");//设定生成图片格式
10 echo file_get_contents($filename);
11  
12 function image_resize($f, $t, $tw, $th){
13 // 按指定大小生成缩略图,而且不变形,缩略图函数
14         $temp = array(1=>'gif', 2=>'jpeg', 3=>'png');
15  
16         list($fw, $fh, $tmp) = getimagesize($f);
17  
18         if(!$temp[$tmp]){
19                 return false;
20         }
21         $tmp = $temp[$tmp];
22         $infunc = "imagecreatefrom$tmp";
23         $outfunc = "image$tmp";
24  
25         $fimg = $infunc($f);
26  
27         if($fw/$tw > $fh/$th){
28                 $fw = $tw * ($fh/$th);
29         }else{
30                 $fh = $th * ($fw/$tw);
31         }
32  
33         $timg = imagecreatetruecolor($tw, $th);
34         imagecopyresampled($timg, $fimg, 0,0, 0,0, $tw,$th, $fw,$fh);
35         if($outfunc($timg, $t)){
36                 return true;
37         }else{
38                 return false;
39         }
40 }
41 ?>
缩略图3

 

原文地址:https://www.cnblogs.com/6luv-ml/p/6371187.html