Upload-Resize
Eine schöne Anwendung ist auch wenn man hochgeladene Bilder, die zu groß sind nicht verwirft sondern in der Größe anpasst. Hier ein Ansatz für das upload-Handling in einer eigenen Extension:
/** * This handles uploads of new photos: All files are checked against a number of constraints (filetype, * filesize, etc.). If all checks are successfull the files are moved to the configured storage folder. * * @param array SubArray from $_FILES, which includes information about * uploaded files like temporary folder, size, ... * @param int The callers $conf array (this is usually passed in the 'main()' function of a plugin.) * evaluated values: * formatsAllowed string of allowed extensions * maxSize maximum size in Kb * maxW maxwidth in pixel * maxH maxHeight in pixel * storageFolder directore to store the uploaded files * * @return string The error HTML. If no errors have occured null is returned. */ function handleUploadedFile($infile,$conf) { $outfile =$infile; $errors = null; $imgtypes=array(1=>'gif',2=>'jpg',3=>'png'); if ( $infile['name'] != '' && $infile['type'] != '' && $infile['tmp_name'] != '' // && checkYourOwnConditions ) { $hasFileErrors = false; // Check if file is OK if (!$infile['error'] == '0' && $infile['name'] != '') { $errors .= 'An unknown error has occured. Please contact your system administrator.'; $hasFileErrors = true; } // Check if file type is allowed $type = explode('.', $infile['name']); $type = strtolower($type[sizeof($type) - 1]); $allowedTypes = explode(',', $conf['formatsAllowed']); if (array_search($type, $allowedTypes) == '') { $errors .= '"'.$infile['name'].'": this file has a wrong filetype<br/>', $conf); $hasFileErrors = true; } // Check if file size is below the allowed limit $allowedSize = $conf['maxSize'] * 1024; $imgdim = getimagesize($infile['tmp_name']); if ( $infile['size'] > $allowedSize // size in bytes || $imgdim[0] > $conf['maxW'] // width || $imgdim[1] > $conf['maxH'] // height ) { if (0 < $imgdim[2] && $imgdim[2] < 4) { // valid filetype we can resize // move file for handling with IM into directory we can access $storageFolder = $conf['storageFolder']; $uniqueTmpFilename = tempnam($storagefolder,'tmp_'.$infile['name']); // Move file to configured storage directory t3lib_div::upload_copy_move($infile['tmp_name'], $uniqueTmpFilename); // resize image $ts['img']='IMG_RESOURCE'; $ts['img.']['file']=$uniqueTmpFilename; //$ts['img.']['format']=$imgtypes[$imgdim[2]]; // keep fileformat $ts['img.']['file.']['maxW']=$conf['maxW']; $ts['img.']['file.']['maxH']=$conf['maxH']; $outfile['tmp_name']=$this->cObj->IMG_RESOURCE($ts['img.']); $outfile['size']=filesize($outfile['tmp_name']); // delete original pic unlink($uniqueTmpFilename); // as this file will not be moved (it is no uploaded file) we have to do it by ourself // get final name: $uniqueFilename = tempnam($storeagefolder,$infile['name']); // move tmp file to final destination: if (!rename($outfile['tmp_name'], $uniqueFilename)) { if (copy($outfile['tmp_name'], $uniqueFilename)) { unlink($outfile['tmp_name']); } } // do further action with uploaded file: // Create database record // $dimensions = getimagesize($uniqueFilename); // $this->createNewRecord($uniqueFilename, filesize($uniqueFilename), $dimensions[0], $dimensions[1]); // no further handling needed: $hasFileErrors= true; $errors .= '"'.$infile['name'].'": Image was to large. It has been resized.<br/>', $conf); } else { // not resizeable format $errors .= '"'.$infile['name'].'": File was to large. It has been ignored<br/>', $conf); $hasFileErrors = true; } } // Move file, if no errors have occured if (!$hasFileErrors) { // Make filename unique $uniqueFilename = tempnam($conf['storageFolder'],$infile['name']); // Move file to configured storage directory t3lib_div::upload_copy_move($outfile['tmp_name'], $uniqueFilename); $dimensions = getimagesize($uniqueFilename); // do further action with uploaded file: // Create database record // $dimensions = getimagesize($uniqueFilename); // $this->createNewRecord($uniqueFilename, filesize($uniqueFilename), $dimensions[0], $dimensions[1]); } } return $errors; }