设为首页 收藏本站
查看: 670|回复: 0

[经验分享] Nginx+PHP实时生成不同尺寸图片

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-5-1 15:24:08 | 显示全部楼层 |阅读模式
原来图片服务器采用Windows .net架构,鉴于需求需要生成各种尺寸图片。

wKiom1cjJSSQEzn6AABzdrDHnIQ604.jpg
流程说明:
用户从Nginx请求对应的图片,判断是否存在_200x300的对应参数,如果没有就直接请求到对应目录的原图,否则继续判断是否在本地已经生成了对应的缓存图片,如果存在返回已经生成过的定制尺寸图片,否则请求PHP动态生成。
Nginx部分配置:
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
    server {
        listen       80;
        server_name  pics.abc.com;
        location / {
            root   /var/www/html;
            index  index.html index.htm index.php;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location ~ \_(\d+)x(\d+)\.(jpg|png|gif|jpeg|bmp)$ {  //判断是否定制图
                try_files $uri /temp/$uri /get.php;    //判断是否已生成过定制图否则转交给/get.php
                expires      30d;
        }

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp)$
        {
                expires      30d;
        }

        }
    }



在/var/www/html我们以只读方式挂载Windows的目录,修改/etc/fstab,添加
1
\\192.168.2.3\f$\pics.abc.com\pics /var/www/html/pics/ cifs    ro,username=user,password=pass   1  2



然后重启netfs服务,另外执行下面命令,安装依赖的包
1
2
3
yum -y install samba-client cifs-utils
service netfs restart
chkconfig netfs on



生成的缩率图会放到网站目录的temp目录下,如请求的http://pics.abc.com/pics/201604/29/abc_200x300.jpg
则生成的图片放在temp/pics/201604/29/abc_200x300.jpg目录下

PHP脚本:
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
function thumb($src, $width, $height, $filename, $mode = 'scale', $quality = '100') {
try {
        $imageValue = getimagesize($src);
        $sourceWidth = $imageValue[0]; //原图宽
        $sourceHeight = $imageValue[1]; //原图高
        $thumbWidth = $width; //缩略图宽
        $thumbHeight = $height; //缩略图高
        $_x = 0;
        $_y = 0;
        $w = $sourceWidth;
        $h = $sourceHeight;
        if ($mode == 'scale') {
                if ($sourceWidth <= $thumbWidth && $sourceHeight <= $thumbHeight) {
                        $_x = floor(($thumbWidth - $sourceWidth) / 2);
                        $_y = floor(($thumbHeight - $sourceHeight) / 2);
                        $thumbWidth = $sourceWidth;
                        $thumbHeight = $sourceHeight;
                } else {
                        if ($thumbHeight * $sourceWidth > $thumbWidth * $sourceHeight) {
                                $thumbHeight = floor($sourceHeight * $width / $sourceWidth);
                                $_y = floor(($height - $thumbHeight) / 2);
                        } else {
                                $thumbWidth = floor($sourceWidth * $height / $sourceHeight);
                                $_x = floor(($width - $thumbWidth) / 2);
                        }
                }
        } else if ($mode == 'crop') {
                if ($sourceHeight < $thumbHeight) { //如果原图尺寸小于当前尺寸
                $thumbWidth = floor($thumbWidth * $sourceHeight / $thumbHeight);
                $thumbHeight = $sourceHeight;
                }
                if ($sourceWidth < $thumbWidth) {
                        $thumbHeight = floor($thumbHeight * $sourceWidth / $thumbWidth);
                        $thumbWidth = $sourceWidth;
                }

                $s1 = $sourceWidth / $sourceHeight; //原图比例
                $s2 = $width / $height; //新图比例
                if ($s1 == $s2) {
                } else if ($s1 > $s2) { //全高度
                        $y = 0;
                        $ax = floor($sourceWidth * ($thumbHeight / $sourceHeight));
                        $x = ($ax - $thumbWidth) / 2;
                        $w = $thumbWidth / ($thumbHeight / $sourceHeight);

                } else { //全宽度
                        $x = 0;
                        $ay = floor($sourceHeight * ($thumbWidth / $sourceWidth)); //模拟原图比例高度
                        $y = ($ay - $thumbHeight) / 2;
                        $h = $thumbHeight / ($thumbWidth / $sourceWidth);
                }

        }
        switch ($imageValue[2]) {
                case 2: $source = imagecreatefromjpeg($src);
                        break;
                case 1: $source = imagecreatefromgif($src);
                        break;
                case 3: $source = imagecreatefrompng($src);
                        break;
                case 6: $source = imagecreatefromwbmp($src);
                        break;
                default: defulat();
                return;
        }
        header("Content-type: image/jpeg");
        $thumb = imagecreatetruecolor($width, $height);
        imagefill($thumb, 0, 0, imagecolorallocate($thumb, 255, 255, 255));
        imagecopyresampled($thumb, $source, 0, 0, $x, $y, $width, $height, $w, $h);
        imagejpeg($thumb, null, $quality);
        imagejpeg($thumb, $filename, $quality);
        imagedestroy($thumb);
        imagedestroy($source);
} catch (Exception $ex) {
        defulat();
}
}


function defulat() {
/*
        $default_img = realpath('../pictures/nopic.gif');
        ob_start();
        header('Content-type:image/jpeg');
        readfile($default_img);
        ob_flush();
        flush();
*/
echo 'error';
}

function mkDirs($dir){
    if(!is_dir($dir)){
        if(!mkDirs(dirname($dir))){
            return false;
        }
        if(!mkdir($dir,0755)){
            return false;
        }
    }
    return true;
}

$uri=$_SERVER['REQUEST_URI'];
$image=basename($uri);

$temp='./temp/'.dirname($uri).'/';
$imgpath='.'.dirname($uri).'/';

/*
//检查本地是否存在文件,原图
if(file_exists($temp.$image)){
        ob_start();
        header('Content-type:image/jpeg');
        readfile($temp.$image);
        ob_flush();
        flush();
        exit();
}
*/

//检查生成的图片是否曾经生成过,存在即返回,否则重新生成新图
if(!preg_match('/_(\d+)x(\d+)/', $image, $wh)){
        ob_start();
        header('Content-type:image/jpeg');
        readfile($imgpath.$image);
        ob_flush();
        flush();
        exit();
}

$width = $wh[1];
$height = $wh[2];
$source_img=preg_replace('/_(\d+)x(\d+)/', '', $image);
//对长宽都超过的图片返回原图
if($width>=2000 || $height>=2000){
    ob_start();
        header('Content-type:image/jpeg');
        readfile($imgpath.$source_img);
        ob_flush();
        flush();
        exit();
}

//图片处理
$src=$imgpath.$source_img;
$filename=$temp.$image;
mkDirs($temp);
//thumb(realpath($src), $width, $height, $filename, 'crop', '85');
thumb(realpath($src), $width, $height, $filename, 'crop', '100');




PHP生成尺寸部分参考<PHP图片自动裁切应付不同尺寸的显示>


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-211362-1-1.html 上篇帖子: Nginx安装和配置文件 下篇帖子: nginx如何设置自定义404页面 图片
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表