I am trying to fix orientation of images from mobile devices, using EXIF to get the orientation and switch/case to fix the orientation. Apparently my function was working offline and now it's not working on Google Apps Engine. Could Google have a specific function to do this?
The function:
$image = imagecreatefromjpeg($path);
$exif = exif_read_data($path);
if (!empty($exif['Orientation'])) {
switch ($exif['Orientation']) {
case 3:
$image = imagerotate($image, 180, 0);
break;
case 6:
$image = imagerotate($image, -90, 0);
break;
case 8:
$image = imagerotate($image, 90, 0);
break;
}
imagejpeg($image, $path);
}
Related
I'm leaning PHP and I want to rotate an image after upload. The following code works on localhost, but it is not run in the hosting environment.
$exif = exif_read_data($_FILES["file"]["tmp_name"];
if (!empty($exif['Orientation'])) {
$imageResource = imagecreatefromjpeg($_FILES["file"]["tmp_name"];); // provided that the image is jpeg. Use relevant function otherwise
switch ($exif['Orientation']) {
case 3:
$image = imagerotate($imageResource, 180, 0);
break;
case 6:
$image = imagerotate($imageResource, -90, 0);
break;
case 8:
$image = imagerotate($imageResource, 90, 0);
break;
default:
$image = $imageResource;
}
}
imagejpeg($image, $name, 60);
I've come across some code on this site to help fix the orientation issue in regrades with iOS photos.
Here is the PhP
<?php
session_start();
$filename = $_FILES['file']['name'];
$filePath = $_FILES['file']['tmp_name'];
$exif = exif_read_data($_FILES['file']['tmp_name']);
if (!empty($exif['Orientation'])) {
$imageResource = imagecreatefromjpeg('/uploads'); // provided that the image is jpeg. Use relevant function otherwise
switch ($exif['Orientation']) {
case 3:
$image = imagerotate($imageResource, 180, 0);
break;
case 6:
$image = imagerotate($imageResource, -90, 0);
break;
case 8:
$image = imagerotate($imageResource, 90, 0);
break;
default:
$image = $imageResource;
}
}
imagejpeg($image, $filename, 90);
?>
This code seems to work but I havn't seen the outputted image as I can't move the altered image to the server. What I need to do is move the image onto the servers.
I didn't even know that Apple phone did this little bit of extra evil.
Thanks for any help.
I found this, maybe stripping the EXIF from the image?
$filename = $_FILES['file']['name'];
$filePath = $_FILES['file']['tmp_name'];
$exif = exif_read_data($_FILES['file']['tmp_name']);
if (!empty($exif['Orientation'])) {
$imageResource = imagecreatefromjpeg($filePath); // provided that the
image is jpeg. Use relevant function otherwise
switch ($exif['Orientation']) {
case 3:
$image = imagerotate($imageResource, 180, 0);
break;
case 6:
$image = imagerotate($imageResource, -90, 0);
break;
case 8:
$image = imagerotate($imageResource, 90, 0);
break;
default:
$image = $imageResource;
}
}
I'm not 100% sure if its the best practice but if it works!
I want to use watermark image with php. This is my reference page
function imagecreatefromfile($image_path)
{
list($width, $height, $image_type) = getimagesize($image_path);
switch ($image_type)
{
case IMAGETYPE_GIF: return imagecreatefromgif($image_path); break;
case IMAGETYPE_JPEG: return imagecreatefromjpeg($image_path); break;
case IMAGETYPE_PNG: return imagecreatefrompng($image_path); break;
default: return ''; break;
}
}
$image = imagecreatefromfile($_GET['image']);
if (!$image) die('Unable to open image');
$watermark = imagecreatefromfile('uploads/files/water.png');
if (!$image) die('Unable to open watermark');
$watermark_pos_x = imagesx($image) - imagesx($watermark) - 8;
$watermark_pos_y = imagesy($image) - imagesy($watermark) - 10;
imagecopy($image, $watermark, $watermark_pos_x, $watermark_pos_y, 0, 0,
imagesx($watermark), imagesy($watermark));
header('Content-Type: image/jpg');
imagejpeg($image, '', 100);
imagedestroy($image);
imagedestroy($watermark);`
but my page seems empty ,
This is my test page
http://www.alanyaticaretrehberi.com/watermark.php?image=uploads/firmaresim/750/address-cikcilli-3jpg.jpeg
this worked in other servers but not worked in my server.
This is my phpinfo page..
http://www.alanyaticaretrehberi.com/php.php
I guess some setting missed in my php settings but i dont know what it is. May be it is about gd library or something else, can you give some advices for this issue.
I'm using the following php code to save images from various devices. All works fine except iPhone images appearing sideways. I have found a way to fix this by rotating the image before it's saved. However when I upload the image it does not appear on my webpage and in my file manager it still appears sideways. Am I targeting the wrong file to rotate? Or am I using something else incorrectly?
Here's my code:
$file = $_FILES["newsnap"];
$id = $_SESSION['id'];
$username = $_SESSION['username'];
$aboutitems = nl2br(mysqli_real_escape_string($database, $_POST['about-snap']));
$uploadloc = mkdir("../$username/");
$image_temp = $_FILES["newsnap"]['tmp_name'];//Temporary location
$filename = mysqli_real_escape_string($database, htmlentities($file["name"]));
$sourcePath = $image_temp; // source path of the file
$exif = exif_read_data($sourcePath);
$orientation = $exif['Orientation'];
switch($orientation)
{
case 3:
$sourcePath = imagerotate($sourcePath, 180, 0);
break;
case 6:
$sourcePath = imagerotate($sourcePath, -90, 0);
break;
case 8:
$sourcePath = imagerotate($sourcePath, 90, 0);
break;
}
$targetPath = "../$username/$filename"; // Target path where file is to be stored
move_uploaded_file($sourcePath, $targetPath) ; // Moving Uploaded file
$added = date("y.m.d");
mysqli_query($database, "INSERT INTO piqs(userid, chicpiq, aboutpic, added) VALUES('$id', '$targetPath', '$aboutitems', '$added')");
The code works perfectly fine without the below code. I added code below only to rotate a sideways image:
$exif = exif_read_data($sourcePath);
$orientation = $exif['Orientation'];
switch($orientation)
{
case 3:
$sourcePath = imagerotate($sourcePath, 180, 0);
break;
case 6:
$sourcePath = imagerotate($sourcePath, -90, 0);
break;
case 8:
$sourcePath = imagerotate($sourcePath, 90, 0);
break;
}
Thank you for your help.
When I see it correctly $sourcePath is a variable to the file path, which can't be rotated...
See http://php.net/manual/en/function.imagerotate.php, you have to pass the opened resourcere of the picture. So you have to do something like this
$oldImage = ImageCreateFromJPEG($sourcePath);
switch($orientation){
case 3:
$newImage = imagerotate($oldImage, 180, 0);
break;
case 6:
$newImage = imagerotate($oldImage, -90, 0);
break;
case 8:
$newImage = imagerotate($oldImage, 90, 0);
break;
default:
$newImage = $oldImage;
}
imagejpeg($newImage, $targetPath, 90);
I'm having a weird issue with my uploaded images. They're rotated correctly when I view them on iPhones and iPads, but whenever I try to view them on desktop, they're displayed with their wrong orientation.
I can't find the error, and after spending hours messing with the EXIF data I'm close to giving up.
After fixing the orientation, I'm also resizing the images, but that shouldn't interfere with the other code. In case it does, I'm including it.
I don't have enough reputation to upload images, but here's a link to them:
http://i.imgur.com/ARwSUuV.png
http://i.imgur.com/00yj7OJ.png
Here's the code I'm using to upload:
$path_parts = pathinfo($_FILES["file"]["name"]);
$filepath = $_FILES['file']['tmp_name'];
$image = imagecreatefromstring(file_get_contents($filepath));
// Rotate image correctly!
$exif = exif_read_data($image);
if(!empty($exif['Orientation'])) {
switch($exif['Orientation']){
case 1: // nothing
break;
case 2: // horizontal flip
$image = imageflip($image, IMG_FLIP_HORIZONTAL);
break;
case 3: // 180 rotate left
$image = imagerotate($image,180,0);
break;
case 4: // vertical flip
$image = imageflip($image, IMG_FLIP_VERTICAL);
break;
case 5: // vertical flip + 90 rotate right
$image = imageflip($image, IMG_FLIP_VERTICAL);
$image = imagerotate($image,-90,0);
break;
case 6: // 90 rotate right
$image = imagerotate($image,-90,0);
break;
case 7: // horizontal flip + 90 rotate right
$image = imageflip($image, IMG_FLIP_HORIZONTAL);
$image = imagerotate($image,-90,0);
break;
case 8: // 90 rotate left
$image = imagerotate($image,90,0);
break;
}
}
switch ($path_parts['extension']) {
case 'gif' :
$im = imagecreatefromgif($image);
break;
case 'jpg' :
$im = imagecreatefromjpeg($image);
break;
case 'png' :
$im = imagecreatefrompng($image);
break;
case 'bmp' :
$im = imagecreatefrombmp($image);
break;
}
if($im){
imagejpeg($im, $_FILES['file']['tmp_name'], 40);
}
$image_path = 'd_'.time() . "." . $path_parts['extension'];
$move_result = move_uploaded_file($_FILES['file']['tmp_name'], '../img/results/' . $image_path);
If you have any idea why it's only rotating correctly on some platforms, I'd be very grateful!
EDIT: Should probably clarify that images will most often be uploaded from smartphones or tablets.
There are some errors that stop the code working. Try turning on error reporting to help you debug problems like this.
exif_read_data() works on a file, not a GD resource, so pass $filepath instead of $image.
imageflip() manipulates the resource directly and returns a bool so assigning the return value to $image destroys the resource.
The second switch() statement isn't needed at all. The imagecreatefrom___() functions create a resource from a file, but you're passing them an already created resource - all you want to do is output it.
Otherwise the orientation correction seems accurate and should work for you (it does on the various test photos I took with my phone).
Here's the corrected code:
$path_parts = pathinfo($_FILES["file"]["name"]);
$filepath = $_FILES['file']['tmp_name'];
$image = imagecreatefromstring(file_get_contents($filepath));
// Rotate image correctly!
$exif = exif_read_data($filepath);
if (!empty($exif['Orientation'])) {
switch ($exif['Orientation']) {
case 1: // nothing
break;
case 2: // horizontal flip
imageflip($image, IMG_FLIP_HORIZONTAL);
break;
case 3: // 180 rotate left
$image = imagerotate($image, 180, 0);
break;
case 4: // vertical flip
imageflip($image, IMG_FLIP_VERTICAL);
break;
case 5: // vertical flip + 90 rotate right
imageflip($image, IMG_FLIP_VERTICAL);
$image = imagerotate($image, -90, 0);
break;
case 6: // 90 rotate right
$image = imagerotate($image, -90, 0);
break;
case 7: // horizontal flip + 90 rotate right
imageflip($image, IMG_FLIP_HORIZONTAL);
$image = imagerotate($image, -90, 0);
break;
case 8: // 90 rotate left
$image = imagerotate($image, 90, 0);
break;
}
}
imagejpeg($image, $_FILES['file']['tmp_name'], 40);
$image_path = 'd_'.time() . "." . $path_parts['extension'];
$move_result = move_uploaded_file($_FILES['file']['tmp_name'], '../img/results/' . $image_path);