public function addTemplate($fileName, $roleIds = null, $default = false) { $canvasWidth = self::CANVAS_WIDTH; $canvasHeight = $this->config['cover_height']; $coverImage = new UTIL_Image($fileName); $imageHeight = $coverImage->getHeight(); $imageWidth = $coverImage->getWidth(); $css = array('width' => 'auto', 'height' => 'auto'); $tmp = $canvasWidth * $imageHeight / $imageWidth; if ($tmp >= $canvasHeight) { $css['width'] = '100%'; } else { $css['height'] = '100%'; } $template = new UHEADER_BOL_Template(); $extension = UTIL_File::getExtension($fileName); $template->file = uniqid('template-') . '.' . $extension; $template->default = $default; $template->timeStamp = time(); $dimensions = array('height' => $imageHeight, 'width' => $imageWidth); $template->setSettings(array('dimensions' => $dimensions, 'css' => $css, 'canvas' => array('width' => $canvasWidth, 'height' => $canvasHeight), 'position' => array('top' => 0, 'left' => 0))); $this->service->saveTemplate($template); if ($roleIds !== null) { $this->service->saveRoleIdsForTemplateId($template->id, $roleIds); } $templatePath = $this->service->getTemplatePath($template); OW::getStorage()->copyFile($fileName, $templatePath); }
protected function getTplInfo(UHEADER_BOL_Template $template) { $canvas = $template->getCanvas(self::CANVAS_WIDTH); $previewCanvas = $template->getCanvas(self::ITEM_WIDTH); $css = $template->getCss(); $cssStr = $template->getCssString(); return array("id" => $template->id, "default" => (bool) $template->default, "src" => $this->service->getTemplateUrl($template), "data" => $template->getSettings(), "css" => $css, "cssStr" => $cssStr, "canvas" => $canvas, "previewCss" => $css, "previewCssStr" => $cssStr, "previewCanvas" => $previewCanvas, "users" => rand(1, 100)); }
private function addTemplate($file, $roleId = null) { $canvasWidth = self::CANVAS_WIDTH; $canvasHeight = $this->config['cover_height']; $this->validateFile($file); $pluginfilesDir = OW::getPluginManager()->getPlugin('uheader')->getPluginFilesDir(); $tmpTplPath = $pluginfilesDir . uniqid('tmp_') . '.jpg'; if (!move_uploaded_file($file['tmp_name'], $tmpTplPath)) { throw new InvalidArgumentException('Moving uploaded file faild'); } $coverImage = new UTIL_Image($tmpTplPath); $imageHeight = $coverImage->getHeight(); $imageWidth = $coverImage->getWidth(); $css = array('width' => 'auto', 'height' => 'auto'); $tmp = $canvasWidth * $imageHeight / $imageWidth; if ($tmp >= $canvasHeight) { $css['width'] = '100%'; } else { $css['height'] = '100%'; } $this->validateImage($coverImage, $canvasWidth, $canvasHeight); $template = new UHEADER_BOL_Template(); $extension = UTIL_File::getExtension($file['name']); $template->file = uniqid('template-') . '.' . $extension; $template->default = false; $template->timeStamp = time(); $dimensions = array('height' => $imageHeight, 'width' => $imageWidth); $template->setSettings(array('dimensions' => $dimensions, 'css' => $css, 'canvas' => array('width' => $canvasWidth, 'height' => $canvasHeight), 'position' => array('top' => 0, 'left' => 0))); $this->service->saveTemplate($template); if ($roleId !== null) { $this->service->saveRoleIdsForTemplateId($template->id, array($roleId)); } $templatePath = $this->service->getTemplatePath($template); OW::getStorage()->copyFile($tmpTplPath, $templatePath); @unlink($tmpTplPath); }