451 lines
12 KiB
PHP
451 lines
12 KiB
PHP
<?php
|
|
require_once(ROOT."include/bbcode.inc.php");
|
|
|
|
/** Text/Image Block
|
|
* 2-column text/image or image/text block
|
|
*
|
|
* @version 0.0.1
|
|
* @since 2007-06-26
|
|
* @author martin lenzelbauer
|
|
*
|
|
* @todo publish()
|
|
*/
|
|
class TextImageBlock extends BuildingBlock{
|
|
|
|
var $file; //file name
|
|
var $caption; //caption
|
|
var $hyperlink; //hyperlink
|
|
|
|
var $content; //text content
|
|
|
|
var $layout; //text/image or image/text layout
|
|
|
|
var $thumb; //true, if a thumbnail should be generated
|
|
var $imgDir; //output directory for images
|
|
var $thumbDir; //output directory for thumbs
|
|
var $maxWidth;
|
|
var $maxHeight;
|
|
var $thumbWidth;
|
|
var $thumbHeight;
|
|
var $stretchThumb;//true: stretch thumb to fit size
|
|
//false: keep aspect ratio and clip original image
|
|
var $displayWidth;//max width to display image in cms
|
|
|
|
var $LAYOUT_TEXT_IMAGE = "text/image";
|
|
var $LAYOUT_IMAGE_TEXT = "image/text";
|
|
|
|
|
|
/** C'tor
|
|
*/
|
|
//------------------------------------------------
|
|
function TextImageBlock($id=0){
|
|
//------------------------------------------------
|
|
parent::BuildingBlock($id);
|
|
$this->displayWidth = 640;
|
|
$this->content = "";
|
|
$this->caption = "";
|
|
$this->hyperlink = "";
|
|
$this->setDirectories(IMG_DIR, THUMB_DIR);
|
|
$this->setSize();
|
|
$this->useThumbs(false);
|
|
$this->layout = $this->LAYOUT_TEXT_IMAGE;
|
|
}
|
|
|
|
|
|
/** @see BuildingBlock::install()
|
|
*/
|
|
//-----------------------------------------------
|
|
function install(){
|
|
//-----------------------------------------------
|
|
$query = sprintf("CREATE TABLE IF NOT EXISTS bruckm_textimageblock (
|
|
id INT not null AUTO_INCREMENT,
|
|
content TEXT not null,
|
|
file VARCHAR(128) not null,
|
|
caption TEXT not null,
|
|
hyperlink TEXT not null,
|
|
layout ENUM('text/image', 'image/text') not null default 'text/image',
|
|
PRIMARY KEY(id)
|
|
)");
|
|
$result = dbQuery($query);
|
|
}
|
|
|
|
|
|
/** @see BuildingBlock::load()
|
|
*/
|
|
//-------------------------------------------------
|
|
function load(){
|
|
//-------------------------------------------------
|
|
$query = sprintf("SELECT * FROM bruckm_textimageblock WHERE id = %d", $this->id);
|
|
$result = dbQuery($query);
|
|
$line = mysqli_fetch_array($result, MYSQLI_ASSOC);
|
|
$this->file = $line['file'];
|
|
$this->caption = $line['caption'];
|
|
$this->hyperlink = $line['hyperlink'];
|
|
$this->content = $line['content'];
|
|
$this->layout = $line['layout'];
|
|
}
|
|
|
|
|
|
/** @see BuildingBlock::doSave()
|
|
*/
|
|
//-----------------------------------------------
|
|
function doSave(){
|
|
//-----------------------------------------------
|
|
$query = sprintf("UPDATE bruckm_textimageblock SET file = %s, caption = %s, hyperlink = %s, content = %s, layout = %s WHERE id = %d",
|
|
sqlstring($this->file),
|
|
sqlstring($this->caption),
|
|
sqlstring($this->hyperlink),
|
|
sqlstring($this->content),
|
|
sqlstring($this->layout),
|
|
$this->id);
|
|
dbQuery($query);
|
|
}
|
|
|
|
|
|
/** @see BuildingBlock::doCreate()
|
|
*/
|
|
//-----------------------------------------------
|
|
function doCreate(){
|
|
//-----------------------------------------------
|
|
$query = sprintf("INSERT INTO bruckm_textimageblock (file, caption, hyperlink) VALUES (%s, %s, %s)",
|
|
sqlstring($this->file),
|
|
sqlstring($this->caption),
|
|
sqlstring($this->hyperlink));
|
|
dbQuery($query);
|
|
return mysql_insert_id();
|
|
}
|
|
|
|
|
|
/** @see BuildingBlock::delete()
|
|
*/
|
|
//-------------------------------------------------
|
|
function delete(){
|
|
//-------------------------------------------------
|
|
$query = sprintf("DELETE FROM bruckm_textimageblock WHERE id = %d", $this->id);
|
|
dbQuery($query);
|
|
@unlink($this->imgDir.$this->file);
|
|
@unlink($this->thumbDir.$this->file);
|
|
}
|
|
|
|
|
|
/** @see BuildingBlock::update()
|
|
*/
|
|
//-----------------------------------------------
|
|
function update(){
|
|
//-----------------------------------------------
|
|
if(isset($_FILES["textimageblock_file".$this->id]) && $_FILES["textimageblock_file".$this->id]['error'] == UPLOAD_ERR_OK){
|
|
if($this->file && file_exists($this->imgDir.$this->file)){
|
|
@unlink($this->imgDir.$this->file);
|
|
}
|
|
$this->file = $this->findName($this->imgDir, $_FILES["textimageblock_file".$this->id]['name']);
|
|
$this->uploadImage($_FILES["textimageblock_file".$this->id]);
|
|
if($this->thumb){
|
|
$this->createThumbnail();
|
|
}
|
|
}
|
|
$this->caption = trim($_POST["textimageblock_caption".$this->id]);
|
|
$this->hyperlink = trim($_POST["textimageblock_hyperlink".$this->id]);
|
|
$this->content = html_entity_decode($_POST["textimageblock_content".$this->id]);
|
|
$this->layout = $_POST["textimageblock_layout".$this->id];
|
|
}
|
|
|
|
|
|
/** @see BuildingBlock::printContent()
|
|
*/
|
|
//-----------------------------------------------
|
|
function printContent($position){
|
|
//-----------------------------------------------
|
|
$t = new Template(CMS_TEMPLATE_DIR."textimageblock.html");
|
|
$t->setVar("ID", $this->id);
|
|
$t->setVar("POSITION", $position);
|
|
$t->setVar("CAPTION", htmlspecialchars($this->caption));
|
|
$t->setVar("HYPERLINK", htmlspecialchars($this->hyperlink));
|
|
if(!empty($this->file) && file_exists($this->imgDir.$this->file)){
|
|
$t->setVar("PATH", $this->imgDir.$this->file);
|
|
$imgSize = getImageSize($this->imgDir.$this->file);
|
|
$width = $imgSize[0];
|
|
$height = $imgSize[1];
|
|
if($width > $this->displayWidth){
|
|
$height = round($height * ($this->displayWidth/$width));
|
|
$width = $this->displayWidth;
|
|
}
|
|
$t->setVar("WIDTH", $width);
|
|
$t->setVar("HEIGHT", $height);
|
|
}
|
|
else{
|
|
$t->removeBlock("IMAGE");
|
|
}
|
|
$c = htmlspecialchars($this->content);
|
|
$c = str_replace("&#", "&#", $c);
|
|
$t->setVar("CONTENT", $c);
|
|
if($this->layout == $this->LAYOUT_TEXT_IMAGE){
|
|
$t->setVar("LAYOUT_TEXT_IMAGE", "selected=\"selected\"");
|
|
$t->setVar("LAYOUT_IMAGE_TEXT", "");
|
|
}
|
|
else{
|
|
$t->setVar("LAYOUT_IMAGE_TEXT", "selected=\"selected\"");
|
|
$t->setVar("LAYOUT_TEXT_IMAGE", "");
|
|
}
|
|
return $t->toString();
|
|
}
|
|
|
|
|
|
/** @see BuildingBlock::publish()
|
|
*/
|
|
//----------------------------------------------
|
|
function publish(){
|
|
//----------------------------------------------
|
|
if(!file_exists(IMG_DIR.$this->file)){
|
|
logError(3, "Image ".IMG_DIR.$this->file." doesn't exist", __FILE__, __LINE__);
|
|
return "";
|
|
}
|
|
$alt = htmlspecialchars($this->caption);
|
|
if(empty($alt)){
|
|
$alt = htmlspecialchars($this->file);
|
|
}
|
|
$content = bbcode2html($this->content);
|
|
$img = "<img src=\"images/".$this->file."\" alt=\"$alt\" />";
|
|
if(!empty($this->hyperlink)){
|
|
$img = "<a href=\"".$this->hyperlink."\" target=\"_self\">$img</a>";
|
|
}
|
|
if($this->layout == $this->LAYOUT_TEXT_IMAGE){
|
|
return "<table class=\"textimageblock\"><tr><td valign=\"top\">$content</td><td>$img</td></tr></table>";
|
|
}
|
|
else{
|
|
return "<table class=\"textimageblock\"><tr><td>$img</td><td valign=\"top\">$content</td></tr></table>";
|
|
}
|
|
}
|
|
|
|
|
|
// === ADDITIONAL METHODS ======================================================== //
|
|
|
|
|
|
/** uploads an image file
|
|
* @param file $_FILES file data
|
|
*/
|
|
//------------------------------------------------
|
|
function uploadImage($file){
|
|
//------------------------------------------------
|
|
|
|
$path = $this->imgDir.$this->file;
|
|
|
|
if(file_exists($path)){
|
|
@unlink($path);
|
|
}
|
|
|
|
//resize image
|
|
$imgSize = getImageSize($file['tmp_name']);
|
|
if($imgSize[0] > $this->maxWidth || $imgSize[1] > $this->maxHeight){
|
|
switch($imgSize[2]){
|
|
case 1: //gif
|
|
$original = imageCreateFromGif($file['tmp_name']);
|
|
break;
|
|
case 2: //jpg
|
|
$original = imageCreateFromJpeg($file['tmp_name']);
|
|
break;
|
|
case 3: //png
|
|
$original = imageCreateFromPng($file['tmp_name']);
|
|
break;
|
|
}
|
|
$origWidth = $imgSize[0];
|
|
$origHeight = $imgSize[1];
|
|
$width = $origWidth;
|
|
$height = $origHeight;
|
|
if($width > $this->maxWidth){
|
|
$fact = $this->maxWidth/$origWidth;
|
|
$width *= $fact;
|
|
$height *= $fact;
|
|
}
|
|
if($height > $this->maxHeight){
|
|
$fact = $this->maxHeight/$height;
|
|
$width *= $fact;
|
|
$height *= $fact;
|
|
}
|
|
$small = imageCreateTrueColor($width,$height);
|
|
imageCopyResampled($small, $original, 0, 0, 0, 0, $width, $height, $origWidth, $origHeight);
|
|
switch($imgSize[2]){
|
|
case 1:
|
|
if(!@imageGif($small, $path)){
|
|
logError(4, "Could not create gif image: $path", __FILE__, __LINE__);
|
|
return "";
|
|
}
|
|
break;
|
|
case 2:
|
|
if(!@imageJpeg($small, $path, 100)){
|
|
logError(4, "Could not create jpg image: $path", __FILE__, __LINE__);
|
|
return "";
|
|
}
|
|
break;
|
|
case 3:
|
|
if(!@imagePng($small, $path)){
|
|
logError(4, "Could not create png image: $path", __FILE__, __LINE__);
|
|
return "";
|
|
}
|
|
break;
|
|
}
|
|
imageDestroy($small);
|
|
imageDestroy($original);
|
|
@chmod($path, 0777);
|
|
}
|
|
else{
|
|
copy($file['tmp_name'], $path);
|
|
@chmod($path, 0777);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
/** creates a thumbnail from an existing imag
|
|
*/
|
|
//------------------------------------------------
|
|
function createThumbnail(){
|
|
//------------------------------------------------
|
|
|
|
$path = $this->thumbDir.$this->file;
|
|
$source = $this->imgDir.$this->file;
|
|
|
|
if(!file_exists($source)){
|
|
$this->addError("Thumbnail kann nicht erzeugt werden!");
|
|
return false;
|
|
}
|
|
|
|
$imgSize = getImageSize($source);
|
|
switch($imgSize[2]){
|
|
case 1: //gif
|
|
$original = imageCreateFromGif($source);
|
|
break;
|
|
case 2: //jpg
|
|
$original = imageCreateFromJpeg($source);
|
|
break;
|
|
case 3: //png
|
|
$original = imageCreateFromPng($source);
|
|
break;
|
|
}
|
|
$origWidth = $imgSize[0];
|
|
$origHeight = $imgSize[1];
|
|
$width = $this->thumbWidth;
|
|
$height = $this->thumbHeight;
|
|
$origX = 0;
|
|
$origY = 0;
|
|
//clip image
|
|
if(!$this->stretchThumb){
|
|
if($origWidth > $origHeight){
|
|
$origX = ($origWidth - $origHeight)/2;
|
|
$origWidth = $origHeight;
|
|
}
|
|
else{
|
|
$origY = ($origHeight - $origWidth)/2;
|
|
$origHeight = $origWidth;
|
|
}
|
|
}
|
|
$small = imageCreateTrueColor($width, $height);
|
|
imageCopyResampled($small, $original, 0, 0, $origX, $origY, $width, $height, $origWidth, $origHeight);
|
|
switch($imgSize[2]){
|
|
case 1:
|
|
imageGif($small, $path);
|
|
break;
|
|
case 2:
|
|
imageJpeg($small, $path, 100);
|
|
break;
|
|
case 3:
|
|
imagePng($small, $path);
|
|
break;
|
|
}
|
|
imageDestroy($small);
|
|
imageDestroy($original);
|
|
@chmod($path, 0777);
|
|
|
|
}
|
|
|
|
|
|
/** sets the image and thumb size
|
|
*/
|
|
//-----------------------------------------------
|
|
function setSize($maxW=300, $maxH=500, $thumbW=50, $thumbH=50, $stretch=false){
|
|
//-----------------------------------------------
|
|
$this->maxWidth = $maxW;
|
|
$this->maxHeight = $maxH;
|
|
$this->thumbWidth = $thumbW;
|
|
$this->thumbHeight = $thumbH;
|
|
$this->stretchThumb = $stretch;
|
|
}
|
|
|
|
|
|
/** sets the output directories
|
|
*/
|
|
//-----------------------------------------------
|
|
function setDirectories($imgDir, $thumbDir){
|
|
//-----------------------------------------------
|
|
$this->imgDir = $imgDir;
|
|
$this->thumbDir = $thumbDir;
|
|
}
|
|
|
|
|
|
/** sets flag for creating thumbs
|
|
* @param useThumbs true to enable thumbs
|
|
* false to disable thumbs
|
|
*/
|
|
//----------------------------------------------
|
|
function useThumbs($useThumbs=true){
|
|
//----------------------------------------------
|
|
$this->thumb = $useThumbs;
|
|
}
|
|
|
|
|
|
/** finds a path that does not exist
|
|
* @param dir target directory
|
|
* @param name name of the source image
|
|
* @return filename in the target directory that does not exist
|
|
*/
|
|
//-----------------------------------------------
|
|
function findName($dir, $name){
|
|
//-----------------------------------------------
|
|
$name = strtolower($name);
|
|
$name = str_replace("ä", "ae", $name);
|
|
$name = str_replace("ö", "oe", $name);
|
|
$name = str_replace("ü", "ue", $name);
|
|
$name = str_replace("ß", "sz", $name);
|
|
$name = ereg_replace("[^A-Za-z0-9\.-_]", "", $name);
|
|
if(!file_exists($dir.$name)){
|
|
return $name;
|
|
}
|
|
|
|
//extract file extension
|
|
$dot = strrpos($name, ".");
|
|
$ext = substr($name, $dot+1);
|
|
$name = substr($name, 0, $dot);
|
|
|
|
//add 3-digit number
|
|
$count = 1;
|
|
while(file_exists(sprintf("%s%s%03d.%s", $dir, $name, $count, $ext))){
|
|
$count++;
|
|
}
|
|
return sprintf("%s%03d.%s", $name, $count, $ext);
|
|
}
|
|
|
|
|
|
/** prints a thumbnail version of the image
|
|
* @return string
|
|
*/
|
|
//----------------------------------------------------
|
|
function printThumb(){
|
|
//----------------------------------------------------
|
|
$out = "";
|
|
if(!empty($this->file)){
|
|
if($this->thumb){
|
|
$out = "<img src=\"".$this->thumbDir.$this->file."\" alt=\"thumbnail\" ";
|
|
}
|
|
else{
|
|
$out = "<img src=\"".$this->imgDir.$this->file."\" alt=\"preview\" ";
|
|
}
|
|
$out .= "width=\"".$this->thumbWidth."\" height=\"".$this->thumbHeight."\" /> ";
|
|
}
|
|
return $out;
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
?>
|