View file vkclone-0.0.1/protected/workers/GalleryWorker.php

File size: 1.48Kb
<?php
Yii::import('ext.ImageEditor.ImageEditor');

/**
 * Gallery worker
 */
class GalleryWorker extends Worker {

	/**
	 * Saves image to static folder and make previews.
	 * Workload contains array:
	 * - temp - tmp file
	 * - original - where to move tmp file
	 * - previews - array (maxWidth=>filename)
	 */
	public function actionSaveImage(WorkerJob $job) {
		$data = unserialize($job->getWorkload());
		if (!file_exists($data['temp'])) {
			echo '[!] Tmp file '.$data['temp'].' missing'.PHP_EOL;
			$job->sendException('Tmp file '.$data['temp'].' missing');
			return false;
		}

		Yii::app()->staticManager->prepareDir(dirname($data['original']));
		if (rename($data['temp'], $data['original']))
			echo 'Moved original '.basename($data['temp']).PHP_EOL;

		krsort($data['previews']);
		$image = ImageEditor::createFromFile($data['original']);
		foreach ($data['previews'] as $maxWidth => $filename) {
			Yii::app()->staticManager->prepareDir(dirname($filename));
			$image->zoomWidthTo($maxWidth);
			$image->saveToFile($filename, 'jpeg');
			echo 'Saved preview '.$maxWidth.PHP_EOL;
		}
		unset($image);
		$job->sendComplete('ok');
	}

	/**
	 * Deletes files.
	 * Workload contains list of files to delete.
	 */
	public function actionDeleteFiles(WorkerJob $job) {
		$data = unserialize($job->getWorkload());

		foreach ($data as $filename) {
			if (file_exists($filename))
				unlink($filename);
		}

		echo 'Deleted files: '.count($data).PHP_EOL;
		$job->sendComplete('Deleted files: '.count($data));
	}
}