Creating crop & scale derivative

josno

Joined: 2007-09-17
Posts: 10
Posted: Mon, 2009-03-30 15:49

I'm using Gallery2 embedded, and am trying to write a chooser in my host application. I want this chooser to be able to add derivatives to the chosen item (crop and scale, or just scale depending on certain factors). I've taken the code from the Gallery2 codebase and modified it slightly, but it's not working and I can't see why not. There's no error on saving the derivative, but it just doesn't get inserted into the database. Anyone have any ideas? Here's the code:

Quote:
(Here I do all the calculations necessary to create the derivative operations, and determine if such a derivative already exists. Example operation string: 'crop|8.75,0,83.125,99.75;scale|250,250')

// There's no matching derivative, so we need to add one
list ($ret, $source) = GalleryCoreApi::fetchPreferredSource($item);
if ($ret)
{
throw new GalleryException('Could not find the source of your image', 'Source of item ' . $item->getId() . ' could not be found: ' . $ret->getAsText());
}

list ($ret, $toolkit, $outputMimeType) = GalleryCoreApi::getToolkitByOperation($source->getMimeType(), 'crop');
if ($ret || !isset($toolkit))
{
throw new GalleryException(null, 'Could not find the crop toolkit for mimetype ' . $source->getMimeType());
}

list ($ret, $newOperations, $newOutputMimeType) = GalleryCoreApi::makeSupportedViewableOperationSequence($outputMimeType, $op);
if ($ret)
{
throw new GalleryException(null, $ret->getAsText());
}
if (!$newOperations || !$newOutputMimeType)
{
$newOperations = $op;
$newOutputMimeType = $outputMimeType;
}

list ($ret, $derivative) = GalleryCoreApi::newFactoryInstanceByHint('GalleryDerivative', $source->getEntityType());
if ($ret || !isset($derivative))
{
throw new GalleryException('Could not create a resize of your photo');
}

$ret = $derivative->create($item->getId(), DERIVATIVE_TYPE_IMAGE_RESIZE);
if ($ret)
{
throw new GalleryException('Could not create a resize of your photo');
}

$derivative->setMimeType($newOutputMimeType);
$derivative->setDerivativeOperations($newOperations);
$derivative->setDerivativeSourceId($source->getId());

$ret = $derivative->save();
if ($ret)
{
throw new GalleryException('Could not create a resize of your photo', 'Could not save resize: ' . $ret->getAsText());
}

 
josno

Joined: 2007-09-17
Posts: 10
Posted: Wed, 2009-04-01 13:46

I should have really checked out my previous problem with getting groups sync'd - same thing! Wasn't calling GalleryEmbed::done(); Now I have working automatic crop and scale!