View source
<?php
namespace Drupal\media_watermark\Watermark;
use Drupal\Component\Utility\Html;
use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;
use Drupal\media_watermark\Entity\MediaWatermark;
class Watermark {
public static function addWatermark($main_img_obj, $watermark_img_obj, $watermark, $ext) {
$main_img_obj_w = imagesx($main_img_obj);
$main_img_obj_h = imagesy($main_img_obj);
$watermark_img_obj_w = imagesx($watermark_img_obj);
$watermark_img_obj_h = imagesy($watermark_img_obj);
$vm = $watermark
->getVerticalMargin();
$hm = $watermark
->getHorizontalMargin();
switch ($watermark
->getHorizontalPosition()) {
case 'left':
$margin_x = $hm;
break;
case 'middle':
$margin_x = floor($main_img_obj_w / 2 - $watermark_img_obj_w / 2) + $hm;
break;
case 'right':
$margin_x = $main_img_obj_w - $watermark_img_obj_w + $hm;
break;
}
switch ($watermark
->getVerticalPosition()) {
case 'top':
$margin_y = $vm;
break;
case 'center':
$margin_y = floor($main_img_obj_h / 2 - $watermark_img_obj_h / 2) + $hm;
break;
case 'bottom':
$margin_y = $main_img_obj_h - $watermark_img_obj_h + $vm;
break;
}
$sx = imagesx($watermark_img_obj);
$sy = imagesy($watermark_img_obj);
imagecopy($main_img_obj, $watermark_img_obj, $margin_x, $margin_y, 0, 0, $sx, $sy);
imagejpeg($main_img_obj);
return $main_img_obj;
}
public static function createImage($file, $watermark, $type = 'add') {
$file_path = \Drupal::service('file_system')
->realpath($file
->getFileUri());
$fid = $watermark
->getFid();
if (!empty($fid) && is_array($fid)) {
$fid = reset($fid);
}
else {
\Drupal::messenger()
->addStatus(t("Watermark doesn't have file to be applied."));
return;
}
$watermark_file = File::load($fid);
$watermark_filepath = \Drupal::service('file_system')
->realpath($watermark_file
->getFileUri());
$watermark_extension = pathinfo($watermark_filepath, PATHINFO_EXTENSION);
$ext = pathinfo($file_path, PATHINFO_EXTENSION);
$ext = self::getFuncName($ext);
if (!empty($ext)) {
$func_name = 'imagecreatefrom' . self::getFuncName($ext);
$img = $func_name($file_path);
if (!empty($img)) {
$get_watermark = 'imagecreatefrom' . $watermark_extension;
$watermark_img = $get_watermark($watermark_filepath);
ob_start();
$im = self::addWatermark($img, $watermark_img, $watermark, $ext);
$func_name = self::getFuncName($ext);
$func_name = 'image' . $func_name;
$func_name($im, $file_path);
imagedestroy($im);
ob_end_clean();
self::flushImageStylesCache($file
->getFileUri());
}
}
else {
\Drupal::messenger()
->addWarning(t('Unknown or unsupported image extension.'));
}
}
public static function getFuncName($ext) {
$func_name = '';
if (!empty($ext) && is_string($ext)) {
$ext = strtolower($ext);
if ($ext == 'jpg' || $ext == 'jpeg') {
$func_name = 'jpeg';
}
elseif ($ext == 'png') {
$func_name = 'png';
}
elseif ($ext == 'gif') {
$func_name = 'gif';
}
}
return $func_name;
}
public static function flushImageStylesCache(string $file_uri) {
$styles = ImageStyle::loadMultiple();
if (!empty($styles) && is_array($styles)) {
foreach ($styles as $style) {
if (method_exists($style, 'flush')) {
$style
->flush($file_uri);
}
else {
$message = t('Method flush() is not available into ImageStyle class');
\Drupal::logger('media_watermark')
->error($message);
}
}
}
}
public static function batchCreateImage(File $file, MediaWatermark $watermark, array &$context) {
self::createImage($file, $watermark, 'edit');
$context['results'][] = $file
->id() . ' : ' . Html::escape($file
->getFilename());
$context['message'] = t('Loading node "@title"', [
'@title' => $file
->getFilename(),
]);
$_SESSION['http_request_count']++;
}
public function batchFinished(bool $success, int $results, array $operations) {
$messenger = \Drupal::messenger();
if ($success) {
$count = count($results);
$messenger
->addStatus(t('Added watermarks to @count images.', [
'@count' => $count,
]));
$messenger
->addMessage(t('Also has been flushed image styles generated for updated images. If images still seems to be same as before, please flush your browser cache.'));
}
else {
$error_operation = reset($operations);
$messenger
->addError(t('An error occurred while processing @operation with arguments : @args', [
'@operation' => $error_operation[0],
'@args' => print_r($error_operation[0], TRUE),
]));
}
}
}