forked from PhieF/OhMyGif
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload_helper.php
More file actions
109 lines (98 loc) · 3.33 KB
/
upload_helper.php
File metadata and controls
109 lines (98 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
require_once('config/config.php');
require_once 'vendor/autoload.php';
class UploadHelper {
function saveUploadedGif($file){
$uploaddir = 'uploads/';
$uploadfile = $uploaddir . md5(uniqid()).".gif";
$result =array();
echo $file['type'];
if(!empty($file['type']) AND $file['type'] != "image/gif"){
$result["status"] = 1;
echo "img should be a gif";
return $result;
}
echo "moving";
if (move_uploaded_file($file['tmp_name'], $uploadfile)) {
echo "moved";
$result["status"] = 0;
$result["url"] = $uploadfile;
$result["webm"] = $this->toWebM($uploadfile);
$result["original_name"] = $file['name'];
$result["thumbnail"] = $this->getThumbnail($uploadfile);
} else {
$result["status"] = 1;
}
return $result;
}
function downloadGif($url, $webm){
$uploaddir = 'uploads/';
$name = md5(uniqid());
$uploadfile = $uploaddir . $name.".gif";
$result =array();
$result["status"] = 1;
if(file_put_contents($uploadfile,tor_file_get_contents($url))){
$result["status"] = 0;
$result["url"] = $uploadfile;
if(empty($webm))
$result["webm"] = $this->toWebM($uploadfile);
else{
$uploadfile = $uploaddir . $name.".webm";
if(file_put_contents($uploadfile,tor_file_get_contents($webm)))
$result["webm"] = $uploadfile;
}
$result["thumbnail"] = $this->getThumbnail($uploadfile);
unlink($uploadfile);
$result["original_name"] = "";
}
return $result;
}
function toWebM($path){
echo "starting encoding";
$ffmpeg = \FFMpeg\FFMpeg::create(array(
'ffmpeg.binaries' =>'/usr/bin/ffmpeg',
'ffprobe.binaries' => '/usr/bin/ffprobe',
'timeout' => 3600
));
$video = $ffmpeg->open($path);
$format = new \FFMpeg\Format\Video\WebM();
$format->setAdditionalParameters(array("-auto-alt-ref","0"));
if($video
->save($format, "uploads/".basename($path,"gif")."webm"))
return "uploads/".basename($path,"gif")."webm";
}
function toGif($path){
$tmpPath = sys_get_temp_dir()."/ohmygif";
if(!file_exists($tmpPath))
mkdir($tmpPath);
$ffprobe = \FFMpeg\FFProbe::create(array(
'ffmpeg.binaries' =>'/usr/bin/ffmpeg',
'ffprobe.binaries' => '/usr/bin/ffprobe',
'timeout' => 3600
));
$duration = (int) $ffprobe->format($path)->get('duration');
$dimensions = $ffprobe->streams($path)->videos()->first()->getDimensions();
$gifPath = $tmpPath."/".basename($path,"webm")."gif";
// Transform
$ffmpeg = \FFMpeg\FFMpeg::create(array(
'ffmpeg.binaries' =>'/usr/bin/ffmpeg',
'ffprobe.binaries' => '/usr/bin/ffprobe',
'timeout' => 3600
));
$ffmpegVideo = $ffmpeg->open($path);
$ffmpegVideo->gif(FFMpeg\Coordinate\TimeCode::fromSeconds(0), $dimensions, $duration)->save($gifPath);
return $gifPath;
}
function getThumbnail($path){
echo "creating thumbnail";
$ffmpeg = \FFMpeg\FFMpeg::create(array(
'ffmpeg.binaries' =>'/usr/bin/ffmpeg',
'ffprobe.binaries' => '/usr/bin/ffprobe',
'timeout' => 3600
));
$video = $ffmpeg->open($path);
if($video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(0))->save("uploads/".basename($path,"gif")."png"))
return "uploads/".basename($path,"gif")."png";
}
}
?>