369 lines
11 KiB
PHP
369 lines
11 KiB
PHP
<?php
|
|
/**
|
|
* Created by CS medien- & kommunikationssysteme.
|
|
* @author Christian Steinle
|
|
* @date 05.10.2016
|
|
*
|
|
* @copyright CS medien- & kommunikationssysteme (http://www.steinle-computer.de)
|
|
*/
|
|
|
|
namespace Model;
|
|
|
|
|
|
use Helper\Database;
|
|
use Helper\Registry;
|
|
|
|
class ImageModel extends Database
|
|
{
|
|
const TBL_NAME = TBL_IMAGE;
|
|
|
|
const PRIMARY_KEY = 'imageID';
|
|
|
|
const ORDER_BY = '';
|
|
|
|
/**
|
|
* Erzeugt die benötigten Bilder im Medien-Pfad und bereitet den Request auf
|
|
* @author Christian Steinle
|
|
*
|
|
* @param array $request
|
|
*/
|
|
protected static function beforeInsert(array &$request)
|
|
{
|
|
if (isset($request['imageTextModel']) && $request['imageTextModel'] === true)
|
|
{
|
|
return;
|
|
}
|
|
|
|
$registry = Registry::getInstance();
|
|
$navID = $request['navID'];
|
|
unset($request['navID']);
|
|
|
|
$mediaPath = $registry->editorConfig['mediaPath'] . $navID . '/';
|
|
if (!is_dir($mediaPath))
|
|
{
|
|
mkdir($mediaPath, 0775, true);
|
|
}
|
|
|
|
ImageModel::createOrigImage($mediaPath, $request);
|
|
ImageModel::createWebImage($mediaPath, $request, 'keyVisual');
|
|
}
|
|
|
|
/**
|
|
* Erzeugt einen Unique-Filename in einem vorgegebenen Pfad
|
|
* @author Christian Steinle
|
|
* @param string $path
|
|
* @param string $filename
|
|
* @return string
|
|
*/
|
|
public static function getUniqueFilename($path, $filename)
|
|
{
|
|
$filenameArray = self::filenameToArray($filename);
|
|
$filesInPath = glob($path . $filenameArray['filename'] . '*_orig.' . $filenameArray['extension']);
|
|
if (count($filesInPath) === 0)
|
|
{
|
|
return $filenameArray['filename'];
|
|
}
|
|
else
|
|
{
|
|
$newFilename = $filenameArray['filename'] . '_' . count($filesInPath);
|
|
return $newFilename;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Liefert ein Array zum übergebenen Dateinamen
|
|
* - 'filename' => Dateiname ohne Erweiterung
|
|
* - 'extension' => Datei-Erweiterung
|
|
* @author Christian Steinle
|
|
*
|
|
* @param string $filename
|
|
* @return array
|
|
*/
|
|
public static function filenameToArray($filename)
|
|
{
|
|
$tmpData = explode('.', $filename, -1);
|
|
$newFilename = implode('.', $tmpData);
|
|
$extension = str_replace($newFilename . '.', '', $filename);
|
|
return array('filename' => $newFilename, 'extension' => $extension);
|
|
}
|
|
|
|
/**
|
|
* Kopiert das Standard-Bild oder -KeyVisual
|
|
* @author Christian Steinle
|
|
*
|
|
* @param string $newPath
|
|
* @param string $filename
|
|
* @param string $type
|
|
*/
|
|
public static function copyStandardImage($newPath, $filename, $type)
|
|
{
|
|
$registry = Registry::getInstance();
|
|
switch ($type)
|
|
{
|
|
case 'image':
|
|
$imageData = $registry->editorConfig['imageDimension']['image'];
|
|
break;
|
|
|
|
case 'keyVisual':
|
|
$imageData = $registry->editorConfig['imageDimension']['keyVisual'];
|
|
break;
|
|
|
|
default:
|
|
return;
|
|
}
|
|
|
|
if (!is_dir($newPath))
|
|
{
|
|
mkdir($newPath, 0777, true);
|
|
}
|
|
|
|
$standardImagePath = substr(str_replace(PATH_PREFIX, '', PATH_ROOT), 0, -1) . $imageData['standardImage'];
|
|
$filenameArray = self::filenameToArray($filename);
|
|
|
|
copy($standardImagePath, $newPath . $filename);
|
|
copy($standardImagePath, $newPath . $filenameArray['filename'] . '_orig.' . $filenameArray['extension']);
|
|
self::createThumb($newPath, $filename);
|
|
}
|
|
|
|
/**
|
|
* Erstellt ein Thumbnail von einem Bild
|
|
* @author Christian Steinle
|
|
*
|
|
* @param string $path
|
|
* @param string $file
|
|
*/
|
|
public static function createThumb($path, $file)
|
|
{
|
|
$registry = Registry::getInstance();
|
|
$imageData = $registry->editorConfig['imageDimension']['thumb'];
|
|
$newWidth = $imageData['width'];
|
|
$newQuality = $imageData['quality'];
|
|
$filenameArray = self::filenameToArray($file);
|
|
|
|
list($width, $height) = getimagesize($path . $file);
|
|
$newHeight = $height * $newWidth / $width;
|
|
|
|
$newImage = imagecreatetruecolor($newWidth, $newHeight);
|
|
switch ($filenameArray['extension'])
|
|
{
|
|
case 'jpg':
|
|
$origImage = imagecreatefromjpeg($path . $file);
|
|
imagecopyresampled($newImage, $origImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
|
|
imagejpeg($newImage, $path . $filenameArray['filename'] . '_thumb.jpg', $newQuality);
|
|
break;
|
|
|
|
case 'png':
|
|
$origImage = imagecreatefrompng($path . $file);
|
|
imagecopyresampled($newImage, $origImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
|
|
imagepng($newImage, $path . $filenameArray['filename'] . '_thumb.png');
|
|
break;
|
|
|
|
case 'gif':
|
|
$origImage = imagecreatefromgif($path . $file);
|
|
imagecopyresampled($newImage, $origImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
|
|
imagegif($newImage, $path . $filenameArray['filename'] . '_thumb.gif');
|
|
break;
|
|
|
|
default:
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Löscht vorhandene Bilder vor entfernen aus der Datenbank
|
|
* @author Christian Steinle
|
|
*
|
|
* @param int $id
|
|
*/
|
|
protected static function beforeDelete($id)
|
|
{
|
|
$registry = Registry::getInstance();
|
|
$data = self::getItem($id);
|
|
|
|
$mediaPath = $registry->editorConfig['mediaPath'] . $registry->navID . '/';
|
|
ImageModel::deleteImages($mediaPath, $data['imageName'], $data['imageExtension']);
|
|
}
|
|
|
|
/**
|
|
* Löscht die Bilder aus dem Medienpfad
|
|
* @author Christian Steinle
|
|
*
|
|
* @param $mediaPath
|
|
* @param $imageName
|
|
* @param $imageExtension
|
|
*/
|
|
public static function deleteImages($mediaPath, $imageName, $imageExtension)
|
|
{
|
|
unlink($mediaPath . $imageName . '.' . $imageExtension);
|
|
unlink($mediaPath . $imageName . '_orig.' . $imageExtension);
|
|
unlink($mediaPath . $imageName . '_thumb.' . $imageExtension);
|
|
}
|
|
|
|
/**
|
|
* Verschiebt / Erzeugt Bilder und generiert Derivate
|
|
* @author Christian Steinle
|
|
*
|
|
* @param array $request
|
|
*/
|
|
protected static function beforeUpdate(array &$request)
|
|
{
|
|
$registry = Registry::getInstance();
|
|
$mediaPath = $registry->editorConfig['mediaPath'] . $registry->navID . '/';
|
|
|
|
$imageType = 'image';
|
|
if (isset($request['keyVisualID']))
|
|
{
|
|
$imageType = 'keyVisual';
|
|
}
|
|
|
|
$oldData = self::getItem($request[static::PRIMARY_KEY]);
|
|
|
|
if (isset($_FILES['changeUploadFile']) && $_FILES['changeUploadFile']['tmp_name'] !== '' && $_FILES['changeUploadFile']['error'] === 0)
|
|
{
|
|
ImageModel::deleteImages($mediaPath, $oldData['imageName'], $oldData['imageExtension']);
|
|
$request['imageName'] = ImageModel::getUniqueFilename($mediaPath, $request['imageName'] . '.' . $request['imageExtension']);
|
|
ImageModel::createOrigImage($mediaPath, $request);
|
|
ImageModel::createWebImage($mediaPath, $request, $imageType);
|
|
}
|
|
elseif (isset($_FILES['changeUploadFile']) && $_FILES['changeUploadFile']['tmp_name'] === '' && $oldData['imageName'] !== $request['imageName'])
|
|
{
|
|
$request['imageName'] = ImageModel::getUniqueFilename($mediaPath, $request['imageName'] . '.' . $request['imageExtension']);
|
|
rename($mediaPath . $oldData['imageName'] . '_orig.' . $oldData['imageExtension'], $mediaPath . $request['imageName'] . '_orig.' . $request['imageExtension']);
|
|
rename($mediaPath . $oldData['imageName'] . '.' . $oldData['imageExtension'], $mediaPath . $request['imageName'] . '.' . $request['imageExtension']);
|
|
rename($mediaPath . $oldData['imageName'] . '_thumb.' . $oldData['imageExtension'], $mediaPath . $request['imageName'] . '_thumb.' . $request['imageExtension']);
|
|
}
|
|
|
|
if (isset($_FILES['changeUploadFile']) &&
|
|
$_FILES['changeUploadFile']['tmp_name'] === '' &&
|
|
(abs($oldData['imageTop'] - $request['imageTop']) > .5 ||
|
|
abs($oldData['imageLeft'] - $request['imageLeft']) > .5 ||
|
|
abs($oldData['imageHeight'] - $request['imageHeight']) > .5 ||
|
|
abs($oldData['imageWidth'] - $request['imageWidth']) > .5)
|
|
)
|
|
{
|
|
unlink($mediaPath . $request['imageName'] . '.' . $request['imageExtension']);
|
|
unlink($mediaPath . $request['imageName'] . '_thumb.' . $request['imageExtension']);
|
|
ImageModel::createWebImage($mediaPath, $request, $imageType);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Legt ein hochgeladenes Bild im Medienpfad ab und verkleinert es, falls die Abmessungen zu groß sind
|
|
* @author Christian Steinle
|
|
*
|
|
* @param $mediaPath
|
|
* @param $request
|
|
*/
|
|
public static function createOrigImage($mediaPath, &$request)
|
|
{
|
|
$registry = Registry::getInstance();
|
|
$origData = $registry->editorConfig['imageDimension']['orig'];
|
|
|
|
$tmpName = $request['imageName'] . '__tmp__.' . $request['imageExtension'];
|
|
|
|
if (!is_dir($mediaPath))
|
|
{
|
|
mkdir($mediaPath, 0777, true);
|
|
}
|
|
|
|
move_uploaded_file($_FILES['changeUploadFile']['tmp_name'], $mediaPath . $tmpName);
|
|
|
|
list($width, $height) = getimagesize($mediaPath . $tmpName);
|
|
if ($width > $origData['width'] || $height > $origData['height'])
|
|
{
|
|
$newWidth = $origData['width'];
|
|
$newHeight = $height * $newWidth / $width;
|
|
|
|
$newImage = imagecreatetruecolor($newWidth, $newHeight);
|
|
switch ($request['imageExtension'])
|
|
{
|
|
case 'jpg':
|
|
$origImage = imagecreatefromjpeg($mediaPath . $tmpName);
|
|
imagecopyresampled($newImage, $origImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
|
|
imagejpeg($newImage, $mediaPath . $request['imageName'] . '_orig.jpg', $origData['quality']);
|
|
break;
|
|
|
|
case 'png':
|
|
$origImage = imagecreatefrompng($mediaPath . $tmpName);
|
|
imagecopyresampled($newImage, $origImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
|
|
imagepng($newImage, $mediaPath . $request['imageName'] . '_orig.png');
|
|
break;
|
|
|
|
case 'gif':
|
|
$origImage = imagecreatefromgif($mediaPath . $tmpName);
|
|
imagecopyresampled($newImage, $origImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
|
|
imagegif($newImage, $mediaPath . $request['imageName'] . '_orig.gif');
|
|
break;
|
|
|
|
default:
|
|
return;
|
|
}
|
|
|
|
unlink($mediaPath . $tmpName);
|
|
}
|
|
else
|
|
{
|
|
rename($mediaPath . $tmpName, $mediaPath . $request['imageName'] . '_orig.' . $request['imageExtension']);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Erzeugt das WebImage mit dem passenden Ausschnitt aus dem Original
|
|
* @author Christian Steinle
|
|
* TODO: Transparenz bei pngs
|
|
*
|
|
* @param $mediaPath
|
|
* @param $request
|
|
* @param $type
|
|
*/
|
|
public static function createWebImage($mediaPath, $request, $type)
|
|
{
|
|
$registry = Registry::getInstance();
|
|
switch ($type)
|
|
{
|
|
case 'image':
|
|
$imageData = $registry->editorConfig['imageDimension']['image'];
|
|
break;
|
|
|
|
case 'keyVisual':
|
|
$imageData = $registry->editorConfig['imageDimension']['keyVisual'];
|
|
break;
|
|
|
|
default:
|
|
return;
|
|
}
|
|
|
|
$origName = $request['imageName'] . '_orig.' . $request['imageExtension'];
|
|
$webName = $request['imageName'] . '.' . $request['imageExtension'];
|
|
list($width, $height) = getimagesize($mediaPath . $origName);
|
|
$newWidth = $imageData['width'];
|
|
$newHeight = $newWidth * $request['imageHeight'] / $request['imageWidth'];
|
|
|
|
$newImage = imagecreatetruecolor($newWidth, $newHeight);
|
|
switch ($request['imageExtension'])
|
|
{
|
|
case 'jpg':
|
|
$origImage = imagecreatefromjpeg($mediaPath . $origName);
|
|
imagecopyresampled($newImage, $origImage, 0, 0, $request['imageLeft'] * $width / $newWidth, $request['imageTop'] * $width / $newWidth, $newWidth, $newHeight, $width * $request['imageWidth'] / $newWidth, $width * $request['imageWidth'] * $newHeight / ($newWidth * $newWidth));
|
|
imagejpeg($newImage, $mediaPath . $webName, $imageData['quality']);
|
|
break;
|
|
|
|
case 'png':
|
|
$origImage = imagecreatefrompng($mediaPath . $origName);
|
|
imagecopyresampled($newImage, $origImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
|
|
imagepng($newImage, $mediaPath . $webName);
|
|
break;
|
|
|
|
case 'gif':
|
|
$origImage = imagecreatefromgif($mediaPath . $origName);
|
|
imagecopyresampled($newImage, $origImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
|
|
imagegif($newImage, $mediaPath . $webName);
|
|
break;
|
|
|
|
default:
|
|
return;
|
|
}
|
|
|
|
self::createThumb($mediaPath, $webName);
|
|
}
|
|
} |