| Server IP : 172.67.178.83 / Your IP : 216.73.217.141 Web Server : Apache System : Linux hosting01.arsenalhost.com 4.18.0-425.13.1.lve.el8.x86_64 #1 SMP Mon Feb 27 15:23:24 EST 2023 x86_64 User : corbizre ( 1013) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /home/corbizre/gotofindhomes.com3/components/Image/ |
Upload File : |
<?php
/**
* Documentation
* @author keeper, djes
* @category
* @package
* @subpackage
* @copyright Copyright (c) 2005-2011 ITCrimea Ukraine Inc. (http://www.itcrimea.com)
* @license
*/
class Image extends System_Db_Object
{
const EXTERNAL_MONGODB = 1;
/**
* predelete - remove all thumbnails
* @todo finish
* @author keeper, djes
* @return mixed
*/
protected function _delete()
{
$path_to_file = Zend_Registry::get('AppFolder').$this->Path.$this->File;
if (is_file($path_to_file)){
unlink($path_to_file);
}
$this->deleteThumbs();
}
public function deleteThumbs()
{
$listThumbs = $this->findDependentRowset('Image_Thumb_Table');
foreach ($listThumbs as $objectThumb){
$objectThumb->delete();
}
}
/**
* calculate (based on image ID) and returns the image path
* @author keeper
* @return string
*/
function getPath()
{
$strPath = floor($this->ID / 100) * 100;
$strPath = str_pad($strPath, 8, '0', STR_PAD_LEFT);
$config = System_Components::getComponentConfig('Image');
return $config->Image->Path . $strPath . '/';
}
/**
* returns the temporary image path
* @author keeper
* @return string
*/
public static function getPathTemp($Absolute=true)
{
$config = System_Components::getComponentConfig('Image');
if (!$Absolute) return $config->Image->PathTemp;
return Zend_Registry::get('AppFolder').$config->Image->PathTemp;
}
/**
* get list of assigns components
* @author keeper
* @return array
*/
function getAssignsList()
{
$config = System_Components::getComponentConfig('Image');
return $config->Image->Assigns;
}
/**
* Calculate the thumbnail size based on requested params
* @author keeper
* @param int $Width
* @param int $Height
* @param int $WidthMax
* @param int $HeightMax
* @return array
*/
function calcThumbParams($Width=0, $Height=0, $WidthMax=0, $HeightMax=0)
{
$x = 0;
$y = 0;
if ($Width > 0 && !$Height) {
$koef = $Width/$this->Width;
$Height = round($koef * $this->Height);
if ($HeightMax > 0 && $Height > $HeightMax) {
$Height = $HeightMax;
//$Width = round($this->Width * $Height/$this->Height);
$y = ceil( ($this->Height - ceil($Height/$koef) ) / 2 );
$this->Height = ceil($Height/$koef);
}
} elseif (!$Width && $Height>0) {
$koef = $Height/$this->Height;
$Width = round($koef * $this->Width);
if ($WidthMax > 0 && $Width > $WidthMax) {
$Width = $WidthMax;
//$Height = round($this->Height * $Width/$this->Width);
$x = ceil( ($this->Width - ceil($Width/$koef) ) / 2 );
$this->Width = ceil($Width/$koef);
}
} elseif ($Width>0 && $Height>0) {
$koef = max($Width/$this->Width, $Height/$this->Height);
if (ceil($Width/$koef) < $this->Width) {
$x = ceil( ($this->Width - ceil($Width/$koef) ) / 2 );
$this->Width = ceil($Width/$koef);
}
if (ceil($Height/$koef) < $this->Height) {
$y = ceil( ($this->Height - ceil($Height/$koef) ) / 2 );
$this->Height = ceil($Height/$koef);
}
} elseif ($WidthMax > 0 || $HeightMax > 0) {
if ($WidthMax >= $this->Width && $HeightMax >= $this->Height) {
$Height = $this->Height;
$Width = $this->Width;
} else {
if (!$HeightMax) $HeightMax = $this->Height;
if (!$WidthMax) $WidthMax = $this->Width;
$koef = min($WidthMax/$this->Width, $HeightMax/$this->Height);
$Height = round($koef * $this->Height) ;
$Width = round($koef * $this->Width) ;
}
} else {
$Height = $this->Height;
$Width = $this->Width;
}
return $arrThumbParams = array('Width'=>$Width, 'Height'=>$Height, 'X'=>$x, 'Y'=>$y);
}
/**
* generate thumbnail filename based on original image name and thumbnail size
* @author keeper
* @param int $Width
* @param int $Height
* @return string
*/
function getThumbName($Width, $Height)
{
$Name = substr($this->File, 0, strrpos($this->File, '.'));
//$Extension = substr($this->File, strrpos($this->File, '.')+1);
return $Name.'-'.intval($Width).'x'.intval($Height).'.jpg';
}
/**
* generated thumbnail from original image by requested params
* @author keeper
* @param array $arrThumbParams
* @return Image_Thumb|null
*/
function generateThumb($arrThumbParams)
{
$config = System_Components::getComponentConfig('Image');
$tableThumb = new Image_Thumb_Table();
$objThumb = $tableThumb->fetchNew();
$objThumb->Image_ID = $this->ID;
$objThumb->Width = $arrThumbParams['Width'];
$objThumb->Height = $arrThumbParams['Height'];
$objThumb->File = '';
$objThumb->Path = '';
$objThumb->save();
$objThumb->Path = $objThumb->getPath();
$objThumb->File = $this->getThumbName($arrThumbParams['Width'], $arrThumbParams['Height']);
if (!is_dir(Zend_Registry::get('AppFolder').$objThumb->Path)) {
if (!mkdir(Zend_Registry::get('AppFolder').$objThumb->Path, 0777, true)) {
$objThumb->delete();
return;
}
}
$Nameonly = substr($objThumb->File, 0, strrpos($objThumb->File, '.'));
$Extension = substr($objThumb->File, strrpos($objThumb->File, '.')+1);
$i = 0;
while (file_exists(Zend_Registry::get('AppFolder').$objThumb->Path.$objThumb->File)) {
$objThumb->File = $Nameonly.$i.'.'.$Extension;
$i++;
}
$objThumb->save();
$original = Image::create(Zend_Registry::get('AppFolder').$this->Path.$this->File);
$thumb = imagecreatetruecolor($arrThumbParams['Width'], $arrThumbParams['Height']);
imagecopyresampled($thumb, $original, 0, 0, $arrThumbParams['X'], $arrThumbParams['Y'], $arrThumbParams['Width'], $arrThumbParams['Height'], $this->Width, $this->Height);
imagejpeg($thumb, Zend_Registry::get('AppFolder').$objThumb->Path.$objThumb->File, $config->Thumb->Quality);
chmod(Zend_Registry::get('AppFolder').$objThumb->Path.$objThumb->File, 0777);
imagedestroy($original);
imagedestroy($thumb);
return $objThumb;
}
/**
* try to move file from $PathSource to $PathDest and returns the moved File Name
* @author keeper
* @param string $Filename
* @param string $PathSource
* @param string $PathDest
* @return string|null Filename on success or NULL on error
*/
public static function moveFile($Filename, $PathSource, $PathDest)
{
if (!file_exists($PathSource . $Filename)) {
return;
}
if (!is_dir($PathDest)) {
if (!mkdir($PathDest, 0777, true)) {
return;
}
}
$Nameonly = substr($Filename, 0, strrpos($Filename, '.'));
$Extension = substr($Filename, strrpos($Filename, '.')+1);
$FilenameDest = $Filename;
$i = 0;
while (file_exists($PathDest . $FilenameDest)) {
$FilenameDest = $Nameonly.$i.'.'.$Extension;
$i++;
}
if (rename($PathSource.$Filename, $PathDest.$FilenameDest)) {
return $FilenameDest;
} else {
// try to set up permissions
chmod($PathDest, 0777);
if (rename($PathSource.$Filename, $PathDest.$FilenameDest)) {
return $FilenameDest;
}
}
return;
}
/**
* Create a new image from file or URL
* @todo add more supported formats
* @author keeper
* @param string $File path to file
* @return mixed Image identifier representing the image obtained from the given filename
*/
public static function create($File)
{
$Pathinfo = pathinfo($File);
switch(strtolower($Pathinfo["extension"]))
{
case "jpeg":
case "jpg":
$Image = imagecreatefromjpeg($File);
break;
case "gif":
$Image = imagecreatefromgif($File);
break;
case "png":
$Image = imagecreatefrompng($File);
break;
case 'bmp':
$Image = imagecreatefromwbmp($File);
break;
default:
return false;
}
return $Image;
}
/**
* download image by url
*/
public static function download($Source)
{
$config = System_Components::getComponentConfig('Image');
$tableImage = new Image_Table();
$objImage = $tableImage->fetchNew();
$Dirname = Zend_Registry::get('AppFolder').$config->Image->PathTemp;
if (!is_dir($Dirname)) {
if (!mkdir($Dirname, 0777, true)) {
return null;
}
}
$objImage->File = basename($Source);
if (strpos($objImage->File, '?')) {
$objImage->File = substr($objImage->File, 0, strrpos($objImage->File, '?'));
}
if (!$objImage->File) {
return null;
}
if (strpos($objImage->File, '.') === false) {
return null;
}
// get extension and check
$extension = substr($objImage->File, strrpos($objImage->File, '.')+1);
if (!in_array(strtolower($extension), array('bmp','jpeg','jpg','tif','tiff','png','gif'))) {
return null;
}
$nameonly = substr($objImage->File, 0, strrpos($objImage->File, '.'));
$i = 0;
while (file_exists($Dirname . $objImage->File)) {
$objImage->File = $nameonly.$i.'.'.$extension;
$i++;
}
// move file to temporary folder
file_put_contents($Dirname . $objImage->File, file_get_contents($Source));
// convert tiff file
if (in_array(strtolower($extension), array('bmp','tif','tiff'))) {
$image = new Imagick($Dirname.$objImage->File);
$image->setImageFormat('jpeg');
$image->writeImage($Dirname.$nameonly.'.jpg');
unlink($Dirname.$objImage->File);
$objImage->File = $nameonly.'.jpg';
}
$objImage->Path = '';
$objImage->save();
// move file to destination folder
$objImage->Path = $objImage->getPath();
$objImage->File = Image::moveFile($objImage->File, $Dirname, Zend_Registry::get('AppFolder').$objImage->Path);
if ($objImage->File) {
$Image = Image::create(Zend_Registry::get('AppFolder').$objImage->Path.$objImage->File);
$objImage->Width = imagesx($Image);
$objImage->Height = imagesy($Image);
$objImage->save();
return $objImage;
} else {
$objImage->delete();
return null;
}
}
/*
* convert tiff to jpeg
*/
public static function tifftojpeg($Source)
{
}
}