pwet

Sign in or create your account | Project List | Help | pwet.fr | Planet eZ Publish.fr | Bioutifoul Photos

pwet Svn Source Tree

Root/trunk/www/extension/pwet2/index_hotlink.php

  • Property svn:mergeinfo set to:
1<?php
2chdir('../..');
3require 'autoload.php';
4
5define('IMAGE_CACHE_DIR', 'var/cache/tag_images/');
6define('TTF_FONT', 'extension/pwet2/share/Trebuchet_MS.ttf');
7define('IMG_TEXTE', 'Image sous Licence CC-By-Sa - http://pwet.fr/photos');
8define('BASE_FONT_SIZE', 25); // font size pour 1000px large
9define('LOG_FILE', 'hotlink.log');
10
11if ( !array_key_exists('img', $_GET) )
12    exit();
13$image = $_GET['img'];
14if ( $image{0} == '/' )
15    $image = substr($image, 1);
16if ( !is_image($image) )
17    exit();
18//$log = new eZLog();
19eZLog::write('# '.$_GET['img'].' # '.$_GET['ref'], LOG_FILE);
20tag_image($image, IMG_TEXTE);
21
22function tag_image($image, $message)
23{
24    $tagImageName = IMAGE_CACHE_DIR.md5($image).'.jpg';
25    if ( !file_exists($tagImageName) )
26    {
27        if ( !is_dir(IMAGE_CACHE_DIR) )
28        {
29            mkdir(IMAGE_CACHE_DIR);
30        }
31        $imgInfo = getImageSize($image);
32        $imgRessource = imageCreateFromJPEG($image);
33        $color = imageColorAllocate($imgRessource, 255, 255, 255);
34        $fontSize = get_font_size($imgInfo[0]);
35        $coordInfo = get_start_coordinates($imgInfo[0], $imgInfo[1], $fontSize, $message);
36        imageTTFText($imgRessource, $fontSize, 0, $coordInfo['x'], $coordInfo['y'], $color, TTF_FONT, $message);
37        imageJPEG($imgRessource, $tagImageName);
38    }
39    header('Content-type: image/jpg');
40    header('Content-length: '.filesize($tagImageName));
41    readfile($tagImageName);
42}
43
44function get_font_size($width)
45{
46    return ceil($width*BASE_FONT_SIZE/1000);
47}
48
49function get_start_coordinates($imgWith, $imgHeight, $fontSize, $text)
50{
51    $result = array();
52    $boxInfo = imageTTFBBox($fontSize, 0, TTF_FONT, $text);
53    $widthBox = $boxInfo[2] - $boxInfo[0];
54    $heightBox = $boxInfo[3] - $boxInfo[1];
55    $result['x'] = $fontSize;
56    $result['y'] = $imgHeight - $heightBox - $fontSize;
57    return $result;
58}
59
60function is_image($file)
61{
62    if ( !file_exists($file) || !@getImageSize($file) )
63        return false;
64    return true;
65}
66
67?>
68

Archive Download this file

Revision: HEAD