Skip to content

Commit

Permalink
Add rotate option to calaos_picture script and use it if rotate optio…
Browse files Browse the repository at this point in the history
…n is passes in json params
  • Loading branch information
naguirre committed Apr 16, 2023
1 parent 74b1106 commit c1a9840
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 43 deletions.
26 changes: 17 additions & 9 deletions src/bin/calaos_server/JsonApiHandlerHttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,12 @@ void JsonApiHandlerHttp::processGetCover()
return;
}

string w;
string width;
if (jsonParam.Exists("width"))
w = jsonParam["width"];
width = jsonParam["width"];

string rotate;
if (jsonParam.Exists("rotate"))
player->get_album_cover([=](AudioPlayerData data)
{
//do not start another exe if one is running already
Expand All @@ -420,8 +422,8 @@ void JsonApiHandlerHttp::processGetCover()
}

string cmd = "calaos_picture " + data.svalue + " " + tempfname;
if (!w.empty())
cmd += " " + w;
if (!width.empty())
cmd += " -w " + width;

exe_thumb = uvw::Loop::getDefault()->resource<uvw::ProcessHandle>();
exe_thumb->once<uvw::ExitEvent>([this](const uvw::ExitEvent &ev, auto &h)
Expand Down Expand Up @@ -455,13 +457,20 @@ void JsonApiHandlerHttp::processGetCameraPic()
return;
}

string w;
string width;
if (jsonParam.Exists("width"))
w = jsonParam["width"];
width = jsonParam["width"];

string rotate;
if (jsonParam.Exists("rotate"))
rotate = jsonParam["rotate"];

string cmd = "calaos_picture " + camera->getPictureUrl() + " " + tempfname;
if (!w.empty())
cmd += " " + w;
if (!width.empty())
cmd += " -w " + width;

if (!rotate.empty())
cmd += " -r " + rotate;

exe_thumb = uvw::Loop::getDefault()->resource<uvw::ProcessHandle>();
exe_thumb->once<uvw::ExitEvent>([this](const uvw::ExitEvent &ev, auto &h)
Expand Down Expand Up @@ -934,4 +943,3 @@ void JsonApiHandlerHttp::processRegisterPush()
Json ret = {{ "success", registerPushToken(jsonParam)?"true":"false" }};
sendJson(ret);
}

85 changes: 51 additions & 34 deletions src/bin/tools/calaos_picture
Original file line number Diff line number Diff line change
@@ -1,62 +1,79 @@
#/bin/sh
#!/bin/bash

#Usage:
#calaos_picture <url> <filename> <width>
#calaos_picture <url> <filename> [width] [rotate]

set -e
set -x

url=$1
filename=$2
width=$3
# Parse command line arguments
OPTIONS=$(getopt -o r:w: --long rotate:,width: -- "$@")
eval set -- "$OPTIONS"

# Set default values
rotate=""
width=""

# Process options
while true; do
case "$1" in
-r|--rotate)
rotate="$2"
shift 2;;
-w|--width)
width="$2"
shift 2;;
--)
shift; break;;
*)
echo "Invalid option: $1" >&2; exit 1;;
esac
done

# Get remaining arguments
url="$1"
filename="$2"

# Download file from URL
curl -o "$filename" "$url"

showUsage()
{
echo "Usage: calaos_picture <url> <filename> [width]"
echo "Usage: calaos_picture <url> <filename> -w|--width [width] -r|--rotate [rotate]"
exit 1
}


if [ "x$url" = "x" ]; then
if [ "$url" == "" ]; then
showUsage
fi
if [ "x$filename" = "x" ]; then
if [ "$filename" == "" ]; then
showUsage
fi

if ! which curl &> /dev/null; then
if ! which curl > /dev/null 2>&1 ; then
echo "curl is not installed."
exit 1
fi

curl $url --output ${filename}_curl
curl "$url" --output "${filename}"_curl

if ! which convert &> /dev/null; then
if ! which convert > /dev/null 2>&1 ; then
echo "imagemagick is not installed. Resizing is not done."
mv ${filename}_curl ${filename}
mv "${filename}"_curl "${filename}"
exit 0
fi

if [ "x$width" = "x" ]; then
mv ${filename}_curl ${filename}
if [ "$width" == "" ] && [ "$rotate" == "" ]; then
mv "${filename}"_curl "${filename}"
exit 0
fi

convert ${filename}_curl \
-filter Triangle \
-define filter:support=2 \
-thumbnail ${width} \
-unsharp 0.25x0.25+8+0.065 \
-dither None \
-posterize 136 \
-quality 82 \
-define jpeg:fancy-upsampling=off \
-define png:compression-filter=5 \
-define png:compression-level=9 \
-define png:compression-strategy=1 \
-define png:exclude-chunk=all \
-interlace none \
-colorspace sRGB \
-strip ${filename}

rm -fr ${filename}_curl
# Resize image and rotate before resizing
if [ ! "$rotate" == "" ]; then
convert "${filename}"_curl -rotate "${rotate}" "${filename}_curl"
fi

if [ ! "$width" == "" ]; then
convert "${filename}"_curl -thumbnail "${width}" "${filename}_curl"
fi

mv "${filename}"_curl "${filename}"

0 comments on commit c1a9840

Please sign in to comment.