dc2-photoblog/class.dc.photoblog.php

599 lines
23 KiB
PHP

<?php
$default_now_text = 'Vous &ecirc;tes ici';
$default_same_thumbs = 1;
class dcPhotoblog
{
const quality = 95; /// <b>integer</b> Quality setting for jpeg compression
const default_thumb_size = 90; /// <b>integer</b> Default thumbnail size
const default_sepia = 2; /// <b>integer</b> Default sepia toning for thumbnails
public static function __init()
{
// Default settings
dcCore::app()->blog->settings->addNamespace('photoblog');
dcCore::app()->blog->settings->photoblog->put('photoblog_thumbnail_sepia_nav', dcPhotoblog::default_sepia, 'integer', 'Sepia toning for navigation thumbnails');
dcCore::app()->blog->settings->photoblog->put('photoblog_thumbnail_width_nav', dcPhotoblog::default_thumb_size, 'integer', 'Width of thumbnails used in navigation');
dcCore::app()->blog->settings->photoblog->put('photoblog_thumbnail_height_nav', dcPhotoblog::default_thumb_size, 'integer', 'Height of thumbnails used in navigation');
dcCore::app()->blog->settings->photoblog->put('photoblog_use_same_thumbs', true, 'boolean', 'Do we use the same thumbnails for both navigation and archives?');
dcCore::app()->blog->settings->photoblog->put('photoblog_thumbnail_sepia_archive', dcPhotoblog::default_sepia, 'integer', 'Sepia toning for archive thumbnails');
dcCore::app()->blog->settings->photoblog->put('photoblog_thumbnail_width_archive', dcPhotoblog::default_thumb_size, 'integer', 'Width of thumbnails used in archives');
dcCore::app()->blog->settings->photoblog->put('photoblog_thumbnail_height_archive', dcPhotoblog::default_thumb_size, 'integer', 'Height of thumbnails used in archives');
}
public static function getNextPosts($params)
{
if ($params['dir'] > 0) {
$sign = '>';
$order = 'ASC';
} else {
$sign = '<';
$order = 'DESC';
}
$dt = date('YmdHis', (integer) $params['nav_post_dt']);
$params['limit'] = $params['nb_entries'];
$params['order'] = 'post_dt ' . $order;
$params['sql'] .= ' AND ' .
dcCore::app()->blog->con->concat(dcCore::app()->blog->con->dateFormat('post_dt', '%Y%m%d%H%M%S'), "'.'", 'P.post_id') .
" $sign '$dt.".$params['nav_post_id']."'";
$rs = dcCore::app()->blog->getPosts($params);
if ($rs->isEmpty()) {
$rs = null;
}
return $rs;
}
public static function getPostImages($post_id, $limit=false)
{
$params['post_id'] = $post_id;
$post = dcCore::app()->blog->getPosts($params);
if (!$post->isEmpty()) {
$img_src_regexp = '/<img .*?src="(.+?)"/i';
$result = array();
if (preg_match_all($img_src_regexp, $post->post_excerpt_xhtml, $matches)) {
foreach ($matches[1] as $match) {
$result[] = $match;
}
}
if (preg_match_all($img_src_regexp, $post->post_content_xhtml, $matches)) {
foreach ($matches[1] as $match) {
$result[] = $match;
}
}
if ($limit) {
return $result[0];
} else {
return $result;
}
}
return false;
}
public static function getPostImage($post_id)
{
return dcPhotoblog::getPostImages($post_id, true);
}
/**
* @deprecated Deprecated since the first release of Dotclear 2.
*/
public static function getDC12ImageThumbFileName($imageFileName, $isBW, $isForArchive)
{
$suffix = 'photo';
if ($isBW) {
$suffix .= 'bw';
}
if (!$isForArchive) {
$suffix .= 'nav';
}
return preg_replace('/^(.*)([.]\\w+)$/', '$1.' . $suffix . 'TN__$2', $imageFileName);
}
public static function getImageThumbFileName($imageFileName, $isBW, $isForArchive)
{
$suffix = 'photo';
if ($isBW) {
$suffix .= 'bw';
}
if (!$isForArchive) {
$suffix .= 'nav';
}
return preg_replace('/^(.*)\/(.*?)([.]\\w+)$/', '$1/.$2_' . $suffix . '$3', $imageFileName);
}
private static function getPostImageFileColorThumb($imagefile, $is_for_archive)
{
$is_for_archive = $is_for_archive || dcCore::app()->blog->settings->photoblog->photoblog_use_same_thumbs;
if (!dcCore::app()->blog->settings->photoblog->photoblog_thumbnail_width_nav) {
$thumb_width = dcPhotoblog::default_thumb_size;
$thumb_height = dcPhotoblog::default_thumb_size;
$thumb_sepia = dcPhotoblog::default_sepia;
} else {
if ($is_for_archive) {
$thumb_width = dcCore::app()->blog->settings->photoblog->photoblog_thumbnail_width_archive;
$thumb_height = dcCore::app()->blog->settings->photoblog->photoblog_thumbnail_height_archive;
$thumb_sepia = dcCore::app()->blog->settings->photoblog->photoblog_thumbnail_sepia_archive;
} else {
$thumb_width = dcCore::app()->blog->settings->photoblog->photoblog_thumbnail_width_nav;
$thumb_height = dcCore::app()->blog->settings->photoblog->photoblog_thumbnail_height_nav;
$thumb_sepia = dcCore::app()->blog->settings->photoblog->photoblog_thumbnail_sepia_nav;
}
}
if (preg_match('!' . dcCore::app()->blog->settings->system->public_url . '(.+)!i', $imagefile)) {
$dc_thumb = preg_replace('/^(.*)\/(.*?)([.]\\w+)$/', '$1/.$2_sq$3', $imagefile);
$photo_thumb = dcPhotoBlog::getImageThumbFileName($imagefile, false, $is_for_archive);
$photo_bwthumb = dcPhotoBlog::getImageThumbFileName($imagefile, true, $is_for_archive);
dcPhotoBlog::createBWPhotoThumb($imagefile, $photo_bwthumb, $thumb_width, $thumb_height, $thumb_sepia);
if (dcPhotoBlog::createPhotoThumb($imagefile, $photo_thumb, $thumb_width, $thumb_height)) {
return $photo_thumb;
} else {
return $dc_thumb;
}
}
return false;
}
public static function getPostImagesColorThumb($post_id, $is_for_archive, $limit=false)
{
if ($images = dcPhotoBlog::getPostImages($post_id)) {
foreach ($images as $image) {
$result[] = dcPhotoBlog::getPostImageFileColorThumb($image, $is_for_archive);
}
if ($limit) {
return $result[0];
} else {
return $result;
}
}
return false;
}
public static function getPostImageColorThumb($post_id, $is_for_archive)
{
return dcPhotoblog::getPostImagesColorThumb($post_id, $is_for_archive, true);
}
private static function getPostImageFileBWThumb($imagefile, $is_for_archive)
{
$is_for_archive = $is_for_archive || dcCore::app()->blog->settings->photoblog->photoblog_use_same_thumbs;
if (!dcCore::app()->blog->settings->photoblog->photoblog_thumbnail_width_nav) {
$thumb_width = dcPhotoblog::default_thumb_size;
$thumb_height = dcPhotoblog::default_thumb_size;
$thumb_sepia = dcPhotoblog::default_sepia;
} else {
if ($is_for_archive) {
$thumb_width = dcCore::app()->blog->settings->photoblog->photoblog_thumbnail_width_archive;
$thumb_height = dcCore::app()->blog->settings->photoblog->photoblog_thumbnail_height_archive;
$thumb_sepia = dcCore::app()->blog->settings->photoblog->photoblog_thumbnail_sepia_archive;
} else {
$thumb_width = dcCore::app()->blog->settings->photoblog->photoblog_thumbnail_width_nav;
$thumb_height = dcCore::app()->blog->settings->photoblog->photoblog_thumbnail_height_nav;
$thumb_sepia = dcCore::app()->blog->settings->photoblog->photoblog_thumbnail_sepia_nav;
}
}
if (preg_match('!' . dcCore::app()->blog->settings->system->public_url . '(.+)!i', $imagefile)) {
$dc_thumb = preg_replace('/^(.*)\/(.*?)([.]\\w+)$/', '$1/.$2_sq$3', $imagefile);
$photo_thumb = dcPhotoBlog::getImageThumbFileName($imagefile, false, $is_for_archive);
$photo_bwthumb = dcPhotoBlog::getImageThumbFileName($imagefile, true, $is_for_archive);
dcPhotoBlog::createPhotoThumb($imagefile, $photo_thumb, $thumb_width, $thumb_height);
if (dcPhotoBlog::createBWPhotoThumb($imagefile, $photo_bwthumb, $thumb_width, $thumb_height, $thumb_sepia)) {
return $photo_bwthumb;
} else {
return $dc_thumb;
}
}
return false;
}
public static function getPostImagesBWThumb($post_id, $is_for_archive, $limit=false)
{
if ($images = dcPhotoBlog::getPostImages($post_id)) {
foreach ($images as $image) {
$result[] = dcPhotoBlog::getPostImageFileBWThumb($image, $is_for_archive);
}
if ($limit) {
return $result[0];
} else {
return $result;
}
}
return false;
}
public static function getPostImageBWThumb($post_id, $is_for_archive)
{
return dcPhotoblog::getPostImagesBWThumb($post_id, $is_for_archive, true);
}
public static function createBWPhotoThumb($uri, $file, $w=90, $h=90, $sepia_toning=2)
{
if (empty($w)) {
$w = dcPhotoblog::default_thumb_size;
}
if (empty($h)) {
$h = dcPhotoblog::default_thumb_size;
}
$quality = dcPhotoblog::quality;
$src_rel = preg_replace('!' . dcCore::app()->blog->settings->system->public_url . '!i', '', $uri);
$dest_rel = preg_replace('!' . dcCore::app()->blog->settings->system->public_url . '!i', '', $file);
$img_p = dcPhotoBlog::getImagePath();
$uri = $img_p . '/' . $src_rel;
$file = $img_p . '/' . $dest_rel;
if (file_exists($file)) {
return true;
}
if (!file_exists($uri)) {
return false;
}
if (($size = @getimagesize($uri)) === false) {
return false;
}
$origW = $size[0];
$origH = $size[1];
$type = $size[2];
if ($type == '1') {
$function = 'imagecreatefromgif';
} elseif ($type == '2') {
$function = 'imagecreatefromjpeg';
} elseif ($type == '3') {
$function = 'imagecreatefrompng';
} else {
return false;
}
if (!function_exists($function)) {
return false;
}
if (!($img = @$function($uri))) {
return false;
}
if (($w < $h) || (($origW > $origH) && ($w = $h))) { // Portrait output, or (Square output, landscape input)
$srcW = round($origH * ($w / $h));
$srcH = $origH;
$srcX = round(($origW - $srcW) / 2);
$srcY = 0;
} elseif (($w > $h) || (($origW <= $origH) && ($w = $h))) { // Landscape output, or (Square output, portrait or square input)
$srcW = $origW;
$srcH = round($origW * ($h / $w));
$srcX = 0;
$srcY = round(($origH - $srcH) / 2);
}
if (($img2 = imagecreatetruecolor($w, $h * 2)) === false) {
return false;
}
imagecopyresampled($img2, $img, 0, 0, $srcX, $srcY, $w, $h, $srcW, $srcH);
// Does the user wants some kind of sepia toning for his thumbnails?
if ($sepia_toning > 0) {
// convert to a palette
imagetruecolortopalette($img, 1, 256);
// init Lagrange denominators and curve points
switch ($sepia_toning) {
case 2:
// Sepia 1 (according to http://www.epaperpress.com/psphoto/bawClassicTones.html )
$cr = [[0, 0], [66, 79], [127, 151], [194, 206], [255, 255]];
$cg = [[0, 0], [66, 63], [127, 121], [194, 190], [255, 255]];
$cb = [[0, 0], [66, 48], [127, 91], [194, 174], [255, 255]];
break;
case 3:
// Sepia 2 (according to http://www.epaperpress.com/psphoto/bawClassicTones.html )
$cr = [[0, 0], [61, 82], [128, 157], [189, 214], [255, 255]];
$cg = [[0, 0], [61, 55], [128, 121], [189, 185], [255, 255]];
$cb = [[0, 0], [61, 22], [128, 71], [189, 145], [255, 255]];
break;
case 4:
// Sepia 3 (according to http://www.epaperpress.com/psphoto/bawClassicTones.html )
$cr = [[0, 0], [67, 98], [127, 165], [192, 214], [255, 255]];
$cg = [[0, 0], [67, 54], [127, 116], [192, 185], [255, 255]];
$cb = [[0, 0], [67, 19], [127, 84], [192, 171], [255, 255]];
break;
case 5:
// Selenium (according to http://www.epaperpress.com/psphoto/bawClassicTones.html )
$cr = [[0, 0], [64, 79], [127, 139], [191, 198], [255, 255]];
$cg = [[0, 0], [64, 59], [127, 123], [191, 188], [255, 255]];
$cb = [[0, 0], [61, 61], [127, 133], [191, 197], [255, 255]];
break;
case 6:
// Silver (according to http://www.epaperpress.com/psphoto/bawClassicTones.html )
$cr = [[0, 0], [64, 57], [128, 119], [191, 188], [255, 255]];
$cg = [[0, 0], [64, 65], [128, 130], [191, 192], [255, 255]];
$cb = [[0, 0], [64, 67], [128, 129], [191, 192], [255, 255]];
break;
case 7:
// Cyanotype (according to http://www.epaperpress.com/psphoto/bawClassicTones.html )
$cr = [[0, 0], [129, 25], [193, 120], [255, 255]];
$cg = [[0, 0], [129, 135], [193, 204], [255, 255]];
$cb = [[0, 0], [129, 220], [193, 250], [255, 255]];
break;
case 8:
// Platinum (according to http://www.epaperpress.com/psphoto/bawClassicTones.html )
$cr = [[0, 0], [63, 75], [127, 129], [191, 193], [255, 255]];
$cg = [[0, 0], [255, 255]];
$cb = [[0, 0], [63, 51], [127, 112], [191, 176], [255, 255]];
break;
case 9:
// Palladium (according to http://www.epaperpress.com/psphoto/bawClassicTones.html )
$cr = [[0, 0], [64, 67], [126, 143], [195, 213], [255, 255]];
$cg = [[0, 0], [64, 60], [126, 122], [195, 192], [255, 255]];
$cb = [[0, 0], [64, 47], [126, 103], [195, 173], [255, 255]];
break;
case 10:
// Silver Gelatin (according to http://www.epaperpress.com/psphoto/bawClassicTones.html )
$cr = [[0, 0], [64, 79], [127, 148], [193, 213], [255, 255]];
$cg = [[0, 0], [64, 59], [127, 123], [193, 188], [255, 255]];
$cb = [[0, 0], [64, 34], [127, 93], [193, 157], [255, 255]];
break;
default:
// Black and white
$cr = [[0, 0], [255, 255]];
$cg = [[0, 0], [255, 255]];
$cb = [[0, 0], [255, 255]];
break;
}
$dr = [1, 1, 1, 1, 1]; $dg = [1, 1, 1, 1, 1]; $db = [1, 1, 1, 1, 1];
for ($i = 0; $i < count($cr); $i++) {
for ($j = 0; $j < count($cr); $j++) {
if ($j == $i) { continue; }
$dr[$i] = $dr[$i] * ($cr[$i][0] - $cr[$j][0]);
}
}
for ($i = 0; $i < count($cg); $i++) {
for ($j = 0; $j < count($cg); $j++) {
if ($j == $i) { continue; }
$dg[$i] = $dg[$i] * ($cg[$i][0] - $cg[$j][0]);
}
}
for ($i = 0; $i < count($cb); $i++) {
for ($j = 0; $j < count($cb); $j++) {
if ($j == $i) { continue; }
$db[$i] = $db[$i] * ($cb[$i][0] - $cb[$j][0]);
}
}
// loop through palette
for ($x = 0; $x < imagecolorstotal($img); $x++) {
$a = array();
$a = imagecolorsforindex($img, $x);
$r = 0; $g = 0; $b = 0;
// calc a grayscale value
$gs = intval(($a['red'] / 3) + ($a['green'] / 3) + ($a['blue'] / 3));
// Lagrange polynomial interpolation
for ($i = 0; $i < count($cr); $i++) {
$temp = $cr[$i][1];
for ($j = 0; $j < count($cr); $j++) {
if ($j == $i) { continue; }
$temp = $temp * ($gs - $cr[$j][0]);
}
$r = $r + $temp / $dr[$i];
}
for ($i = 0; $i < count($cg); $i++) {
$temp = $cg[$i][1];
for ($j = 0; $j < count($cg); $j++) {
if ($j == $i) { continue; }
$temp = $temp * ($gs - $cg[$j][0]);
}
$g = $g + $temp / $dg[$i];
}
for ($i = 0; $i < count($cb); $i++) {
$temp = $cb[$i][1];
for ($j = 0; $j < count($cb); $j++) {
if ($j == $i) { continue; }
$temp = $temp * ($gs - $cb[$j][0]);
}
$b = $b + $temp / $db[$i];
}
imagecolorset(
$img,
$x,
dcPhotoblog::keepColorInRange($r),
dcPhotoblog::keepColorInRange($g),
dcPhotoblog::keepColorInRange($b)
);
}
}
imagecopyresampled($img2, $img, 0, $h, $srcX, $srcY, $w, $h, $srcW, $srcH);
if (!is_dir(dirname($file))) {
$dmod = fileperms(dirname(dirname($file)));
if (@mkdir(dirname($file), $dmod) === false) {
return false;
}
chmod(dirname($file), $dmod);
}
if ($type == '1') {
$endfunction = 'imagegif';
} elseif ($type == '2') {
$endfunction = 'imagejpeg';
} elseif ($type == '3') {
$endfunction = 'imagepng';
} else {
return false;
}
if (!function_exists($function)) {
return false;
}
if (@$endfunction($img2, $file, $quality) === false) {
return false;
}
chmod($file, fileperms(dirname($file)) & ~0111);
imagedestroy($img);
imagedestroy($img2);
return true;
}
public static function createPhotoThumb($uri, $file, $w=90, $h=90)
{
if (empty($w)) {
$w = dcPhotoblog::default_thumb_size;
}
if (empty($h)) {
$h = dcPhotoblog::default_thumb_size;
}
$quality = dcPhotoblog::quality;
$src_rel = preg_replace('!' . dcCore::app()->blog->settings->system->public_url . '!i', '', $uri);
$dest_rel = preg_replace('!' . dcCore::app()->blog->settings->system->public_url . '!i', '', $file);
$img_p = dcPhotoBlog::getImagePath();
$uri = $img_p . '/' . $src_rel;
$file = $img_p . '/' . $dest_rel;
if (file_exists($file)) {
return true;
}
if (!file_exists($uri)) {
return false;
}
if (($size = @getimagesize($uri)) === false) {
return false;
}
$origW = $size[0];
$origH = $size[1];
$type = $size[2];
if ($type == '1') {
$function = 'imagecreatefromgif';
} elseif ($type == '2') {
$function = 'imagecreatefromjpeg';
} elseif ($type == '3') {
$function = 'imagecreatefrompng';
} else {
return false;
}
if (!function_exists($function)) {
return false;
}
if (!($img = @$function($uri))) {
return false;
}
if (($w < $h) || (($origW > $origH) && ($w = $h))) { // Portrait output, or (Square output, landscape input)
$srcW = round($origH * ($w / $h));
$srcH = $origH;
$srcX = round(($origW - $srcW) / 2);
$srcY = 0;
} elseif (($w > $h) || (($origW <= $origH) && ($w = $h))) { // Landscape output, or (Square output, portrait or square input)
$srcW = $origW;
$srcH = round($origW * ($h / $w));
$srcX = 0;
$srcY = round(($origH - $srcH) / 2);
}
if (($img2 = imagecreatetruecolor($w, $h)) === false) {
return false;
}
imagecopyresampled($img2, $img, 0, 0, $srcX, $srcY, $w, $h, $srcW, $srcH);
if (!is_dir(dirname($file))) {
$dmod = fileperms(dirname(dirname($file)));
if (@mkdir(dirname($file), $dmod) === false) {
return false;
}
chmod(dirname($file), $dmod);
}
if ($type == '1') {
$endfunction = 'imagegif';
} elseif ($type == '2') {
$endfunction = 'imagejpeg';
} elseif ($type == '3') {
$endfunction = 'imagepng';
} else {
return false;
}
if (!function_exists($function)) {
return false;
}
if (@$endfunction($img2, $file, $quality) === false) {
return false;
}
chmod($file, fileperms(dirname($file)) & ~0111);
imagedestroy($img);
imagedestroy($img2);
return true;
}
public static function getImagePath()
{
$img_p = dcCore::app()->blog->settings->system->public_path;
$img_p = path::fullFromRoot($img_p, DC_ROOT);
if ($img_p == '') {
# preg_match('$' . dc_app_url . '/(.*)$', dc_img_root, $matches); //debugjmu
$image_dir = isset($matches[1]) ? $matches[1] : '';
if ($image_dir == '') {
// Woops... couldn't find the directory name for the images,
// defaulting to images
$image_dir = 'images/';
}
if ($image_dir[strlen($image_dir) - 1] != '/') {
$image_dir = $image_dir . '/';
}
$img_p = dirname(__FILE__) . '/../../../' . $image_dir;
$img_p = path::real($img_p);
}
return $img_p;
}
private static function keepColorInRange($color) {
return max(min(255, round($color)), 0);
}
}