Exemplo n.º 1
0
	/**
	 * Uploads category or group canvas or logo
	 *
	 * @param string                   $type
	 * @param CategoryTable|GroupTable $row
	 * @return bool
	 */
	static public function uploadImage( $type = 'canvas', &$row )
	{
		global $_CB_framework, $_PLUGINS;

		if ( ( ! $type ) || ( ! in_array( $type, array( 'canvas', 'logo' ) ) ) ) {
			return false;
		}

		$method							=	Application::Input()->get( 'post/' . $type . '_method', null, GetterInterface::INT );

		if ( $method === 0 ) {
			return true;
		}

		static $params					=	null;

		if ( ! $params ) {
			$plugin						=	$_PLUGINS->getLoadedPlugin( 'user', 'cbgroupjive' );
			$params						=	$_PLUGINS->getPluginParams( $plugin );
		}

		$basePath						=	$_CB_framework->getCfg( 'absolute_path' ) . '/images/comprofiler/plug_cbgroupjive';

		if ( $row instanceof GroupTable ) {
			$imagePath					=	$basePath . '/' . (int) $row->get( 'category' ) . '/' . (int) $row->get( 'id' );
		} else {
			$imagePath					=	$basePath . '/' . (int) $row->get( 'id' );
		}

		if ( ( ( $method === null ) || ( $method === 1 ) ) && isset( $_FILES[$type]['tmp_name'] ) && ( ! empty( $_FILES[$type]['tmp_name'] ) ) ) {
			if ( $row instanceof GroupTable ) {
				self::createDirectory( $basePath, $row->get( 'category' ), $row->get( 'id' ) );
			} else {
				self::createDirectory( $basePath, $row->get( 'id' ) );
			}

			$resample					=	$params->get( $type . '_resample', 1 );
			$aspectRatio				=	$params->get( $type . '_maintain_aspect_ratio', 1 );
			$imageHeight				=	(int) $params->get( $type . '_image_height', 640 );

			if ( ! $imageHeight ) {
				$imageHeight			=	640;
			}

			$imageWidth					=	(int) $params->get( $type . '_image_width', 1280 );

			if ( ! $imageWidth ) {
				$imageWidth				=	1280;
			}

			$thumbHeight				=	(int) $params->get( $type . '_thumbnail_height', 320 );

			if ( ! $thumbHeight ) {
				$thumbHeight			=	320;
			}

			$thumbWidth					=	(int) $params->get( $type . '_thumbnail_width', 640 );

			if ( ! $thumbWidth ) {
				$thumbWidth				=	640;
			}

			$conversionType				=	(int) Application::Config()->get( 'conversiontype', 0 );
			$imageSoftware				=	( $conversionType == 5 ? 'gmagick' : ( $conversionType == 1 ? 'imagick' : 'gd' ) );
			$imageId					=	uniqid();

			try {
				$image					=	new \CBLib\Image\Image( $imageSoftware, $resample, $aspectRatio );

				$image->setName( $imageId );
				$image->setSource( $_FILES[$type] );
				$image->setDestination( $imagePath . '/' );

				$image->processImage( $imageWidth, $imageHeight );

				$newFileName			=	$image->getCleanFilename();

				$image->setName( 'tn' . $imageId );

				$image->processImage( $thumbWidth, $thumbHeight );

				if ( $row->get( $type ) ) {
					$oldImage			=	$imagePath . '/' . $row->get( $type );

					if ( file_exists( $oldImage ) ) {
						@unlink( $oldImage );
					}

					$oldThumbnail		=	$imagePath . '/tn' . $row->get( $type );

					if ( file_exists( $oldThumbnail ) ) {
						@unlink( $oldThumbnail );
					}
				}

				$row->set( $type, $newFileName );
			} catch ( \Exception $e ) {
				$row->setError( $e->getMessage() );

				return false;
			}
		} elseif ( ( $method === 2 ) && $row->get( $type ) ) {
			$image						=	$imagePath . '/' . $row->get( $type );

			if ( file_exists( $image ) ) {
				@unlink( $image );
			}

			$thumbnail					=	$imagePath . '/tn' . $row->get( $type );

			if ( file_exists( $thumbnail ) ) {
				@unlink( $thumbnail );
			}

			$row->set( $type, '' );
		}

		return true;
	}