475 lines
13 KiB
PHP
475 lines
13 KiB
PHP
<?php
|
|
|
|
/** Image Block
|
|
*
|
|
* @version 1.9.0
|
|
* @since 2007-04-16
|
|
* @author martin lenzelbauer
|
|
*/
|
|
class ImageBlock extends BuildingBlock{
|
|
|
|
var $file; //file name
|
|
var $caption; //caption
|
|
var $hyperlink; //hyperlink
|
|
|
|
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 $absoluteUrl;
|
|
|
|
|
|
/** C'tor
|
|
*/
|
|
//------------------------------------------------
|
|
function ImageBlock($id=0){
|
|
//------------------------------------------------
|
|
parent::BuildingBlock($id);
|
|
$this->displayWidth = 640;
|
|
$this->caption = "";
|
|
$this->hyperlink = "";
|
|
$this->setDirectories(IMG_DIR, THUMB_DIR);
|
|
$this->setSize();
|
|
$this->useThumbs(false);
|
|
$this->useAbsoluteUrl(false);
|
|
}
|
|
|
|
|
|
/** @see BuildingBlock::install()
|
|
*/
|
|
//-----------------------------------------------
|
|
function install(){
|
|
//-----------------------------------------------
|
|
$query = sprintf("CREATE TABLE IF NOT EXISTS bruckm_imageblock (
|
|
id INT not null AUTO_INCREMENT,
|
|
file VARCHAR(128) not null,
|
|
caption TEXT not null,
|
|
hyperlink TEXT not null,
|
|
PRIMARY KEY(id)
|
|
)");
|
|
$result = dbQuery($query);
|
|
}
|
|
|
|
|
|
/** @see BuildingBlock::load()
|
|
*/
|
|
//-------------------------------------------------
|
|
function load(){
|
|
//-------------------------------------------------
|
|
$query = sprintf("SELECT * FROM bruckm_imageblock 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'];
|
|
}
|
|
|
|
|
|
/** @see BuildingBlock::doSave()
|
|
*/
|
|
//-----------------------------------------------
|
|
function doSave(){
|
|
//-----------------------------------------------
|
|
$query = sprintf("UPDATE bruckm_imageblock SET file = %s, caption = %s, hyperlink = %s WHERE id = %d",
|
|
sqlstring($this->file),
|
|
sqlstring($this->caption),
|
|
sqlstring($this->hyperlink),
|
|
$this->id);
|
|
dbQuery($query);
|
|
}
|
|
|
|
|
|
/** @see BuildingBlock::doCreate()
|
|
*/
|
|
//-----------------------------------------------
|
|
function doCreate(){
|
|
//-----------------------------------------------
|
|
$query = sprintf("INSERT INTO bruckm_imageblock (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_imageblock 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["imageblock_file".$this->id]) && $_FILES["imageblock_file".$this->id]['error'] == UPLOAD_ERR_OK){
|
|
$this->file = $this->findName($this->imgDir, $_FILES["imageblock_file".$this->id]['name']);
|
|
$this->uploadImage($_FILES["imageblock_file".$this->id]);
|
|
if($this->thumb){
|
|
$this->createThumbnail();
|
|
}
|
|
}
|
|
$this->caption = trim($_POST["imageblock_caption".$this->id]);
|
|
$this->hyperlink = trim($_POST["imageblock_hyperlink".$this->id]);
|
|
}
|
|
|
|
|
|
/** @see BuildingBlock::printContent()
|
|
*/
|
|
//-----------------------------------------------
|
|
function printContent($position){
|
|
//-----------------------------------------------
|
|
$t = new Template(CMS_TEMPLATE_DIR."imageblock.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);
|
|
$t->removeBlock("UPLOAD");
|
|
}
|
|
else{
|
|
$t->removeBlock("IMAGE");
|
|
}
|
|
return $t->toString();
|
|
}
|
|
|
|
|
|
/** @see BuildingBlock::publish()
|
|
*/
|
|
//----------------------------------------------
|
|
function publish(){
|
|
//----------------------------------------------
|
|
/*if(!file_exists($this->imgDir.$this->file)){
|
|
logError(3, "Image ".$this->imgDir.$this->file." doesn't exist", __FILE__, __LINE__);
|
|
return "";
|
|
}*/
|
|
$alt = htmlspecialchars($this->caption);
|
|
if(empty($alt)){
|
|
$alt = "unbenanntes Bild";
|
|
}
|
|
if($this->absoluteUrl){
|
|
$img = "<div><img src=\"http://www.bruckmuehle.at/newsletter/".$this->file."\" alt=\"$alt\" />";
|
|
}
|
|
else{
|
|
$img = "<div><img src=\"images/".$this->file."\" alt=\"$alt\" />";
|
|
}
|
|
if(empty($this->hyperlink)){
|
|
return $img."</div>";
|
|
}
|
|
else{
|
|
return "<a href=\"".$this->hyperlink."\" target=\"_blank\">$img</a></div>\n";
|
|
}
|
|
}
|
|
|
|
|
|
/** @see BuildingBlock::publishForNewsletter()
|
|
*/
|
|
//----------------------------------------------
|
|
function publishForNewsletter(){
|
|
//----------------------------------------------
|
|
$content = bbcode2html($this->content);
|
|
$p = new Template(NEWSLETTER_DIR . "subparts/image.html");
|
|
$p->setVar("SRC", $this->file);
|
|
$p->setVar("ALT", htmlspecialchars($this->caption));
|
|
return $p->toString();
|
|
}
|
|
|
|
// === 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{
|
|
move_uploaded_file($file['tmp_name'], $path);
|
|
//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=580, $maxH=1000, $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;
|
|
}
|
|
|
|
|
|
/** sets flag for switching between relative paths and absolute url
|
|
* @param useAbsoluteUrl true to use absolute url
|
|
* false to use relative paths
|
|
*/
|
|
//---------------------------------------------
|
|
function useAbsoluteUrl($useAbsoluteUrl){
|
|
//---------------------------------------------
|
|
$this->absoluteUrl = $useAbsoluteUrl;
|
|
}
|
|
|
|
|
|
/** 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;
|
|
}
|
|
|
|
|
|
/** returns image description as xml
|
|
* @return xml string
|
|
*/
|
|
//---------------------------------------------------
|
|
function toXml(){
|
|
//---------------------------------------------------
|
|
$xml = sprintf("<image file=\"%s\" hyperlink=\"%s\"><![CDATA[%s]]></image>",
|
|
$this->file,
|
|
$this->hyperlink,
|
|
$this->caption);
|
|
return $xml;
|
|
}
|
|
|
|
|
|
/** @see BuildingBlock::publishForPdf()
|
|
*/
|
|
//----------------------------------------------
|
|
function publishForPdf($pdf){
|
|
//----------------------------------------------
|
|
$img = getImageSize(IMG_DIR.$this->file);
|
|
$mm = $img[0] / 72 * 25.4;
|
|
if ($mm > 190) {
|
|
$pdf->Image(IMG_DIR.$this->file, NULL, NULL, 190);
|
|
}
|
|
else {
|
|
$pdf->Image(IMG_DIR.$this->file);
|
|
}
|
|
$pdf->Ln();
|
|
$pdf->Ln();
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
?>
|