Skip to content

Instantly share code, notes, and snippets.

@sashabeep
Created April 15, 2024 20:00
Show Gist options
  • Save sashabeep/0367df7ed7750084cd7a966c20c340e3 to your computer and use it in GitHub Desktop.
Save sashabeep/0367df7ed7750084cd7a966c20c340e3 to your computer and use it in GitHub Desktop.
multitv crop

all you need to do is get the data and pass it to your image processor class. *file mtvCrop,js is part MultiTV and automatic load in back-end. not use in front-end. prepare multitv and config

if you have already installed and configured multitv and the necessary configuration files, then proceed to creating a snippet create tv 'goods_images', type: multitv description of the structure (can be skipped)

in back-end on page edit image (in TV goods_images). after that image`s info saved in site_tmplvar_contentvalues like this:

{"fieldValue":[ {"fieldTab":"", "image":"assets/images/img1.jpg", "img16x9":"x:12,y:108,width:967,height:543", "img4x3":"x:4,y:108,width:967,height:725", "img1x1":"x:5,y:106,width:967,height:967", "img2x3":"x:65,y:33,width:826,height:1239", "id":"img1" } ]}

in front-end template call [!getImage? &ratio=img16x9!] [!getImage? &ratio=img4x3!]

require_once MODX_BASE_PATH .'/assets/snippets/phpthumb/phpthumb.class.php';
$img_source = null;
$img_ration = null;
//get raw image`s ratio from db or use multitv method
$modxobject = $modx->getDocumentObject('id', 3, true);
$_images = json_decode($modxobject['goods_images'][1], true);
foreach ($_images['fieldValue'] as $item) {
if (isset($item['image'])) {
$img_source = $item['image'];
$img_ratio = $item[$ratio] ?? null;
break; // get only one image for sample
}
}
//convert multitv image`s data for use in phpthub class
$img_ratio = str_replace(array(':', 'x', 'y', 'width', 'height',','), array('=', 'sx', 'sy', 'sw', 'sh','&'), $img_ratio);
//w & h is image`s output size. you can change this or add your own variables when calling the snippet and replace them here.
$img_params = str_replace('img_ratio',$img_ratio,'q=80&w=450&h=253&img_ratio');
parse_str($img_params,$_params);
$path_parts = pathinfo($img_source);
$path = $path_parts['dirname'];
$outputFilename = $path_parts['basename']; // generate public image`s filename here
$defaultCacheFolder = 'assets/cache/';
$path = $defaultCacheFolder . $ratio . '/' . $path;
if (!file_exists($path) && mkdir($path,0755,true) && is_dir($path)) {
chmod($path, 0755);
}
$phpThumb = new phpthumb();
$phpThumb->config_cache_directory = MODX_BASE_PATH . $defaultCacheFolder;
$phpThumb->config_temp_directory = $defaultCacheFolder;
$phpThumb->config_document_root = MODX_BASE_PATH;
$phpThumb->setSourceFilename(MODX_BASE_PATH . $img_source);
foreach ($_params as $key => $value) {
$phpThumb->setParameter($key, $value);
}
if ($phpThumb->GenerateThumbnail()) {
$phpThumb->RenderToFile(MODX_BASE_PATH . $path . $outputFilename);
}
return $path . $outputFilename;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment