Skip to content

Instantly share code, notes, and snippets.

@pavel-one
Created November 24, 2018 04:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pavel-one/3c55474ff70a2ecde4e04ddbe01de20c to your computer and use it in GitHub Desktop.
Save pavel-one/3c55474ff70a2ecde4e04ddbe01de20c to your computer and use it in GitHub Desktop.
Переименовывает изображение, сжимает картинку и обрезает все img
<?php
switch ($modx->event->name) {
case 'OnLoadWebDocument':
$content = $modx->resource->content;
$content = $content;
$dom = new DOMDocument;
$dom->loadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$imgs = $dom->getElementsByTagName('img');
foreach ($imgs as $img) {
$img->removeAttribute('width');
$img->removeAttribute('height');
$img->setAttribute('class', 'img-thumbnail');
}
$html = $dom->saveHTML();
$modx->resource->set('content', $html);
break;
case 'OnFileManagerBeforeUpload':
$fullPath = $source->getBases()['pathAbsolute'].$directory;
$dir = scandir($fullPath);
$name = $file['name'];
$info = pathinfo($name);
if (!in_array($name, $dir)) {
return ;
}
$file['name'] = $info['filename'].'_'.rand(1,999999).'.'.$info['extension'];
$modx->event->params['file'] = $file;
break;
case 'OnFileManagerUpload':
$fullPath = $source->getBases()['pathAbsolute'].$directory;
foreach ($files as $file) {
if(strripos($file['type'], 'image') === false) {
return ;
}
$name = $file['name'];
$params = array(
'w' => 1200 ,
);
$phpThumb = $modx->getService('modphpthumb','modPhpThumb', MODX_CORE_PATH . 'model/phpthumb/', array());
$phpThumb->setSourceFilename($fullPath.$name);
foreach ($params as $k => $v) {
$phpThumb->setParameter($k, $v);
}
if ($phpThumb->GenerateThumbnail()) {
if (!$phpThumb->renderToFile($fullPath.$name)) {
$modx->log(modX::LOG_LEVEL_ERROR, 'Could not save rendered image to'.$fullPath.$name);
}
}
else {
$modx->log(modX::LOG_LEVEL_ERROR, print_r($phpThumb->debugmessages, 1));
}
}
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment