ACC SHELL
<?php
/**
* Image manipulation class, helps creating thumbnails.
*
*/
class TF_Image
{
private $_image = null;
private $_imageFilename = null;
private $_thumbFilename = null;
private $_middleFilename = null;
private $_manipulator = null;
private $_ext = '';
function __construct ($path, $createImage = false)
{
$this->_manipulator = new TF_ImageManipulate();
if (file_exists($path)) {
$this->_imageFilename = $path;
} else {
throw new TF_Exception("Obrázek ($path) neexistuje!", 500);
}
list ($this->_imageData["w"], $this->_imageData["h"]) = getimagesize($path);
$this->_ext = str_replace(".", "", substr($this->_imageFilename, - 4));
if ($createImage) {
$this->createImage();
}
$patharr = explode("/", $this->_imageFilename);
$size = sizeof($patharr);
$patharr[$size - 1] = "t_" . $patharr[$size - 1];
$this->_thumbFilename = implode("/", $patharr);
$patharr = explode("/", $this->_imageFilename);
$size = sizeof($patharr);
$patharr[$size - 1] = "m_" . $patharr[$size - 1];
$this->_middleFilename = implode("/", $patharr);
}
/**
* createImage
*/
public function createImage ()
{
switch ($this->_ext) {
case "jpg":
case "jpeg":
$this->_image = imagecreatefromjpeg($this->_imageFilename);
break;
case "gif":
$this->_image = imagecreatefromgif($this->_imageFilename);
break;
case "png":
$this->_image = imagecreatefrompng($this->_imageFilename);
break;
default:
throw new Zend_Exception("Naplatná přípona obrázku", 500);
break;
}
if (! $this->_image) {
$this->_image = 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, 150, 30, $bgc);
imagestring($this->_image, 3, 5, 5, "Nelze nacíst $this->_imageFilename", $tc);
}
} /* of createImage -------------------------------------*/
/**
* createThumbnail
*/
public function createThumbnail ($longerSide = 228, $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"] < $this->_imageData["h"]) { //když je vyšší než širší
if ($longerSide > $this->_imageData["h"]) { //
$noResize = true;
} else {
$noResize = false;
$perc = $longerSide / $this->_imageData["h"];
}
} else { // když je širší než vyšší
if ($longerSide > $this->_imageData["w"]) {
$noResize = true;
} else {
$noResize = false;
$perc = $longerSide / $this->_imageData["w"];
}
}
if (! $noResize) {
$newwidth = round($this->_imageData["w"] * $perc);
$newheight = round($this->_imageData["h"] * $perc);
$tmp = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($tmp, $this->_image, 0, 0, 0, 0, $newwidth, $newheight, $this->_imageData["w"],
$this->_imageData["h"]);
if ($dynamic) {
switch ($this->_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 ($this->_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;
}
}
} else {
throw new TF_Exception("Obrázek je již zmenšený!", 500);
}
} /* of createThumbnail -------------------------------------*/
/**
* 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"] < $this->_imageData["h"]) {
if ($longerSide > $this->_imageData["w"]) {
$noResize = true;
} else {
$noResize = false;
$perc = $longerSide / $this->_imageData["w"];
}
} else {
if ($longerSide > $this->_imageData["h"]) {
$noResize = true;
} else {
$noResize = false;
$perc = $longerSide / $this->_imageData["h"];
}
}
if (! $noResize) {
$newwidth = round($this->_imageData["w"] * $perc);
$newheight = round($this->_imageData["h"] * $perc);
$tmp = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($tmp, $this->_image, 0, 0, 0, 0, $newwidth, $newheight, $this->_imageData["w"],
$this->_imageData["h"]);
} else {
$tmp = $this->_image;
}
if ($dynamic) {
switch ($this->_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 ($this->_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