ACC SHELL
<?php
/**
* Image manipulation class, helps creating thumbnails.
*
*/
class TF_ImageManipulate
{
private static $_allowedTypes = array('jpg' , 'jpeg' , 'gif' , 'png');
/**
* __contruct
*/
private function __contruct ()
{} /* of __contruct -------------------------------------*/
/**
* getImageDimensions
*/
public static function getImageDimensions ($path)
{
$data = getimagesize($path);
if (! $data) {
throw new TF_Exception('Nelze načíst informace o obrázku.', 404);
}
$res['width'] = $data[0];
$res['height'] = $data[1];
$res['imagetype'] = $data[2];
$res['html_dimensions'] = $data[3];
$res['mime'] = $data['mime'];
return $res;
} /* of getImageDimensions -------------------------------------*/
/**
* createImage
*/
public static function createImageFromFile ($path)
{
$ext = pathinfo($path, PATHINFO_EXTENSION);
switch ($ext) {
case "jpg":
case "jpeg":
$img = imagecreatefromjpeg($path);
break;
case "gif":
$img = imagecreatefromgif($path);
break;
case "png":
$img = imagecreatefrompng($path);
break;
default:
throw new Zend_Exception("Naplatná přípona obrázku", 500);
break;
}
if (! $img) {
$img = imagecreatetruecolor(320, 240); /* Create a black image */
$bgc = imagecolorallocate($this->_image, 255, 255, 255);
$tc = imagecolorallocate($this->_image, 0, 0, 0);
imagefilledrectangle($this->_image, 0, 0, 320, 240, $bgc);
imagestring($this->_image, 3, 5, 5, "Nelze nacíst soubor $path", $tc);
}
return $img;
} /* of createImage -------------------------------------*/
/**
* createThumbnail
*/
public function resample ($imagePath, $longerSide, $dontResizeSmaller = true, $saveSmaller = true)
{
$image = self::createImageFromFile($imagePath);
$data = self::getImageDimensions();
if (! $image) {
throw new Zend_Exception("Obrázek je pravděpodobně poškozen.", 500);
}
if ($data["width"] < $data['height']) { //když je vyšší než širší
if ($longerSide > $data['height']) { //
$noResize = true;
$perc = $longerSide / $data['height'];
} else {
$noResize = false;
$perc = $longerSide / $data['height'];
}
} else { // když je širší než vyšší
if ($longerSide > $data['width']) {
$noResize = true;
$perc = $longerSide / $data['width'];
} else {
$noResize = false;
$perc = $longerSide / $data['width'];
}
}
if ((! $noResize) || (! $dontResizeSmaller)) {
$newwidth = round($data['width'] * $perc);
$newheight = round($data['height'] * $perc);
$tmp = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($tmp, $this->_image, 0, 0, 0, 0, $newwidth, $newheight, $data['width'],
$data['height']);
return $tmp;
} else if(1){ //FIXME Dodělat !
}
return false;
} /* of createThumbnail -------------------------------------*/
/**
* outImage
*/
public static function outImage ($image, $path = null)
{
if ($dynamic) {
switch ($ext) {
case "jpeg":
case "jpg":
header("Content-type: image/jpeg");
imagejpeg($tmp);
break;
case "gif":
header("Content-type: image/gif");
imagegif($tmp);
break;
case "png":
header("Content-type: image/jpeg");
imagepng($tmp);
break;
default:
throw new TF_Exception("Neznámý typ souboru!", 500);
break;
}
die();
} else {
switch ($ext) {
case "jpg":
case "jpeg":
imagejpeg($tmp, $this->_thumbFilename, 75);
unset($tmp);
break;
default:
throw new TF_Exception(
"Ukládání není podporováno pro jiné než JPEG...",
500);
break;
}
}
} /* of outImage -------------------------------------*/
/**
* createMiddleImage
*/
public function createMiddleImage ($longerSide = 266, $dynamic = false)
{
if (! $this->_image) {
throw new Zend_Exception("Nelze vytvořit náhled, pokud není načten originál!", 500);
}
if ($this->imageData["w"] < $data['height']) {
if ($longerSide > $data['width']) {
$noResize = true;
} else {
$noResize = false;
$perc = $longerSide / $data['width'];
}
} else {
if ($longerSide > $data['height']) {
$noResize = true;
} else {
$noResize = false;
$perc = $longerSide / $data['height'];
}
}
if (! $noResize) {
$newwidth = round($data['width'] * $perc);
$newheight = round($data['height'] * $perc);
$tmp = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($tmp, $this->_image, 0, 0, 0, 0, $newwidth, $newheight, $data['width'],
$data['height']);
} else {
$tmp = $this->_image;
}
if ($dynamic) {
switch ($ext) {
case "jpeg":
case "jpg":
header("Content-type: image/jpeg");
imagejpeg($tmp);
break;
case "gif":
header("Content-type: image/gif");
imagegif($tmp);
break;
case "png":
header("Content-type: image/jpeg");
imagepng($tmp);
break;
default:
throw new TF_Exception("Neznámý typ souboru!", 500);
break;
}
die();
} else {
switch ($ext) {
case "jpg":
case "jpeg":
imagejpeg($tmp, $this->_middleFilename, 75);
unset($tmp);
break;
default:
throw new TF_Exception(
"Ukládání není podporováno pro jiné než JPEG...",
500);
break;
}
}
} /* of createMiddleImage -------------------------------------*/
/**
* getThumbFilename
*/
public function getThumbFilename ()
{
return $this->_thumbFilename;
} /* of getThumbFilename -------------------------------------*/
/**
* getMiddleFilename
*/
public function getMiddleFilename ()
{
return $this->_middleFilename;
} /* of getMiddleFilename -------------------------------------*/
/**
* getImageFilename
*/
public function getImageFilename ()
{
return $this->_imageFilename;
} /* of getImageFilename -------------------------------------*/
/**
* dump
*/
public function dump ()
{
TF_Debug::dump($this);
} /* of dump -------------------------------------*/
}
ACC SHELL 2018