Cotonti / Open Source PHP Content Management FrameworkContent Management Framework

Add watermarks for any uploaded images on-the-fly

Документация / Инструкции и типовые решения / Add watermarks for any uploaded images on-the-fly

Simple guide explaining watermark adding technique

Эта страница еще не переведена на русский. Отображен оригинал страницы на английском языке. Вы можете зарегистрироваться и помочь с переводом.

Watermarks as a first-line content protection asset is required for almost any celling or commercial website. Most simple solution for this is the use of a watermark adding function for all image uploads. Watermarks, however, are often required for websited already having tens or hundreds uploads. In such cases you can use simple yet effective PHP/Apache solution.

First off you need to create a watermark, say PNG-24 watermark.png and place it in the website root folder.

Second off, create watermark.php file with the following contents:

// Insert watermarks on the fly

// Requested file path
$path = $_GET['path'];
$realpath = realpath('../../../'.$path);

// Check path and extension
if (strpos($realpath, $_SERVER['DOCUMENT_ROOT']) === FALSE || !preg_match('#\.(gif|jpeg|jpg|png)$#i', $path))
    header('HTTP/1.1 403 Forbidden');
    echo '<h1>Forbidden</h1>';

// Load the image
$image = imagecreatefromstring(file_get_contents($realpath));

$w = imagesx($image);
$h = imagesy($image);

// Load the watermark
$watermark = imagecreatefrompng('watermark.png');
$ww = imagesx($watermark);
$wh = imagesy($watermark);

// Insert watermark to the right bottom corner
imagecopy($image, $watermark, $w-$ww, $h-$wh, 0, 0, $ww, $wh);

// ... or to the image center
// imagecopy($image, $watermark, (($w/2)-($ww/2)), (($h/2)-($wh/2)), 0, 0, $ww, $wh);

// Send the image
header('Content-type: image/jpeg');

Last thing to do is adding rewrite rule for images:

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^datas/photos/page_([0-9]+)\.(gif|jpeg|jpg|png)$ watermark.php [QSA,NC]

This rule would add watermarks to all images placed in the datas/photos/ folder and complying with the specified mask.

For the servers that do not support mod_rewrite you can use Watermark plugin by Trustmaster.

1. Alexandre Ferreira  25.06.2014 13:38

Thanks: 0

Hello, thanks for sharing.

When i use this, i get always the forbiden message

In my case i have my path is

/home/aferreir/public_html/demo/uploaded_files/classified_img/    is the images folder

the watermak.php file is at /home/aferreir/public_html/demo/watermark.php

$path = $_GET['path'];  at path i must insert 'uploaded_files/classified_img/'
$realpath = realpath('../../../'.$path);  at realpath i must insert '/home/aferreir/public_html/demo/'
I dont know what i doing wrong... this dont work, please help me

Всего: 1, на странице: 1

Добавление комментариев доступно только зарегистрированным пользователям