1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
<?php
/**
***********************************************************************************************
* @copyright 2004-2016 The Admidio Team
* @see http://www.admidio.org/
* @license https://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2.0 only
***********************************************************************************************
*/
require_once(SERVER_PATH.'/adm_program/libs/jquery-file-upload/server/php/UploadHandler.php');
/**
* @class UploadHandlerPhoto
* @brief Improved checks and update of database after upload of photos.
*
* This class extends the UploadHandler of the jquery-file-upload library. After
* the upload of a photo we do some checks on the file and if no check fails then
* the Admidio database will be updated. If you want do upload files for the download
* module just create an instance of this class.
* @par Examples
* @code // create object and do upload
* $uploadHandler = new UploadHandlerPhoto(array('upload_dir' => $uploadDir,
* 'upload_url' => $uploadUrl,
* 'image_versions' => array(),
* 'accept_file_types' => '/\.(jpe?g|png)$/i'), true,
* 'array('accept_file_types' => $gL10n->get('PHO_PHOTO_FORMAT_INVALID'))); @endcode
*/
class UploadHandlerPhoto extends UploadHandler
{
/**
* Override the default method to handle the specific things of the photo module and
* update the database after file was succesful uploaded.
* This method has the same parameters as the default.
* @param string $uploaded_file
* @param string $name
* @param int $size
* @param $type
* @param $error
* @param $index
* @param $content_range
* @return \stdClass
*/
protected function handle_file_upload($uploaded_file, $name, $size, $type, $error, $index = null, $content_range = null)
{
global $photoAlbum, $gPreferences, $gL10n;
$file = parent::handle_file_upload($uploaded_file, $name, $size, $type, $error, $index, $content_range);
if(!isset($file->error))
{
try
{
$fileLocation = SERVER_PATH.'/adm_my_files/photos/upload/'.$file->name;
$albumFolder = SERVER_PATH.'/adm_my_files/photos/'.$photoAlbum->getValue('pho_begin', 'Y-m-d').'_'.$photoAlbum->getValue('pho_id');
// create folder if not exists
if(!file_exists($albumFolder))
{
$error = $photoAlbum->createFolder();
if($error['text'] !== '')
{
$file->error = $gL10n->get($error['text'], $error['path']);
return $file;
}
}
$newPhotoFileNumber = $photoAlbum->getValue('pho_quantity') + 1;
// read image size
$imageProperties = getimagesize($fileLocation);
$imageDimensions = $imageProperties[0] * $imageProperties[1];
if($imageDimensions > admFuncProcessableImageSize())
{
$errorText = $gL10n->get('PHO_RESOLUTION_MORE_THAN').' '.round(admFuncProcessableImageSize() / 1000000, 2).' '.$gL10n->get('MEGA_PIXEL');
throw new AdmException($errorText);
}
// check mime type and set file extension
if($imageProperties['mime'] === 'image/jpeg')
{
$fileExtension = 'jpg';
}
elseif($imageProperties['mime'] === 'image/png')
{
$fileExtension = 'png';
}
else
{
throw new AdmException('PHO_PHOTO_FORMAT_INVALID');
}
// create image object and scale image to defined size of preferences
$image = new Image($fileLocation);
$image->setImageType('jpeg');
$image->scaleLargerSide($gPreferences['photo_save_scale']);
$image->copyToFile(null, $albumFolder.'/'.$newPhotoFileNumber.'.jpg');
$image->delete();
// if enabled then save original image
if ($gPreferences['photo_keep_original'] == 1)
{
if(!file_exists($albumFolder.'/originals'))
{
$folder = new Folder($albumFolder);
$folder->createFolder('originals', true);
}
rename($fileLocation, $albumFolder.'/originals/'.$newPhotoFileNumber.'.'.$fileExtension);
}
// save thumbnail
if(!file_exists($albumFolder.'/thumbnails'))
{
$folder = new Folder($albumFolder);
$folder->createFolder('thumbnails', true);
}
$image = new Image($fileLocation);
$image->scaleLargerSide($gPreferences['photo_thumbs_scale']);
$image->copyToFile(null, $albumFolder.'/thumbnails/'.$newPhotoFileNumber.'.jpg');
$image->delete();
// delete image from upload folder
if(file_exists($fileLocation))
{
unlink($fileLocation);
}
// if image was successfully saved in filesystem then update image count of album
if(file_exists($albumFolder.'/'.$newPhotoFileNumber.'.jpg'))
{
$photoAlbum->setValue('pho_quantity', $photoAlbum->getValue('pho_quantity')+1);
$photoAlbum->save();
}
else
{
throw new AdmException('PHO_PHOTO_PROCESSING_ERROR');
}
}
catch(AdmException $e)
{
$file->error = $e->getText();
unlink($this->options['upload_dir'].$file->name);
return $file;
}
}
return $file;
}
}