public static function Temporary($directory, $extension = DEFAULT_EXTENSION, $create = false) { $directory = Dir::StripTrailingSlash($directory); if (!is_dir($directory)) { throw new BaseException('Not a directory', $directory); } do { $filename = ''; for ($i = 0; $i < 10; $i++) { $filename .= self::$temp_file_chars[array_rand(self::$temp_file_chars)]; } $filename = "{$directory}/{$filename}.{$extension}"; } while (file_exists($filename)); if ($create) { self::Create($filename); } return $filename; }
function tbxGlobalSettingsSave() { Privileges::CheckSuper(); $si = ServerInfo::GetCached(); $required = array('site_name' => 'Site Name', 'meta_description' => 'Meta Description', 'meta_keywords' => 'Meta Keywords', 'document_root' => 'Document Root', 'base_url' => 'TubeX URL', 'cookie_domain' => 'Cookie Domain', 'cookie_path' => 'Cookie Path', 'email_address' => 'E-mail Address', 'email_name' => 'E-mail Name', 'date_format' => 'Date Format', 'time_format' => 'Time Format', 'dec_point' => 'Decimal Point', 'thousands_sep' => 'Thousands Separator', 'video_extensions' => 'File Extensions'); switch (Request::Get('mailer')) { case Mailer::SMTP: $required['smtp_hostname'] = 'SMTP Hostname'; $required['smtp_port'] = 'SMTP Port'; break; case Mailer::SENDMAIL: $required['sendmail_path'] = 'Sendmail Path'; break; } $v = Validator::Get(); foreach ($required as $field => $label) { $v->Register(Request::Get($field), Validator_Type::NOT_EMPTY, 'The ' . $label . ' field is required'); } if (!$v->Validate()) { $output['message'] = 'Settings could not be saved; please fix the following items'; $output['errors'] = $v->GetErrors(); return JSON::Failure($output); } unset($_REQUEST['r']); // Setup mcf file for VP6 encoding if ($_REQUEST['video_format'] == Video_Converter::FORMAT_VP6 && preg_match('~^[0-9]+$~', $_REQUEST['video_bitrate'])) { $fp = fopen(INCLUDES_DIR . '/vp6.mcf', 'r+'); fseek($fp, 0x14); fwrite($fp, pack('s', $_REQUEST['video_bitrate'])); fclose($fp); } $_REQUEST['max_upload_size'] = Format::BytesToString(min(Format::StringToBytes($si->php_settings[ServerInfo::PHP_UPLOAD_MAX_FILESIZE]), Format::StringToBytes($_REQUEST['max_upload_size']))); $_REQUEST['document_root'] = Dir::StripTrailingSlash($_REQUEST['document_root']); $_REQUEST['base_url'] = Dir::StripTrailingSlash($_REQUEST['base_url']); $_REQUEST['base_uri'] = parse_url($_REQUEST['base_url'], PHP_URL_PATH); $_REQUEST['template_uri'] = $_REQUEST['base_uri'] . '/templates/' . $_REQUEST['template']; if (Config::Get('template') != $_REQUEST['template']) { tbxTemplateCacheFlush(true); TemplateRecompileAll(BASE_DIR . '/templates/' . $_REQUEST['template']); } ServerInfo::Get(true); Config::Save($_REQUEST); JSON::Success('Global software settings have been saved'); }
public static function Resize($image, $size, $quality, $directory, $coords = null) { $si = ServerInfo::GetCached(); $directory = Dir::StripTrailingSlash($directory); $letterbox = Config::Get('flag_letterbox'); $resized = $directory . '/' . basename($image); list($dst_width, $dst_height) = explode('x', $size); // See if image is already the correct size $imgsize = @getimagesize($image); if ($imgsize === false) { return null; } else { if (empty($coords) && $imgsize[0] <= $dst_width && $imgsize[1] <= $dst_height) { copy($image, $resized); return $resized; } } $src_width = $imgsize[0]; $src_height = $imgsize[1]; $src_aspect = $src_width / $src_height; $dst_aspect = $dst_width / $dst_height; // Get crop information if $coords was supplied // Generally only occurs when image is extracted from a HTML image map $crop = $crop_x = $crop_y = $crop_width = $crop_height = null; if (!empty($coords)) { $crop = explode(',', str_replace(' ', '', $coords)); // Currently only supporting rect if (count($crop) == 4) { $crop_x = $crop[0]; $crop_y = $crop[1]; $crop_width = $crop[2] - $crop_x; $crop_height = $crop[3] - $crop_y; $src_aspect = $crop_width / $crop_height; } } // Resize with ImageMagick (preferred) if (!$si->shell_exec_disabled && $si->binaries[ServerInfo::BIN_CONVERT]) { if ($letterbox) { shell_exec($si->binaries[ServerInfo::BIN_CONVERT] . ' ' . escapeshellarg($image) . ' ' . (!empty($crop) ? '-crop ' . escapeshellarg($crop_width . 'x' . $crop_height . '!+' . $crop_x . '+' . $crop_y) . ' ' : '') . '-resize ' . $size . ' ' . '-modulate 110,102,100 ' . '-unsharp 1.5x1.2+1.0+0.10 ' . '-enhance ' . '-size ' . $size . ' ' . 'xc:black ' . '+swap ' . '-gravity center ' . '-quality ' . $quality . ' ' . '-format jpeg ' . '-composite ' . escapeshellarg($resized)); } else { $recrop = null; if ($src_aspect > $dst_aspect) { $recrop = '-gravity center -crop ' . escapeshellarg(round($src_height * $dst_aspect) . 'x' . $src_height . '+0+0') . ' '; } else { if ($src_aspect < $dst_aspect) { $recrop = '-gravity center -crop ' . escapeshellarg($src_width . 'x' . round($src_width / $dst_aspect) . '+0+0') . ' '; } } shell_exec($si->binaries[ServerInfo::BIN_CONVERT] . ' ' . escapeshellarg($image) . ' ' . (!empty($crop) ? '-crop ' . escapeshellarg($crop_width . 'x' . $crop_height . '!+' . $crop_x . '+' . $crop_y) . ' ' : '') . (!empty($recrop) ? $recrop : '') . '-resize ' . $size . ' ' . '-modulate 110,102,100 ' . '-unsharp 1.5x1.2+1.0+0.10 ' . '-enhance ' . '-quality ' . $quality . ' ' . '-format jpeg ' . escapeshellarg($resized)); } return $resized; } else { if ($si->php_extensions[ServerInfo::EXT_GD]) { if ($crop) { $temp_src = imagecreatefromjpeg($image); $src = imagecreatetruecolor($crop_width, $crop_height); imagecopy($src, $temp_src, 0, 0, $crop_x, $crop_y, $crop_width, $crop_height); imagedestroy($temp_src); } else { $src = imagecreatefromjpeg($image); } $new_width = $dst_width; $new_height = $dst_height; $dst_x = 0; $dst_y = 0; if ($src_aspect > $dst_aspect) { $new_height = round($dst_width * $src_height / $src_width); $dst_y = ($dst_height - $new_height) / 2; } else { if ($src_aspect < $dst_aspect) { $new_width = round($dst_height * $src_width / $src_height); $dst_x = ($dst_width - $new_width) / 2; } } $dst = imagecreatetruecolor($dst_width, $dst_height); $black = imagecolorallocate($dst, 0, 0, 0); imagefill($dst, 0, 0, 0); imagecopyresampled($dst, $src, $dst_x, $dst_y, 0, 0, $new_width, $new_height, $src_width, $src_height); imagejpeg($dst, $resized, $quality); imagedestroy($src); imagedestroy($dst); return $resized; } } return $image; }
private static function ConvertToMp4($vi, $filename, $directory, $vbitrate, $abitrate, $scale, $callback) { $tools = Video_Tools::Get(); $vbitrate = ($vbitrate <= 40 ? 'crf=' : 'ratetol=1.0:bitrate=') . $vbitrate; $tmp_file = File::Temporary($directory, self::EXTENSION_AVI); $cmd = (!empty($tools->nice) ? $tools->nice . ' ' : '') . $tools->mencoder . ' ' . escapeshellarg($filename) . ' ' . '-o ' . escapeshellarg($tmp_file) . ' ' . '-sws 9 ' . '-noskip ' . '-ovc x264 ' . '-x264encopts ' . escapeshellarg($vbitrate . ':bframes=1:me=umh:partitions=all:trellis=1:qp_step=4:qcomp=0.7:direct_pred=auto:keyint=300:threads=' . self::THREADS) . ' ' . '-vf ' . escapeshellarg((!empty($scale) ? $scale . ',' : '') . 'harddup') . ' ' . ($vi->has_audio ? '-oac faac -faacopts ' . escapeshellarg('br=' . $abitrate . ':mpeg=4:object=2') . ' -channels ' . self::CHANNELS_AAC . ' -srate ' . self::SAMPLE_RATE_AAC . ' ' : ' -nosound ') . '-ofps ' . escapeshellarg($vi->video_fps); if (self::ExecuteCmdAsync($cmd, $callback)) { File::Delete($tmp_file); throw new BaseException('Video conversion was interrupted by user request'); } // Verify video file generated if (filesize($tmp_file) == 0) { File::Delete($tmp_file); throw new BaseException('Unable to convert video file to H.264/AAC MP4', $filename, $output); } // Get the filenames of the extracted raw streams $directory = Dir::StripTrailingSlash($directory); $basename = basename($tmp_file, '.' . self::EXTENSION_AVI); $videofile = $directory . '/' . $basename . '_video.h264'; $audiofile_raw = $directory . '/' . $basename . '_audio.raw'; $audiofile = $directory . '/' . $basename . '_audio.aac'; // Extract the raw streams $cmd = (!empty($tools->nice) ? $tools->nice . ' ' : '') . $tools->mp4box . ' -aviraw video ' . escapeshellarg($tmp_file) . ' 2>&1'; self::Log($cmd); $output = shell_exec($cmd); self::Log($output); if (!file_exists($videofile)) { File::Delete($tmp_file); throw new BaseException('Unable to extract video from file using MP4Box', $videofile, $output); } $output_file = File::Temporary($directory, self::EXTENSION_MP4); // Process video files that do have an audio stream if ($vi->has_audio) { $cmd = (!empty($tools->nice) ? $tools->nice . ' ' : '') . $tools->mp4box . ' -aviraw audio ' . escapeshellarg($tmp_file) . ' 2>&1'; self::Log($cmd); $output = shell_exec($cmd); self::Log($output); if (!file_exists($audiofile_raw)) { File::Delete($tmp_file); File::Delete($videofile); throw new BaseException('Unable to extract audio from file using MP4Box', $audiofile_raw, $output); } rename($audiofile_raw, $audiofile); $cmd = (!empty($tools->nice) ? $tools->nice . ' ' : '') . $tools->mp4box . ' -add ' . escapeshellarg($videofile) . ' -add ' . escapeshellarg($audiofile) . ' -fps ' . escapeshellarg($vi->video_fps) . ' -inter 500 ' . escapeshellarg($output_file) . ' 2>&1'; self::Log($cmd); $output = shell_exec($cmd); self::Log($output); File::Delete($audiofile); } else { $cmd = (!empty($tools->nice) ? $tools->nice . ' ' : '') . $tools->mp4box . ' -add ' . escapeshellarg($videofile) . ' -fps ' . escapeshellarg($vi->video_fps) . ' -inter 500 ' . escapeshellarg($output_file) . ' 2>&1'; self::Log($cmd); $output = shell_exec($cmd); self::Log($output); } // Remove temporary files File::Delete($tmp_file); File::Delete($videofile); if (!file_exists($output_file)) { throw new BaseException('Unable to generate MP4 file using MP4Box', $output_file, $output); } return $output_file; }
public function MoveFiles($src, $dest, $extension) { $src = Dir::StripTrailingSlash($src); $dest = Dir::StripTrailingSlash($dest); $dest_files = array(); $files = preg_grep('~\\.' . $extension . '$~i', scandir($src)); foreach ($files as $file) { rename("{$src}/{$file}", "{$dest}/{$file}"); $dest_files[] = "{$dest}/{$file}"; } return $dest_files; }