PHP code example of gozoro / yii2-preview

1. Go to this page and download the library: Download gozoro/yii2-preview library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gozoro / yii2-preview example snippets



'components' => [

	...

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

	...

],


$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');



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

	...

],