Skip to content

Instantly share code, notes, and snippets.

@faitno
Last active August 21, 2019 18:05
Show Gist options
  • Save faitno/c45dbb700bfd3ded0e5b to your computer and use it in GitHub Desktop.
Save faitno/c45dbb700bfd3ded0e5b to your computer and use it in GitHub Desktop.
ms2gallery 2.5 crop on upload on server-side, watermark
//in public function generateThumbnails(modMediaSource $mediaSource = null)
//before if ($image = $this->makeThumbnail($options, $info)) {
//start center watermark insert
if(isset($options['fltr']) && !is_array($options['fltr'])) $options['fltr'] = [$options['fltr']];
$fltrs = [];
foreach ($options['fltr'] as $fltr){
$width = $this->get('properties')['width'];
$height = $this->get('properties')['height'];
if ($width > $height) {
$h = floor(($height/$width)*$options['w']);
$w = $options['w'];
} else {
$w = floor(($width/$height)*$options['h']);
$h = $options['h'];
}
$wc=floor($w/2);
$hc=floor($h/2);
if(strpos($fltr, 'wmi|') === 0){
//var_dump($options['fltr']);
$fltr = str_replace('|WCxHC|','|'.$wc.'x'.$hc.'|',$fltr);
$fltr = str_replace('|w|h','|'.$w.'|'.$h,$fltr);
}
elseif(strpos($fltr, 'wmt|') === 0){
$fltr = preg_replace_callback('/(.*)<w(\d+\.?\d*)>(.*)/miu',
function ($m) use ($w,$h) {
return $m[1].($m[2]*$w).$m[3];
},
$fltr
);
}
$fltrs[] = $fltr;
}
$options['fltr'] = $fltrs;
// var_dump($options['fltr']);
// die;
//end center watermark insert
//core/components/ms2gallery/processors/mgr/gallery/upload.class.php
//https://github.com/bezumkin/ms2Gallery/blob/b5f41d6311cec4a0662987ee4a8258ecc26ba330/core/components/ms2gallery/processors/mgr/gallery/upload.class.php#L118
//insert and edit $options array, where 'h' => 600, - height, 'w' => 120, - width
require_once MODX_CORE_PATH . 'model/phpthumb/modphpthumb.class.php';
$options = [
//'w' => 120,
'h' => 600,
'q' => 90,
'f' => 'jpg'
];
$phpThumb = new modPhpThumb($this->modx);
$phpThumb->initialize();
$tf = tempnam(MODX_BASE_PATH, 'ms2g_');
file_put_contents($tf, $data['stream']);
$phpThumb->setSourceFilename($tf);
foreach ($options as $k => $v) {
$phpThumb->setParameter($k, $v);
}
if ($phpThumb->GenerateThumbnail()) {
ImageInterlace($phpThumb->gdimg_output, true);
if ($phpThumb->RenderOutput()) {
@unlink($phpThumb->sourceFilename);
@unlink($tf);
$data['stream'] = $phpThumb->outputImageData;
}
}
//insert before clearstatcache(true, $tf);
//insert logic
clearstatcache(true, $tf);
if (file_exists($tf) && !empty($name) && $size = filesize($tf) && function_exists('getimagesize') && $info = @getimagesize($tf)) {
if (!class_exists('modPhpThumb')) {
/** @noinspection PhpIncludeInspection */
require MODX_CORE_PATH . 'model/phpthumb/modphpthumb.class.php';
}
$options = [
"fltr" => ["wmi|watermark.png|WCxHC|30|w|h","wmt|www.site.ru|<w0.04>|20x40|EE721E|font.ttf|45"]
//core/model/phpthumb/images
//core/model/phpthumb/fonts
];
if(isset($options['fltr']) && !is_array($options['fltr'])) $options['fltr'] = [$options['fltr']];
$fltrs = [];
$options['w'] = isset($options['w']) ? $options['w'] : $info[0];
$options['h'] = isset($options['h']) ? $options['h'] : $info[1];
foreach ($options['fltr'] as $fltr){
$width = $info[0];
$height = $info[1];
if ($width > $height) {
$h = floor(($height/$width)*$options['w']);
$w = $options['w'];
} else {
$w = floor(($width/$height)*$options['h']);
$h = $options['h'];
}
$wc=floor($w/2);
$hc=floor($h/2);
if(strpos($fltr, 'wmi|') === 0){
//var_dump($options['fltr']);
$fltr = str_replace('|WCxHC|','|'.$wc.'x'.$hc.'|',$fltr);
$fltr = str_replace('|w|h','|'.$w.'|'.$h,$fltr);
}
elseif(strpos($fltr, 'wmt|') === 0){
$fltr = preg_replace_callback('/(.*)<w(\d+\.?\d*)>(.*)/miu',
function ($m) use ($w,$h) {
return $m[1].($m[2]*$w).$m[3];
},
$fltr
);
}
$fltrs[] = $fltr;
}
$options['fltr'] = $fltrs;
//$this->modx->log(1,print_r($fltr,true));
$phpThumb = new modPhpThumb($this->modx);
$phpThumb->initialize();
$phpThumb->setSourceFilename($tf);
//$phpThumb->setSourceData(file_get_contents(MYBB_ROOT.'inc/plugins/topposts/temp/'.$thumb_file_name));
foreach ($options as $k => $v) {
$phpThumb->setParameter($k, $v);
}
if ($phpThumb->GenerateThumbnail()) {
ImageInterlace($phpThumb->gdimg_output, true);
if ($phpThumb->RenderOutput()) {
file_put_contents($tf, $phpThumb->outputImageData);
}
}
}
//end insert logic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment