# bsg_data **Repository Path**: fu-dongsheng/bsg_data ## Basic Information - **Project Name**: bsg_data - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-03-07 - **Last Updated**: 2024-03-07 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 基本用法 > * composer install > * copy .env.example .env > * php artisan key:generate > * 配置.env相关参数 > * 数据库迁移 php artisan migrate > * 内容填充 php artisan db:seed > * 账号密码 admin@1188.com admin1188 ## elasticsearch搜索 ### 文章 > * php artisan elastic:create-index 'App\Index\ArticleIndexConfigurator' > * php artisan elastic:update-mapping 'App\Models\Article' ### 手游 > * php artisan elastic:create-index 'App\Index\MobileGameIndexConfigurator' > * php artisan elastic:update-mapping 'App\Models\MobileGame' ### 排行 > * php artisan elastic:create-index 'App\Index\RankIndexConfigurator' > * php artisan elastic:update-mapping 'App\Models\Rank' ### 导入 > * php artisan import:elasticsearch ## nginx配制 ```nginx server { listen 80; server_name manage.lzcms.com; root E:/web/lzcms/public/admin/; index index.html index.htm index.php; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { fastcgi_pass bakend; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } server { listen 80; server_name asset.lzcms.com; root E:/web/lzcms/public/static/; index index.html index.htm; location ~* \.(eot|ttf|woff|woff2)$ { add_header Access-Control-Allow-Origin *; } } server { listen 80; server_name img.lzcms.com; root E:/web/lzcms_img/; } ``` ## 缩略图 生成缩略图有2种方式 不管使用哪种方式,生成的路径规则一致 原图: xxxx.jpg 缩略图: xxxx_([rc])_(\d+|-)_(\d+|-).jpg 规则: http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_image_filter_module.html ###1. linux服务器使用nginx的http_image_filter_module模块生成 ```nginx server { listen 80; server_name img.lzcms.com; root /opt/case/lzcms_img/; set $watermark "/usr/local/nginx/conf/watermark/rengwan.png"; # 水印图路径 location ~* /(.*)/(.*)_([rc])_(\d+|-)_(\d+|-)\.(jpg|png|jpeg|gif)$ { set $type $3; # 裁剪方式 set $width $4; # 宽 set $height $5; # 高 set $quality 95; # 图片质量 set $cache_prefix 'cache'; # 缓存目录 set $image_path "/$1/$2.$6"; # 原图路径 set $cache_path "/$cache_prefix/$3_$4_$5/$1/$2.$6"; # 缩略图缓存路径 if ($type = 'r') { set $type 'image-resize'; } if ($type = 'c') { set $type 'image-crop'; } set $image_uri /$type$image_path?width=$width&height=$height&quality=$quality&alias=$document_root&watermark=$watermark; # 检测缩略图缓存是否存在 if (-f $document_root$cache_path) { rewrite ".*" $cache_path; break; } # 检测原图是否存在 if (!-f $document_root$image_path) { return 404; } # 本地12580端口代理生成缩略图并缓存 if (!-f $document_root$cache_path) { proxy_pass http://127.0.0.1:12580$image_uri; } proxy_store $document_root$cache_path; proxy_store_access user:rw group:rw all:r; proxy_set_header Host $host; expires 30d; access_log off; } location ~* \.(gif)$ { if ($query_string = 'imageInfo') { set $image_path /image-size$uri?alias=$document_root; proxy_pass http://127.0.0.1:12580/$image_path; break; } access_log off; expires 30d; } location ~* (.*)\.(jpg|jpeg|png)$ { if ($query_string = 'imageInfo') { set $image_path /image-size$uri?alias=$document_root; proxy_pass http://127.0.0.1:12580/$image_path; break; } expires 30d; access_log off; set $image_path "$1.$2"; # 文件路径 set $image_uri /watermark$image_path?alias=$document_root&watermark=$watermark; set $flag 1; if ($query_string != 'ori') { set $flag "${flag}1"; } if ($watermark != '') { set $flag "${flag}1"; } if ($flag = "111") { proxy_pass http://127.0.0.1:12580$image_uri; } } } server { listen 127.0.0.1:12580; location /image-resize { alias $arg_alias; image_filter resize $arg_width $arg_height; image_filter_jpeg_quality $arg_quality; image_filter_buffer 20M; access_log off; if ($arg_watermark = '') { break; } image_filter_watermark $arg_watermark; # 水印文件位置 (绝对路径) image_filter_watermark_position bottom-right; # 水印位置 image_filter_watermark_width_from 450; # 打水印的图片最小宽度,只有大于这个宽度的才会打水印 image_filter_watermark_height_from 250; # 打水印的图片最小高度,只有大于这个高度的才会打水印 } location /image-crop { alias $arg_alias; image_filter crop $arg_width $arg_height; image_filter_jpeg_quality $arg_quality; image_filter_buffer 10M; access_log off; if ($arg_watermark = '') { break; } image_filter_watermark $arg_watermark; # 水印文件位置 (绝对路径) image_filter_watermark_position bottom-right; # 水印位置 image_filter_watermark_width_from 450; # 打水印的图片最小宽度,只有大于这个宽度的才会打水印 image_filter_watermark_height_from 250; # 打水印的图片最小高度,只有大于这个高度的才会打水印 } location /image-size { alias $arg_alias; image_filter size; access_log off; } location /watermark { alias $arg_alias; access_log off; image_filter watermark; # 开启水印 image_filter_watermark $arg_watermark; # 水印文件位置 (绝对路径) image_filter_watermark_position bottom-right; # 水印位置 image_filter_jpeg_quality 100; # 图片质量 image_filter_buffer 20M; # 缓存 image_filter_watermark_width_from 450; # 打水印的图片最小宽度,只有大于这个宽度的才会打水印 image_filter_watermark_height_from 250; # 打水印的图片最小高度,只有大于这个高度的才会打水印 } } ``` ###2. windows开发环境使用php生成 ```nginx server { listen 80; server_name img.lzcms.com; root E:/web/lzcms_img; location ~* /(.*)/(.*)_([rc])_(\d+|-)_(\d+|-)\.(jpg|png|jpeg|gif)$ { set $type $3; # 裁剪方式 set $width $4; # 宽 set $height $5; # 高 set $quality 95; # 图片质量 set $cache_prefix 'cache'; # 缓存目录 set $image_path "/$1/$2.$6"; # 原图路径 set $cache_path "/$cache_prefix/$3_$4_$5/$1/$2.$6"; # 缩略图缓存路径 try_files $cache_path /thumb.php; expires 30d; access_log off; } location ~* \.(jpg|jpeg|gif|png)$ { if (-f $request_filename) { access_log off; expires 30d; break; } } location ~ \.php$ { fastcgi_pass bakend; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } ``` ```php thumb.php err404(); $type = $match[2]; // 裁剪模式 $width = $match[3]; // 宽 $height = $match[4]; // 高 $quality = 95; // 图片质量 $cachePrefix = 'cache'; // 缓存目录 $imagePath = "/{$match[1]}.{$match[5]}"; // 原图路径 $cachePath = "/{$cachePrefix}/{$type}_{$width}_{$height}/{$match[1]}.{$match[5]}"; // 缩略图缓存路径 // 检测缩略图缓存是否存在 if (is_file(__DIR__ . $cachePath)) { readfile(__DIR__ . $cachePath); exit(); } // 检测原图是否存在 if (!is_file(__DIR__ . $imagePath)) $this->err404(); // 生成缩略图 $this->load(__DIR__ . $imagePath, $type, $width, $height); // 先save后ouput $this->save(__DIR__ . $cachePath); $this->output(); } /** * 装载图像 * * @param string $filename 文件完整路径 * @param string $type 裁剪模式 * @param mixed $width 缩略图宽 * @param mixed $height 缩略图高 * @return boolean */ public function load($filename, $type, $width, $height) { $image_info = getimagesize($filename); $this->_width = $image_info[0]; $this->_height = $image_info[1]; $this->_image_type = $image_info[2]; $this->_mime = $image_info['mime']; // 如果是 bmp 格式先将 bmp 格式转为 jpg 格式 if ($this->_mime == 'image/x-ms-bmp' || $this->_mime == 'image/bmp') { $tmpImg = $this->imageCreateFromBmp($filename); if ($tmpImg) { imagejpeg($tmpImg, $filename, 100); $this->load($filename); return true; } } if (IMAGETYPE_JPEG === $this->_image_type) { $this->_ext = 'jpg'; $this->_image = @imagecreatefromjpeg($filename); } elseif (IMAGETYPE_GIF === $this->_image_type) { $this->_ext = 'gif'; $this->_image = imagecreatefromgif($filename); } elseif (IMAGETYPE_PNG === $this->_image_type) { $this->_ext = 'png'; $this->_image = imagecreatefrompng($filename); } else { return false; } if (!$this->_image) exit(); if ($type == 'r') { if ($width != '-' && $height == '-') $this->resizeToWidth($width); elseif ($width == '-' && $height != '-') $this->resizeToHeight($height); elseif ($width != '-' && $height != '-') { if ($width / $height > $this->_width / $this->_height) { $this->resizeToHeight($height); } else { $this->resizeToWidth($width); } } } else { if ($width == '-') $width = $this->_width; if ($height == '-') $height = $this->_height; $this->resizeCut($width, $height); } } /** * 任意改变图像大小 * * @param int $width 改变后的宽度 * @param int $height 改变后的高 * @return this */ public function resize($width = 0, $height = 0) { if ($width <= 0 || $height <= 0) { return false; } $width = min(ceil($width), $this->_width); $height = min(ceil($height), $this->_height); $img_new = imagecreatetruecolor($width, $height); // png, gif 透明 if (IMAGETYPE_GIF === $this->_image_type || IMAGETYPE_PNG === $this->_image_type) { imagealphablending($img_new, false); imagesavealpha($img_new, true); } imagecopyresampled($img_new, $this->_image, 0, 0, 0, 0, $width, $height, $this->_width, $this->_height); $this->_width = $width; $this->_height = $height; return ($this->_image = $img_new); } /** * 将图像缩放到指定宽度 * * @param int $width 宽度 */ public function resizeToWidth($width) { $width = min($width, $this->_width); return $this->resize($width, $this->_height * $width / $this->_width); } /** * 将图像缩放到制定高度 * * @param int $height 高度 */ public function resizeToHeight($height = 0) { $height = min($height, $this->_height); return $this->resize($this->_width * $height / $this->_height, $height); } /** * 裁剪图像 * * @param int $width 裁剪后的宽度 * @param int $height 裁剪后的高度 * @param int $left 裁剪相对左边偏移值 * @param int $top 裁剪相对顶部偏移值 */ public function cut($width, $height, $left = 0, $top = 0) { $img_new = imagecreatetruecolor($width, $height); // png, gif 透明 if (IMAGETYPE_GIF === $this->_image_type || IMAGETYPE_PNG === $this->_image_type) { imagealphablending($img_new, false); imagesavealpha($img_new, true); } imagecopy($img_new, $this->_image, 0, 0, $left, $top, $this->_width, $this->_height); $this->_width = $width; $this->_height = $height; return ($this->_image = $img_new); } /** * 截取指定大小的块 * * @param int $length 长度 */ public function resizeCut($width = 80, $height = 80) { $left = 0; $top = 0; $width = min(ceil($width), $this->_width); $height = min(ceil($height), $this->_height); if (($this->_height / $this->_width) > ($height / $width)) { $this->resizeToWidth($width); $top = ($this->_height - $height) / 2; } else { $this->resizeToHeight($height); $left = ($this->_width - $width) / 2; } return $this->cut($width, $height, $left, $top); } /** * * @convert BMP to GD * * @param string $src * @param string|bool $dest * @return bool * */ public function bmp2gd($src, $dest = false) { /*** try to open the file for reading ***/ if (!($src_f = fopen($src, "rb"))) { return false; } /*** try to open the destination file for writing ***/ if (!($dest_f = fopen($dest, "wb"))) { return false; } /*** grab the header ***/ $header = unpack("vtype/Vsize/v2reserved/Voffset", fread( $src_f, 14)); /*** grab the rest of the image ***/ $info = unpack("Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vimportant", fread($src_f, 40)); /*** extract the header and info into varibles ***/ extract($info); extract($header); /*** check for BMP signature ***/ if ($type != 0x4D42) { return false; } /*** set the pallete ***/ $palette_size = $offset - 54; $ncolor = $palette_size / 4; $gd_header = ""; /*** true-color vs. palette ***/ $gd_header .= ($palette_size == 0) ? "\xFF\xFE" : "\xFF\xFF"; $gd_header .= pack("n2", $width, $height); $gd_header .= ($palette_size == 0) ? "\x01" : "\x00"; if ($palette_size) { $gd_header .= pack("n", $ncolor); } /*** we do not allow transparency ***/ $gd_header .= "\xFF\xFF\xFF\xFF"; /*** write the destination headers ***/ fwrite($dest_f, $gd_header); /*** if we have a valid palette ***/ if ($palette_size) { /*** read the palette ***/ $palette = fread($src_f, $palette_size); /*** begin the gd palette ***/ $gd_palette = ""; $j = 0; /*** loop of the palette ***/ while($j < $palette_size) { $b = $palette{$j++}; $g = $palette{$j++}; $r = $palette{$j++}; $a = $palette{$j++}; /*** assemble the gd palette ***/ $gd_palette .= "$r$g$b$a"; } /*** finish the palette ***/ $gd_palette .= str_repeat("\x00\x00\x00\x00", 256 - $ncolor); /*** write the gd palette ***/ fwrite($dest_f, $gd_palette); } /*** scan line size and alignment ***/ $scan_line_size = (($bits * $width) + 7) >> 3; $scan_line_align = ($scan_line_size & 0x03) ? 4 - ($scan_line_size & 0x03) : 0; /*** this is where the work is done ***/ for($i = 0, $l = $height - 1; $i < $height; $i++, $l--) { /*** create scan lines starting from bottom ***/ fseek($src_f, $offset + (($scan_line_size + $scan_line_align) * $l)); $scan_line = fread($src_f, $scan_line_size); if($bits == 24) { $gd_scan_line = ""; $j = 0; while($j < $scan_line_size) { $b = $scan_line{$j++}; $g = $scan_line{$j++}; $r = $scan_line{$j++}; $gd_scan_line .= "\x00$r$g$b"; } } elseif($bits == 8) { $gd_scan_line = $scan_line; } elseif($bits == 4) { $gd_scan_line = ""; $j = 0; while($j < $scan_line_size) { $byte = ord($scan_line{$j++}); $p1 = chr($byte >> 4); $p2 = chr($byte & 0x0F); $gd_scan_line .= "$p1$p2"; } $gd_scan_line = substr($gd_scan_line, 0, $width); } elseif($bits == 1) { $gd_scan_line = ""; $j = 0; while($j < $scan_line_size) { $byte = ord($scan_line{$j++}); $p1 = chr((int) (($byte & 0x80) != 0)); $p2 = chr((int) (($byte & 0x40) != 0)); $p3 = chr((int) (($byte & 0x20) != 0)); $p4 = chr((int) (($byte & 0x10) != 0)); $p5 = chr((int) (($byte & 0x08) != 0)); $p6 = chr((int) (($byte & 0x04) != 0)); $p7 = chr((int) (($byte & 0x02) != 0)); $p8 = chr((int) (($byte & 0x01) != 0)); $gd_scan_line .= "$p1$p2$p3$p4$p5$p6$p7$p8"; } /*** put the gd scan lines together ***/ $gd_scan_line = substr($gd_scan_line, 0, $width); } /*** write the gd scan lines ***/ fwrite($dest_f, $gd_scan_line); } /*** close the source file ***/ fclose($src_f); /*** close the destination file ***/ fclose($dest_f); return true; } /** * * @ceate a BMP image * * @param string $filename * * @return bin string on success * * @return bool false on failure * */ function imageCreateFromBmp($filename) { /*** create a temp file ***/ $tmpDir = dirname(__FILE__) . '/temp'; if (!is_dir($tmpDir)) mkdir($tmpDir, 0777, true); $tmp_name = tempnam($tmpDir, "GD"); /*** convert to gd ***/ if($this->bmp2gd($filename, $tmp_name)) { /*** create new image ***/ $img = imagecreatefromgd($tmp_name); /*** remove temp file ***/ unlink($tmp_name); /*** return the image ***/ return $img; } return false; } /** * 保存图像 * * @param string $filename 图像保存路径 */ public function save($filename) { $ext = (IMAGETYPE_JPEG === $this->_image_type) ? 'jpeg' : $this->_ext; if (!is_dir(dirname($filename))) mkdir(dirname($filename), 0777, true); if (IMAGETYPE_JPEG === $this->_image_type) { return call_user_func_array("image{$ext}", array($this->_image, $filename, 100)); } else { return call_user_func_array("image{$ext}", array($this->_image, $filename)); } } /** * 输出图像 */ public function output() { header("Content-Type: {$this->_mime}"); $ext = (IMAGETYPE_JPEG === $this->_image_type) ? 'jpeg' : $this->_ext; if (IMAGETYPE_JPEG === $this->_image_type) { return call_user_func_array("image{$ext}", array($this->_image, null, 100)); } else { return call_user_func_array("image{$ext}", array($this->_image)); } } /** * 404 */ public function err404() { $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'; $status = "{$protocol} 404 Not Found"; header($status); exit(); } } new ThumbImage(); ```