gozoro/yii2-preview

Yii2 component for resizing, cropping and creating preview image and saving to cache folder.

Installs: 70

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 1

Forks: 0

Open Issues: 0

Type:yii2-extension

v1.0.1 2021-04-01 23:13 UTC

This package is auto-updated.

Last update: 2025-03-29 01:01:05 UTC


README

Yii2 component for resizing, cropping and creating preview image and saving to cache folder. Used only GD library.

Installation

	composer require gozoro/yii2-preview

Configuration

'components' => [

	...

	'preview' => [
		'class' => 'gozoro\preview\PreviewComponent',
		'previewPath' => '/var/www/site/www/preview_cache',
		'previewWebPath' => '/preview_cache',
		'defaultPreview' => 'default.jpg',
	],

	...

],

Usage

$filename = "/var/www/site/images/image.jpg";

//Get preview url
$url = Yii::$app->preview->create($filename)->resize(300,300)->crop(200,200)->cache()->url;
print '<img src="'.$url.'">';

//Get preview path
$imagePath = Yii::$app->preview->create($filename)->resize(300,300)->crop(200,200)->cache()->filename;

//Save As
Yii::$app->preview->create($filename)->resize(300,300)->crop(200,200)->saveAs('/var/www/site/images/image2.jpg');

Other methods see the link gozoro/image

Configuration for PDF

'components' => [

	...

	'preview' => [
		'class' => 'gozoro\preview\PreviewComponent',
		'previewPath' => '/var/www/site/www/preview_cache',
		'previewWebPath' => '/preview_cache',
		'on beforeOpen' => function($event)
		{
			if($event->extension == 'pdf')
			{
				$pdf_file = $event->filename;
				$hash = 'pdf_'.md5($pdf_file);

				$pdf_image = '/var/www/site/www/preview_cache/'.$hash.'.jpg';

				if(!file_exists($pdf_image))
				{
					system( "/usr/bin/nice -2 /usr/bin/gs -dNOPAUSE -q -dBATCH -dSAFER -sDEVICE=jpeg "
						. " -dJPEGQ=100 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r150 -dFirstPage=1 -dLastPage=1 "
						. " -sOutputFile=".$pdf_image." ".$pdf_file   );

				}
				$event->filename = $pdf_image;
			}
		},
	],

	...

],