<p>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.</p>

<p>Craft has an <a href="https://github.com/craftcms/cms/blob/develop-v2/src/assetsourcetypes/BaseAssetSourceType.php#L166-L170" rel="nofollow noreferrer">example</a> in it's codebase to help you out. Just remember to delete the temp file afterwards.</p>

CC BY-SA 3.0
2017-11-07T10:44:29.220Z
This is an excellent example. Works now, thanks!
Dries
CC BY-SA 4.0
2018-10-24T13:08:18.437Z
Link is broken. Any other example?
r-ninja
CC BY-SA 4.0
2018-10-24T13:12:16.237Z
Just fixed the link. It's Craft 2 specific, though.
Andris Sevcenko
CC BY-SA 4.0
2017-11-06T11:44:12.337Z
2018-10-24T13:11:57.697Z
2018-10-24T13:11:57.697Z
Andris Sevcenko
<p>I am using a front-end form to upload images and store them as assets on an amazon S3 bucket.</p>

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

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

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

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

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

<p>So I investigated:
The function 'insertFileByLocalPath' calls the function 'insertFileByPath' </p>

<p>In its turn this function performs the following check:</p>

<pre><code>if (IOHelper::getFileKind(IOHelper::getExtension($localFilePath)) == 'image'
            &amp;&amp; ImageHelper::isImageManipulatable(IOHelper::getExtension($localFilePath))
)
{
    craft()-&gt;images-&gt;cleanImage($localFilePath);
}
</code></pre>

<p>The cleanImage function should rotate the image and strip the exif data afterwards.</p>

<p>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)</p>

<p>Any idea how I could work around this issue?</p>

<assets><image><upload>
Rotate image according to EXIF orientation not working from a plugin
# Rotate image according to EXIF orientation not working from a plugin

(Asked by: username_0 on 2017-11-04)

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?

**Tags:** assets, image, upload


---

## 1 answers

### Answer by username_1 on 2017-11-06. Score: 1 | Accepted answer

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](https://github.com/craftcms/cms/blob/develop-v2/src/assetsourcetypes/BaseAssetSourceType.php#L166-L170) in it's codebase to help you out. Just remember to delete the temp file afterwards.

> **Comments:**
> **username_0**: This is an excellent example. Works now, thanks!  
> **username_2**: Link is broken. Any other example?  
> **username_1**: Just fixed the link. It's Craft 2 specific, though.  

---

