169 lines
5.5 KiB
PHP
169 lines
5.5 KiB
PHP
<?php defined('SYSPATH') or die('No direct script access.');
|
|
/*************************************************************************
|
|
* Copyright (C) 2012 Michel A. Mayer *
|
|
* *
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
* it under the terms of the GNU General Public License as published by *
|
|
* the Free Software Foundation, either version 3 of the License, or *
|
|
* (at your option) any later version. *
|
|
* *
|
|
* This program is distributed in the hope that it will be useful, *
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
* GNU General Public License for more details. *
|
|
* *
|
|
* You should have received a copy of the GNU General Public License *
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
|
*************************************************************************/
|
|
class emboss_task_Core {
|
|
|
|
static function available_tasks() {
|
|
$q = emboss::find_dirty();
|
|
$n = $q->count();
|
|
|
|
$description = ( ($n==0)
|
|
? (t('All photo overlays are up to date') )
|
|
: t2('one Photo needs its emboss overlay updated',
|
|
"$n Photos need their emboss overlay updated", $n) );
|
|
|
|
$tasks[] = Task_Definition::factory()
|
|
->callback('emboss_task::update_overlays')
|
|
->name(t('Update photo embossing'))
|
|
->description($description)
|
|
->severity($n>0 ? log::WARNING : log::SUCCESS);
|
|
return $tasks;
|
|
}
|
|
|
|
static function update_overlays($task)
|
|
{
|
|
$errors = array();
|
|
try {
|
|
$mode = $task->get('mode','init');
|
|
switch($mode) {
|
|
case 'init':
|
|
$q = emboss::find_dirty();
|
|
foreach ($q as $item) {
|
|
$ids[] = array('id'=>$item->id,
|
|
'image_id'=>$item->image_id,
|
|
'overlay_id'=>$item->best_overlay_id,
|
|
'rotation'=>$item->cur_rotation);
|
|
}
|
|
$count = count($ids);
|
|
|
|
if($count>0) {
|
|
$task->set('ids',$ids);
|
|
$task->set('count',$count);
|
|
$task->set('current',0);
|
|
$task->set('mode','continue');
|
|
} else {
|
|
$task->done = true;
|
|
$task->state = 'success';
|
|
$task->percent_complete = 100;
|
|
site_status::clear('emboss_dirty');
|
|
return;
|
|
}
|
|
break;
|
|
|
|
case 'continue':
|
|
$ids = $task->get('ids');
|
|
$count = $task->get('count');
|
|
$current = $task->get('current');
|
|
break;
|
|
}
|
|
|
|
$i = 1*$current;
|
|
$id = $ids[$i];
|
|
$current++;
|
|
$task->set('current',$current);
|
|
|
|
emboss_task::do_embossing($id['id'],$id['image_id'],$id['overlay_id'],$id['rotation']);
|
|
|
|
if($current>=$count) {
|
|
$task->done = true;
|
|
$task->state = 'success';
|
|
$task->percent_complete = 100;
|
|
$task->status = 'Complete';
|
|
site_status::clear('emboss_dirty');
|
|
} else {
|
|
$task->percent_complete = $current/$count * 100;
|
|
$task->status = t("Reembossed $current of $count photos");
|
|
}
|
|
|
|
|
|
} catch (Exception $e) {
|
|
Kohana_Log::add('error',(string)$e);
|
|
$task->done = true;
|
|
$task->state = 'error';
|
|
$task->status = $e->getMessage();
|
|
$errors[] = (string)$e;
|
|
}
|
|
if ($errors) {
|
|
$task->log($errors);
|
|
}
|
|
}
|
|
|
|
static function do_embossing($id,$image_id,$overlay_id,$rotation)
|
|
{
|
|
$gravity = module::get_var('emboss','gravity');
|
|
$transparency = module::get_var('emboss','transparency');
|
|
|
|
$item = ORM::factory('item')->where('id','=',$image_id)->find();
|
|
$path = $item->file_path() . $name;
|
|
$orig = str_replace(VARPATH.'albums/',VARPATH.'originals/',$path);
|
|
|
|
@unlink($path);
|
|
|
|
if($overlay_id<0) {
|
|
log::info('emboss','Remove embossing from '.$item->name);
|
|
@copy($orig,$path);
|
|
|
|
} else {
|
|
$overlay = ORM::factory('emboss_overlay')->where('id','=',$overlay_id)->find();
|
|
$overlay_path = VARPATH.'modules/emboss/'.$overlay->name;
|
|
|
|
$opts['file'] = $overlay_path;
|
|
$opts['position'] = $gravity;
|
|
$opts['transparency'] = 100-$transparency;
|
|
|
|
if($rotation==0)
|
|
{
|
|
log::info('emboss','Embossing '.$item->name.' with '.$overlay->name);
|
|
gallery_graphics::composite($orig,$path,$opts);
|
|
}
|
|
else
|
|
{
|
|
log::info('emboss','Embossing Rotation('.$rotation.') '.$item->name.' with '.$overlay->name);
|
|
$orig_path = explode('/',$orig);
|
|
$orig_name = array_pop($orig_path);
|
|
$orig_path = implode('/',$orig_path);
|
|
$rotpath = $orig_path.'/rotated_'.$orig_name;
|
|
|
|
Image::factory($orig)
|
|
->quality(module::get_var('gallery', 'image_quality'))
|
|
->rotate($rotation)
|
|
->save($rotpath);
|
|
|
|
gallery_graphics::composite($rotpath,$path,$opts);
|
|
}
|
|
}
|
|
|
|
$item->thumb_dirty = 1;
|
|
$item->resize_dirty = 1;
|
|
$item->save();
|
|
|
|
graphics::generate($item);
|
|
|
|
db::build()->update('emboss_mappings')
|
|
->where('id','=',$id)
|
|
->set('cur_overlay_id',$overlay_id)
|
|
->set('cur_gravity',$gravity)
|
|
->set('cur_transparency',$transparency)
|
|
->execute();
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|