Example #1
0
 private static function __getImage($img, $type)
 {
     $client = new Client(Config::get('services.dropbox.token'), Config::get('services.dropbox.appName'));
     $fileSystem = new Filesystem(new DropboxAdapter($client, '/images/'));
     $biggerW = $biggerH = false;
     if ($img->width > $img->height) {
         $biggerW = true;
     } else {
         $biggerH = true;
     }
     $image = $w = $h = null;
     $public_path = public_path($img->path);
     //code minh
     //get image from dropbox
     if ($img->store == 'dropbox') {
         try {
             $file = $fileSystem->read($img->path);
             $public_path = $file;
         } catch (Exception $e) {
             return false;
         }
     }
     //end code
     if (in_array($type, ['with-logo', 'large-thumb'])) {
         if ($biggerW) {
             $w = 450;
         } else {
             $h = 450;
         }
     } else {
         if (in_array($type, ['thumb', 'small-thumb'])) {
             if ($type == 'thumb') {
                 if ($biggerW) {
                     $w = 150;
                 } else {
                     $h = 150;
                 }
             } else {
                 if ($biggerW) {
                     $w = 100;
                 } else {
                     $h = 100;
                 }
             }
         } else {
             if (in_array($type, ['crop', 'newcrop'])) {
                 $h = 300;
                 $w = 300;
                 if ($type == 'newcrop') {
                     $w = 600;
                 }
             }
         }
     }
     try {
         if (in_array($type, ['crop', 'newcrop'])) {
             $image = Image::make($public_path)->fit($w, $h, function ($constraint) {
                 $constraint->aspectRatio();
             });
         } else {
             $image = Image::make($public_path)->resize($w, $h, function ($constraint) {
                 $constraint->aspectRatio();
             });
         }
     } catch (Exception $e) {
         return false;
     }
     if ($type == 'with-logo') {
         if (Cache::has('mask')) {
             $mask = Cache::get('mask');
         } else {
             $mask = Configure::where('ckey', 'mask')->pluck('cvalue');
             if (empty($mask)) {
                 $mask = 'Visual Impact';
             }
             Cache::forever('mask', $mask);
         }
         $size = 50;
         $w = $image->width();
         $h = $image->height();
         $x = round($w / 2);
         $y = round($h / 2);
         $img = Image::canvas($w, $h);
         $string = wordwrap($mask, 15, '|');
         $strings = explode('|', $string);
         $line = 2;
         $i = round($y - count($strings) / 2 * ($size + $line));
         $from = $i - 20;
         foreach ($strings as $string) {
             $draw = new \ImagickDraw();
             $draw->setStrokeAntialias(true);
             $draw->setTextAntialias(true);
             $draw->setFont(public_path('assets' . DS . 'fonts' . DS . 'times.ttf'));
             $draw->setFontSize($size);
             $draw->setFillColor('rgb(0, 0, 0)');
             $draw->setFillOpacity(0.2);
             $draw->setTextAlignment(\Imagick::ALIGN_CENTER);
             $draw->setStrokeColor('#fff');
             $draw->setStrokeOpacity(0.2);
             $draw->setStrokeWidth(1);
             $dimensions = $img->getCore()->queryFontMetrics($draw, $string);
             $posy = $i + $dimensions['textHeight'] * 0.65 / 2;
             $img->getCore()->annotateImage($draw, $x, $posy, 0, $string);
             $i += $size + $line;
         }
         return $image->insert($img, 'center', $x, $y)->encode('jpg');
     }
     if ($image) {
         return $image->encode('jpg');
     }
     return false;
 }