Merge pull request 'Migrate to Alpine' (#5) from development into master
All checks were successful
/ docker (push) Successful in 29s

Reviewed-on: #5
This commit is contained in:
Lee 2023-07-07 23:54:10 +00:00
commit f3febd737d
4 changed files with 33 additions and 39 deletions

@ -1,12 +1,10 @@
FROM ubuntu:23.04
FROM alpine:3.18.2
# Install dependencies
RUN apt update
RUN DEBIAN_FRONTEND=noninteractive \
apt install nginx php8.1 php8.1-fpm php8.1-gd php8.1-imagick -y
# Clean up
RUN apt clean
RUN apk update && \
apk upgrade && \
apk add --no-cache nginx php81 php81-fpm php81-gd && \
rm -rf /var/cache/apk/*
# Set up nginx
COPY ./docker/nginx.conf /etc/nginx/nginx.conf
@ -16,4 +14,4 @@ COPY ./upload.php /tmp/upload.php
COPY ./docker/start.sh /start.sh
# Start server
CMD ["bash", "/start.sh"]
CMD ["sh", "/start.sh"]

@ -44,7 +44,7 @@ http {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_pass unix:/run/php/php.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;

@ -1,5 +1,9 @@
if [[ -z "${MAX_UPLOAD_SIZE}" ]]; then
#!/bin/sh
# TODO: add all the other fallback values for the other variables
if [ -z "$MAX_UPLOAD_SIZE" ]; then
MAX_UPLOAD_SIZE="8M" # Default fallback value
echo "MAX_UPLOAD_SIZE was not set, using default value of ${MAX_UPLOAD_SIZE}"
fi
echo "Checking if upload script exists in /var/www/html"
@ -11,8 +15,15 @@ else
fi
# Letting php know that we are running in docker
echo "env[DOCKER] = true" >> /etc/php/8.1/fpm/pool.d/www.conf
echo "clear_env = no" >> /etc/php/8.1/fpm/pool.d/www.conf
echo "env[DOCKER] = true" >> /etc/php81/php-fpm.d/www.conf
echo "clear_env = no" >> /etc/php81/php-fpm.d/www.conf
# Create the directory for PHP socket
mkdir -p /run/php
# Set php-fpm to listen on socket
touch /run/php/php.sock
sed -i 's/^listen = .*/listen = \/run\/php\/php.sock/' /etc/php81/php-fpm.d/www.conf
echo "Setting permissions for upload script"
chmod 777 /var/www/html/upload.php
@ -20,16 +31,16 @@ chmod 777 /var/www/html/upload.php
echo "Setting max upload size to ${MAX_UPLOAD_SIZE}"
# Set max upload size for php
sed -i "s/^upload_max_filesize = .*/upload_max_filesize = ${MAX_UPLOAD_SIZE}/" /etc/php/8.1/fpm/php.ini
sed -i "s/^post_max_size = .*/post_max_size = ${MAX_UPLOAD_SIZE}/" /etc/php/8.1/fpm/php.ini
sed -i "s/^upload_max_filesize = .*/upload_max_filesize = ${MAX_UPLOAD_SIZE}/" /etc/php81/php.ini
sed -i "s/^post_max_size = .*/post_max_size = ${MAX_UPLOAD_SIZE}/" /etc/php81/php.ini
# Set max upload size for nginx
sed -i "s/client_max_body_size 500M;/client_max_body_size ${MAX_UPLOAD_SIZE};/" /etc/nginx/nginx.conf
function start() {
echo "Starting PHP & Nginx"
/etc/init.d/php8.1-fpm start &&
chmod 777 /run/php/php8.1-fpm.sock &&
php-fpm81 &&
chmod 777 /run/php/php.sock &&
nginx -g 'daemon off;'
}

@ -162,28 +162,13 @@ try {
// Check the file type and size
if ($shouldConvertToWebp && in_array($fileType, ["png", "jpeg", "jpg"]) && $_FILES["sharex"]["size"] > $webpThreadhold) {
// Create an Imagick object from the uploaded file
$image = new Imagick($_FILES["sharex"]["tmp_name"]);
// Convert the image to WebP
$image->setImageFormat("webp");
$image->setImageCompressionQuality($webpQuality);
// Set the output filename
$image = imagecreatefromstring(file_get_contents($_FILES["sharex"]["tmp_name"]));
$webp_file = pathinfo($finalName, PATHINFO_FILENAME) . ".webp";
// Save the converted image
$image->writeImage($webp_file);
// Free up memory
$image->clear();
$image->destroy();
$fileSize = filesize($webp_file); // Update the file size
// Update the final filename
imagewebp($image, $webp_file, 90); // Convert the image and save it
imagedestroy($image); // Free up memory
$finalName = $webp_file;
$needsToBeSaved = false;
$shouldSave = false;
$fileSize = filesize($webp_file); // Update the file size
}
if ($needsToBeSaved) { // Save the file if it has not been saved yet