refactor(web/image_hosting): use php-imagick
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Baoshuo Ren 2022-10-13 21:23:52 +08:00
parent 2e51c20d9f
commit d490de8bd4
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
3 changed files with 14 additions and 12 deletions

View File

@ -6,7 +6,7 @@ ENV DEBIAN_FRONTEND=noninteractive
RUN dpkg -s gnupg 2>/dev/null || (apt-get update && apt-get install -y gnupg) &&\ RUN dpkg -s gnupg 2>/dev/null || (apt-get update && apt-get install -y gnupg) &&\
echo "deb http://ppa.launchpad.net/ondrej/php/ubuntu jammy main" | tee /etc/apt/sources.list.d/ondrej-php.list && apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 4F4EA0AAE5267A6C &&\ echo "deb http://ppa.launchpad.net/ondrej/php/ubuntu jammy main" | tee /etc/apt/sources.list.d/ondrej-php.list && apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 4F4EA0AAE5267A6C &&\
apt-get update --allow-unauthenticated &&\ apt-get update --allow-unauthenticated &&\
apt-get install -y --allow-unauthenticated -o Dpkg::Options::="--force-overwrite" php7.4 php7.4-yaml php7.4-xml php7.4-dev php7.4-zip php7.4-mysql php7.4-mbstring php7.4-gd libseccomp-dev git vim ntp zip unzip curl wget libapache2-mod-xsendfile mysql-server php-pear cmake fp-compiler re2c libyaml-dev python2.7 python3.10 python3-requests openjdk-8-jdk openjdk-11-jdk openjdk-17-jdk apt-get install -y --allow-unauthenticated -o Dpkg::Options::="--force-overwrite" php7.4 php7.4-yaml php7.4-xml php7.4-dev php7.4-zip php7.4-mysql php7.4-mbstring php7.4-gd php7.4-imagick libseccomp-dev git vim ntp zip unzip curl wget libapache2-mod-xsendfile mysql-server php-pear cmake fp-compiler re2c libyaml-dev python2.7 python3.10 python3-requests openjdk-8-jdk openjdk-11-jdk openjdk-17-jdk
ADD . /opt/uoj ADD . /opt/uoj
WORKDIR /opt/uoj WORKDIR /opt/uoj

View File

@ -54,6 +54,7 @@
list($width, $height, $type) = $size; list($width, $height, $type) = $size;
$hash = hash_file("sha256", $_FILES['image_upload_file']['tmp_name']); $hash = hash_file("sha256", $_FILES['image_upload_file']['tmp_name']);
$scale = ceil($width / 750.0);
$watermark_text = UOJConfig::$data['profile']['oj-name-short']; $watermark_text = UOJConfig::$data['profile']['oj-name-short'];
if (isSuperUser($myUser) && $_POST['watermark'] == 'no_watermark') { if (isSuperUser($myUser) && $_POST['watermark'] == 'no_watermark') {
@ -70,16 +71,17 @@
die(json_encode(['status' => 'success', 'path' => $existing_image['path']])); die(json_encode(['status' => 'success', 'path' => $existing_image['path']]));
} }
$img = imagecreatefromstring(file_get_contents($_FILES["image_upload_file"]["tmp_name"])); $image = new Imagick($_FILES["image_upload_file"]["tmp_name"]);
$white = imagecolorallocatealpha($img, 255, 255, 255, 30); $draw = new ImagickDraw();
$black = imagecolorallocatealpha($img, 50, 50, 50, 70); $draw->setFont(UOJContext::documentRoot().'/fonts/roboto-mono/RobotoMono-Bold.ttf');
$scale = ceil($width / 750.0); $draw->setFontSize($scale * 14);
$draw->setGravity(Imagick::GRAVITY_SOUTHEAST);
imagettftext($img, strval($scale * 16), 0, ($scale * 16) + $scale, max(0, $height - ($scale * 16) + 5) + $scale, $black, UOJContext::documentRoot().'/fonts/roboto-mono/RobotoMono-Bold.ttf', $watermark_text); $draw->setFillColor("rgba(100,100,100,0.5)");
imagefilter($img, IMG_FILTER_GAUSSIAN_BLUR); $image->annotateImage($draw, 15, 10, 0, $watermark_text);
imagettftext($img, strval($scale * 16), 0, ($scale * 16), max(0, $height - ($scale * 16) + 5), $white, UOJContext::documentRoot().'/fonts/roboto-mono/RobotoMono-Bold.ttf', $watermark_text); $draw->setFillColor("rgba(255,255,255,0.65)");
imagepng($img, $_FILES["image_upload_file"]["tmp_name"]); $image->annotateImage($draw, 15 + $scale, 10 + $scale, 0, $watermark_text);
imagedestroy($img); $image->setImageFormat('png');
$image->writeImage();
if (($size = filesize($_FILES["image_upload_file"]["tmp_name"])) > 5242880) { // 5 MB if (($size = filesize($_FILES["image_upload_file"]["tmp_name"])) > 5242880) { // 5 MB
throwError('too_large'); throwError('too_large');

View File

@ -20,7 +20,7 @@ getAptPackage(){
dpkg -s gnupg 2>/dev/null || (apt-get update && apt-get install -y gnupg) dpkg -s gnupg 2>/dev/null || (apt-get update && apt-get install -y gnupg)
echo "deb http://ppa.launchpad.net/ondrej/php/ubuntu jammy main" | tee /etc/apt/sources.list.d/ondrej-php.list && apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 4F4EA0AAE5267A6C echo "deb http://ppa.launchpad.net/ondrej/php/ubuntu jammy main" | tee /etc/apt/sources.list.d/ondrej-php.list && apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 4F4EA0AAE5267A6C
apt-get update --allow-unauthenticated apt-get update --allow-unauthenticated
apt-get install -y --allow-unauthenticated -o Dpkg::Options::="--force-overwrite" php7.4 php7.4-yaml php7.4-xml php7.4-dev php7.4-zip php7.4-mysql php7.4-mbstring php7.4-gd libseccomp-dev git vim ntp zip unzip curl wget libapache2-mod-xsendfile mysql-server php-pear cmake fp-compiler re2c libyaml-dev python2.7 python3.10 python3-requests openjdk-8-jdk openjdk-11-jdk openjdk-17-jdk apt-get install -y --allow-unauthenticated -o Dpkg::Options::="--force-overwrite" php7.4 php7.4-yaml php7.4-xml php7.4-dev php7.4-zip php7.4-mysql php7.4-mbstring php7.4-gd php7.4-imagick libseccomp-dev git vim ntp zip unzip curl wget libapache2-mod-xsendfile mysql-server php-pear cmake fp-compiler re2c libyaml-dev python2.7 python3.10 python3-requests openjdk-8-jdk openjdk-11-jdk openjdk-17-jdk
} }
setLAMPConf(){ setLAMPConf(){