|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright (c) 2018-present, Renderforest, LLC. |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * This source code is licensed under the license found in the |
| 7 | + * LICENSE file in the root directory. |
| 8 | + */ |
| 9 | + |
| 10 | +require 'vendor/autoload.php'; |
| 11 | + |
| 12 | +class Sample { |
| 13 | + private $renderforest; |
| 14 | + private $request; |
| 15 | + |
| 16 | + /** |
| 17 | + * Sample constructor. |
| 18 | + */ |
| 19 | + public function __construct() |
| 20 | + { |
| 21 | + $this->renderforest = new \Renderforest\Client(['signKey' => '<signKey>', 'clientId' => -1]); |
| 22 | + $this->request = new GuzzleHttp\Client(); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Gets project from API, checks if project is rendering, then waits for $seconds and goes recursive. |
| 27 | + * @param number $projectId - The project id to check. |
| 28 | + * @param string $quality - The video quality. |
| 29 | + * @param int $seconds - Seconds to wait until next request. |
| 30 | + * @return string |
| 31 | + * @throws \GuzzleHttp\Exception\GuzzleException |
| 32 | + */ |
| 33 | + private function getRenderedVideoURL($projectId, $quality, $seconds = 10) |
| 34 | + { |
| 35 | + $project = $this->renderforest->getProject(['projectId' => $projectId]); |
| 36 | + |
| 37 | + if (array_key_exists('rendering', $project)) { |
| 38 | + sleep($seconds); |
| 39 | + return $this->getRenderedVideoURL($projectId, $quality, $seconds); |
| 40 | + } |
| 41 | + |
| 42 | + return $project['renderedQualities']->{is_numeric($quality) ? 'hd' . $quality : 'free'}; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Downloads file with given $url and returns request status. |
| 47 | + * @param $url - The url to download. |
| 48 | + * @param $name - The file name to save. |
| 49 | + * @param $extension - The extension of file. |
| 50 | + * @return string |
| 51 | + */ |
| 52 | + private function downloadFile($url, $name, $extension) |
| 53 | + { |
| 54 | + $response = $this->request->get($url, ['sink' => "./$name.$extension", 'w']); |
| 55 | + |
| 56 | + return $response->getReasonPhrase(); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Example of creating project from scratch. |
| 61 | + * @return string |
| 62 | + * @throws \GuzzleHttp\Exception\GuzzleException |
| 63 | + */ |
| 64 | + public function sample() |
| 65 | + { |
| 66 | + $newProject = $this->renderforest->addProject([ |
| 67 | + 'templateId' => 701 |
| 68 | + ]); |
| 69 | + |
| 70 | + $projectDataInstance = $this->renderforest->getProjectData(['projectId' => $newProject['projectId']]); |
| 71 | + |
| 72 | + // make some change |
| 73 | + $projectDataInstance->setMuteMusic(true); |
| 74 | + |
| 75 | + $styles = [ |
| 76 | + 'theme' => '1', // optional |
| 77 | + 'transition' => '2' // optional |
| 78 | + ]; |
| 79 | + |
| 80 | + $projectDataInstance->setStyles($styles); // get theme/transition from ./templates API |
| 81 | + |
| 82 | + $voiceOver = [ |
| 83 | + 'path' => 'https://example.com/voice-ower.mp3' // optional |
| 84 | + ]; |
| 85 | + |
| 86 | + $projectDataInstance->setVoiceOver($voiceOver); |
| 87 | + |
| 88 | + // sound from ./sounds API |
| 89 | + $sound1 = [ |
| 90 | + 'duration' => 120, |
| 91 | + 'id' => 559, |
| 92 | + 'genre' => 'Rock', // optional |
| 93 | + 'lowQuality' => 'https://example.com/sample-low.mp3', |
| 94 | + 'path' => 'https://example.com/sample.mp3', |
| 95 | + 'title' => 'Inspiring Piano' |
| 96 | + ]; |
| 97 | + |
| 98 | + // your own sound |
| 99 | + $sound2 = [ |
| 100 | + 'duration' => 12, |
| 101 | + 'fileSize' => 198658, |
| 102 | + 'id' => 952626, |
| 103 | + 'path' => 'https://example.com/sample.mp3', |
| 104 | + 'title' => 'sound sample.mp3', |
| 105 | + 'userId' => 1469277, |
| 106 | + 'voiceOver' => false |
| 107 | + ]; |
| 108 | + |
| 109 | + $sounds = [$sound1, $sound2]; |
| 110 | + $projectDataInstance->setSounds($sounds); |
| 111 | + |
| 112 | + $screens = $projectDataInstance->getScreens(); |
| 113 | + // set text on text holder area |
| 114 | + if ($screens && $screens[0]) { |
| 115 | + $areas = $screens[0]['getAreas'](); |
| 116 | + |
| 117 | + $area = isset($areas[0]) ? isset($areas[0]) : []; |
| 118 | + |
| 119 | + if ($area && $area['type'] === 'text') { |
| 120 | + $area['setText']('sample text'); |
| 121 | + } |
| 122 | + } |
| 123 | + // set image on image holder area |
| 124 | + if ($screens && $screens[1]) { |
| 125 | + $areas = $screens[1]['getAreas'](); |
| 126 | + |
| 127 | + $area = isset($areas[0]) ? isset($areas[0]) : []; |
| 128 | + |
| 129 | + if ($area && $area['type'] === 'image') { |
| 130 | + $image = [ |
| 131 | + 'fileName' => 'sample file name', // optional |
| 132 | + 'mime' => 'image/png', // optional |
| 133 | + 'filePath' => 'https://example.com/sample.png', |
| 134 | + 'webpPath' => 'https://example.com/sample.webp', // optional |
| 135 | + 'fileType' => 'image', // optional |
| 136 | + 'thumbnailPath' => 'https://example.com/sample-thumbnail.png', // optional |
| 137 | + 'imageCropParams' => [ |
| 138 | + 'transform' => 0, |
| 139 | + 'top' => 11, |
| 140 | + 'left' => 0, |
| 141 | + 'width' => 798, |
| 142 | + 'height' => 456 |
| 143 | + ] |
| 144 | + ]; |
| 145 | + |
| 146 | + $area['setImage']($image); |
| 147 | + } |
| 148 | + } |
| 149 | + // set video on video holder area |
| 150 | + if ($screens && $screens[2]) { |
| 151 | + $areas = $screens[2]['getAreas'](); |
| 152 | + |
| 153 | + $area = $areas[0]; |
| 154 | + if ($area && $area['type'] === 'video') { |
| 155 | + $video = [ |
| 156 | + 'fileName' => 'sample file name', // optional |
| 157 | + 'mime' => 'video/mp4', // optional |
| 158 | + 'filePath' => 'https://example.com/sample.png', |
| 159 | + 'webpPath' => 'https://example.com/sample.webp', // optional |
| 160 | + 'fileType' => 'video', // optional |
| 161 | + 'videoCropParams' => [ |
| 162 | + 'duration' => 6, |
| 163 | + 'mime' => 'video/mp4', |
| 164 | + 'thumbnail' => 'https://example.com/sample-thumbnail.png', |
| 165 | + 'thumbnailVideo' => 'https://example.com/sample-thumbnail-video.mp4', |
| 166 | + 'trims' => [0, 2, 3, 5], |
| 167 | + 'volume' => [ |
| 168 | + 'music' => 10, |
| 169 | + 'video' => 100 |
| 170 | + ] |
| 171 | + ] |
| 172 | + ]; |
| 173 | + |
| 174 | + $area['setVideo']($video); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + $projectColors = [ |
| 179 | + 'ffffff', 'a1d4ec', '1d2e54', '61a371', 'a0b6e7', 'e0d0ef', '5c1313', 'b2e1f4', '706bb5', 'b4ddf5' |
| 180 | + ]; |
| 181 | + |
| 182 | + $projectDataInstance->setProjectColors($projectColors); // get project colors from ./templates API |
| 183 | + |
| 184 | + $screen = [ |
| 185 | + 'id' => 2125620, |
| 186 | + 'characterBasedDuration' => true, |
| 187 | + 'compositionName' => '191_man_Angry_2', |
| 188 | + 'duration' => 5, |
| 189 | + 'extraVideoSecond' => 0, |
| 190 | + 'iconAdjustable' => 0, |
| 191 | + 'gifPath' => 'https://example.com/191_man_Angry_2_1.gif', |
| 192 | + 'gifBigPath' => 'https://example.com/191_man_Angry_2_1.gif', |
| 193 | + 'gifThumbnailPath' => 'https://example.com/191_man_Angry_2_n.jpg', |
| 194 | + 'hidden' => false, |
| 195 | + 'maxDuration' => 15, |
| 196 | + 'order' => 1900, |
| 197 | + 'path' => 'https://example.com/191_man_Angry_2_n.jpg', |
| 198 | + 'tags' => 'business, computer, chair, desk, laptop, occupation, office, worker, arms, boss, boy, businessman,chef, company, employer, professional', |
| 199 | + 'title' => 'Angry Office worker with arms crossed', |
| 200 | + 'type' => 1, |
| 201 | + 'areas' => [ |
| 202 | + ['id' => 3562168, |
| 203 | + 'cords' => [656, 224, 1048, 224, 1048, 332, 656, 332], |
| 204 | + 'height' => 108, |
| 205 | + 'order' => 0, |
| 206 | + 'title' => 'char_Angry_2', |
| 207 | + 'type' => 'text', |
| 208 | + 'value' => '', |
| 209 | + 'wordCount' => 40, |
| 210 | + 'width' => 392] |
| 211 | + ] |
| 212 | + ]; |
| 213 | + |
| 214 | + $_screens = $projectDataInstance->pushScreen($screen); |
| 215 | + |
| 216 | + $projectDataInstance->setScreens($_screens); // get screen from ./templates API |
| 217 | + |
| 218 | + // get payload data |
| 219 | + $projectId = $projectDataInstance->getProjectId(); |
| 220 | + $data = $projectDataInstance->getPatchObject(); |
| 221 | + |
| 222 | + $updatePayload = [ |
| 223 | + 'projectId' => $projectId, |
| 224 | + 'data' => $data |
| 225 | + ]; |
| 226 | + |
| 227 | + $this->renderforest->updateProjectDataPartial($updatePayload); |
| 228 | + |
| 229 | + $projectDataInstance->resetPatchObject(); |
| 230 | + |
| 231 | + $renderPayload = [ |
| 232 | + 'projectId' => $newProject['projectId'], |
| 233 | + 'quality' => 360 |
| 234 | + ]; |
| 235 | + |
| 236 | + $this->renderforest->renderProject($renderPayload); |
| 237 | + |
| 238 | + $videoURL = $this->getRenderedVideoURL($renderPayload['projectId'], $renderPayload['quality']); |
| 239 | + |
| 240 | + return $this->downloadFile($videoURL, 'example-name', 'mp4'); |
| 241 | + } |
| 242 | +} |
| 243 | + |
| 244 | +try { |
| 245 | + $SampleClass = new Sample(); |
| 246 | + var_dump($SampleClass->sample()); // handle the success |
| 247 | +} catch (\GuzzleHttp\Exception\GuzzleException $e) { |
| 248 | + var_dump($e); // handle the error |
| 249 | +} |
0 commit comments