File: D:/HostingSpaces/SBogers10/komma-mediadesign.nl/wwwroot/beheer/app/models/images/m_imageHandler.php
<?php
/*
Handles all tasks regarding image storage
*/
require_once DOCUMENT_ROOT.'app/models/images/m_imageUploader.php';
require_once DOCUMENT_ROOT.'app/models/images/m_imageCropper.php';
require_once DOCUMENT_ROOT.'app/models/images/m_imageStorage.php';
class ImageHandler
{
private $_template, $_pages;
private $_tableNames;
private $_pageinfo;
private $_maxImages;
private $_maxFileSize;
private $_displayFileSize;
private $_dim; //image dimension
private $_imageSession;
private $_imageStorage;
public function __construct($pageId, $imgDim)
{
global $template, $pages;
$this->_template = $template;
$this->_pages = $pages;
$this->_pageinfo = $this->_pages->get($pageId);
$this->_tableNames['images'] = 'page_'.$this->_template->encodeDbName($this->_pageinfo['label']).'_images';
$this->_maxImages = 0;
$this->_maxFileSize = 3000000;
$this->_displayFileSize = '3mb';
$this->_dim = $imgDim;
if( ! isset($_SESSION['images_to_store']))
{
$this->_imageSession = $_SESSION['images_to_store'] = new ImageSession('images_to_store');
}
else{
$this->_imageSession = $_SESSION['images_to_store'];
}
$this->_imageStorage = new ImageStorage($pageId);
}
/*
GET / SET
*/
/**
* Returns tablename
*
* @access public
* @param
* @return string
*/
public function getTableName()
{
return $this->_tableNames['images'];
}
/**
* Set tablename
*
* @access public
* @param string
* @return null
*/
public function setTableName($name)
{
if( ! empty($name) && is_string($name))
{
$this->_tableNames['images'] = $name;
}
}
/**
* Returns the extention of a given filestring
*
* @access public
* @param string
* @return string
*/
public function getExt($input)
{
$temp = explode('.', $input);
$fileExt = strtolower($temp[count($temp) - 1]);
return $fileExt;
}
/*
Upload Images
*/
/**
* Validates and adds images to the database, it also handles the uploading part
*
* @access public
* @param
* @return null
*/
public function addImages()
{
if( ! empty($_FILES['images']))
{
$currentImages = $this->_imageSession->count();
$valid = FALSE;
$crop = isset($_POST['cb_crop']);
unset($_SESSION['images_to_crop']);
foreach($_FILES['images']['name'] as $key => $name)
{
if($name != "")
{
if($key+$currentImages < $this->_maxImages || $this->_maxImages == 0){
$imageUploader = new ImageUploader($name, $_FILES['images']['tmp_name'][$key], $_FILES['images']['size'][$key]);
if($imageUploader->validate())
{
if($original = $imageUploader->upload())
{
$valid = TRUE;
// create images
if( ! $crop)
{
$fileExt = $imageUploader->getExt($original);
// set data
$data = array();
$shortcode = $data['shortcode'] = $key.time();
$largeName = $data['name'] = FILE_PREFIX.'_'.$shortcode.'.'.$fileExt;
$thumbName = $data['thumb'] = FILE_PREFIX.'_'.$shortcode.'_thumb.'.$fileExt;
$moThumbName = $data['mothumb'] = FILE_PREFIX.'_'.$shortcode.'_mothumb.'.$fileExt;
// add data to session
$this->_imageSession->add($data);
// auto crop
$imageCropper = new ImageCropper(DOCUMENT_IMAGE_ROOT.'uploads/'.$original, $largeName);
$imageCropper->setSides($this->_dim['lw'],$this->_dim['lh']);
$imageCropper->createImage();
$imageCropper->setName($thumbName);
$imageCropper->setSides($this->_dim['tw'],$this->_dim['th']);
$imageCropper->createImage();
$imageCropper->setName($moThumbName);
$imageCropper->setSides($this->_dim['mw'],$this->_dim['mh']);
$imageCropper->createImage();
$this->unlink(DOCUMENT_IMAGE_ROOT.'uploads/'.$original);
}
else
{
// crop by hand
$_SESSION['images_to_crop'][]['original'] = $original;
}
}
}
}
}
}
if($valid)
{
$linkname = $this->_template->encodeUrl($this->_pageinfo['label']);
if($crop)
{
$this->_template->redirect(SITE_ROOT.$linkname.'/crop/');
}
return TRUE;
}
return FALSE;
}
}
/**
* Add images after they have been cropped
*
* @access private
* @param file, int(optional), int(optional)
* @return null
*/
public function addCroppedImages()
{
foreach($_SESSION['images_to_crop'] as $key => $options)
{
$temp = explode('-', $_POST['cropValues'.$key]);
foreach($temp as $value)
{
$value = intval($value);
}
list($x,$y,$x2,$y2,$w,$h) = $temp;
$original = $options['original'];
// check if orignal image is < 800 (for crop layout)
list($oriW,$oriH) = getimagesize(DOCUMENT_IMAGE_ROOT.'uploads/'.$original);
$layoutWidth = 800;
if($oriW > 800)
{
$scale = $oriW / $layoutWidth;
$x = $x * $scale;
$y = $y * $scale;
$w = $w * $scale;
$h = $h * $scale;
}
$fileExt = $this->getExt($original);
//set data
$data = array();
$shortcode = $data['shortcode'] = $key.time();
$largeName = $data['name'] = FILE_PREFIX.'_'.$shortcode.'.'.$fileExt;
$thumbName = $data['thumb'] = FILE_PREFIX.'_'.$shortcode.'_thumb.'.$fileExt;
$moThumbName = $data['mothumb'] = FILE_PREFIX.'_'.$shortcode.'_mothumb.'.$fileExt;
// add data to session
$this->_imageSession->add($data);
// auto crop
$imageCropper1 = new ImageCropper(DOCUMENT_IMAGE_ROOT.'uploads/'.$original, $largeName);
$imageCropper1->setSides($this->_dim['lw'],$this->_dim['lh']);
$imageCropper1->createImage();
// cropped by hand
$imageCropper2 = new ImageCropper(DOCUMENT_IMAGE_ROOT.'uploads/'.$original, $thumbName, TRUE);
$imageCropper2->setSides($this->_dim['tw'],$this->_dim['th']);
$imageCropper2->setSrcSides($w,$h);
$imageCropper2->setSrcPosition($x,$y);
$imageCropper2->createImage();
$imageCropper3 = new ImageCropper(DOCUMENT_IMAGE_ROOT.'uploads/'.$thumbName, $moThumbName);
$imageCropper3->setSides($this->_dim['mw'],$this->_dim['mh']);
$imageCropper3->createImage();
$this->unlink(DOCUMENT_IMAGE_ROOT.'uploads/'.$original);
}
unset($_SESSION['images_to_crop']);
unset($_FILES['images']);
$linkname = $this->_template->encodeUrl($this->_pageinfo['label']);
}
/**
* Removes images from the database that are not in the session anymore
*
* @access private
* @param array
* @return null
*/
public function removeStoredImages($itemId)
{
if($arr = $this->_imageStorage->get('itemId',$itemId))
{
foreach($arr as $data)
{
if( ! $this->_imageSession->get('shortcode',$data['shortcode']))
{
$this->_imageStorage->remove($data);
}
}
}
}
/**
* Removes images that are not used from the server and from the database
*
* @access private
* @param
* @return null
*/
public function clean()
{
if(isset($_SESSION['images_to_store']))
{
foreach($this->_imageSession->get() as $data)
{
if( ! $this->_imageStorage->get('shortcode',$data['shortcode']))
{
if( ! empty($data['name']) && is_file(DOCUMENT_IMAGE_ROOT.'uploads/'.$data['name']) ) unlink(DOCUMENT_IMAGE_ROOT.'uploads/'.$data['name']);
if( ! empty($data['thumb']) && is_file(DOCUMENT_IMAGE_ROOT.'uploads/'.$data['thumb']) ) unlink(DOCUMENT_IMAGE_ROOT.'uploads/'.$data['thumb']);
if( ! empty($data['mothumb']) && is_file(DOCUMENT_IMAGE_ROOT.'uploads/'.$data['mothumb']) ) unlink(DOCUMENT_IMAGE_ROOT.'uploads/'.$data['mothumb']);
}
}
unset($_SESSION['images_to_store']);
}
if(isset($_SESSION['images_to_crop']))
{
unset($_SESSION['images_to_crop']);
}
}
/**
* Unlinks the image
*
* @access public
* @param
* @return boolean
*/
public function unlink($path)
{
if( ! empty($path) && is_file($path) ) unlink($path);
}
/*
CREATE PAGE CONTENT
*/
/**
* Returns an string of listitems
*
* @access public
* @param array
* @return
*/
public function createList()
{
$output = '';
$i = 1;
foreach($this->_imageSession->get() as $key => $data)
{
$output .= '<li class="image-container';
if( $i % 4 == 0 && $i > 0) $output .= ' no-margin-right';
$output .= '">';
$output .= '<div class="holder">';
$output .= '<img src="'.IMAGE_ROOT.'uploads/'.$data['mothumb'].'" alt="image" />';
$output .= '</div>';
$output .= '<div class="remove">';
$linkname = $this->_template->encodeUrl($this->_pageinfo['label']);
$output .= '<a href="'.SITE_ROOT.$linkname.'/remove-image/'.$key.'/">';
$output .= '<img src="'.IMAGE_ROOT.'structure/remove.png" alt="remove image" />';
$output .= '</a>';
$output .= '</div>';
$output .= '</li>';
$i++;
}
$this->_template->setData('mp_add_image_list',$output, TRUE);
}
}