function hasMemoryForImage($serverFilename) { // find out how much total memory this script can access $memoryAvailable = return_bytes(@ini_get('memory_limit')); // if memory is unlimited, it will return -1 and we don’t need to worry about it if ($memoryAvailable == -1) { return true; } // find out how much memory we are already using $memoryUsed = memory_get_usage(); $imgsize = @getimagesize($serverFilename); // find out how much memory this image needs for processing, probably only works for jpegs // from comments on http://www.php.net/imagecreatefromjpeg if (is_array($imgsize) && isset($imgsize['bits']) && isset($imgsize['channels'])) { $memoryNeeded = round(($imgsize[0] * $imgsize[1] * $imgsize['bits'] * $imgsize['channels'] / 8 + Pow(2, 16)) * 1.65); $memorySpare = $memoryAvailable - $memoryUsed - $memoryNeeded; if ($memorySpare > 0) { // we have enough memory to load this file return true; } else { // not enough memory to load this file $image_info = sprintf('%.2fKB, %d × %d %d bits %d channels', filesize($serverFilename) / 1024, $imgsize[0], $imgsize[1], $imgsize['bits'], $imgsize['channels']); Log::addMediaLog('Cannot create thumbnail ' . $serverFilename . ' (' . $image_info . ') memory avail: ' . $memoryAvailable . ' used: ' . $memoryUsed . ' needed: ' . $memoryNeeded . ' spare: ' . $memorySpare); return false; } } else { // assume there is enough memory // TODO find out how to check memory needs for gif and png return true; } }
/** * get image properties * * @param string $which specify either 'main' or 'thumb' * @param int $addWidth amount to add to width * @param int $addHeight amount to add to height * * @return array */ public function getImageAttributes($which = 'main', $addWidth = 0, $addHeight = 0) { global $THUMBNAIL_WIDTH; $var = $which . 'imagesize'; if (!empty($this->{$var})) { return $this->{$var}; } $imgsize = array(); if ($this->fileExists($which)) { $imgsize = @getimagesize($this->getServerFilename($which)); // [0]=width [1]=height [2]=filetype ['mime']=mimetype if (is_array($imgsize) && !empty($imgsize['0'])) { // this is an image $imgsize[0] = $imgsize[0] + 0; $imgsize[1] = $imgsize[1] + 0; $imgsize['adjW'] = $imgsize[0] + $addWidth; // adjusted width $imgsize['adjH'] = $imgsize[1] + $addHeight; // adjusted height $imageTypes = array('', 'GIF', 'JPG', 'PNG', 'SWF', 'PSD', 'BMP', 'TIFF', 'TIFF', 'JPC', 'JP2', 'JPX', 'JB2', 'SWC', 'IFF', 'WBMP', 'XBM'); $imgsize['ext'] = $imageTypes[0 + $imgsize[2]]; // this is for display purposes, always show non-adjusted info $imgsize['WxH'] = WT_I18N::translate('%1$s × %2$s pixels', WT_I18N::number($imgsize['0']), WT_I18N::number($imgsize['1'])); $imgsize['imgWH'] = ' width="' . $imgsize['adjW'] . '" height="' . $imgsize['adjH'] . '" '; if ($which == 'thumb' && $imgsize['0'] > $THUMBNAIL_WIDTH) { // don’t let large images break the dislay $imgsize['imgWH'] = ' width="' . $THUMBNAIL_WIDTH . '" '; } } } if (!is_array($imgsize) || empty($imgsize['0'])) { // this is not an image, OR the file doesn’t exist OR it is a url $imgsize[0] = 0; $imgsize[1] = 0; $imgsize['adjW'] = 0; $imgsize['adjH'] = 0; $imgsize['ext'] = ''; $imgsize['mime'] = ''; $imgsize['WxH'] = ''; $imgsize['imgWH'] = ''; if ($this->isExternal($which)) { // don’t let large external images break the dislay $imgsize['imgWH'] = ' width="' . $THUMBNAIL_WIDTH . '" '; } } if (empty($imgsize['mime'])) { // this is not an image, OR the file doesn’t exist OR it is a url // set file type equal to the file extension - can’t use parse_url because this may not be a full url $exp = explode('?', $this->file); $pathinfo = pathinfo($exp[0]); $imgsize['ext'] = @strtoupper($pathinfo['extension']); // all mimetypes we wish to serve with the media firewall must be added to this array. $mime = array('DOC' => 'application/msword', 'MOV' => 'video/quicktime', 'MP3' => 'audio/mpeg', 'PDF' => 'application/pdf', 'PPT' => 'application/vnd.ms-powerpoint', 'RTF' => 'text/rtf', 'SID' => 'image/x-mrsid', 'TXT' => 'text/plain', 'XLS' => 'application/vnd.ms-excel', 'WMV' => 'video/x-ms-wmv'); if (empty($mime[$imgsize['ext']])) { // if we don’t know what the mimetype is, use something ambiguous $imgsize['mime'] = 'application/octet-stream'; if ($this->fileExists($which)) { // alert the admin if we cannot determine the mime type of an existing file // as the media firewall will be unable to serve this file properly Log::addMediaLog('Media Firewall error: >Unknown Mimetype< for file >' . $this->file . '<'); } } else { $imgsize['mime'] = $mime[$imgsize['ext']]; } } $this->{$var} = $imgsize; return $this->{$var}; }
// save the image, if preferences allow if ($which == 'thumb' && $SAVE_WATERMARK_THUMB || $which == 'main' && $SAVE_WATERMARK_IMAGE) { // make sure the folder exists if (!is_dir(dirname($watermarkfile))) { mkdir(dirname($watermarkfile), WT_PERM_EXE, true); } // save the image $imSendFunc($im, $watermarkfile); } // send the image $imSendFunc($im); imagedestroy($im); exit; } else { // this image is defective. log it Log::addMediaLog("Media Firewall error: >" . WT_I18N::translate('This media file is broken and cannot be watermarked') . "< in file >" . $serverFilename . "< memory used: " . memory_get_usage()); // set usewatermark to false so image will simply be passed through below $usewatermark = false; } } // pass the image through without manipulating it if ($usewatermark) { // the stored watermarked image is good, lets use it $serverFilename = $watermarkfile; } // determine filesize of image (could be original or watermarked version) $filesize = filesize($serverFilename); // set content-length header, send file header("Content-Length: " . $filesize); // Some servers disable fpassthru() and readfile() if (function_exists('readfile')) {
chmod($serverFileName, WT_PERM_FILE); Log::addMediaLog('Media file ' . $serverFileName . ' uploaded'); } else { WT_FlashMessages::addMessage(WT_I18N::translate('There was an error uploading your file.') . '<br>' . file_upload_error_text($_FILES['mediafile' . $i]['error'])); $filename = ''; break; } // Now copy the (optional thumbnail) if (!empty($_FILES['thumbnail' . $i]['name']) && preg_match('/^image\\/(png|gif|jpeg)/', $_FILES['thumbnail' . $i]['type'], $match)) { $extension = $match[1]; $thumbFile = preg_replace('/\\.[a-z0-9]{3,5}$/', '.' . $extension, $fileName); $serverFileName = WT_DATA_DIR . $MEDIA_DIRECTORY . 'thumbs/' . $folderName . $thumbFile; if (move_uploaded_file($_FILES['thumbnail' . $i]['tmp_name'], $serverFileName)) { WT_FlashMessages::addMessage(WT_I18N::translate('The file %s was uploaded.', '<span class="filename">' . $serverFileName . '</span>')); chmod($serverFileName, WT_PERM_FILE); Log::addMediaLog('Thumbnail file ' . $serverFileName . ' uploaded'); } } } } } } $controller->pageHeader(); $mediaFolders = WT_Query_Media::folderListAll(); // Determine file size limit // TODO: do we need to check post_max_size size too? $filesize = ini_get('upload_max_filesize'); if (empty($filesize)) { $filesize = "2M"; } // Print the form