Debian - Downscale Webcam in realtime for Internet Access


dropcap webcam internet

As part of a complete domotic project, I had to setup some security cams. These cams are connected thru ethernet or wifi to a domoticz server. They are HD cams, able to serve 1280×720 high quality JPG pictures.

As they are used for motion detection purpose, it's important to keep the picture quality as high as possible for investigation purpose. But on the other side, as they should be available from anywhere on a supervision console, they should also be available thru internet with a resolution compatible with internet limited bandwidth.

Solution to this problem is to allow :

  • full resolution picture while served on the LAN
  • real time picture transcoding if served thru Internet

This article explains how to setup an Apache web server to handle realtime transcoding of some declared security cams images so that their dimension and quality level are adjusted to internet transmission. As a result, your security system will get high quality images on your LAN and optimized size and quality if served thru Internet.

It has been tested on a Debian 8 / Apache 2.4 server and with Dlink DCS-935L webcam. But it should be easily adapted to any other Linux distro or any IP webcam.

1. Principle

Any IP webcam provides a URL allowing to visualize realtime picture. This URL is usually something like http://webcam.ip.address/image.cgi and can be used by motion detection software.

In case of motion detection, corresponding images are saved for further investigation purpose. Because of this motion detection treatment, it is important for the cam to serve images with the best possible resolution & quality.

For example, on a Dlink DCS-935L, image is available thru /cgi/jpg/image.cgi and its size and quality can be configured on the webcam administration interface.

But for supervision purpose, it is important to also get a lower quality image, compatible with low internet bandwidth.

As most webcams can only serve one stream quality, an elegant solution is to use some simple tools to rescale image resolution in realtime when served thru a web server.

As the lower quality should be available thru internet, this transcoding should be done on demand straigh from an Apache web server. To be more flexible, transcoding parameters (size, quality & file format) should be part of the requested URL.

In this article, the canvas chosen is: CamName_width_height_quality.format where

  • width is image max width in pixels
  • height is image max height in pixels
  • quality is image compression quality (0..100 for jpeg images)
  • format is image extension (png, jpg, gif, …)

Transcoding should be done according to all these parameters.

As an example, when you call, you are asking to serve realtime image from cam01 in jpg format, with 640×480 resolution and 75 compression quality.

With this example, you typically get :

Stream Resolution Quality Size
Original webcam image 1280×720 Best quality ~200 kB
Website served image 640×480 Quality 75 ~25 kB


Your webcam image will be served 8 times faster.

This realtime transcoding can be done in 3 simple steps :

  1. install needed tools
  2. setup a PHP page in charge of collecting transcoding parameters and serving resulting image
  3. configure apache to serve a list of webcams thru URL rewriting

2. Instalation of tools

First step is to make sure that your Apache2 server is installed with PHP enabled.

As transcoding will be done thru some URL rewritring, you also need to enable module mod_rewrite (which comes with Apache2 default installation) :

# a2enmod rewrite
# service apache2 restart

Then, you sould install convert from imagemacick. This fantastic tool will be in charge of handling the realtime image scaling and compression. It can be easily installed under Debian :

# apt-get install imagemagick

3. Transcoding script

The script in charge of the image conversion is a very simple PHP script.

It takes few parameters :

  • cam : webcam local URL as seen from the web server
  • width : width of resulting image in pixels
  • height : height of resulting image in pixels
  • quality : compression quality to use
  • format : resulting image extension

// ---------------------------------------
// Webcam image scaling and compression
// This script handles real time resize and compression
// Parameters :
//   cam  - URL of webcam image on the LAN
//   width - maximum width of resized image
//   height - maximum height of resized image
//   quality - quality of recompressed image
//   format - format of new image (jgp, png, gif, ...)
// Revision history :
//   17/01/2016 - V1.0 - Creation by N. Bernaerts
// ---------------------------------------

// parse URL parameters and create variables
if (isset($_SERVER['QUERY_STRING'])) parse_str ($_SERVER['QUERY_STRING']);
// calculate webcam URL
$url_cam = escapeshellarg("http://{$cam}");
// send jpeg file header
header("Content-type: image/{$format}");
// send file conversion result
passthru("convert -quality {$quality} -geometry '{$width}x{$height}' {$url_cam} {$format}:-");

4. Apache rewrite rules

For security purpose, all available cams are declared thru some rewrite rules.

This allows to hide CAMs local IP adressing and to strictly control the number of cams available from Internet.

Every exposed cam should be declared in an Apache rewrite rule, which will allow to collect :

  • width and height of the image
  • compression ratio
  • file format

With all these data, the rule calls the resize script and serve resulting image.

Rules should be placed in .htaccess at the root of your web server.

# ---------------------------------------------------------------------
# Example of URL rewriting for realtime webcam size & quality downscale
# ---------------------------------------------------------------------


# enable URL rewriting
RewriteEngine On

# declaration of webcams available thru internet
RewriteRule ^cams\/cam01_([0-9]+)_([0-9]+)_([0-9]+)\.([a-z]+)$ scripts/cam-resize.php?cam=\/cgi\/jpg\/image.cgi&width=$1&height=$2&quality=$3&format=$4
RewriteRule ^cams\/cam02_([0-9]+)_([0-9]+)_([0-9]+)\.([a-z]+)$ scripts/cam-resize.php?cam=\/image\/jpg.cgi&width=$1&height=$2&quality=$3&format=$4


5. Test connexion

Everything is now setup to allow internet access to your transcoded webcam images.

In this example, if you want a 640×480 image, 75 quality jpeg, both cam are accessible at :

Internet URL LAN URL


If you need to restrict access to authentified users only, you can set some Apache authentification rules for the specific /cams/… location.

Here is an example of Basic authentification (which is not the most secure …) :



<Location /cams/>
  AuthType Basic
  AuthName "Access this page is restricted, please login"
  AuthUserFile ...
  Require user ...


Once setup in your site configuration file, you will be prompted for login/password when you access your webcam URL.

Hope it helps.

Signature Technoblog

This article is published "as is", without any warranty that it will work for your specific need.
If you think this article needs some complement, or simply if you think it saved you lots of time & trouble,
just let me know at This email address is being protected from spambots. You need JavaScript enabled to view it.. Cheers !

icon linux icon debian icon apache icon mysql icon php icon piwik icon googleplus