Page 1 of 1

Run thumbnail resizing Script using Trigger aftersave?

PostPosted: Fri Oct 09, 2009 12:47 pm
by ge11er
I need to create thumbnail images from images that are uploaded to the file system. I have script that will do the resizing and saving to the file system.

If I manually supply the script with the uploaded 'image name' and import the script into the tables delegate class it will run.

Ideally the script would run only after inserting/uploading an image.

upload image > afterSave trigger > pass 'image file name' to script > run script(resize.inc)

Any help/guidance would be appreciated.

PostPosted: Sat Oct 17, 2009 12:39 pm
by ge11er
Got this working using trigger 'aftersave' but a minor issue now when I delete the image from the file system I get a dialog box saying 'Failed to delete file'.

When I refresh the page the file has been delete. I am not sure how this is related to my trigger as It should only run after 'saving' an image only i think?

Code: Select all
function afterSave(&$record){
$imageField =& $record->_table->getField('image');
require ('resizescript.php');
}


Again any help appreciated.

PostPosted: Mon Oct 19, 2009 4:09 pm
by shannah
When you resize the file are you renaming it? Xataface checks for the existing file when deleting it based on the file name stored in the db. If you are renaming the file, it won't be able to find the one to delete. That is one guess at what could be happening.

PostPosted: Mon Nov 02, 2009 6:53 am
by ge11er
Had another look at this one and it appears that the resize script is running even if no image has been added or updated.

My php is terrible have I written the trigger correctly?

Cheers
Graham

PostPosted: Mon Nov 02, 2009 8:34 am
by Jean
Hi Graham,

Try this :

Code: Select all
function afterSave(&$record){
if ($record->strval('image')){
$imageField =& $record->_table->getField('image');
require ('resizescript.php');
}
}


Jean :)

PostPosted: Mon Nov 02, 2009 8:49 am
by shannah
Further improvement:
Code: Select all
function beforeSave(&$record){
    if ( $record->valueChanged('image') and $record->val('image')){
        $record->pouch['image_changed'] = true;
    }
}
function afterSave(&$record){
  if ($record->pouch['image_changed']){
    $record->pouch['image_changed'] = null;
    $imageField =& $record->_table->getField('image');
    require ('resizescript.php');
  }
}

PostPosted: Mon Nov 02, 2009 9:15 am
by Jean
Hi Graham,

Try this :

Code: Select all
function afterSave(&$record){
if ($record->strval('image')){
$imageField =& $record->_table->getField('image');
require ('resizescript.php');
}
}


Jean :)

PostPosted: Mon Nov 02, 2009 12:47 pm
by ge11er
Thanks guys thats sorted it.

Cheers
Graham