Exemplo n.º 1
0
 protected static function convertImpl($binary)
 {
     try {
         $in = new Resource(@imagecreatefromstring($binary), 'imagedestroy');
         if (!$in->get()) {
             throw new Exception();
         }
         $out = new Resource(imagecreatetruecolor(self::OUT_WIDTH, self::OUT_HEIGHT), 'imagedestroy');
         if (!$out->get()) {
             throw new Exception();
         }
         $inW = imagesx($in->get());
         $inH = imagesy($in->get());
         if ($inW < 100 || $inH < 100) {
             throw new Exception();
         }
         $scale = min(self::OUT_WIDTH / $inW, self::OUT_HEIGHT / $inH);
         $cpW = (int) round($inW * $scale);
         $cpH = (int) round($inH * $scale);
         $cpX = (int) round(self::OUT_WIDTH / 2 - $cpW / 2);
         $cpY = (int) round(self::OUT_HEIGHT / 2 - $cpH / 2);
         imagealphablending($out->get(), false);
         imagefill($out->get(), 0, 0, 0xffffff);
         imagealphablending($out->get(), true);
         imagecopyresampled($out->get(), $in->get(), $cpX, $cpY, 0, 0, $cpW, $cpH, $inW, $inH);
         $tmpName = new Resource(tempnam(sys_get_temp_dir(), 'statink-'), 'unlink');
         imagepng($out->get(), $tmpName->get(), 9, PNG_ALL_FILTERS);
         return $tmpName;
     } catch (Exception $e) {
     }
     return false;
 }
Exemplo n.º 2
0
 public function actionSave()
 {
     $outPath = sprintf('%s/statink-%s.dump.xz.aes', Yii::getAlias('@app/runtime/backup'), date('YmdHis', time()));
     if (!file_exists(dirname($outPath))) {
         mkdir(dirname($outPath), 0755, true);
     }
     $tmpPath = new Resource(tempnam('backup-', sys_get_temp_dir()), 'unlink');
     $this->stdout("Dumping database... ", Console::FG_YELLOW);
     $execinfo = $this->createDumpCommandLine($tmpPath->get());
     $descriptorspec = [['pipe', 'r'], ['pipe', 'w']];
     $pipes = [];
     $proc = @proc_open($execinfo['cmdline'], $descriptorspec, $pipes, getcwd(), $execinfo['env']);
     if (!$proc) {
         $this->stdout("ERROR\n", Console::FG_RED);
         return 1;
     }
     fclose($pipes[0]);
     stream_get_contents($pipes[1]);
     fclose($pipes[1]);
     $status = proc_close($proc);
     if ($status !== 0) {
         $this->stdout("ERROR\n", Console::FG_RED);
         return 1;
     }
     $this->stdout("SUCCESS\n", Console::FG_GREEN);
     $this->stdout("Encrypting dump file... ", Console::FG_YELLOW);
     $crypt = new FileCipher();
     $crypt->setKey(include Yii::getAlias('@app/config/backup-secret.php'));
     if (!$crypt->encrypt($tmpPath->get(), $outPath)) {
         $this->stdout("ERROR\n", Console::FG_RED);
         return 1;
     }
     $this->stdout("SUCCESS\n", Console::FG_GREEN);
     return 0;
 }
Exemplo n.º 3
0
 protected static function convertImpl($binary, $myPositionIfFill)
 {
     try {
         $in = new Resource(@imagecreatefromstring($binary), 'imagedestroy');
         if (!$in->get()) {
             throw new Exception();
         }
         $out = new Resource(imagecreatetruecolor(self::OUT_WIDTH, self::OUT_HEIGHT), 'imagedestroy');
         if (!$out->get()) {
             throw new Exception();
         }
         $inW = imagesx($in->get());
         $inH = imagesy($in->get());
         if ($inW < 100 || $inH < 100) {
             throw new Exception();
         }
         $scale = min(self::OUT_WIDTH / $inW, self::OUT_HEIGHT / $inH);
         $cpW = (int) round($inW * $scale);
         $cpH = (int) round($inH * $scale);
         $cpX = (int) round(self::OUT_WIDTH / 2 - $cpW / 2);
         $cpY = (int) round(self::OUT_HEIGHT / 2 - $cpH / 2);
         imagealphablending($out->get(), false);
         imagefill($out->get(), 0, 0, 0xffffff);
         imagealphablending($out->get(), true);
         imagecopyresampled($out->get(), $in->get(), $cpX, $cpY, 0, 0, $cpW, $cpH, $inW, $inH);
         if (is_int($myPositionIfFill)) {
             for ($i = 0; $i < 8; ++$i) {
                 // 自分自身はスキップ
                 if ($myPositionIfFill - 1 == $i) {
                     continue;
                 }
                 $y = ($i < 4 ? 50 : 215) + $i % 4 * 33;
                 imagefilledrectangle($out->get(), 406, $y, 406 + 86, $y + 19, 0x0);
             }
         }
         $tmpName = new Resource(tempnam(sys_get_temp_dir(), 'statink-'), 'unlink');
         imagepng($out->get(), $tmpName->get(), 9, PNG_ALL_FILTERS);
         return $tmpName;
     } catch (Exception $e) {
     }
     return false;
 }
 public function actionGenerate($inFile, $outDir)
 {
     if (!($inImg = imagecreatefrompng($inFile))) {
         echo "Could not read {$inFile} as a PNG image.\n";
         return 1;
     }
     for ($hue = 0; $hue < 360; $hue += 2) {
         $outFile = "{$outDir}/{$hue}.png";
         echo "Creating {$outFile}\n";
         $tmpFile = new Resource(tempnam(sys_get_temp_dir(), 'graphicon-'), 'unlink');
         if (!$this->generate($inImg, $tmpFile->get(), $hue, 0.8, 0.8)) {
             return 1;
         }
         $cmdline = sprintf('/usr/bin/env %s -rem allb -l 9 -q %s %s', escapeshellarg('pngcrush'), escapeshellarg($tmpFile->get()), escapeshellarg($outFile));
         $lines = $status = null;
         exec($cmdline, $lines, $status);
         if ($status !== 0) {
             echo "Could not optimize new PNG image file.\n";
             return 1;
         }
     }
 }
Exemplo n.º 5
0
 private function executeArchivePlan(\stdClass $plan)
 {
     $this->stdout("[archive] Creating archive " . basename($plan->dst) . " ... ", Console::FG_YELLOW);
     if (!file_exists(dirname($plan->dst))) {
         mkdir(dirname($plan->dst), 0755, true);
     }
     if (file_exists($plan->dst)) {
         $this->stdout("ALREADY EXISTS\n", Console::FG_YELLOW);
         return true;
     }
     // tar に喰わせるためのファイルリストを作成
     $tmpFile = new Resource(tempnam(Yii::getAlias('@archive'), 'tmp-'), 'unlink');
     file_put_contents($tmpFile->get(), implode("\n", $plan->srcFiles) . "\n");
     // tar の実行
     $chdir = new Resource(getcwd(), function ($dir) {
         chdir($dir);
     });
     chdir($plan->srcDir);
     $cmdline = sprintf('/usr/bin/env %s -c -T %s -f %s', escapeshellarg('tar'), escapeshellarg($tmpFile->get()), escapeshellarg($plan->dst));
     $lines = $status = null;
     exec($cmdline, $lines, $status);
     if ($status === 0) {
         $this->stdout("SUCCESS\n", Console::FG_GREEN);
         return true;
     }
     $this->stderr("FAILED with code={$status}\n", Console::FG_RED);
     return false;
 }