/** * @covers media_subdef::get_pathfile */ public function testGet_pathfile() { $this->assertEquals(self::$objectPresent->get_path() . self::$objectPresent->get_file(), self::$objectPresent->get_pathfile()); $this->assertEquals(self::$objectNotPresent->get_path() . self::$objectNotPresent->get_file(), self::$objectNotPresent->get_pathfile()); $this->assertTrue(file_exists(self::$objectPresent->get_pathfile())); $this->assertTrue(file_exists(self::$objectNotPresent->get_pathfile())); $this->assertTrue(is_readable(self::$objectPresent->get_pathfile())); $this->assertTrue(is_readable(self::$objectNotPresent->get_pathfile())); $this->assertTrue(is_writable(self::$objectPresent->get_pathfile())); $this->assertTrue(is_writable(self::$objectNotPresent->get_pathfile())); }
/** * * @param Application $app * @param \media_subdef $subdef * * @return boolean|string */ public static function watermark(Application $app, \media_subdef $subdef) { static $palette; if (null === $palette) { $palette = new RGB(); } $base_id = $subdef->get_record()->get_base_id(); if ($subdef->get_name() !== 'preview') { return $subdef->get_pathfile(); } if ($subdef->get_type() !== \media_subdef::TYPE_IMAGE) { return $subdef->get_pathfile(); } if (!$subdef->is_physically_present()) { return false; } $pathIn = $subdef->get_path() . $subdef->get_file(); if (!is_file($pathIn)) { return false; } $pathOut = $subdef->get_path() . 'watermark_' . $subdef->get_file(); // cache if (is_file($pathOut)) { return $pathOut; } $in_image = $app['imagine']->open($pathIn); $in_size = $in_image->getSize(); $in_w = $in_size->getWidth(); $in_h = $in_size->getHeight(); $wm_file = $app['root.path'] . '/config/wm/' . $base_id; if (file_exists($wm_file)) { $wm_image = $app['imagine']->open($wm_file); $wm_size = $wm_image->getSize(); $wm_w = $wm_size->getWidth(); $wm_h = $wm_size->getHeight(); if ($wm_w / $wm_h > $in_w / $in_h) { $wm_size = $wm_size->widen($in_w); } else { $wm_size = $wm_size->heighten($in_h); } $wm_image->resize($wm_size); $in_image->paste($wm_image, new Point($in_w - $wm_size->getWidth() >> 1, $in_h - $wm_size->getHeight() >> 1))->save($pathOut); } else { $collname = $subdef->get_record()->get_collection()->get_name(); $draw = $in_image->draw(); $black = $palette->color("000000"); $white = $palette->color("FFFFFF"); $draw->line(new Point(0, 1), new Point($in_w - 2, $in_h - 1), $black); $draw->line(new Point(1, 0), new Point($in_w - 1, $in_h - 2), $white); $draw->line(new Point(0, $in_h - 2), new Point($in_w - 2, 0), $black); $draw->line(new Point(1, $in_h - 1), new Point($in_w - 1, 1), $white); $fsize = max(8, (int) (max($in_w, $in_h) / 30)); $fonts = [$app['imagine']->font(__DIR__ . '/arial.ttf', $fsize, $black), $app['imagine']->font(__DIR__ . '/arial.ttf', $fsize, $white)]; $testbox = $fonts[0]->box($collname, 0); $tx_w = min($in_w, $testbox->getWidth()); $tx_h = min($in_h, $testbox->getHeight()); $x0 = max(1, $in_w - $tx_w >> 1); $y0 = max(1, $in_h - $tx_h >> 1); for ($i = 0; $i <= 1; $i++) { $x = max(1, ($in_w >> 2) - ($tx_w >> 1)); $draw->text($collname, $fonts[$i], new Point($x - $i, $y0 - $i), 0); $x = max(1, $in_w - $x - $tx_w); $draw->text($collname, $fonts[$i], new Point($x - $i, $y0 - $i), 0); $y = max(1, ($in_h >> 2) - ($tx_h >> 1)); $draw->text($collname, $fonts[$i], new Point($x0 - $i, $y - $i), 0); $y = max(1, $in_h - $y - $tx_h); $draw->text($collname, $fonts[$i], new Point($x0 - $i, $y - $i), 0); } } $in_image->save($pathOut); if (is_file($pathOut)) { return $pathOut; } return false; }