Browse Source

image scale support imagick lib

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

3
Dockerfile

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

4
conf/app.php

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

33
themes/beauty/controller/SiteController.php

@ -494,6 +494,7 @@ Class SiteController extends Controller { @@ -494,6 +494,7 @@ Class SiteController extends Controller {
}
//借助gd库,获取图片类型、尺寸,并实时生成缩略图
//支持imagick库
protected function createSmallJpg($img_filepath, $min_width = 100, $min_height = 100) {
//如果服务器端生成缩略图关闭
if (!empty(FSC::$app['config']['disableGenerateSmallImageInServer']) && FSC::$app['config']['disableGenerateSmallImageInServer'] !== 'false') {
@ -503,6 +504,36 @@ Class SiteController extends Controller { @@ -503,6 +504,36 @@ Class SiteController extends Controller {
$img_data = null;
try {
if (class_exists('Imagick')) { //Imagick库支持
$imagick = new Imagick($img_filepath);
$naturalWidth = $imagick->getImageWidth();
$naturalHeight = $imagick->getImageHeight();
$imgType = $imagick->getImageMimeType();
//小图片则保持原图尺寸
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);
}
$imagick->scaleImage($width, $height, true); //生成缩略图,并自适应
$imagick->setImageFormat('jpeg');
$imagick->setImageCompressionQuality(90);
$img_data = $imagick->getImageBlob();
$imagick->clear();
}else if (function_exists('gd_info')) { //gd库支持
list($naturalWidth, $naturalHeight, $imgTypeIndex, $style) = getimagesize($img_filepath);
$imgType = image_type_to_extension($imgTypeIndex);
@ -563,6 +594,8 @@ Class SiteController extends Controller { @@ -563,6 +594,8 @@ Class SiteController extends Controller {
imagedestroy($dst_img);
}
}
}catch(Exception $e) {
$this->logError('创建缩略图失败:' . $e->getMessage());
}

Loading…
Cancel
Save