Skip to content

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

License

Notifications You must be signed in to change notification settings

gozoro/yii2-preview

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

yii2-preview

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;
			}
		},
	],

	...

],

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages