Пример #1
0
 /**
  * Summary.
  *
  * @since  0.9.0
  * @see
  *
  * @param \Symfony\Component\HttpFoundation\Request $request
  * @param \Silex\Application                        $app
  *
  * @return JsonResponse
  * @author nguyenvanduocit
  */
 public function generate(Request $request, Application $app)
 {
     $backgroundId = $request->get('backgroundId');
     $resultObject = new AttachmentResponse();
     if (array_key_exists($backgroundId, $this->memeList)) {
         $text = $request->get('text');
         $meme = $this->memeList[$backgroundId];
         $fileName = md5(mt_rand(1, 20)) . '.jpg';
         $mainLayer = ImageWorkshop::initFromPath($meme['src']);
         $textPaths = explode(';', $text);
         foreach ($textPaths as $index => $text) {
             if (array_key_exists($index, $meme['position'])) {
                 $position = $meme['position'][$index];
             } else {
                 $lastPostion = count($meme['position']) - 1;
                 $position = $meme['position'][$lastPostion];
             }
             $textLayer = ImageWorkshop::initTextLayer($text, APP_DIR . '/Asset/font/OpenSans-Bold.ttf', $meme['font']['size'], $meme['font']['color'], 0, null);
             if ($textLayer->getWidth() >= $mainLayer->getWidth()) {
                 $textLayer->resizeInPixel($mainLayer->getWidth() - 100, NULL, TRUE);
             }
             if ($position === 'top') {
                 $y = 0 + 20;
             } else {
                 $y = $mainLayer->getHeight() - $textLayer->getHeight() - 20;
             }
             $x = $mainLayer->getWidth() / 2 - $textLayer->getWidth() / 2;
             $mainLayer->addLayer($index, $textLayer, $x, $y);
         }
         $mainLayer->save($this->outputDir, $fileName);
         $resultObject->setImageUrl('http://slackbotapi.senviet.org/web/public/meme/' . $fileName . '?rand=' . uniqid('rand', FALSE));
     } else {
         $resultObject->setImageUrl('http://slackbotapi.senviet.org/web/public/meme/list.jpg');
     }
     return new JsonResponse($resultObject);
 }
 /**
  * Summary.
  *
  * @since  0.9.0
  * @see
  *
  * @param \Symfony\Component\HttpFoundation\Request $request
  * @param \Silex\Application                        $app
  *
  * @return JsonResponse
  * @author nguyenvanduocit
  */
 public function post(Request $request, Application $app)
 {
     $category = $request->get('category', 'all');
     /**
      * Todo support another fact
      */
     $response = new AttachmentResponse();
     if ($category === 'programming') {
         $data = $this->getRandomFactOfProgramming();
         $response->setTitle($data['title']);
         $response->setTitleLink('http://laptrinh.senviet.org');
         $response->setImageUrl($data['image_url']);
     } else {
         $categorylist = implode(', ', array_keys($this->images));
         $response->setText('I don\'t know about this category, I only know abount ' . $categorylist);
     }
     return new JsonResponse($response);
 }