首頁 後端開發 php教程 php 縮圖類別(附呼叫範例)

php 縮圖類別(附呼叫範例)

Jul 25, 2016 am 08:57 AM

本文介绍下,php实现的一个缩略图类,支持加载图片文件与加载图片字符串,按比例拉伸等,代码后面有调用示例供参考。

分享个php缩略图类,可以实现如下的功能:

1,支持加载图片文件和加载图片字符串 2,可以将缩略图输出到浏览器和保持缩略图文件 3,支持gif,png,jpeg类型的缩略 4,可以设定是否按比例来拉伸

代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

<?php

/**

 * 生成缩略图(支持加载图片文件和字符串2种方式)

 * @param       $maxWidth       缩略图最大宽度

 * @param       $maxHeight      缩略图最大高度

 * @param       bool    $scale  是否按比例缩小,否则拉伸

 * @param       bool    $inflate        是否放大以来填充缩略图

 * @edit by bbs.it-home.org

 */

class Thumbnail {

        private $maxWidth;

        private $maxHeight;

        private $scale;

        private $inflate;

        private $types;

        private $imgLoaders;

        private $imgCreators;

        private $source;

        private $sourceWidth;

        private $sourceHeight;

        private $sourceMime;

        private $thumb;

        private $thumbWidth;

        private $thumbHeight;

 

        public function __construct($maxWidth, $maxHeight, $scale = true, $inflate = true) {

                $this->maxWidth = $maxWidth;

                $this->maxHeight = $maxHeight;

                $this->scale = $scale;

                $this->inflate = $inflate;

                $this->types = array(

                    'image/jpeg',

                    'image/png',

                    'image/gif'

                );

                //加载MIME类型图像的函数名称

                $this->imgLoaders = array(

                    'image/jpeg'        =>      'imagecreatefromjpeg',

                    'image/png'         =>      'imagecreatefrompng',

                    'image/gif'         =>      'imagecreatefromgif'

                );

                //储存创建MIME类型图片的函数名称

                $this->imgCreators = array(

                    'image/jpeg'        =>      'imagejpeg',

                    'image/png'         =>      'imagepng',

                    'image/gif'         =>      'imagegif'

                );          

        }

        /**

         * 文件方式加载图片

         * @param       string  $image 源图片

         * @return      bool   

         */

        public function loadFile($image){

                if(!$dims = @getimagesize($image)){

                        trigger_error("源图片不存在");

                }

                if(in_array($dims['mime'], $this->types)){

                        $loader = $this->imgLoaders[$dims['mime']];

                        $this->source = $loader($image);

                        $this->sourceWidth = $dims[0];

                        $this->sourceHeight = $dims[1];

                        $this->sourceMime = $dims['mime'];

                        $this->initThumb();

                        return TRUE;

                }else{

                        trigger_error('不支持'.$dims['mime']."图片类型");

                }

        }

        /**

         * 字符串方式加载图片

         * @param       string $image  字符串

         * @param       string $mime    图片类型

         * @return type

         */

        public function loadData($image,$mime){

                if(in_array($mime, $this->types)){

                        if($this->source = @imagecreatefromstring($image)){

                                $this->sourceWidth = imagesx($this->source);

                                $this->sourceHeight = imagesy($this->source);

                                $this->sourceMime = $mime;

                                $this->initThumb();

                                return TRUE;

                        }else{

                                trigger_error("不能从字符串加载图片");

                        }

                }else{

                        trigger_error("不支持".$mime."图片格式");

                }

        }

        /**

         * 生成缩略图

         * @param       string  $file   文件名。如果不为空则储存为文件,否则直接输出到浏览器

         */

        public function buildThumb($file = null){

                $creator = $this->imgCreators[$this->sourceMime];

                if(isset($file)){

                        return $creator($this->thumb,$file);

                }else{

                        return $creator($this->thumb);

                }

        }

        /**

         * 处理缩放

         */

        public function initThumb(){

                if($this->scale){

                        if($this->sourceWidth > $this->sourceHeight){

                                $this->thumbWidth = $this->maxWidth;

                                $this->thumbHeight = floor($this->sourceHeight*($this->maxWidth/$this->sourceWidth));

                        }elseif($this->sourceWidth < $this->sourceHeight){

                                $this->thumbHeight = $this->maxHeight;

                                $this->thumbWidth = floor($this->sourceWidth*($this->maxHeight/$this->sourceHeight));

                        }else{

                                $this->thumbWidth = $this->maxWidth;

                                $this->thumbHeight = $this->maxHeight;

                        }

                }

                $this->thumb = imagecreatetruecolor($this->thumbWidth, $this->thumbHeight);

                 

                if($this->sourceWidth <= $this->maxWidth && $this->sourceHeight <= $this->maxHeight && $this->inflate == FALSE){

                        $this->thumb = $this->source;

                }else{

                        imagecopyresampled($this->thumb, $this->source, 0, 0, 0, 0, $this->thumbWidth, $this->thumbHeight,

$this->sourceWidth, $this->sourceHeight);

                }

        }

         

        public function getMine(){

                return $this->sourceMime;

        }

         

        public function getThumbWidth(){

                return $this->thumbWidth;

        }

         

        public function getThumbHeight(){

                return $this->thumbHeight;

        }

 

}

 

/**

 * 缩略图类调用示例(文件)

 */

$thumb = new Thumbnail(200, 200);

$thumb->loadFile('wap.gif');

header('Content-Type:'.$thumb->getMine());

$thumb->buildThumb();

/**

 * 缩略图类调用示例(字符串)

 */

$thumb = new Thumbnail(200, 200);

$image = file_get_contents('wap.gif');

$thumb->loadData($image, 'image/jpeg');

$thumb->buildThumb('wap_thumb.gif');

?>

登入後複製


本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章

倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章

倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章標籤

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

11個最佳PHP URL縮短腳本(免費和高級) 11個最佳PHP URL縮短腳本(免費和高級) Mar 03, 2025 am 10:49 AM

11個最佳PHP URL縮短腳本(免費和高級)

在Laravel中使用Flash會話數據 在Laravel中使用Flash會話數據 Mar 12, 2025 pm 05:08 PM

在Laravel中使用Flash會話數據

Instagram API簡介 Instagram API簡介 Mar 02, 2025 am 09:32 AM

Instagram API簡介

簡化的HTTP響應在Laravel測試中模擬了 簡化的HTTP響應在Laravel測試中模擬了 Mar 12, 2025 pm 05:09 PM

簡化的HTTP響應在Laravel測試中模擬了

構建具有Laravel後端的React應用程序:第2部分,React 構建具有Laravel後端的React應用程序:第2部分,React Mar 04, 2025 am 09:33 AM

構建具有Laravel後端的React應用程序:第2部分,React

php中的捲曲:如何在REST API中使用PHP捲曲擴展 php中的捲曲:如何在REST API中使用PHP捲曲擴展 Mar 14, 2025 am 11:42 AM

php中的捲曲:如何在REST API中使用PHP捲曲擴展

在Codecanyon上的12個最佳PHP聊天腳本 在Codecanyon上的12個最佳PHP聊天腳本 Mar 13, 2025 pm 12:08 PM

在Codecanyon上的12個最佳PHP聊天腳本

Laravel中的通知 Laravel中的通知 Mar 04, 2025 am 09:22 AM

Laravel中的通知

See all articles