public static function generateThumbnail(asset $srcAsset, thumbParamsOutput $destThumbParamsOutput, &$errDescription) { $srcSyncKey = $srcAsset->getSyncKey(flavorAsset::FILE_SYNC_FLAVOR_ASSET_SUB_TYPE_ASSET); list($fileSync, $local) = kFileSyncUtils::getReadyFileSyncForKey($srcSyncKey, true, false); if (!$fileSync || $fileSync->getFileType() == FileSync::FILE_SYNC_FILE_TYPE_URL) { $errDescription = 'Source asset could has no valid file sync'; return false; } $srcPath = $fileSync->getFullPath(); $uniqid = uniqid('thumb_'); $destPath = kConf::get('temp_folder') . "/thumb/{$uniqid}.jpg"; $logPath = $destPath . '.log'; if (!file_exists($srcPath)) { $errDescription = "Source file [{$srcPath}] does not exist"; return false; } if (!is_file($srcPath)) { $errDescription = "Source file [{$srcPath}] is not a file"; return false; } try { if ($srcAsset->getType() == assetType::FLAVOR) { // generates the thumbnail $thumbMaker = new KFFMpegThumbnailMaker($srcPath, $destPath, kConf::get('bin_path_ffmpeg')); $created = $thumbMaker->createThumnail($destThumbParamsOutput->getVideoOffset()); if (!$created || !file_exists($destPath)) { $errDescription = "Thumbnail not captured"; return false; } $srcPath = $destPath; $uniqid = uniqid('thumb_'); $destPath = kConf::get('temp_folder') . "/thumb/{$uniqid}.jpg"; } $quality = $destThumbParamsOutput->getQuality(); $cropType = $destThumbParamsOutput->getCropType(); $cropX = $destThumbParamsOutput->getCropX(); $cropY = $destThumbParamsOutput->getCropY(); $cropWidth = $destThumbParamsOutput->getCropWidth(); $cropHeight = $destThumbParamsOutput->getCropHeight(); $bgcolor = $destThumbParamsOutput->getBackgroundColor(); $width = $destThumbParamsOutput->getWidth(); $height = $destThumbParamsOutput->getHeight(); $scaleWidth = $destThumbParamsOutput->getScaleWidth(); $scaleHeight = $destThumbParamsOutput->getScaleHeight(); $cropper = new KImageMagickCropper($srcPath, $destPath, kConf::get('bin_path_imagemagick'), true); $cropped = $cropper->crop($quality, $cropType, $width, $height, $cropX, $cropY, $cropWidth, $cropHeight, $scaleWidth, $scaleHeight, $bgcolor); if (!$cropped || !file_exists($destPath)) { $errDescription = "Crop failed"; return false; } return $destPath; } catch (Exception $ex) { $errDescription = $ex->getMessage(); return false; } }
private function captureThumb(KalturaBatchJob $job, KalturaCaptureThumbJobData $data) { $thumbParamsOutput = self::$kClient->thumbParamsOutput->get($data->thumbParamsOutputId); try { $mediaFile = trim($data->srcFileSyncLocalPath); if (!file_exists($mediaFile)) { return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::NFS_FILE_DOESNT_EXIST, "Source file {$mediaFile} does not exist", KalturaBatchJobStatus::RETRY); } if (!is_file($mediaFile)) { return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::NFS_FILE_DOESNT_EXIST, "Source file {$mediaFile} is not a file", KalturaBatchJobStatus::FAILED); } $this->updateJob($job, "Capturing thumbnail on {$mediaFile}", KalturaBatchJobStatus::QUEUED); } catch (Exception $ex) { return $this->closeJob($job, KalturaBatchJobErrorTypes::RUNTIME, $ex->getCode(), "Error: " . $ex->getMessage(), KalturaBatchJobStatus::FAILED); } try { $data->thumbPath = null; // creates a temp file path $rootPath = self::$taskConfig->params->localTempPath; if (!is_dir($rootPath)) { if (!file_exists($rootPath)) { KalturaLog::info("Creating temp thumbnail directory [{$rootPath}]"); mkdir($rootPath); } else { // already exists but not a directory KalturaLog::err("Cannot create temp thumbnail directory [{$rootPath}] due to an error. Please fix and restart"); die; } } $capturePath = $mediaFile; if ($data->srcAssetType == KalturaAssetType::FLAVOR) { // creates the path $uniqid = uniqid('thumb_'); $capturePath = realpath($rootPath) . DIRECTORY_SEPARATOR . $uniqid; $mediaInfoWidth = null; $mediaInfoHeight = null; $mediaInfoDar = null; $mediaInfoVidDur = null; $mediaInfoFilter = new KalturaMediaInfoFilter(); $mediaInfoFilter->flavorAssetIdEqual = $data->srcAssetId; $this->impersonate($job->partnerId); $mediaInfoList = self::$kClient->mediaInfo->listAction($mediaInfoFilter); $this->unimpersonate(); if (count($mediaInfoList->objects)) { $mediaInfo = reset($mediaInfoList->objects); /* @var $mediaInfo KalturaMediaInfo */ $mediaInfoWidth = $mediaInfo->videoWidth; $mediaInfoHeight = $mediaInfo->videoHeight; $mediaInfoDar = $mediaInfo->videoDar; if ($mediaInfo->videoDuration) { $mediaInfoVidDur = $mediaInfo->videoDuration / 1000; } else { if ($mediaInfo->containerDuration) { $mediaInfoVidDur = $mediaInfo->containerDuration / 1000; } else { if ($mediaInfo->audioDuration) { $mediaInfoVidDur = $mediaInfo->audioDuration / 1000; } } } } // generates the thumbnail $thumbMaker = new KFFMpegThumbnailMaker($mediaFile, $capturePath, self::$taskConfig->params->FFMpegCmd); $created = $thumbMaker->createThumnail($thumbParamsOutput->videoOffset, $mediaInfoWidth, $mediaInfoHeight, null, null, $mediaInfoDar, $mediaInfoVidDur); if (!$created || !file_exists($capturePath)) { return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::THUMBNAIL_NOT_CREATED, "Thumbnail not created", KalturaBatchJobStatus::FAILED); } $this->updateJob($job, "Thumbnail captured [{$capturePath}]", KalturaBatchJobStatus::PROCESSING); } $uniqid = uniqid('thumb_'); $thumbPath = $rootPath . DIRECTORY_SEPARATOR . $uniqid; $quality = $thumbParamsOutput->quality; $cropType = $thumbParamsOutput->cropType; $cropX = $thumbParamsOutput->cropX; $cropY = $thumbParamsOutput->cropY; $cropWidth = $thumbParamsOutput->cropWidth; $cropHeight = $thumbParamsOutput->cropHeight; $bgcolor = $thumbParamsOutput->backgroundColor; $width = $thumbParamsOutput->width; $height = $thumbParamsOutput->height; $scaleWidth = $thumbParamsOutput->scaleWidth; $scaleHeight = $thumbParamsOutput->scaleHeight; $density = $thumbParamsOutput->density; $rotate = $thumbParamsOutput->rotate; $cropper = new KImageMagickCropper($capturePath, $thumbPath, self::$taskConfig->params->ImageMagickCmd, true); $cropped = $cropper->crop($quality, $cropType, $width, $height, $cropX, $cropY, $cropWidth, $cropHeight, $scaleWidth, $scaleHeight, $bgcolor, $density, $rotate); if (!$cropped || !file_exists($thumbPath)) { return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::THUMBNAIL_NOT_CREATED, "Thumbnail not cropped", KalturaBatchJobStatus::FAILED); } $data->thumbPath = $thumbPath; $job = $this->moveFile($job, $data); if ($this->checkFileExists($job->data->thumbPath)) { $updateData = new KalturaCaptureThumbJobData(); $updateData->thumbPath = $data->thumbPath; return $this->closeJob($job, null, null, null, KalturaBatchJobStatus::FINISHED, $updateData); } return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::NFS_FILE_DOESNT_EXIST, 'File not moved correctly', KalturaBatchJobStatus::FAILED, $data); } catch (Exception $ex) { $this->unimpersonate(); return $this->closeJob($job, KalturaBatchJobErrorTypes::RUNTIME, $ex->getCode(), "Error: " . $ex->getMessage(), KalturaBatchJobStatus::FAILED); } }
private static function generateThumbnail(asset $srcAsset, thumbParamsOutput $destThumbParamsOutput, &$errDescription, $rotate = null) { $srcSyncKey = $srcAsset->getSyncKey(flavorAsset::FILE_SYNC_FLAVOR_ASSET_SUB_TYPE_ASSET); list($fileSync, $local) = kFileSyncUtils::getReadyFileSyncForKey($srcSyncKey, true, false); if (!$fileSync || $fileSync->getFileType() == FileSync::FILE_SYNC_FILE_TYPE_URL) { $errDescription = 'Source asset could has no valid file sync'; return false; } $srcPath = $fileSync->getFullPath(); $uniqid = uniqid('thumb_'); $tempDir = kConf::get('cache_root_path') . DIRECTORY_SEPARATOR . 'thumb'; if (!file_exists($tempDir)) { mkdir($tempDir, 0700, true); } $destPath = $tempDir . DIRECTORY_SEPARATOR . $uniqid . '.jpg'; $logPath = $destPath . '.log'; if (!file_exists($srcPath)) { $errDescription = "Source file [{$srcPath}] does not exist"; return false; } if (!is_file($srcPath)) { $errDescription = "Source file [{$srcPath}] is not a file"; return false; } try { if ($srcAsset->getType() == assetType::FLAVOR) { /* @var $srcAsset flavorAsset */ $dar = null; $mediaInfo = mediaInfoPeer::retrieveByFlavorAssetId($srcAsset->getId()); if ($mediaInfo) { $dar = $mediaInfo->getVideoDar(); } // generates the thumbnail $thumbMaker = new KFFMpegThumbnailMaker($srcPath, $destPath, kConf::get('bin_path_ffmpeg')); $created = $thumbMaker->createThumnail($destThumbParamsOutput->getVideoOffset(), $srcAsset->getWidth(), $srcAsset->getHeight(), null, null, $dar); if (!$created || !file_exists($destPath)) { $errDescription = "Thumbnail not captured"; return false; } $srcPath = $destPath; $uniqid = uniqid('thumb_'); $tempDir = kConf::get('cache_root_path') . DIRECTORY_SEPARATOR . 'thumb'; if (!file_exists($tempDir)) { mkdir($tempDir, 0700, true); } $destPath = $tempDir . DIRECTORY_SEPARATOR . $uniqid . '.jpg'; } if ($srcAsset->getType() == assetType::THUMBNAIL) { $tempDir = kConf::get('cache_root_path') . DIRECTORY_SEPARATOR . 'thumb'; if (!file_exists($tempDir)) { mkdir($tempDir, 0700, true); } $destPath = $tempDir . DIRECTORY_SEPARATOR . $uniqid . "." . $srcAsset->getFileExt(); } $quality = $destThumbParamsOutput->getQuality(); $cropType = $destThumbParamsOutput->getCropType(); $cropX = $destThumbParamsOutput->getCropX(); $cropY = $destThumbParamsOutput->getCropY(); $cropWidth = $destThumbParamsOutput->getCropWidth(); $cropHeight = $destThumbParamsOutput->getCropHeight(); $bgcolor = $destThumbParamsOutput->getBackgroundColor(); $width = $destThumbParamsOutput->getWidth(); $height = $destThumbParamsOutput->getHeight(); $scaleWidth = $destThumbParamsOutput->getScaleWidth(); $scaleHeight = $destThumbParamsOutput->getScaleHeight(); $density = $destThumbParamsOutput->getDensity(); $stripProfiles = $destThumbParamsOutput->getStripProfiles(); $cropper = new KImageMagickCropper($srcPath, $destPath, kConf::get('bin_path_imagemagick'), true); $cropped = $cropper->crop($quality, $cropType, $width, $height, $cropX, $cropY, $cropWidth, $cropHeight, $scaleWidth, $scaleHeight, $bgcolor, $density, $rotate, $stripProfiles); if (!$cropped || !file_exists($destPath)) { $errDescription = "Crop failed"; return false; } return $destPath; } catch (Exception $ex) { $errDescription = $ex->getMessage(); return false; } }
/** * * convert an image to a desired size while maintaining its aspect ratio * if the image is of type BMP it will be converted into JPEG * NOTE: images are only scaled down, so a small image wont be changed (apart for the JPEG quality) * the function returns the $target_file after changing its extension * @param unknown_type $source_file - Sourct file path * @param unknown_type $target_file - Target file path (after converting) * @param unknown_type $width - Requested width in pixels * @param unknown_type $height - Requested height in pixels * @param unknown_type $crop_type - Type of crop to be used [1-4] : * self::CROP_TYPE_ORIGINAL_ASPECT_RATIO: Resize according to the given dimensions while maintaining the original aspect ratio. * self::CROP_TYPE_WITHIN_BG_COLOR: Place the image within the given dimensions and fill the remaining spaces using the given background color. * self::CROP_TYPE_EXACT_SIZE: Crop according to the given dimensions while maintaining the original aspect ratio. * The resulting image may be cover only part of the original image. * self::CROP_TYPE_UPPER: Crops the image so that only the upper part of the image remains. * @param unknown_type $bgcolor - backround color (6 hex digits web colorcode) * @param unknown_type $force_jpeg - Force the source image file to convert into a Jpeg file * @param unknown_type $quality - Jpeg quality for output [0-100] * @param unknown_type $src_x - 1st part of a rectangle to take from original picture (starting from vertical picsal {value} to right end of picture) * @param unknown_type $src_y - 2nd part of a rectangle to take from original picture (starting from horizonal picasl {value} downto down end of picture) * @param unknown_type $src_w - 3rd part of a rectangle to take from original picture (starting from picsal left end of picture to vertical pixel {value}) * @param unknown_type $src_h - 4rd part of a rectangle to take from original picture (starting from up end of picture downto horizonal pixesl {value}) * @return path to targetFile or null if the $source_file is not an image file */ public static function convertImage($source_file, $target_file, $width = self::DEFAULT_THUMBNAIL_WIDTH, $height = self::DEFAULT_THUMBNAIL_HEIGHT, $crop_type = self::CROP_TYPE_ORIGINAL_ASPECT_RATIO, $bgcolor = 0xffffff, $force_jpeg = false, $quality = 0, $src_x = 0, $src_y = 0, $src_w = 0, $src_h = 0, $density = 0, $stripProfiles = false, $thumbParams = null, $format = null) { if (is_null($thumbParams) || !$thumbParams instanceof kThumbnailParameters) { $thumbParams = new kThumbnailParameters(); } if (is_string($bgcolor) && strpos($bgcolor, '0x') === false) { $bgcolor = hexdec('0x' . $bgcolor); } // check if the source file is not an image file if (!file_exists($source_file) || filesize($source_file) === 0 || getimagesize($source_file) === false) { KalturaLog::log("convertImage - failed to get image size [{$source_file}] while creating [{$target_file}]"); return null; } // change target file extension if needed list($source_width, $source_height, $type, $attr) = getimagesize($source_file); if ($type == IMAGETYPE_BMP) { // convert bmp to jpeg $type = IMAGETYPE_JPEG; } if ($force_jpeg) { $ext = self::imageExtByType($type); if ($thumbParams->getSupportAnimatedThumbnail() && $ext == "gif") { $target_file = kFile::replaceExt($target_file, "gif"); } else { $target_file = kFile::replaceExt($target_file, "jpg"); $type = IMAGETYPE_JPEG; } } else { $target_file = kFile::replaceExt($target_file, self::imageExtByType($type)); } if (!is_null($format)) { $target_file = kFile::replaceExt($target_file, $format); } // do convertion $status = null; $imageCropper = new KImageMagickCropper($source_file, $target_file, kConf::get('bin_path_imagemagick')); $status = $imageCropper->crop($quality, $crop_type, $width, $height, $src_x, $src_y, $src_w, $src_h, null, null, $bgcolor, $density, null, $stripProfiles); if (!$status) { return null; } return $target_file; }
private function captureThumb(KalturaBatchJob $job, KalturaCaptureThumbJobData $data) { KalturaLog::debug("captureThumb({$job->id})"); try { $mediaFile = trim($data->srcFileSyncLocalPath); if (!file_exists($mediaFile)) { return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::NFS_FILE_DOESNT_EXIST, "Source file {$mediaFile} does not exist", KalturaBatchJobStatus::RETRY); } if (!is_file($mediaFile)) { return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::NFS_FILE_DOESNT_EXIST, "Source file {$mediaFile} is not a file", KalturaBatchJobStatus::FAILED); } KalturaLog::debug("mediaFile [{$mediaFile}]"); $this->updateJob($job, "Capturing thumbnail on {$mediaFile}", KalturaBatchJobStatus::QUEUED, 1); } catch (Exception $ex) { return $this->closeJob($job, KalturaBatchJobErrorTypes::RUNTIME, $ex->getCode(), "Error: " . $ex->getMessage(), KalturaBatchJobStatus::FAILED); } try { $data->thumbPath = null; // creates a temp file path $rootPath = $this->taskConfig->params->localTempPath; if (!is_dir($rootPath)) { if (!file_exists($rootPath)) { KalturaLog::info("Creating temp thumbnail directory [{$rootPath}]"); mkdir($rootPath); } else { // already exists but not a directory KalturaLog::err("Cannot create temp thumbnail directory [{$rootPath}] due to an error. Please fix and restart"); die; } } $capturePath = $mediaFile; if ($data->srcAssetType == KalturaAssetType::FLAVOR) { // creates the path $uniqid = uniqid('thumb_'); $capturePath = realpath($rootPath) . "/{$uniqid}"; // generates the thumbnail $thumbMaker = new KFFMpegThumbnailMaker($mediaFile, $capturePath, $this->taskConfig->params->FFMpegCmd); $created = $thumbMaker->createThumnail($data->thumbParamsOutput->videoOffset); if (!$created || !file_exists($capturePath)) { return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::THUMBNAIL_NOT_CREATED, "Thumbnail not created", KalturaBatchJobStatus::FAILED); } $this->updateJob($job, "Thumbnail captured [{$capturePath}]", KalturaBatchJobStatus::PROCESSING, 40); } else { KalturaLog::info("Source file is already an image"); } $uniqid = uniqid('thumb_'); $thumbPath = realpath($rootPath) . "/{$uniqid}"; $quality = $data->thumbParamsOutput->quality; $cropType = $data->thumbParamsOutput->cropType; $cropX = $data->thumbParamsOutput->cropX; $cropY = $data->thumbParamsOutput->cropY; $cropWidth = $data->thumbParamsOutput->cropWidth; $cropHeight = $data->thumbParamsOutput->cropHeight; $bgcolor = $data->thumbParamsOutput->backgroundColor; $width = $data->thumbParamsOutput->width; $height = $data->thumbParamsOutput->height; $scaleWidth = $data->thumbParamsOutput->scaleWidth; $scaleHeight = $data->thumbParamsOutput->scaleHeight; $cropper = new KImageMagickCropper($capturePath, $thumbPath, $this->taskConfig->params->ImageMagickCmd, true); $cropped = $cropper->crop($quality, $cropType, $width, $height, $cropX, $cropY, $cropWidth, $cropHeight, $scaleWidth, $scaleHeight, $bgcolor); if (!$cropped || !file_exists($thumbPath)) { return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::THUMBNAIL_NOT_CREATED, "Thumbnail not cropped", KalturaBatchJobStatus::FAILED); } $data->thumbPath = $thumbPath; $job = $this->moveFile($job, $data); if ($this->checkFileExists($job->data->thumbPath)) { $updateData = new KalturaCaptureThumbJobData(); $updateData->thumbPath = $data->thumbPath; return $this->closeJob($job, null, null, null, KalturaBatchJobStatus::FINISHED, $updateData); } return $this->closeJob($job, KalturaBatchJobErrorTypes::APP, KalturaBatchJobAppErrors::NFS_FILE_DOESNT_EXIST, 'File not moved correctly', KalturaBatchJobStatus::FAILED, $data); } catch (Exception $ex) { return $this->closeJob($job, KalturaBatchJobErrorTypes::RUNTIME, $ex->getCode(), "Error: " . $ex->getMessage(), KalturaBatchJobStatus::FAILED); } }