Project

General

Profile

Feature #23244 » class.user_t3lib_extfilefunc_hook.php

Administrator Admin, 2010-07-21 15:48

 
<?php

class user_t3lib_extfilefunc_hook implements t3lib_extFileFunctions_processDataHook {
/**
* Post process upload of a picture and make sure it is not too big.
*
* @param string $action
* @param array $cmdArr
* @param array $result
* @param t3lib_extFileFunctions $pObj
*/
public function processData_postProcessAction($action, array $cmdArr, array $result, t3lib_extFileFunctions $pObj) {
if ($action !== 'upload') {
// Early return
return;
}

// Get the latest uploaded file name
$filename = array_pop($result);
$relFilename = substr($filename, strlen(PATH_site));

// Extract the file extension
$imgExt = strtolower(substr($filename, strrpos($filename, '.') + 1));
$maxImageSize = 300 * 1024; // 300 KB
$maxWidth = 800;
$maxHeight = 600;

if (($imgExt === 'jpg' || $imgExt === 'jpeg') && filesize($filename) > $maxImageSize) {
// Image is bigger than allowed, will now resize it to (hopefully) make it lighter
$gifCreator = t3lib_div::makeInstance('tslib_gifbuilder');
$gifCreator->init();
$gifCreator->absPrefix = PATH_site;
$hash = t3lib_div::shortMD5($filename);
$dest = $gifCreator->tempPath . $hash . '.' . $imgExt;
$options = array(
'maxW' => $maxWidth,
'maxH' => $maxHeight,
);

$tempFileInfo = $gifCreator->imageMagickConvert($filename, '', '', '', '', '', $options);
if ($tempFileInfo) {
// Replace original file
@unlink($filename);
@rename($tempFileInfo[3], $filename);

$flashMessage = t3lib_div::makeInstance(
't3lib_FlashMessage',
sprintf('Image %s has been automatically resized to %sx%s pixels', $relFilename, $tempFileInfo[0], $tempFileInfo[1]),
'',
t3lib_FlashMessage::INFO,
TRUE
);
t3lib_FlashMessageQueue::addMessage($flashMessage);
}
}
}
}
(3-3/3)