2015-02-17 18:17:49 +01:00
|
|
|
<?php
|
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
class FileDataHelper
|
|
|
|
{
|
|
|
|
public static function getAudioMimeTypeArray()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'audio/ogg' => 'ogg',
|
|
|
|
'application/ogg' => 'ogg',
|
|
|
|
'audio/vorbis' => 'ogg',
|
|
|
|
'audio/mp3' => 'mp3',
|
|
|
|
'audio/mpeg' => 'mp3',
|
|
|
|
'audio/mpeg3' => 'mp3',
|
|
|
|
'audio/x-aac' => 'aac',
|
|
|
|
'audio/aac' => 'aac',
|
|
|
|
'audio/aacp' => 'aac',
|
|
|
|
'audio/mp4' => 'm4a',
|
|
|
|
'video/mp4' => 'mp4',
|
|
|
|
'audio/x-flac' => 'flac',
|
|
|
|
'audio/flac' => 'flac',
|
|
|
|
'audio/wav' => 'wav',
|
|
|
|
'audio/x-wav' => 'wav',
|
|
|
|
'audio/mp2' => 'mp2',
|
|
|
|
'audio/mp1' => 'mp1',
|
|
|
|
'audio/x-ms-wma' => 'wma',
|
|
|
|
'audio/basic' => 'au',
|
|
|
|
];
|
2015-07-07 21:48:22 +02:00
|
|
|
}
|
|
|
|
|
2022-09-21 14:04:56 +02:00
|
|
|
public static function getUploadAudioMimeTypeArray()
|
|
|
|
{
|
|
|
|
$mimes = self::getAudioMimeTypeArray();
|
|
|
|
unset($mimes['audio/x-ms-wma']);
|
|
|
|
|
2023-12-18 20:32:49 +01:00
|
|
|
// audio/opus is not a real mime type, we only set it here to allow uploading
|
|
|
|
// .opus files
|
|
|
|
$mimes['audio/opus'] = 'opus';
|
|
|
|
|
2022-09-21 14:04:56 +02:00
|
|
|
return $mimes;
|
|
|
|
}
|
|
|
|
|
2015-02-17 18:17:49 +01:00
|
|
|
/**
|
|
|
|
* We want to throw out invalid data and process the upload successfully
|
2021-10-11 16:10:47 +02:00
|
|
|
* at all costs, so check the data and sanitize it if necessary.
|
|
|
|
*
|
2015-02-17 18:17:49 +01:00
|
|
|
* @param array $data array containing new file metadata
|
|
|
|
*/
|
2015-02-18 22:29:08 +01:00
|
|
|
public static function sanitizeData(&$data)
|
|
|
|
{
|
2021-10-11 16:10:47 +02:00
|
|
|
if (array_key_exists('track_number', $data)) {
|
2015-02-18 22:29:08 +01:00
|
|
|
// If the track number isn't numeric, this will return 0
|
2021-10-11 16:10:47 +02:00
|
|
|
$data['track_number'] = intval($data['track_number']);
|
2015-02-18 22:29:08 +01:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
if (array_key_exists('year', $data)) {
|
2015-02-18 22:29:08 +01:00
|
|
|
// If the track number isn't numeric, this will return 0
|
2021-10-11 16:10:47 +02:00
|
|
|
$data['year'] = intval($data['year']);
|
2015-02-18 22:29:08 +01:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
if (array_key_exists('bpm', $data)) {
|
2022-03-14 11:15:04 +01:00
|
|
|
// Some BPM tags are silly and include the word "BPM". Let's strip that...
|
2021-10-11 16:10:47 +02:00
|
|
|
$data['bpm'] = str_ireplace('BPM', '', $data['bpm']);
|
2015-02-19 15:31:23 +01:00
|
|
|
// This will convert floats to ints too.
|
2021-10-11 16:10:47 +02:00
|
|
|
$data['bpm'] = intval($data['bpm']);
|
2015-02-19 15:31:23 +01:00
|
|
|
}
|
2022-07-27 09:52:09 +02:00
|
|
|
if (array_key_exists('track_type_id', $data)) {
|
|
|
|
$data['track_type_id'] = intval($data['track_type_id']);
|
|
|
|
}
|
2015-02-17 18:17:49 +01:00
|
|
|
}
|
2015-07-07 19:54:19 +02:00
|
|
|
|
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Return a suitable extension for the given file.
|
2015-07-07 19:54:19 +02:00
|
|
|
*
|
|
|
|
* @param string $mime
|
|
|
|
*
|
2021-10-11 16:10:47 +02:00
|
|
|
* @return string file extension with(!) a dot (for convenience)
|
2022-09-12 13:16:14 +02:00
|
|
|
*
|
|
|
|
* @throws Exception
|
2015-07-07 19:54:19 +02:00
|
|
|
*/
|
|
|
|
public static function getFileExtensionFromMime($mime)
|
|
|
|
{
|
2015-07-07 21:48:22 +02:00
|
|
|
$mime = trim(strtolower($mime));
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2015-07-07 21:48:22 +02:00
|
|
|
try {
|
2021-10-11 16:10:47 +02:00
|
|
|
return '.' . static::getAudioMimeTypeArray()[$mime];
|
2015-07-07 21:48:22 +02:00
|
|
|
} catch (Exception $e) {
|
2021-10-11 16:10:47 +02:00
|
|
|
throw new Exception("Unknown file type: {$mime}");
|
2015-07-07 19:54:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-20 02:07:50 +02:00
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Gets data URI from artwork file.
|
2019-09-20 02:07:50 +02:00
|
|
|
*
|
|
|
|
* @param string $file
|
2021-10-11 16:10:47 +02:00
|
|
|
* @param int $size
|
2019-09-20 02:07:50 +02:00
|
|
|
* @param string $filepath
|
|
|
|
*
|
|
|
|
* @return string Data URI for artwork
|
|
|
|
*/
|
|
|
|
public static function getArtworkData($file, $size, $filepath = false)
|
|
|
|
{
|
2022-07-07 23:32:43 +02:00
|
|
|
$baseUrl = Config::getPublicUrl();
|
2021-10-11 16:10:47 +02:00
|
|
|
$default = $baseUrl . 'css/images/no-cover.jpg';
|
2019-09-20 02:07:50 +02:00
|
|
|
|
|
|
|
if ($filepath != false) {
|
2021-10-11 16:10:47 +02:00
|
|
|
$path = $filepath . $file . '-' . $size;
|
2019-09-20 02:07:50 +02:00
|
|
|
if (!file_exists($path)) {
|
|
|
|
$get_file_content = $default;
|
|
|
|
} else {
|
2021-10-11 16:10:47 +02:00
|
|
|
$get_file_content = file_get_contents($path);
|
2019-09-20 02:07:50 +02:00
|
|
|
}
|
|
|
|
} else {
|
2022-04-18 20:34:38 +02:00
|
|
|
$path = Config::getStoragePath() . $file . '-' . $size;
|
2019-09-20 02:07:50 +02:00
|
|
|
if (!file_exists($path)) {
|
|
|
|
$get_file_content = $default;
|
|
|
|
} else {
|
2021-10-11 16:10:47 +02:00
|
|
|
$get_file_content = file_get_contents($path);
|
2019-09-20 02:07:50 +02:00
|
|
|
}
|
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2019-09-20 02:07:50 +02:00
|
|
|
return $get_file_content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Add artwork file.
|
2019-09-20 02:07:50 +02:00
|
|
|
*
|
|
|
|
* @param string $analyzeFile
|
|
|
|
* @param string $filename
|
|
|
|
* @param string $importDir
|
|
|
|
* @param string $DbPath
|
|
|
|
*
|
|
|
|
* @return string Path to artwork
|
|
|
|
*/
|
|
|
|
public static function saveArtworkData($analyzeFile, $filename, $importDir = null, $DbPath = null)
|
|
|
|
{
|
2019-10-13 19:07:06 +02:00
|
|
|
if (class_exists('getID3')) {
|
2024-01-07 13:59:02 +01:00
|
|
|
$getID3 = new getID3();
|
2019-10-13 19:07:06 +02:00
|
|
|
$getFileInfo = $getID3->analyze($analyzeFile);
|
|
|
|
} else {
|
|
|
|
$getFileInfo = [];
|
2021-10-11 16:10:47 +02:00
|
|
|
Logging::error('Failed to load getid3 library. Please upgrade Libretime.');
|
2019-10-13 19:07:06 +02:00
|
|
|
}
|
2019-09-20 02:07:50 +02:00
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
if (isset($getFileInfo['comments']['picture'][0])) {
|
|
|
|
$get_img = '';
|
|
|
|
$timestamp = time();
|
|
|
|
$mime = $getFileInfo['comments']['picture'][0]['image_mime'];
|
|
|
|
$Image = 'data:' . $mime . ';charset=utf-8;base64,' . base64_encode($getFileInfo['comments']['picture'][0]['data']);
|
|
|
|
$base64 = @$Image;
|
|
|
|
|
2022-04-18 20:34:38 +02:00
|
|
|
if (!file_exists($importDir . 'artwork/')) {
|
|
|
|
if (!mkdir($importDir . 'artwork/', 0777, true)) {
|
2021-10-11 16:10:47 +02:00
|
|
|
Logging::error('Failed to create artwork directory.');
|
|
|
|
|
|
|
|
throw new Exception('Failed to create artwork directory.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$path_parts = pathinfo($filename);
|
|
|
|
$file = $importDir . 'artwork/' . $path_parts['filename'];
|
|
|
|
|
2022-03-14 11:15:04 +01:00
|
|
|
// Save Data URI
|
2021-10-11 16:10:47 +02:00
|
|
|
if (file_put_contents($file, $base64)) {
|
|
|
|
$get_img = $DbPath . 'artwork/' . $path_parts['filename'];
|
|
|
|
} else {
|
|
|
|
Logging::error('Could not save Data URI');
|
|
|
|
}
|
2019-09-20 02:07:50 +02:00
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
if ($mime == 'image/png') {
|
|
|
|
$ext = 'png';
|
|
|
|
} elseif ($mime == 'image/gif') {
|
|
|
|
$ext = 'gif';
|
|
|
|
} elseif ($mime == 'image/bmp') {
|
|
|
|
$ext = 'bmp';
|
|
|
|
} else {
|
|
|
|
$ext = 'jpg';
|
|
|
|
}
|
|
|
|
self::resizeGroup($file, $ext);
|
2019-09-20 02:07:50 +02:00
|
|
|
} else {
|
2021-10-11 16:10:47 +02:00
|
|
|
$get_img = '';
|
2019-09-20 02:07:50 +02:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2019-09-20 02:07:50 +02:00
|
|
|
return $get_img;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Reset artwork.
|
2019-09-20 02:07:50 +02:00
|
|
|
*
|
|
|
|
* @param string $trackid
|
|
|
|
*
|
|
|
|
* @return string $get_img Path to artwork
|
|
|
|
*/
|
|
|
|
public static function resetArtwork($trackid)
|
|
|
|
{
|
|
|
|
$file = Application_Model_StoredFile::RecallById($trackid);
|
|
|
|
$md = $file->getMetadata();
|
|
|
|
|
2022-04-18 20:34:38 +02:00
|
|
|
$fp = Config::getStoragePath();
|
2019-09-20 02:07:50 +02:00
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
$dbAudioPath = $md['MDATA_KEY_FILEPATH'];
|
2019-09-20 02:07:50 +02:00
|
|
|
$fullpath = $fp . $dbAudioPath;
|
|
|
|
|
2019-10-13 19:07:06 +02:00
|
|
|
if (class_exists('getID3')) {
|
2024-01-07 13:59:02 +01:00
|
|
|
$getID3 = new getID3();
|
2019-10-13 19:07:06 +02:00
|
|
|
$getFileInfo = $getID3->analyze($fullpath);
|
|
|
|
} else {
|
|
|
|
$getFileInfo = [];
|
2021-10-11 16:10:47 +02:00
|
|
|
Logging::error('Failed to load getid3 library. Please upgrade Libretime.');
|
2019-10-13 19:07:06 +02:00
|
|
|
}
|
2019-09-20 02:07:50 +02:00
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
if (isset($getFileInfo['comments']['picture'][0])) {
|
|
|
|
$get_img = '';
|
|
|
|
$mime = $getFileInfo['comments']['picture'][0]['image_mime'];
|
|
|
|
$Image = 'data:' . $getFileInfo['comments']['picture'][0]['image_mime'] . ';charset=utf-8;base64,' . base64_encode($getFileInfo['comments']['picture'][0]['data']);
|
|
|
|
$base64 = @$Image;
|
|
|
|
|
|
|
|
$audioPath = dirname($fullpath);
|
|
|
|
$dbPath = dirname($dbAudioPath);
|
|
|
|
$path_parts = pathinfo($fullpath);
|
|
|
|
$file = $path_parts['filename'];
|
|
|
|
|
2022-03-14 11:15:04 +01:00
|
|
|
// Save Data URI
|
2021-10-11 16:10:47 +02:00
|
|
|
if (file_put_contents($audioPath . '/' . $file, $base64)) {
|
|
|
|
$get_img = $dbPath . '/' . $file;
|
|
|
|
} else {
|
|
|
|
Logging::error('Could not save Data URI');
|
|
|
|
}
|
|
|
|
|
|
|
|
$rfile = $audioPath . '/' . $file;
|
2019-09-20 02:07:50 +02:00
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
if ($mime == 'image/png') {
|
|
|
|
$ext = 'png';
|
|
|
|
} elseif ($mime == 'image/gif') {
|
|
|
|
$ext = 'gif';
|
|
|
|
} elseif ($mime == 'image/bmp') {
|
|
|
|
$ext = 'bmp';
|
|
|
|
} else {
|
|
|
|
$ext = 'jpg';
|
|
|
|
}
|
|
|
|
self::resizeGroup($rfile, $ext);
|
2019-09-20 02:07:50 +02:00
|
|
|
} else {
|
2021-10-11 16:10:47 +02:00
|
|
|
$get_img = '';
|
2019-09-20 02:07:50 +02:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2019-09-20 02:07:50 +02:00
|
|
|
return $get_img;
|
|
|
|
}
|
|
|
|
|
2019-10-25 16:30:33 +02:00
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Upload artwork.
|
2019-10-25 16:30:33 +02:00
|
|
|
*
|
|
|
|
* @param string $trackid
|
|
|
|
* @param string $data
|
|
|
|
*
|
|
|
|
* @return string Path to artwork
|
|
|
|
*/
|
2019-10-30 04:50:01 +01:00
|
|
|
public static function setArtwork($trackid, $data)
|
2019-10-25 16:30:33 +02:00
|
|
|
{
|
|
|
|
$file = Application_Model_StoredFile::RecallById($trackid);
|
|
|
|
$md = $file->getMetadata();
|
|
|
|
|
2022-04-18 20:34:38 +02:00
|
|
|
$fp = Config::getStoragePath();
|
2019-10-25 16:30:33 +02:00
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
$dbAudioPath = $md['MDATA_KEY_FILEPATH'];
|
2019-10-25 16:30:33 +02:00
|
|
|
$fullpath = $fp . $dbAudioPath;
|
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
if ($data == '0') {
|
|
|
|
$get_img = '';
|
2019-10-30 04:50:01 +01:00
|
|
|
self::removeArtwork($trackid, $data);
|
2019-10-25 16:30:33 +02:00
|
|
|
} else {
|
2019-10-30 04:50:01 +01:00
|
|
|
$base64 = @$data;
|
|
|
|
$mime = explode(';', $base64)[0];
|
|
|
|
|
|
|
|
$audioPath = dirname($fullpath);
|
|
|
|
$dbPath = dirname($dbAudioPath);
|
|
|
|
$path_parts = pathinfo($fullpath);
|
|
|
|
$file = $path_parts['filename'];
|
|
|
|
|
2022-03-14 11:15:04 +01:00
|
|
|
// Save Data URI
|
2021-10-11 16:10:47 +02:00
|
|
|
if (file_put_contents($audioPath . '/' . $file, $base64)) {
|
|
|
|
$get_img = $dbPath . '/' . $file;
|
2019-10-30 04:50:01 +01:00
|
|
|
} else {
|
2021-10-11 16:10:47 +02:00
|
|
|
Logging::error('Could not save Data URI');
|
2019-10-30 04:50:01 +01:00
|
|
|
}
|
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
$rfile = $audioPath . '/' . $file;
|
2019-10-30 04:50:01 +01:00
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
if ($mime == 'data:image/png') {
|
2019-10-30 04:50:01 +01:00
|
|
|
$ext = 'png';
|
2021-10-11 16:10:47 +02:00
|
|
|
} elseif ($mime == 'data:image/gif') {
|
2019-10-30 04:50:01 +01:00
|
|
|
$ext = 'gif';
|
2021-10-11 16:10:47 +02:00
|
|
|
} elseif ($mime == 'data:image/bmp') {
|
|
|
|
$ext = 'bmp';
|
2019-10-30 04:50:01 +01:00
|
|
|
} else {
|
2021-10-11 16:10:47 +02:00
|
|
|
$ext = 'jpg';
|
2019-10-30 04:50:01 +01:00
|
|
|
}
|
|
|
|
self::resizeGroup($rfile, $ext);
|
2019-10-25 16:30:33 +02:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2019-10-30 04:50:01 +01:00
|
|
|
return $get_img;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Deletes just the artwork.
|
2019-10-30 04:50:01 +01:00
|
|
|
*
|
2021-10-11 16:10:47 +02:00
|
|
|
* @param mixed $trackid
|
2019-10-30 04:50:01 +01:00
|
|
|
*/
|
|
|
|
public static function removeArtwork($trackid)
|
|
|
|
{
|
|
|
|
$file = Application_Model_StoredFile::RecallById($trackid);
|
|
|
|
$md = $file->getMetadata();
|
2019-10-25 16:30:33 +02:00
|
|
|
|
2022-04-18 20:34:38 +02:00
|
|
|
$fp = Config::getStoragePath();
|
2019-10-30 04:50:01 +01:00
|
|
|
|
2023-02-24 12:04:11 +01:00
|
|
|
$artworkBasePath = $fp . $md['MDATA_KEY_ARTWORK'];
|
|
|
|
$audioPath = $fp . $md['MDATA_KEY_FILEPATH'];
|
|
|
|
|
|
|
|
$artworkPaths = [
|
|
|
|
$artworkBasePath,
|
|
|
|
$artworkBasePath . '-32.jpg',
|
|
|
|
$artworkBasePath . '-64.jpg',
|
|
|
|
$artworkBasePath . '-128.jpg',
|
|
|
|
$artworkBasePath . '-256.jpg',
|
|
|
|
$artworkBasePath . '-512.jpg',
|
|
|
|
$artworkBasePath . '-32',
|
|
|
|
$artworkBasePath . '-64',
|
|
|
|
$artworkBasePath . '-128',
|
|
|
|
$artworkBasePath . '-256',
|
|
|
|
];
|
2019-10-25 16:30:33 +02:00
|
|
|
|
2023-02-24 12:04:11 +01:00
|
|
|
foreach ($artworkPaths as $filename) {
|
|
|
|
// This should never happen. Make sure we don't delete the audio file.
|
|
|
|
if ($filename == $audioPath) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file_exists($filename)) {
|
2019-10-30 04:50:01 +01:00
|
|
|
unlink($filename);
|
|
|
|
}
|
2019-10-25 16:30:33 +02:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
|
|
|
return '';
|
2019-10-25 16:30:33 +02:00
|
|
|
}
|
|
|
|
|
2019-10-25 20:46:16 +02:00
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Resize artwork group.
|
2019-10-25 20:46:16 +02:00
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
* @param string $ext
|
|
|
|
*/
|
|
|
|
public static function resizeGroup($file, $ext)
|
|
|
|
{
|
2021-10-11 16:10:47 +02:00
|
|
|
if (file_exists($file)) {
|
|
|
|
self::resizeImage($file, $file . '-32.jpg', $ext, 32, 100);
|
|
|
|
self::resizeImage($file, $file . '-64.jpg', $ext, 64, 100);
|
|
|
|
self::resizeImage($file, $file . '-128.jpg', $ext, 128, 100);
|
|
|
|
self::resizeImage($file, $file . '-256.jpg', $ext, 256, 100);
|
|
|
|
self::resizeImage($file, $file . '-512.jpg', $ext, 512, 100);
|
|
|
|
self::imgToDataURI($file . '-32.jpg', $file . '-32');
|
|
|
|
self::imgToDataURI($file . '-64.jpg', $file . '-64');
|
|
|
|
self::imgToDataURI($file . '-128.jpg', $file . '-128');
|
|
|
|
self::imgToDataURI($file . '-256.jpg', $file . '-256');
|
|
|
|
} else {
|
|
|
|
Logging::error("The file {$file} does not exist");
|
|
|
|
}
|
2019-10-25 20:46:16 +02:00
|
|
|
}
|
|
|
|
|
2019-09-20 02:07:50 +02:00
|
|
|
/**
|
|
|
|
* Render image
|
2021-10-11 16:10:47 +02:00
|
|
|
* Used in API to render JPEG.
|
2019-09-20 02:07:50 +02:00
|
|
|
*
|
|
|
|
* @param string $file
|
|
|
|
*/
|
|
|
|
public static function renderImage($file)
|
|
|
|
{
|
2021-10-11 16:10:47 +02:00
|
|
|
$im = @imagecreatefromjpeg($file);
|
|
|
|
$img = $im;
|
2022-01-28 06:04:58 +01:00
|
|
|
|
|
|
|
if ($im) {
|
|
|
|
header('Content-Type: image/jpeg');
|
|
|
|
imagejpeg($img);
|
|
|
|
imagedestroy($img);
|
|
|
|
}
|
2019-09-20 02:07:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render Data URI
|
2021-10-11 16:10:47 +02:00
|
|
|
* Used in API to render Data URI.
|
2019-09-20 02:07:50 +02:00
|
|
|
*
|
|
|
|
* @param string $dataFile
|
|
|
|
*/
|
|
|
|
public static function renderDataURI($dataFile)
|
|
|
|
{
|
2021-10-11 16:10:47 +02:00
|
|
|
if ($filecontent = file_get_contents($dataFile) !== false) {
|
|
|
|
$image = @file_get_contents($dataFile);
|
|
|
|
$image = base64_encode($image);
|
|
|
|
if (!$image || $image === '') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$blob = base64_decode($image);
|
|
|
|
$f = finfo_open();
|
|
|
|
$mime_type = finfo_buffer($f, $blob, FILEINFO_MIME_TYPE);
|
|
|
|
finfo_close($f);
|
|
|
|
header('Content-Type: ' . $mime_type);
|
|
|
|
echo $blob;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2019-09-20 02:07:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Resize Image.
|
2019-09-20 02:07:50 +02:00
|
|
|
*
|
|
|
|
* @param string $orig_filename
|
|
|
|
* @param string $converted_filename
|
|
|
|
* @param string $ext
|
2021-10-11 16:10:47 +02:00
|
|
|
* @param string $size Default: 500
|
|
|
|
* @param string $quality Default: 75
|
2019-09-20 02:07:50 +02:00
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public static function resizeImage($orig_filename, $converted_filename, $ext, $size = 500, $quality = 75)
|
2019-09-20 02:07:50 +02:00
|
|
|
{
|
2021-10-11 16:10:47 +02:00
|
|
|
$get_cont = file_get_contents($orig_filename);
|
|
|
|
if ($ext == 'png') {
|
|
|
|
$im = @imagecreatefrompng($get_cont);
|
|
|
|
} elseif ($ext == 'gif') {
|
|
|
|
$im = @imagecreatefromgif($get_cont);
|
|
|
|
} else {
|
|
|
|
$im = @imagecreatefromjpeg($get_cont);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if one of those bombs, create an error image instead
|
|
|
|
if (!$im) {
|
|
|
|
$im = imagecreatetruecolor(150, 30);
|
|
|
|
$bgc = imagecolorallocate($im, 255, 255, 255);
|
|
|
|
$tc = imagecolorallocate($im, 0, 0, 0);
|
|
|
|
imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
|
|
|
|
imagestring($im, 1, 5, 5, 'Error loading ' . $converted_filename, $tc);
|
|
|
|
}
|
|
|
|
|
|
|
|
// scale if appropriate
|
|
|
|
if ($size) {
|
|
|
|
$im = imagescale($im, $size);
|
|
|
|
}
|
|
|
|
|
|
|
|
$img = $im;
|
|
|
|
imagejpeg($img, $converted_filename, $quality);
|
|
|
|
imagedestroy($img);
|
2019-09-20 02:07:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Convert image to Data URI.
|
2019-09-20 02:07:50 +02:00
|
|
|
*
|
|
|
|
* @param string $orig_filename
|
|
|
|
* @param string $conv_filename
|
|
|
|
*/
|
|
|
|
public static function imgToDataURI($orig_filename, $conv_filename)
|
|
|
|
{
|
2021-10-11 16:10:47 +02:00
|
|
|
$file = file_get_contents($orig_filename);
|
|
|
|
$Image = 'data:image/jpeg;charset=utf-8;base64,' . base64_encode($file);
|
|
|
|
$base64 = @$Image;
|
2019-09-20 02:07:50 +02:00
|
|
|
|
2022-03-14 11:15:04 +01:00
|
|
|
// Save Data URI
|
2021-10-11 16:10:47 +02:00
|
|
|
if (file_put_contents($conv_filename, $base64)) {
|
|
|
|
} else {
|
|
|
|
Logging::error('Could not save Data URI');
|
|
|
|
}
|
2019-09-20 02:07:50 +02:00
|
|
|
}
|
|
|
|
|
2019-11-07 21:31:09 +01:00
|
|
|
/**
|
2021-10-11 16:10:47 +02:00
|
|
|
* Track Type.
|
2019-11-07 21:31:09 +01:00
|
|
|
*
|
2020-01-06 23:15:04 +01:00
|
|
|
* @return string Track type key value
|
2019-11-07 21:31:09 +01:00
|
|
|
*/
|
2020-01-06 23:15:04 +01:00
|
|
|
public static function saveTrackType()
|
2019-11-07 21:31:09 +01:00
|
|
|
{
|
2020-01-06 23:15:04 +01:00
|
|
|
if (isset($_COOKIE['tt_upload'])) {
|
|
|
|
$tt = $_COOKIE['tt_upload'];
|
2019-11-07 21:31:09 +01:00
|
|
|
} else {
|
2021-10-11 16:10:47 +02:00
|
|
|
// Use default track type
|
|
|
|
$tt = Application_Model_Preference::GetTrackTypeDefault();
|
2019-11-07 21:31:09 +01:00
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2020-01-06 23:15:04 +01:00
|
|
|
return $tt;
|
2019-11-07 21:31:09 +01:00
|
|
|
}
|
2015-07-31 21:02:53 +02:00
|
|
|
}
|