| 1 | <?php |
| 2 | chdir('../..'); |
| 3 | require 'autoload.php'; |
| 4 | |
| 5 | define('IMAGE_CACHE_DIR', 'var/cache/tag_images/'); |
| 6 | define('TTF_FONT', 'extension/pwet2/share/Trebuchet_MS.ttf'); |
| 7 | define('IMG_TEXTE', 'Image sous Licence CC-By-Sa - http://pwet.fr/photos'); |
| 8 | define('BASE_FONT_SIZE', 25); // font size pour 1000px large |
| 9 | define('LOG_FILE', 'hotlink.log'); |
| 10 | |
| 11 | if ( !array_key_exists('img', $_GET) ) |
| 12 | exit(); |
| 13 | $image = $_GET['img']; |
| 14 | if ( $image{0} == '/' ) |
| 15 | $image = substr($image, 1); |
| 16 | if ( !is_image($image) ) |
| 17 | exit(); |
| 18 | //$log = new eZLog(); |
| 19 | eZLog::write('# '.$_GET['img'].' # '.$_GET['ref'], LOG_FILE); |
| 20 | tag_image($image, IMG_TEXTE); |
| 21 | |
| 22 | function 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 | |
| 44 | function get_font_size($width) |
| 45 | { |
| 46 | return ceil($width*BASE_FONT_SIZE/1000); |
| 47 | } |
| 48 | |
| 49 | function 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 | |
| 60 | function is_image($file) |
| 61 | { |
| 62 | if ( !file_exists($file) || !@getImageSize($file) ) |
| 63 | return false; |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | ?> |
| 68 | |