Browse Source

image scale support imagick lib

master
filesite 4 weeks ago
parent
commit
ecc169583a
  1. 3
      Dockerfile
  2. 4
      conf/app.php
  3. 137
      themes/beauty/controller/SiteController.php

3
Dockerfile

@ -1,6 +1,5 @@
FROM php:8.2-fpm-alpine3.20 FROM php:8.2-fpm-alpine3.20
RUN apk add autoconf gcc musl-dev make imagemagick-dev \ RUN apk add autoconf gcc musl-dev make imagemagick-dev rsync nginx zlib-dev libpng-dev freetype-dev libjpeg-turbo-dev libwebp-dev \
&& apk add rsync nginx zlib-dev libpng-dev freetype-dev libjpeg-turbo-dev libwebp-dev \
&& docker-php-ext-configure gd --with-freetype --with-jpeg --with-webp \ && docker-php-ext-configure gd --with-freetype --with-jpeg --with-webp \
&& docker-php-ext-install -j$(nproc) gd \ && docker-php-ext-install -j$(nproc) gd \
&& pecl install imagick && docker-php-ext-enable imagick \ && pecl install imagick && docker-php-ext-enable imagick \

4
conf/app.php

@ -3,8 +3,8 @@
* Config * Config
*/ */
$configs = array( $configs = array(
'version' => '0.3.7', 'version' => '0.3.8',
'releaseDate' => '2024-10-22', 'releaseDate' => '2024-10-31',
'showVersion' => false, //默认不显示版本号和发布日期 'showVersion' => false, //默认不显示版本号和发布日期
'default_timezone' => 'Asia/Hong_Kong', //timezone, check more: https://www.php.net/manual/en/timezones.asia.php 'default_timezone' => 'Asia/Hong_Kong', //timezone, check more: https://www.php.net/manual/en/timezones.asia.php

137
themes/beauty/controller/SiteController.php

@ -494,6 +494,7 @@ Class SiteController extends Controller {
} }
//借助gd库,获取图片类型、尺寸,并实时生成缩略图 //借助gd库,获取图片类型、尺寸,并实时生成缩略图
//支持imagick库
protected function createSmallJpg($img_filepath, $min_width = 100, $min_height = 100) { protected function createSmallJpg($img_filepath, $min_width = 100, $min_height = 100) {
//如果服务器端生成缩略图关闭 //如果服务器端生成缩略图关闭
if (!empty(FSC::$app['config']['disableGenerateSmallImageInServer']) && FSC::$app['config']['disableGenerateSmallImageInServer'] !== 'false') { if (!empty(FSC::$app['config']['disableGenerateSmallImageInServer']) && FSC::$app['config']['disableGenerateSmallImageInServer'] !== 'false') {
@ -503,65 +504,97 @@ Class SiteController extends Controller {
$img_data = null; $img_data = null;
try { try {
list($naturalWidth, $naturalHeight, $imgTypeIndex, $style) = getimagesize($img_filepath); if (class_exists('Imagick')) { //Imagick库支持
$imgType = image_type_to_extension($imgTypeIndex);
//小图片则保持原图尺寸 $imagick = new Imagick($img_filepath);
if ($naturalWidth <= $min_width || $naturalHeight <= $min_height) { $naturalWidth = $imagick->getImageWidth();
return false; $naturalHeight = $imagick->getImageHeight();
} $imgType = $imagick->getImageMimeType();
//生成同比例缩略图尺寸 //小图片则保持原图尺寸
$width = $min_width; if ($naturalWidth <= $min_width || $naturalHeight <= $min_height) {
$height = $min_height; return false;
$aspect = $naturalHeight / $naturalWidth; }
if ($naturalWidth <= $naturalHeight) {
$height = (int)($width * $aspect);
}else {
$width = (int)($height / $aspect);
}
$imgSource = null; //生成同比例缩略图尺寸
switch ($imgType) { $width = $min_width;
case '.jpeg': $height = $min_height;
$imgSource = imagecreatefromjpeg($img_filepath); $aspect = $naturalHeight / $naturalWidth;
break; if ($naturalWidth <= $naturalHeight) {
case '.png': $height = (int)($width * $aspect);
$imgSource = imagecreatefrompng($img_filepath); }else {
break; $width = (int)($height / $aspect);
case '.gif': }
$imgSource = imagecreatefromgif($img_filepath);
break; $imagick->scaleImage($width, $height, true); //生成缩略图,并自适应
case '.webp': $imagick->setImageFormat('jpeg');
if (function_exists('imagecreatefromwebp')) { $imagick->setImageCompressionQuality(90);
$imgSource = imagecreatefromwebp($img_filepath); $img_data = $imagick->getImageBlob();
} $imagick->clear();
break;
case '.bmp': }else if (function_exists('gd_info')) { //gd库支持
if (function_exists('imagecreatefrombmp')) {
$imgSource = imagecreatefrombmp($img_filepath); list($naturalWidth, $naturalHeight, $imgTypeIndex, $style) = getimagesize($img_filepath);
$imgType = image_type_to_extension($imgTypeIndex);
//小图片则保持原图尺寸
if ($naturalWidth <= $min_width || $naturalHeight <= $min_height) {
return false;
}
//生成同比例缩略图尺寸
$width = $min_width;
$height = $min_height;
$aspect = $naturalHeight / $naturalWidth;
if ($naturalWidth <= $naturalHeight) {
$height = (int)($width * $aspect);
}else {
$width = (int)($height / $aspect);
}
$imgSource = null;
switch ($imgType) {
case '.jpeg':
$imgSource = imagecreatefromjpeg($img_filepath);
break;
case '.png':
$imgSource = imagecreatefrompng($img_filepath);
break;
case '.gif':
$imgSource = imagecreatefromgif($img_filepath);
break;
case '.webp':
if (function_exists('imagecreatefromwebp')) {
$imgSource = imagecreatefromwebp($img_filepath);
}
break;
case '.bmp':
if (function_exists('imagecreatefrombmp')) {
$imgSource = imagecreatefrombmp($img_filepath);
}
break;
}
//保存base64格式的缩略图到缓存文件
if (!empty($imgSource)) {
//方法1: 使用imagecopyresampled复制部分图片
//$dst_img = imagecreatetruecolor($width, $height);
//$copy_done = imagecopyresampled($dst_img, $imgSource, 0, 0, 0, 0, $width, $height, $naturalWidth, $naturalHeight);
//方法2: 直接缩小图片
$dst_img = imagescale($imgSource, $width, $height, IMG_CATMULLROM);
$copy_done = !empty($dst_img) ? true : false;
if ($copy_done) {
ob_start();
$quality = !empty(FSC::$app['config']['smallImageQuality']) ? FSC::$app['config']['smallImageQuality'] : 90;
imagejpeg($dst_img, null, $quality);
$img_data = ob_get_clean();
ob_end_clean();
} }
break;
}
//保存base64格式的缩略图到缓存文件 imagedestroy($dst_img);
if (!empty($imgSource)) {
//方法1: 使用imagecopyresampled复制部分图片
//$dst_img = imagecreatetruecolor($width, $height);
//$copy_done = imagecopyresampled($dst_img, $imgSource, 0, 0, 0, 0, $width, $height, $naturalWidth, $naturalHeight);
//方法2: 直接缩小图片
$dst_img = imagescale($imgSource, $width, $height, IMG_CATMULLROM);
$copy_done = !empty($dst_img) ? true : false;
if ($copy_done) {
ob_start();
$quality = !empty(FSC::$app['config']['smallImageQuality']) ? FSC::$app['config']['smallImageQuality'] : 90;
imagejpeg($dst_img, null, $quality);
$img_data = ob_get_clean();
ob_end_clean();
} }
imagedestroy($dst_img);
} }
}catch(Exception $e) { }catch(Exception $e) {
$this->logError('创建缩略图失败:' . $e->getMessage()); $this->logError('创建缩略图失败:' . $e->getMessage());

Loading…
Cancel
Save