} if ($is_unset_opts) { fn_update_addon($_REQUEST['addon_data']); return array(CONTROLLER_STATUS_REDIRECT, 'addons.manage'); } } } if ($mode == 'update') { if ($_REQUEST['addon'] == 'watermarks') { $wt_fonts = array('arial' => 'Arial', 'arialbd' => 'Arial Bold', 'georgia' => 'Georgia', 'symbol' => 'Symbol', 'times' => 'Times New Roman', 'verdana' => 'Verdana'); Tygh::$app['view']->assign('wt_fonts', $wt_fonts); $wt_font_sizes = range(5, 50, 5); Tygh::$app['view']->assign('wt_font_sizes', $wt_font_sizes); $wt_font_colors = array('white' => 'White', 'black' => 'Black', 'gray' => 'Gray', 'clear_gray' => 'Clear gray'); Tygh::$app['view']->assign('wt_font_colors', $wt_font_colors); $wt_settings = fn_get_watermark_settings(); Tygh::$app['view']->assign('wt_settings', $wt_settings); } } function fn_update_watermark_image_settings($wt_settings, $company_id = null, $attached_image_id = null) { if (!($setting_id = Settings::instance()->getId('watermark', ''))) { $setting_id = Settings::instance()->update(array('name' => 'watermark', 'section_id' => 0, 'section_tab_id' => 0, 'type' => 'A', 'position' => 0, 'is_global' => 'N', 'handler' => '')); } Settings::instance()->updateValueById($setting_id, serialize($wt_settings), $company_id); if ($wt_settings['type'] == 'G') { $_REQUEST['wt_image_image_data'][0]['image_alt'] = ''; $_REQUEST['wt_image_image_data'][0]['detailed_alt'] = ''; $image_id = !empty($company_id) ? $company_id : WATERMARK_IMAGE_ID; if (!is_null($attached_image_id)) { fn_clone_image_pairs($image_id, $attached_image_id, 'watermark');
function fn_watermark_create($source_filepath, $target_filepath, $is_detailed = false, $company_id = null, $generate_watermark = true) { $original_abs_path = Storage::instance('images')->getAbsolutePath($source_filepath); list(, , , $original_abs_path) = fn_get_image_size($original_abs_path); if (!$generate_watermark) { Storage::instance('images')->put($target_filepath, array('file' => $original_abs_path, 'keep_origins' => true)); return true; } $settings = fn_get_watermark_settings($company_id); if (empty($settings)) { return false; } list($settings['horizontal_position'], $settings['vertical_position']) = explode('_', $settings['position']); /** @var \Imagine\Image\ImagineInterface $imagine */ $imagine = Tygh::$app['image']; try { $image = $imagine->open($original_abs_path); $image->usePalette(new \Imagine\Image\Palette\RGB()); $filter = $imagine instanceof \Imagine\Gd\Imagine ? \Imagine\Image\ImageInterface::FILTER_UNDEFINED : \Imagine\Image\ImageInterface::FILTER_LANCZOS; if ($settings['type'] == WATERMARK_TYPE_GRAPHIC) { $watermark_image_file_path = false; if ($is_detailed) { if (!empty($settings['image_pair']['detailed']['absolute_path'])) { $watermark_image_file_path = $settings['image_pair']['detailed']['absolute_path']; } } elseif (!empty($settings['image_pair']['icon']['absolute_path'])) { $watermark_image_file_path = $settings['image_pair']['icon']['absolute_path']; } if (!$watermark_image_file_path) { return false; } list(, , , $watermark_image_file_path) = fn_get_image_size($watermark_image_file_path); $watermark_image = $imagine->open($watermark_image_file_path); $watermark_image->usePalette(new \Imagine\Image\Palette\RGB()); // Watermark image > canvas image $watermark_size = $watermark_image->getSize()->increase(WATERMARK_PADDING); if (!$image->getSize()->contains($watermark_size)) { $ratio = min(array($image->getSize()->getWidth() / $watermark_size->getWidth(), $image->getSize()->getHeight() / $watermark_size->getHeight())); $watermark_image->resize($watermark_size->scale($ratio), $filter); } $watermark_position = ImageHelper::positionLayerOnCanvas($image->getSize(), $watermark_image->getSize(), $settings['horizontal_position'], $settings['vertical_position'], new \Imagine\Image\Box(WATERMARK_PADDING, WATERMARK_PADDING)); $image->paste($watermark_image, $watermark_position); } elseif ($settings['type'] == WATERMARK_TYPE_TEXT) { $font_path = Registry::get('config.dir.lib') . 'other/fonts/' . $settings['font'] . '.ttf'; $font_size = $is_detailed ? $settings['font_size_detailed'] : $settings['font_size_icon']; $font_alpha_blend = 100; switch ($settings['font_color']) { case 'white': $font_color = array(255, 255, 255); break; case 'black': $font_color = array(0, 0, 0); break; case 'gray': $font_color = array(120, 120, 120); break; case 'clear_gray': default: $font_color = array(120, 120, 120); $font_alpha_blend = WATERMARK_FONT_ALPHA; break; } $font = $imagine->font($font_path, $font_size, $image->palette()->color($font_color, $font_alpha_blend)); $text_layer_size = ImageHelper::calculateTextSize($settings['text'], $font); $watermark_position = ImageHelper::positionLayerOnCanvas($image->getSize(), $text_layer_size, $settings['horizontal_position'], $settings['vertical_position'], new \Imagine\Image\Box(WATERMARK_PADDING, WATERMARK_PADDING)); $image->draw()->text($settings['text'], $font, $watermark_position); } $settings = Settings::instance()->getValues('Thumbnails'); $options = array('jpeg_quality' => $settings['jpeg_quality'], 'png_compression_level' => 9, 'filter' => $filter); if ($original_file_type = fn_get_image_extension(fn_get_mime_content_type($original_abs_path, false))) { $format = $original_file_type; } else { $format = 'png'; } Storage::instance('images')->put($target_filepath, array('contents' => $image->get($format, $options))); return true; } catch (\Exception $e) { return false; } }
function fn_watermark_create($original_image, $watermarked_image, $is_detailed = false, $company_id = null, $generate_watermark = true) { $w_settings = fn_get_watermark_settings($company_id); if (empty($w_settings)) { return false; } $original_abs_path = Storage::instance('images')->getAbsolutePath($original_image); list($w_settings['horizontal_position'], $w_settings['vertical_position']) = explode('_', $w_settings['position']); list($original_width, $original_height, $original_mime_type) = fn_get_image_size($original_abs_path); if (empty($original_width) || empty($original_height)) { return false; } if (!($image = fn_create_image_from_file($original_abs_path, $original_mime_type))) { return false; } if (!$generate_watermark) { Storage::instance('images')->put($watermarked_image, array('file' => $original_abs_path, 'keep_origins' => true)); return true; } $dest_x = $dest_y = $watermark_width = $watermark_height = 0; if ($w_settings['type'] == 'G') { $watermark_image = false; if ($is_detailed) { if (!empty($w_settings['image_pair']['detailed']['absolute_path'])) { $watermark_image = $w_settings['image_pair']['detailed']['absolute_path']; } } elseif (!empty($w_settings['image_pair']['icon']['absolute_path'])) { $watermark_image = $w_settings['image_pair']['icon']['absolute_path']; } list($watermark_width, $watermark_height, $watermark_mime_type) = fn_get_image_size($watermark_image); if (empty($watermark_image) || !($watermark = fn_create_image_from_file($watermark_image, $watermark_mime_type))) { return false; } } else { $font_path = Registry::get('config.dir.lib') . 'other/fonts/' . $w_settings['font'] . '.ttf'; if (!is_file($font_path) || empty($w_settings['text'])) { return false; } if ($is_detailed) { $font_size = $w_settings['font_size_detailed']; } else { $font_size = $w_settings['font_size_icon']; } if (empty($font_size)) { return false; } $ttfbbox = imagettfbbox($font_size, 0, $font_path, $w_settings['text']); $watermark_height = abs($ttfbbox[7]); $watermark_width = abs($ttfbbox[2]); } if (empty($watermark_width) || empty($watermark_height)) { return false; } // Paddings $delta_x = 3; $delta_y = 3; $new_wt_width = $watermark_width; $new_wt_height = $watermark_height; if ($new_wt_width + $delta_x > $original_width) { $new_wt_height = $new_wt_height * ($original_width - $delta_x) / $new_wt_width; $new_wt_width = $original_width - $delta_x; } if ($new_wt_height > $original_height) { $new_wt_width = $new_wt_width * ($original_height - $delta_y) / $new_wt_height; $new_wt_height = $original_height - $delta_y; } if ($w_settings['vertical_position'] == 'top') { $dest_y = $delta_y; } elseif ($w_settings['vertical_position'] == 'center') { $dest_y = (int) (($original_height - $new_wt_height) / 2); } elseif ($w_settings['vertical_position'] == 'bottom') { $dest_y = $original_height - $new_wt_height - $delta_y; } if ($w_settings['horizontal_position'] == 'left') { $dest_x = $delta_x; } elseif ($w_settings['horizontal_position'] == 'center') { $dest_x = (int) (($original_width - $new_wt_width) / 2); } elseif ($w_settings['horizontal_position'] == 'right') { $dest_x = $original_width - $new_wt_width - $delta_x; } if ($dest_x < 1) { $dest_x = 1; } if ($dest_y < 1) { $dest_y = 1; } if ($w_settings['type'] == 'G') { imagecolortransparent($watermark, imagecolorat($watermark, 0, 0)); if (function_exists('imageantialias')) { imageantialias($image, true); } $result = imagecopyresampled($image, $watermark, $dest_x, $dest_y, 0, 0, $new_wt_width, $new_wt_height, $watermark_width, $watermark_height); imagedestroy($watermark); } else { if ($w_settings['font_color'] == 'white') { $font_color = imagecolorallocate($image, 255, 255, 255); } elseif ($w_settings['font_color'] == 'black') { $font_color = imagecolorallocate($image, 0, 0, 0); } elseif ($w_settings['font_color'] == 'gray') { $font_color = imagecolorallocate($image, 120, 120, 120); } elseif ($w_settings['font_color'] == 'clear_gray') { $font_color = imagecolorallocatealpha($image, 120, 120, 120, WATERMARK_FONT_ALPHA); } $result = imagettftext($image, $font_size, 0, $dest_x, $dest_y + $font_size, $font_color, $font_path, $w_settings['text']); } if ($result === false) { return false; } $ext = fn_get_image_extension($original_mime_type); ob_start(); if ($ext == 'gif') { $result = imagegif($image); } elseif ($ext == 'jpg') { $result = imagejpeg($image, null, 85); } elseif ($ext == 'png') { $result = imagepng($image, null, 8); } $content = ob_get_clean(); imagedestroy($image); Storage::instance('images')->put($watermarked_image, array('contents' => $content)); return $result; }