2

I am using a front-end form to upload images and store them as assets on an amazon S3 bucket.

My server has imagick installed an uses it and in my config I have "rotateImagesOnUploadByExifData" set to true.

When uploading an image I use this code to get the image and store it as an asset:

$file = UploadedFile::getInstanceByName('file');
$tempFilePath = $file->getTempName();
$folder = craft()->assets->findFolder(array(
    'sourceId' => craft()->config->get('assetSourceId', 'myPlugin'),
    'id'       => craft()->config->get('assetFolderId', 'myPlugin')
));            

// Turn the file into a managed asset within that folder
$response = craft()->assets->insertFileByLocalPath(
    $tempFilePath,
    $file->getName(),
    $folder->id,
    AssetConflictResolution::KeepBoth
);

Everything is working as expected, an asset is created, files get transferred to Amazon S3 bucket, but when transforming the image it shows rotated.

So I investigated: The function 'insertFileByLocalPath' calls the function 'insertFileByPath'

In its turn this function performs the following check:

if (IOHelper::getFileKind(IOHelper::getExtension($localFilePath)) == 'image'
            && ImageHelper::isImageManipulatable(IOHelper::getExtension($localFilePath))
)
{
    craft()->images->cleanImage($localFilePath);
}

The cleanImage function should rotate the image and strip the exif data afterwards.

But since these functions work with the temporary file name (which is e.g. "/tmp/phpZB1A8y") this does not pass the test in the previous code. (it is not recognized as an image filename since it has no extension)

Any idea how I could work around this issue?

1

The best way to do this would be to follow the same flow that Craft does - clean the filename, move to a temporary location, operate from there and afterwards delete the temporary file.

Craft has an example in it's codebase to help you out. Just remember to delete the temp file afterwards.

| improve this answer | |
  • This is an excellent example. Works now, thanks! – Dries Nov 7 '17 at 10:44
  • Link is broken. Any other example? – r-ninja Oct 24 '18 at 13:08
  • Just fixed the link. It's Craft 2 specific, though. – Andris Sevcenko Oct 24 '18 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.