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 @@ @@ -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

137
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,65 +504,97 @@ Class SiteController extends Controller { @@ -503,65 +504,97 @@ Class SiteController extends Controller {
$img_data = null;
try {
list($naturalWidth, $naturalHeight, $imgTypeIndex, $style) = getimagesize($img_filepath);
$imgType = image_type_to_extension($imgTypeIndex);
if (class_exists('Imagick')) { //Imagick库支持
//小图片则保持原图尺寸
if ($naturalWidth <= $min_width || $naturalHeight <= $min_height) {
return false;
}
$imagick = new Imagick($img_filepath);
$naturalWidth = $imagick->getImageWidth();
$naturalHeight = $imagick->getImageHeight();
$imgType = $imagick->getImageMimeType();
//生成同比例缩略图尺寸
$width = $min_width;
$height = $min_height;
$aspect = $naturalHeight / $naturalWidth;
if ($naturalWidth <= $naturalHeight) {
$height = (int)($width * $aspect);
}else {
$width = (int)($height / $aspect);
}
//小图片则保持原图尺寸
if ($naturalWidth <= $min_width || $naturalHeight <= $min_height) {
return false;
}
$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);
//生成同比例缩略图尺寸
$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);
//小图片则保持原图尺寸
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格式的缩略图到缓存文件
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);
}
imagedestroy($dst_img);
}
}catch(Exception $e) {
$this->logError('创建缩略图失败:' . $e->getMessage());

Loading…
Cancel
Save