/** * 水印预览 */ public function viewAction() { $config = array('mark.limitwidth' => abs(intval($this->getInput('markLimitwidth', 'post'))), 'mark.limitheight' => abs(intval($this->getInput('markLimitheight', 'post'))), 'mark.position' => $this->getInput('markPosition', 'post'), 'mark.gif' => $this->getInput('markGif', 'post'), 'mark.type' => $this->getInput('markType', 'post'), 'mark.text' => $this->getInput('markText', 'post'), 'mark.fontfamily' => $this->getInput('markFontfamily', 'post'), 'mark.fontsize' => $this->getInput('markFontsize', 'post'), 'mark.fontcolor' => $this->getInput('markFontcolor', 'post'), 'mark.quality' => abs(intval($this->getInput('markQuality', 'post'))), 'mark.file' => $this->getInput('markFile', 'post'), 'mark.transparency' => abs(intval($this->getInput('markTransparency', 'post'))), 'mark.quality' => abs(intval($this->getInput('markQuality', 'post')))); Wind::import('LIB:image.PwImage'); Wind::import('LIB:image.PwImageWatermark'); $image = new PwImage(Wind::getRealDir('REP:demo', false) . '/demo.jpg'); $watermark = new PwImageWatermark($image); $watermark->setPosition($config['mark.position'])->setType($config['mark.type'])->setTransparency($config['mark.transparency'])->setQuality($config['mark.quality'])->setDstfile(Wind::getRealDir('PUBLIC:attachment', false) . '/demo.jpg'); if ($config['mark.type'] == 1) { $watermark->setFile($config['mark.file']); } else { $watermark->setText($config['mark.text'])->setFontfamily($config['mark.fontfamily'])->setFontsize($config['mark.fontsize'])->setFontcolor($config['mark.fontcolor']); } $watermark->execute(); $this->setOutput(Wekit::url()->attach . '/demo.jpg?' . time(), 'data'); $this->showMessage('ADMIN:success'); }
/** * 图片生成水印 * * @param PwImage $image 图片对象 * @param array $options 生成方案配置 */ public static function watermark(PwImage $image, $options = array()) { if (!in_array($image->type, array('gif', 'jpeg', 'png'))) { return; } $config = Wekit::C('attachment'); if ($options) { foreach ($options as $key => $value) { $config['mark.' . $key] = $value; } } if ($image->type == 'gif' && !$config['mark.gif']) { return; } if ($image->width < $config['mark.limitwidth'] || $image->height < $config['mark.limitheight']) { return; } Wind::import('LIB:image.PwImageWatermark'); $watermark = new PwImageWatermark($image); $watermark->setPosition($config['mark.position'])->setType($config['mark.type'])->setTransparency($config['mark.transparency'])->setQuality($config['mark.quality']); if ($config['mark.type'] == 1) { $watermark->setFile($config['mark.file']); } else { $watermark->setText($config['mark.text'])->setFontfamily($config['mark.fontfamily'])->setFontsize($config['mark.fontsize'])->setFontcolor($config['mark.fontcolor']); } $watermark->execute(); }