Skip to content

Instantly share code, notes, and snippets.

@mklooss
Last active December 9, 2024 06:47
Show Gist options
  • Save mklooss/7dc74a417238c00a878ac5da80f254af to your computer and use it in GitHub Desktop.
Save mklooss/7dc74a417238c00a878ac5da80f254af to your computer and use it in GitHub Desktop.
webp
# inside of http {}, outside of server
# may create file: /etc/nginx/conf.d/webp.conf
map $http_accept $webp_suffix {
default "";
"~*webp" ".webp";
}
# inside server {}
# /etc/nginx/sites.d/*.conf
location ^~ /thumbnail/ {
expires 1y;
add_header Pragma public;
add_header Cache-Control "public";
add_header X-OUPUT static;
try_files /webp/$uri$webp_suffix $uri =404;
}
location ^~ /media/ {
expires 1y;
add_header Pragma public;
add_header Cache-Control "public";
add_header X-OUPUT static;
try_files /webp/$uri$webp_suffix $uri =404;
}
# WebP
15 */1 * * * bash /home/USER/bin/webpconvertOnlyNewFiles.sh > /dev/null
30 23 * * 7 bash /home/USER/bin/webpconvertComplete.sh > /dev/null
# BOF: WEBP
AddType image/webp .webp
RewriteCond %{REQUEST_URI} (thumbnail|media)
RewriteCond %{HTTP_ACCEPT} image/webp
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} \.(jpg|png)$ [NC]
RewriteCond %{DOCUMENT_ROOT}/webp%{REQUEST_URI}.webp -f
RewriteRule ^ /webp%{REQUEST_URI}.webp [L,T=image/webp]
# EOF: WEBP
<?php
$home = getenv("HOME");
$configSqlite = $home.DIRECTORY_SEPARATOR.'.config'.DIRECTORY_SEPARATOR.'loewebp'.DIRECTORY_SEPARATOR.'checksum.db';
$isNew = false;
if (!file_exists($configSqlite))
{
$isNew = true;
if (!is_dir(dirname($configSqlite)))
{
var_dump(dirname($configSqlite));
mkdir(dirname($configSqlite), 0755, true);
}
}
$db = new \SQLite3($configSqlite, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE);
if ($isNew)
{
$db->exec('CREATE TABLE checksum (filepath TEXT, filepathsum VARCHAR(255), checksum VARCHAR(255))');
}
$shortopts = "";
$shortopts .= "f:";
$shortopts .= "o:";
$longopts = array(
"file:",
"output:",
"option", // No value
"opt", // No value
);
$options = getopt($shortopts);
$file = $options['f'] ?? NULL;
$output = $options['o'] ?? NULL;
if (is_null($output))
{
$output = $file.'.webp';
}
$file = realpath($file);
if (file_exists($output))
{
$output = realpath($output);
}
$filenamehash = hash('sha256', $file);
$file_hash = hash_file('sha256', $file);
$exists = false;
$statement = $db->prepare('SELECT checksum, filepathsum FROM checksum WHERE filepathsum = :filepathsum LIMIT 1');
$statement->bindValue(':filepathsum', $filenamehash);
$results = $statement->execute();
while ($row = $results->fetchArray()) {
if ($row['checksum'] === $file_hash)
{
$exists = true;
}
}
if (is_string($output) && substr($output, -5) !== '.webp')
{
$output = $output.'.webp';
}
if (!file_exists($output))
{
$exists = false;
}
if (!$exists)
{
if (!is_dir(dirname($output)))
{
mkdir(dirname($output), 0755, true);
}
echo "create webp bcz, does not exists\n";
$imagick = new \Imagick($file);
if ($imagick->getImageColorspace() != \Imagick::COLORSPACE_SRGB) {
$imagick->transformimagecolorspace(\Imagick::COLORSPACE_SRGB);
}
$imagick->stripImage();
$imagick->writeImage($output);
$output = realpath($output);
$statement = $db->prepare("DELETE FROM checksum WHERE filepathsum = :filepathsum");
$statement->bindValue(':filepathsum', $filenamehash);
$statement->execute();
$statement = $db->prepare("INSERT INTO checksum (filepath, filepathsum, checksum) values (:filepath, :filepathsum, :checksum)");
$statement->bindValue(':filepath', $file);
$statement->bindValue(':filepathsum', $filenamehash);
$statement->bindValue(':checksum', $file_hash);
$statement->execute();
echo "$file -> $output\n";
} else {
touch($output);
echo "already exists\n";
}
#!/bin/bash
# File Location: /home/USER/bin/webpconvert.sh
FILEPATH=$(echo $1)
echo "$FILEPATH"
DIRNAME=$(dirname "$FILEPATH")
BASEPATH=$(basename "$FILEPATH")
NEWDIR="webp/${DIRNAME}"
echo " "
echo $DIRNAME
echo $BASEPATH
mkdir -p "webp/$DIRNAME"
echo " "
cwebp "$FILEPATH" -o "${NEWDIR}/${BASEPATH}.webp"
# alternative
# php webpconvert.php -f "$FILEPATH" -o "${NEWDIR}/${BASEPATH}.webp"
#!/bin/bash
# File Location: /home/USER/bin/webpconvertComplete.sh
# Shopware 6 Sample!
cd /home/USER/htdocs/public && find thumbnail/ -type f -iname "*.png" -exec webpconvert.sh "{}" \;
cd /home/USER/htdocs/public && find thumbnail/ -type f -iname "*.jpeg" -exec webpconvert.sh "{}" \;
cd /home/USER/htdocs/public && find thumbnail/ -type f -iname "*.jpg" -exec webpconvert.sh "{}" \;
cd /home/USER/htdocs/public && find media/ -type f -iname "*.png" -exec webpconvert.sh "{}" \;
cd /home/USER/htdocs/public && find media/ -type f -iname "*.jpeg" -exec webpconvert.sh "{}" \;
cd /home/USER/htdocs/public && find media/ -type f -iname "*.jpg" -exec webpconvert.sh "{}" \;
find /home/USER/htdocs/public/webp -type f -mtime +2 -exec rm -f "{}" \;
#!/bin/bash
# File Location: /home/USER/bin/webpconvertOnlyNewFiles.sh
# Shopware 6 Sample!
cd /home/USER/htdocs/public && find thumbnail/ -newermt '2 hour ago' -type f -iname "*.png" -exec webpconvert.sh "{}" \;
cd /home/USER/htdocs/public && find thumbnail/ -newermt '2 hour ago' -type f -iname "*.jpeg" -exec webpconvert.sh "{}" \;
cd /home/USER/htdocs/public && find thumbnail/ -newermt '2 hour ago' -type f -iname "*.jpg" -exec webpconvert.sh "{}" \;
cd /home/USER/htdocs/public && find media/ -newermt '2 hour ago' -type f -iname "*.png" -exec webpconvert.sh "{}" \;
cd /home/USER/htdocs/public && find media/ -newermt '2 hour ago' -type f -iname "*.jpeg" -exec webpconvert.sh "{}" \;
cd /home/USER/htdocs/public && find media/ -newermt '2 hour ago' -type f -iname "*.jpg" -exec webpconvert.sh "{}" \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment