From d490de8bd45335553ced2dc91f314a7a93575dc8 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 13 Oct 2022 21:23:52 +0800 Subject: [PATCH] refactor(web/image_hosting): use php-imagick --- web/Dockerfile | 2 +- web/app/controllers/image_hosting/index.php | 22 +++++++++++---------- web/install.sh | 2 +- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/web/Dockerfile b/web/Dockerfile index 9b16e68..75a5dbe 100644 --- a/web/Dockerfile +++ b/web/Dockerfile @@ -6,7 +6,7 @@ ENV DEBIAN_FRONTEND=noninteractive 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 &&\ 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 WORKDIR /opt/uoj diff --git a/web/app/controllers/image_hosting/index.php b/web/app/controllers/image_hosting/index.php index 3b87372..c7682cd 100644 --- a/web/app/controllers/image_hosting/index.php +++ b/web/app/controllers/image_hosting/index.php @@ -54,6 +54,7 @@ list($width, $height, $type) = $size; $hash = hash_file("sha256", $_FILES['image_upload_file']['tmp_name']); + $scale = ceil($width / 750.0); $watermark_text = UOJConfig::$data['profile']['oj-name-short']; if (isSuperUser($myUser) && $_POST['watermark'] == 'no_watermark') { @@ -70,16 +71,17 @@ die(json_encode(['status' => 'success', 'path' => $existing_image['path']])); } - $img = imagecreatefromstring(file_get_contents($_FILES["image_upload_file"]["tmp_name"])); - $white = imagecolorallocatealpha($img, 255, 255, 255, 30); - $black = imagecolorallocatealpha($img, 50, 50, 50, 70); - $scale = ceil($width / 750.0); - - 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); - imagefilter($img, IMG_FILTER_GAUSSIAN_BLUR); - 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); - imagepng($img, $_FILES["image_upload_file"]["tmp_name"]); - imagedestroy($img); + $image = new Imagick($_FILES["image_upload_file"]["tmp_name"]); + $draw = new ImagickDraw(); + $draw->setFont(UOJContext::documentRoot().'/fonts/roboto-mono/RobotoMono-Bold.ttf'); + $draw->setFontSize($scale * 14); + $draw->setGravity(Imagick::GRAVITY_SOUTHEAST); + $draw->setFillColor("rgba(100,100,100,0.5)"); + $image->annotateImage($draw, 15, 10, 0, $watermark_text); + $draw->setFillColor("rgba(255,255,255,0.65)"); + $image->annotateImage($draw, 15 + $scale, 10 + $scale, 0, $watermark_text); + $image->setImageFormat('png'); + $image->writeImage(); if (($size = filesize($_FILES["image_upload_file"]["tmp_name"])) > 5242880) { // 5 MB throwError('too_large'); diff --git a/web/install.sh b/web/install.sh index c385272..cff3f28 100644 --- a/web/install.sh +++ b/web/install.sh @@ -20,7 +20,7 @@ getAptPackage(){ 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 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(){