Пример #1
0
	function generateThumbnail($originalImgPath, $thumbDir, $prefix, $thumbWidth = 0, $thumbHeight = 0, $type = 'resize', $typeParams = array(), $filters = array())
	{
		$thumbSize = AriImageHelper::getThumbnailDimension($originalImgPath, $thumbWidth, $thumbHeight);
		if (!$thumbSize['w'] || !$thumbSize['h'] || !@is_readable($originalImgPath))
			return null;

		$width = $thumbSize['w'];
		$height = $thumbSize['h'];
		$path_parts = pathinfo($originalImgPath);

		$thumbName = AriImageHelper::generateThumbnailFileName($prefix, $originalImgPath, $width, $height, $type);
		$thumbImgPath = $thumbDir . DS . $thumbName;
		if (@file_exists($thumbImgPath) && @filemtime($thumbImgPath) > @filemtime($originalImgPath))
			return $thumbName;

		if (!AriImageHelper::initAsido())
			return ;
		$thumbImg = Asido::image($originalImgPath, $thumbImgPath);
		$needResize = true;		
		
		if ($type == 'crop')
		{
			Asido::crop(
				$thumbImg,
				intval(AriUtils::getParam($typeParams, 'x', 0), 10),
				intval(AriUtils::getParam($typeParams, 'y', 0), 10),
				$width ? $width : $height,
				$height ? $height : $width
			);
			$needResize = false;
		}
		else if ($type == 'cropresize')
		{
			Asido::crop(
				$thumbImg,
				intval(AriUtils::getParam($typeParams, 'x', 0), 10),
				intval(AriUtils::getParam($typeParams, 'y', 0), 10),
				intval(AriUtils::getParam($typeParams, 'width', 0), 10),
				intval(AriUtils::getParam($typeParams, 'height', 0), 10)
			);
		}

		if ($filters)
		{
			if (AriUtils::parseValueBySample(
					AriUtils::getParam($filters, 'grayscale', false),
					false)
				)
			{
				Asido::grayscale($thumbImg);
			}
			
			$rotateFilter = AriUtils::getParam($filters, 'rotate');
			if (is_array($rotateFilter) && 
				AriUtils::parseValueBySample(
					AriUtils::getParam($rotateFilter, 'enable', false),
					false)
				)
			{
				$angle = 0;
				$rotateType = AriUtils::getParam($rotateFilter, 'type', 'fixed');
				if ($rotateType == 'random')
				{
					$startAngle = intval(AriUtils::getParam($rotateFilter, 'startAngle', 0), 10);
					$endAngle = intval(AriUtils::getParam($rotateFilter, 'endAngle', 0), 10);
					$angle = rand($startAngle, $endAngle);
				}
				else
				{
					$angle = intval(AriUtils::getParam($rotateFilter, 'angle', 0), 10);
				}

				$angle = $angle % 360;
				if ($angle != 0)
				{
					Asido::rotate($thumbImg, $angle);
				}
			}
		}

		if ($needResize)
		{
			if (!$width)
				Asido::height($thumbImg, $height);
			else if (!$height)
				Asido::width($thumbImg, $width);
			else
				Asido::resize($thumbImg, $width, $height, ASIDO_RESIZE_STRETCH);
		}

		$thumbImg->save(ASIDO_OVERWRITE_ENABLED);
		
		return $thumbName;
	}
	function getData($params)
	{
		$data = array();
		
		$prefix = $this->_prefix;
		$descrFile = $params['descrFile'];
		$thumbPath = $params['thumbPath'];
		$cacheDir = $this->_cacheDir;
		$cacheUri = $cacheDir;
		if (strpos($cacheUri, JPATH_ROOT . DS) === 0)
			$cacheUri = substr($cacheUri, strlen(JPATH_ROOT . DS));
		$cacheUri = str_replace(DS, '/', $cacheUri);
		
		$folders = $params['folders'];
		$thumbWidth = $params['thumbWidth'];
		$thumbHeight = $params['thumbHeight'];
		$sortBy = $params['sortBy'];
		$sortDir = $params['sortDir'];
		$thumbType = $params['thumbType'];
		$thumbTypeParams = $params['thumbTypeParams'];
		$thumbBehavior = $thumbType == 'resize' ? AriUtils::getParam($thumbTypeParams, 'behavior') : null;
		$emptyTitle = $params['emptyTitle'];
		$needProcessEmptyTitle = ($emptyTitle && strpos($emptyTitle, '{$') !== false);

		foreach ($folders as $folder)
		{
			$descriptions = $this->getDescriptions($descrFile, $folder);
			$files = $this->getImageFiles($folder, $params['fileFilter']);
			$inCSV = false;
			$folderData = array();
			foreach ($files as $file)
			{
				$dataItem = null;
				$fileUri = str_replace(DS, '/', $file);
				$filePath = JPATH_ROOT . DS . $file;
				$baseFileName = basename($file);
				$thumbImagePath = $thumbPath
					? JPATH_ROOT . DS . $folder . DS . str_replace('{$fileName}', $baseFileName, $thumbPath)
					: null;
				if ($thumbImagePath && file_exists($thumbImagePath) && is_readable($thumbImagePath))
				{
					$thumbSize = getimagesize($thumbImagePath);
					$dataItem = array(
						'image' => $fileUri,
						'thumb' => str_replace(DS, '/', $folder . DS . str_replace('{$fileName}', $baseFileName, $thumbPath)),
						'w' => $thumbSize[0],
						'h' => $thumbSize[1]
					);
				}
				else
				{
					$thumbSize = AriImageHelper::getThumbnailDimension($filePath, $thumbWidth, $thumbHeight, $thumbBehavior);
					$dataItem = array(
						'image' => $fileUri,
						'thumb' => $fileUri,
						'w' => $thumbSize['w'],
						'h' => $thumbSize['h']
					);
					
					if ($thumbSize['w'] && $thumbSize['h'])
					{
						$thumbFile = AriImageHelper::generateThumbnailFileName($prefix, $filePath, $thumbSize['w'], $thumbSize['h'], $thumbType);
						if (@file_exists($cacheDir . DS . $thumbFile))
							$dataItem['thumb'] = $cacheUri . '/' . $thumbFile;
					}
				}

				if (isset($descriptions[$baseFileName]))
				{
					$dataItem = array_merge($descriptions[$baseFileName], $dataItem);
					$inCSV = true;
				}
				
				if ($emptyTitle && empty($dataItem['Title']))
				{
					$title = $emptyTitle;
					if ($needProcessEmptyTitle)
					{
						$pathInfo = pathinfo($baseFileName);
						
						$title = AriSimpleTemplate::parse(
							$title, 
							array(
								'fileName' => $baseFileName,
								'baseFileName' => basename($baseFileName, '.' . $pathInfo['extension'])
							)
						);
					}
					
					$dataItem['Title'] = $title;
				}
				
				$key = $this->getDataItemKey($filePath, $baseFileName, $sortBy, $inCSV);
				if (empty($key))
					$folderData[$baseFileName] = $dataItem;
				else
					$folderData[$key] = $dataItem;
			}
			
			if (count($folderData) == 0)
				continue ;
				
			if ($inCSV && $sortBy == 'csv')
			{
				$tempData = array();
				
				foreach ($descriptions as $fileName => $value)
				{
					if (!isset($folderData[$fileName]))
						continue ;
						
					$tempData[] = $folderData[$fileName];
					unset($folderData[$fileName]);
				}
				
				if ($sortDir == 'desc')
					$tempData = array_reverse($tempData);

				$folderData = array_values($folderData);
				$folderData = array_merge($tempData, array_values($folderData));
			}
			else if ($sortBy != 'filename' && $sortBy != 'modified')
			{
				$folderData = array_values($folderData);
			}
				
			$data = array_merge($data, $folderData);
		}
		
		if ($sortBy && $sortBy != 'random' && $sortBy != 'csv')
		{
			if ($sortDir == 'asc')
				ksort($data);
			else
				krsort($data); 
		}

		return $data;
	}