/**
  * Обработаем создание нового изображения для дальнейшего скачивания пользователем.
  */
 private function handleSaveImage()
 {
     $items = $_POST['items'];
     $category = $_POST['cat'];
     $image = $_POST['img'];
     $fileName = Image::getName($category, $image);
     $Palette = new RGB();
     $Imagine = new Imagine();
     $Image = $Imagine->open(IMG_1920x1080 . $fileName);
     foreach ($items as $item) {
         $text = $item['text'];
         $fontSize = $item['fontSize'];
         $x = $item['relativeX'];
         $y = $item['relativeY'];
         $sizedFontSize = $fontSize * 1920 / 500;
         $sizedX = $this->coeffX * $x;
         $sizedY = $this->coeffY * $y;
         $Font = $Imagine->font(FONT_DIR . 'Arial.ttf', $sizedFontSize, $Palette->color($item['color']));
         $Position = new Point($sizedX, $sizedY);
         $angle = rad2deg($item['angle']);
         $Image->draw()->text($text, $Font, $Position, $angle);
     }
     $id = uniqid();
     $filePath = IMG_DOWNLOAD . $id . Image::EXTENSION;
     $Image->save($filePath);
     header('Content-Type: application/json; charset=utf8');
     echo json_encode(['id' => $id], JSON_UNESCAPED_UNICODE);
     exit;
 }
 /**
  * {@inheritdoc}
  */
 public function run()
 {
     $Imagine = new Imagine();
     $ids = [];
     $id = $this->getFileCount();
     foreach ($this->getUrlMap() as $category => $images) {
         foreach ($images as $url) {
             $id++;
             echo 'Processed ' . $url . PHP_EOL;
             $resource = fopen($url, 'r');
             try {
                 $Image = $Imagine->read($resource);
             } catch (InvalidArgumentException $Ex) {
                 echo $Ex->getMessage() . ' Url: ' . $url . PHP_EOL;
                 continue;
             }
             $fileName = Image::getName($category, $id);
             $Image->save(IMG_1920x1080 . $fileName);
             $Image->resize(new Box(500, 281))->save(IMG_500xY . $fileName);
             $Image->resize(new Box(230, 129))->save(IMG_230xY . $fileName);
             $ids[$category][] = $id;
             echo '.... done ' . PHP_EOL;
         }
     }
     $result = [];
     foreach ($ids as $category => $imageIds) {
         $result[$category] = json_encode($imageIds);
     }
     file_put_contents('ids.txt', var_export($result, true));
     echo PHP_EOL . 'See ids.txt' . PHP_EOL . PHP_EOL;
 }
 /**
  * {@inheritdoc}
  */
 public function run()
 {
     $Imagine = new Imagine();
     $ids = [];
     $id = $this->getFileCount();
     foreach (CollectionCategory::getAllCategories() as $category) {
         $dirName = IMG_CREATE . $category . '/';
         if (!is_dir($dirName)) {
             continue;
         }
         $DirHandler = opendir($dirName);
         while ($file = readdir($DirHandler)) {
             if ($file == '.' || $file == '..') {
                 continue;
             }
             $imageName = $dirName . $file;
             $id++;
             echo 'Processed category ' . $category . ', file ' . $file . PHP_EOL;
             try {
                 $Image = $Imagine->open($imageName);
             } catch (InvalidArgumentException $Ex) {
                 echo $Ex->getMessage() . ' file: ' . $imageName . PHP_EOL;
                 continue;
             }
             $fileName = Image::getName($category, $id);
             $Image->save(IMG_1920x1080 . $fileName);
             $Image->resize(new Box(500, 281))->save(IMG_500xY . $fileName);
             $Image->resize(new Box(230, 129))->save(IMG_230xY . $fileName);
             unlink($imageName);
             $ids[$category][] = $id;
         }
     }
     $result = [];
     foreach ($ids as $category => $imageIds) {
         $result[$category] = json_encode($imageIds);
     }
     file_put_contents('ids.txt', var_export($result, true));
     var_dump($result);
     echo PHP_EOL . 'See ids.txt' . PHP_EOL . PHP_EOL;
 }
Beispiel #4
0
 /**
  * Применяет указанный фильтр к изображению.
  */
 private function handleApplyImageEffect()
 {
     header('Content-Type: application/json; charset=utf8');
     $image = $_POST['img'];
     $effect = $_POST['effect'];
     $previewPath = Image::getPreviewFile($image);
     $Imagine = new CustomImagine();
     $Image = $Imagine->open($previewPath);
     if (!$this->applyEffect($Image, $effect, $_POST['additional'])) {
         echo json_encode(['error' => 'Unknown filter effect']);
         exit;
     }
     $Image->save(Image::getWorkFile($image));
     echo json_encode(['url' => Image::getWorkWebPath($image) . '?r=' . rand(1, 119)]);
     exit;
 }