1+ <?php
2+ namespace Dosarkz \LaravelUploader ;
3+
4+ use Intervention \Image \Facades \Image ;
5+
6+ abstract class Uploader
7+ {
8+ public function __construct ()
9+ {
10+ $ this ->setFileName ($ this ->generateFileName ());
11+ $ this ->setThumb ($ this ->generateThumb ());
12+ $ this ->upload ();
13+ }
14+
15+ /**
16+ * @var
17+ */
18+ protected $ fileName ;
19+ /**
20+ * @var
21+ */
22+ protected $ thumb ;
23+ /**
24+ * @var
25+ */
26+ protected $ image ;
27+ /**
28+ * @var
29+ */
30+ protected $ destination ;
31+ /**
32+ * @var
33+ */
34+ public $ model ;
35+ /**
36+ * @var int
37+ */
38+ public $ imageHeight = null ;
39+ /**
40+ * @var int
41+ */
42+ public $ imageWidth = 1024 ;
43+ /**
44+ * @var int
45+ */
46+ public $ thumbHeight = null ;
47+ /**
48+ * @var int
49+ */
50+ public $ thumbWidth = 385 ;
51+
52+ /**
53+ * @return mixed
54+ */
55+ public function getFileName ()
56+ {
57+ return $ this ->fileName ;
58+ }
59+
60+ /**
61+ * @param $fileName
62+ */
63+ public function setFileName ($ fileName )
64+ {
65+ $ this ->fileName = $ fileName ;
66+ }
67+
68+ /**
69+ * @return string
70+ */
71+ public function getThumb ()
72+ {
73+ return $ this ->thumb ;
74+ }
75+
76+ /**
77+ * @param $thumb
78+ */
79+ public function setThumb ($ thumb )
80+ {
81+ $ this ->thumb = $ thumb ;
82+ }
83+
84+ /**
85+ * @return string
86+ */
87+ public function generateFileName ()
88+ {
89+ return time () . '_ ' . rand (0 ,100 ) . '. ' . $ this ->image ->getClientOriginalExtension ();
90+ }
91+
92+ /**
93+ * @return string
94+ */
95+ public function generateThumb ()
96+ {
97+ return 'thumb_ ' . $ this ->getFileName ();
98+ }
99+
100+ public function upload ()
101+ {
102+ if ($ this ->image ->getClientOriginalExtension () == 'gif ' )
103+ {
104+ $ this ->uploadFile ($ this ->getFileName ());
105+ }else {
106+ $ this ->uploadImageFile ($ this ->getFileName ());
107+ $ this ->uploadImageThumb ($ this ->getThumb ());
108+ }
109+ }
110+
111+ public function uploadFile ($ filename )
112+ {
113+ return $ this ->image ->move (public_path ($ this ->destination ), $ filename );
114+ }
115+
116+ /**
117+ * @param $filename
118+ * @param $resize
119+ * @return mixed
120+ */
121+ public function uploadImageFile ($ filename , $ resize = true )
122+ {
123+ if ($ resize )
124+ {
125+ list ($ width , $ height ) = getimagesize ($ this ->image ->getRealPath ());
126+
127+ if ($ width > $ this ->imageWidth )
128+ {
129+ return Image::make ($ this ->image ->getRealPath ())
130+ ->resize ($ this ->imageWidth , $ this ->imageHeight , function ($ constraint ) {
131+ $ constraint ->aspectRatio ();
132+ })->save (public_path ($ this ->destination . $ filename ));
133+ }else {
134+ return Image::make ($ this ->image ->getRealPath ())
135+ ->save (public_path ($ this ->destination . $ filename ));
136+ }
137+ }else {
138+ return Image::make ($ this ->image ->getRealPath ())
139+ ->save (public_path ($ this ->destination . $ filename ));
140+ }
141+
142+ }
143+
144+ /**
145+ * @param $thumb
146+ * @return mixed
147+ */
148+ public function uploadImageThumb ($ thumb )
149+ {
150+ list ($ width , $ height ) = getimagesize ($ this ->image ->getRealPath ());
151+
152+ if ($ width > $ this ->thumbWidth )
153+ {
154+ Image::make ($ this ->image ->getRealPath ())->resize ($ this ->thumbWidth , $ this ->thumbHeight , function ($ constraint ) {
155+ $ constraint ->aspectRatio ();
156+ })
157+ ->save (public_path ($ this ->destination . $ thumb ));
158+ }else {
159+ return Image::make ($ this ->image ->getRealPath ())
160+ ->save (public_path ($ this ->destination . $ thumb ));
161+ }
162+
163+
164+ return $ thumb ;
165+ }
166+
167+ }
0 commit comments