File: D:/HostingSpaces/SBogers68/resortouddorpduin.nl/wwwroot/kms/lib/files/image_cropper.class.php
<?php
/*
Handles all tasks regarding the cropping and recreating of an image
*/
class Image_Cropper
{
private $_src,
$_name,
$_uploadTarget,
$_manual,
$_trgtW,
$_trgtH,
$_srcW,
$_srcH,
$_srcX,
$_srcY,
$_dstW,
$_dstH,
$_dstX,
$_dstY;
public function __construct($src, $name, $manual = FALSE)
{
$this->_src = $src;
$this->_name = $name;
$this->_manual = $manual;
list($this->_srcW, $this->_srcH) = getimagesize($this->_src);
$this->_uploadTarget = DOCUMENT_UPLOADS_ROOT;
$this->_srcX = $this->_srcY = $this->_dstX = $this->_dstY = $this->_dstW = $this->_dstH = 0;
}
/*
GET / SET
*/
/**
* Sets the name of the image
*
* @access public
* @param string
* @return null
*/
public function setName($input)
{
if(is_string($input))
{
$this->_name = $input;
}
}
/**
* Sets the variable to crop manual or automatic
*
* @access public
* @param boolean
* @return null
*/
public function setManual($input)
{
if(is_bool($input))
{
$this->_manual = $input;
}
}
/**
* Sets the width and height of the destination image
*
* @access public
* @param int, int
* @return null
*/
public function setSides($intW, $intH)
{
$this->_trgtW = $intW;
$this->_trgtH = $intH;
}
public function setSrcSides($intW, $intH)
{
$this->_srcW = $intW;
$this->_srcH = $intH;
}
/**
* Sets the x and y of the source image
*
* @access public
* @param int, int
* @return null
*/
public function setSrcPosition($intX, $intY)
{
if(is_numeric($intX) && is_numeric($intY))
{
$this->_srcX = $intX;
$this->_srcY = $intY;
}
}
/**
* Sets the x and y of the destination image
*
* @access public
* @param int, int
* @return null
*/
public function setDstPosition($intX, $intY)
{
if(is_numeric($intX) && is_numeric($intY))
{
$this->_dstX = $intX;
$this->_dstY = $intY;
}
}
/*
IMAGE HANDLING
*/
/**
* Creates the image
*
* @access public
* @param
* @return string
*/
public function createImage()
{
if( ! $this->_manual){
$this->calculateCrop();
}
else{
$this->_dstW = $this->_trgtW;
$this->_dstH = $this->_trgtH;
}
$temp = explode('.', $this->_src);
$fileExt = strtolower($temp[(count($temp) - 1)]);
switch($fileExt){
case 'jpg':
case 'jpeg':
$img_raw = imagecreatefromjpeg($this->_src);
break;
case 'png':
$img_raw = imagecreatefrompng($this->_src);
break;
case 'gif':
$img_raw = imagecreatefromgif($this->_src);
break;
}
$dst_r = imagecreatetruecolor($this->_trgtW, $this->_trgtH);
imagealphablending($dst_r, FALSE);
imagesavealpha($dst_r, TRUE);
//echo $dst_r.', '.$img_raw.', dstX:'. $this->_dstX.', dstY:'. $this->_dstY.', srcX:'. $this->_srcX.', srcY:'. $this->_srcY.', dstW:'. $this->_dstW.', dstH:'. $this->_dstH.', srcW:'. $this->_srcW.', srcH:'. $this->_srcH;
imagecopyresampled($dst_r, $img_raw, $this->_dstX, $this->_dstY, $this->_srcX, $this->_srcY, $this->_dstW, $this->_dstH, $this->_srcW, $this->_srcH );
$path = $this->_uploadTarget.urlencode($this->_name);
$valid = FALSE;
if($fileExt == 'png' || $fileExt == 'gif')
{
if(imagepng($dst_r, $path, 8))
{
$valid = TRUE;
}
}
else if(imagejpeg($dst_r, $path, 90))
{
$valid = TRUE;
}
return $valid;
}
private function calculateCrop()
{
// option 1; crop by width and height
if( ! empty($this->_trgtW) && ! empty($this->_trgtH))
{
if( ($this->_srcW / $this->_srcH) > ($this->_trgtW / $this->_trgtH) )
{
// dst image is less wide then src image
if($this->_trgtH <= $this->_srcH){
$this->_dstH = $this->_trgtH;
$this->_dstW = ceil(($this->_dstH * $this->_srcW) / $this->_srcH);
}
else{
// if target is larger then source, don't scale up
// don't scale height
$this->_dstH = $this->_trgtH = $this->_srcH;
if($this->_trgtW > $this->_srcW){
// don't scale with
$this->_dstW = $this->_trgtW = $this->_srcW;
}
else{
$this->_dstW = ceil( ($this->_dstH * $this->_srcW) / $this->_srcH );
}
}
$this->_dstX = ceil( ($this->_dstW - $this->_trgtW) / -2 );
$this->_dstY = 0;
}
else{
// dst image is wider then src image
if($this->_trgtW <= $this->_srcW){
$this->_dstW = $this->_trgtW;
$this->_dstH = ceil( ($this->_dstW * $this->_srcH) / $this->_srcW );
}
else{
// if target is larger then source, don't scale up
// don't scale width
$this->_dstW = $this->_trgtW = $this->_srcW;
if($this->_trgtH > $this->_srcH){
// don't scale height
$this->_dstH = $this->_trgtH = $this->_srcH;
}
else{
$this->_dstH = ceil( ($this->_dstW * $this->_srcH) / $this->_srcW );
}
}
$this->_dstX = 0;
$this->_dstY = ceil( ( $this->_dstH - $this->_trgtH ) / -2);
}
}
// option 2; crop by width
else if ( ! empty($this->_trgtW) && empty($this->_trgtH))
{
if($this->_trgtW <= $this->_srcW)
{
$this->_dstW = $this->_trgtW;
}
else
{
$this->_dstW = $this->_trgtW = $this->_srcW;
}
$this->_dstH = $this->_trgtH = ceil( ($this->_dstW * $this->_srcH) / $this->_srcW);
$this->_dstX = 0;
$this->_dstY = 0;
}
// option 3; crop by height
else if (empty($this->_trgtW) && ! empty($this->_trgtH))
{
if($this->_trgtH <= $this->_srcH)
{
$this->_dstH = $this->_trgtH;
}
else
{
$this->_dstH = $this->_trgtH = $this->_srcH;
}
$this->_dstW = $this->_trgtW = ceil( ($this->_dstH * $this->_srcW) / $this->_srcH);
$this->_dstX = 0;
$this->_dstY = 0;
}
// option 4; don't crop
else if (empty($this->_trgtW) && empty($this->_trgtH))
{
$this->_dstW = $this->_trgtW = $this->_srcW;
$this->_dstH = $this->_trgtH = $this->_srcH;
$this->_dstX = 0;
$this->_dstY = 0;
}
}
}