If I edit the pictures with the photo editor (from smartphone) or photoshop (from PC), the pictures are displayed correctly (portrait oriented).
How to fix this problem?
Thanks.
"For us, OpenCart is not 'just' a shopping cart. OpenCart is bigger than that. We bring OpenCart to the next level."
Produk:
- Toko Online - Toko Online Dropshipper - Toko Online Marketpace - Online Directory - Digital Marketplace - Property & Business Listings - News Portal - Corporate Website - Social Network
Info: http://www.bukausahaonline.com
at best, by using the landscape format, when creating images with a smartphone.
OC cannot handle such, by default, and if some Smartphones do technically not
'follow' common Web Standard regulations, it's not OC's fault either.
Ernie
My Github OC Site: https://github.com/IP-CAM
5'500 + FREE OC Extensions, on the World's largest private Github OC Repository Archive Site.
It seems the the exif orientation info is ignored
To solve, the originally uploaded image should have it's exif information read by PHP
Code: Select all
$exif = exif_read_data($source_image);
Code: Select all
if(isset($exif['Orientation'])&& $exif['Orientation'] == '6'){
// rotate the image 90° clockwise
} elseif(isset($exif['Orientation'])&& $exif['Orientation'] == '8'){
// rotate the image 270° clockwise
}
I think it should be done before resizing for sure.
Need to test when I have 20 minutes to spare.
Unless someone else has time
It works! Thank you very much!uksitebuilder wrote:I have seen this happen a couple of times with a client who snaps away on her mobile phone and uploads to her blog in OC via the phone/admin
It seems the the exif orientation info is ignored
To solve, the originally uploaded image should have it's exif information read by PHP
Then:Code: Select all
$exif = exif_read_data($source_image);
Just trying to figure out where in the system/library/image.php file to do this test/fixCode: Select all
if(isset($exif['Orientation'])&& $exif['Orientation'] == '6'){ // rotate the image 90° clockwise } elseif(isset($exif['Orientation'])&& $exif['Orientation'] == '8'){ // rotate the image 270° clockwise }
I think it should be done before resizing for sure.
Need to test when I have 20 minutes to spare.
Unless someone else has time
"For us, OpenCart is not 'just' a shopping cart. OpenCart is bigger than that. We bring OpenCart to the next level."
Produk:
- Toko Online - Toko Online Dropshipper - Toko Online Marketpace - Online Directory - Digital Marketplace - Property & Business Listings - News Portal - Corporate Website - Social Network
Info: http://www.bukausahaonline.com
http://php.net/manual/en/function.exif-read-data.php (the top User Contributed Notes)
It is similar to your code. The basic idea is if the image is in JPG format and the EXIF Orientation is 8 or 3 or 6 then rotate the image first before upload it to server.
Here is the full code:
OpenCart v2.3.0.2
Modified file: admin/controller/common/filemanager.php (I use ocMod to handle file modification)
Find this script (Line-288):
Code: Select all
move_uploaded_file($file['tmp_name'], $directory . '/' . $filename);
Code: Select all
$image = imagecreatefromstring(file_get_contents($file['tmp_name']));
$ext = utf8_strtolower(utf8_substr(strrchr($filename, '.'), 1));
if ($ext == 'jpg') {
$exif = exif_read_data($file['tmp_name']);
if(!empty($exif['Orientation'])) {
switch($exif['Orientation']) {
case 8:
$image = imagerotate($image,90,0);
break;
case 3:
$image = imagerotate($image,180,0);
break;
case 6:
$image = imagerotate($image,-90,0);
break;
}
}
imagejpeg($image, $directory . '/' . $filename);
// Free up memory
imagedestroy($image);
} else {
move_uploaded_file($file['tmp_name'], $directory . '/' . $filename);
}
If you find any bugs, please let me know.
Thanks.
"For us, OpenCart is not 'just' a shopping cart. OpenCart is bigger than that. We bring OpenCart to the next level."
Produk:
- Toko Online - Toko Online Dropshipper - Toko Online Marketpace - Online Directory - Digital Marketplace - Property & Business Listings - News Portal - Corporate Website - Social Network
Info: http://www.bukausahaonline.com
Code: Select all
$ext = utf8_strtolower(utf8_substr(strrchr($filename, '.'), 1));
if ($ext == 'jpg' || $ext == 'jpeg') {
$image = imagecreatefromstring(file_get_contents($file['tmp_name']));
$exif = exif_read_data($file['tmp_name']);
if(!empty($exif['Orientation'])) {
switch($exif['Orientation']) {
case 8:
$image = imagerotate($image,90,0);
break;
case 3:
$image = imagerotate($image,180,0);
break;
case 6:
$image = imagerotate($image,-90,0);
break;
}
}
imagejpeg($image, $directory . '/' . $filename);
// Free up memory
imagedestroy($image);
} else {
move_uploaded_file($file['tmp_name'], $directory . '/' . $filename);
}
Ernie
---
Unfortunately, the OC v.1.5.x Versions have this in a different way, and this is, where my wisdom finds it's limits...
Code: Select all
if (!isset($json['error'])) {
if (@move_uploaded_file($this->request->files['image']['tmp_name'], $directory . '/' . $filename)) {
$json['success'] = $this->language->get('text_uploaded');
} else {
$json['error'] = $this->language->get('error_uploaded');
}
}
My Github OC Site: https://github.com/IP-CAM
5'500 + FREE OC Extensions, on the World's largest private Github OC Repository Archive Site.
Please try the below code, replacing the code you posted
Code: Select all
if (!isset($json['error'])) {
if (@move_uploaded_file($this->request->files['image']['tmp_name'], $directory . '/' . $filename)) {
$ext = utf8_strtolower(utf8_substr(strrchr($filename, '.'), 1));
if ($ext == 'jpg' || $ext == 'jpeg') {
$image = imagecreatefromjpeg($directory . '/' . $filename);
$exif = exif_read_data($directory . '/' . $filename);
if(!empty($exif['Orientation'])) {
switch($exif['Orientation']) {
case 8:
$image = imagerotate($image,90,0);
break;
case 3:
$image = imagerotate($image,180,0);
break;
case 6:
$image = imagerotate($image,-90,0);
break;
}
}
imagejpeg($image, $directory . '/' . $filename, 90);
// Free up memory
imagedestroy($image);
}
$json['success'] = $this->language->get('text_uploaded');
} else {
$json['error'] = $this->language->get('error_uploaded');
}
}
Ernie
My Github OC Site: https://github.com/IP-CAM
5'500 + FREE OC Extensions, on the World's largest private Github OC Repository Archive Site.
I am running opencart v2.1.0.2 and unfortunately none of these code mods work for me. I noticed that the line in filemanager.php which needs replacing is slightly different than the one posted. Would that be the problem?
The script in my code is:
Code: Select all
move_uploaded_file($this->request->files['file']['tmp_name'], $directory . '/' . $filename);
Code: Select all
move_uploaded_file($file['tmp_name'], $directory . '/' . $filename);
uksitebuilder wrote: ↑Sat Dec 17, 2016 7:36 pmOnly made a small change, just moved the imagecreatefromstring into the if/else condition as it is not needed if the image is not a jpeg.
Code: Select all
$ext = utf8_strtolower(utf8_substr(strrchr($filename, '.'), 1)); if ($ext == 'jpg' || $ext == 'jpeg') { $image = imagecreatefromstring(file_get_contents($file['tmp_name'])); $exif = exif_read_data($file['tmp_name']); if(!empty($exif['Orientation'])) { switch($exif['Orientation']) { case 8: $image = imagerotate($image,90,0); break; case 3: $image = imagerotate($image,180,0); break; case 6: $image = imagerotate($image,-90,0); break; } } imagejpeg($image, $directory . '/' . $filename); // Free up memory imagedestroy($image); } else { move_uploaded_file($file['tmp_name'], $directory . '/' . $filename); }
yes it worked thanks. Also I am using Bulk image upload module, can we apply it?
(I am using OC version 3.0.3.6.)public/system/library/image.php
First, add this function in the public/system/library/image.php file:
Code: Select all
private function rotatePortrait() {
$exif = exif_read_data($this->file);
if (is_array($exif) && array_key_exists('Orientation', $exif) && !empty($exif['Orientation'])) {
switch ($exif['Orientation']) {
case 8:
$this->image = imagerotate($this->image, 90, 0);
$tmpWidth = $this->width;
$this->width = $this->height;
$this->height = $tmpWidth;
break;
case 3:
$this->image = imagerotate($this->image, 180, 0);
break;
case 6:
$this->image = imagerotate($this->image, -90, 0);
$tmpWidth = $this->width;
$this->width = $this->height;
$this->height = $tmpWidth;
break;
}
}
}
Code: Select all
...
if ($this->mime == 'image/gif') {
$this->image = imagecreatefromgif($file);
} elseif ($this->mime == 'image/png') {
$this->image = imagecreatefrompng($file);
} elseif ($this->mime == 'image/jpeg') {
$this->image = imagecreatefromjpeg($file);
$this->rotatePortrait(); // <-- ADD THIS LINE
}
...
Users browsing this forum: No registered users and 3 guests