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);
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'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);
I'm trying to add an image to a template which is another image too; I'm using PHP codeigniter ; I have got a nice snippet of code that works well for this purpose when both files are .PNG files; my code is as following:
<?php
function attachIcon($imgname)
{
$mark = imagecreatefrompng($imgname);
imagesavealpha($mark, true);
list($icon_width, $icon_height) = getimagesize($imgname);
$img = imagecreatefrompng('images/sprites/navIcons.png');
imagesavealpha($img, true);
$move_left = 10;
$move_up = 9;
list($mainpic_width, $mainpic_height) = getimagesize('images/sprites/navIcons.png');
imagecopy($img, $mark, $mainpic_width-$icon_width-$move_left, $mainpic_height-$icon_height-$move_up, 0, 0, $icon_width, $icon_height);
imagepng($img); // display the image + positioned icon in the browser
//imagepng($img,'newnavIcon.png'); // rewrite the image with icon attached.
}
header('Content-Type: image/png');
attachIcon('icon.png');
?>
However if the template 'images/sprites/navIcons.png' is a png file and the other image is another type (lets say .JPG) my follwoing code is not working:
<?php
function attachIcon($imgname)
{
$mark = imagecreatefrompng($imgname);
imagesavealpha($mark, true);
list($icon_width, $icon_height) = getimagesize($imgname);
$img = imagecreatefrompng('images/sprites/navIcons.png');
imagesavealpha($img, true);
$move_left = 10;
$move_up = 9;
list($mainpic_width, $mainpic_height) = getimagesize('images/sprites/navIcons.png');
imagecopy($img, $mark, $mainpic_width-$icon_width-$move_left, $mainpic_height-$icon_height-$move_up, 0, 0, $icon_width, $icon_height);
imagepng($img); // display the image + positioned icon in the browser
//imagepng($img,'newnavIcon.png'); // rewrite the image with icon attached.
}
header('Content-Type: image/jpg');
attachIcon('icon.jpg');
?>
-Am I missing anything?
- Is it supposed to work with any file extensions?
- can the template be .PNG file and the other image another type (lets say .JPG)!May be I need to change something;
Any help is appreciated!
PS: I got the code from a very old post in this forum ; but I thought it was pretty old and no one checks it, so thats why I ask my question in a new thread! Thanks
Thanks sinni800,
I was supposed to reply to you in comments, but since I want to add my code I added a new message here to reply.
I tried the following code too in which I used "imagecreatefromjpeg"; my code:
<?php
function attachIcon($imgname) {
$mark = imagecreatefromjpeg($imgname);
imagesavealpha($mark, true);
list($icon_width, $icon_height) = getimagesize($imgname);
$img = imagecreatefrompng(base_url() . '/uploaded_images/output/viewer.png');
imagesavealpha($img, true);
$move_left = 280;
$move_up = 450;
list($mainpic_width, $mainpic_height) = getimagesize(base_url() . '/uploaded_images/output/viewer.png');
imagecopy($img, $mark, $mainpic_width - $icon_width - $move_left, $mainpic_height - $icon_height - $move_up, 0, 0, $icon_width, $icon_height);
imagepng($img); // display the image + positioned icon in the browser
//imagepng($img,'newnavIcon.png'); // rewrite the image with icon attached.
}
header('Content-Type: image/jpeg');
attachIcon( base_url().'/uploaded_images/output/koala.jpg');
?>
but still not working!
I think #sinni800 wanted you to do something like this.
<?php
function open_image ($file) {
$size=getimagesize($file);
switch($size["mime"]){
case "image/jpeg":
$im = imagecreatefromjpeg($file); //jpeg file
break;
case "image/gif":
$im = imagecreatefromgif($file); //gif file
break;
case "image/png":
$im = imagecreatefrompng($file); //png file
break;
default:
$im=false;
break;
}
return $im;
}
function attachIcon($imgname)
{
$mark = open_image($imgname);
if($mark !== false){
imagesavealpha($mark, true);
list($icon_width, $icon_height) = getimagesize($imgname);
$img = imagecreatefrompng('images/sprites/navIcons.png');
imagesavealpha($img, true);
$move_left = 10;
$move_up = 9;
list($mainpic_width, $mainpic_height) = getimagesize('images/sprites/navIcons.png');
imagecopy($img, $mark, $mainpic_width-$icon_width-$move_left, $mainpic_height-$icon_height-$move_up, 0, 0, $icon_width, $icon_height);
imagepng($img); // display the image + positioned icon in the browser
}
}
header('Content-Type: image/png');
attachIcon('icon.png');
?>
It seems obvious your problem is that you are trying to use imagecreatefrompng() for a JPEG.
Please look at the comments at http://www.php.net/manual/de/function.imagecreatefromjpeg.php. Especially the one by juozaspo at gmail dot com. It has a function to open any image type, because PHP's internal image function do not support choosing the file type automatically.
function open_image ($file) {
//detect type and process accordinally
global $type;
$size=getimagesize($file);
switch($size["mime"]){
case "image/jpeg":
$im = imagecreatefromjpeg($file); //jpeg file
break;
case "image/gif":
$im = imagecreatefromgif($file); //gif file
break;
case "image/png":
$im = imagecreatefrompng($file); //png file
break;
default:
$im=false;
break;
}
return $im;
}
How do you download, resize and store an image from a remote server using php?
This is the code I am using
$temp_image = file_get_contents($url);
$image = imagecreatefromstring($temp_image);
$thumb = imageToCanvas($image,100,75,true);
imagejpeg($thumb,$base_image_path . $thumb_path,90)
function imageToCanvas($_image, $_canvasWidth, $_canvasHeight, $forceScale=false,$x=false,$y=false)
{
$newImage = imagecreatetruecolor($_canvasWidth, $_canvasHeight);
$imageinfo = getimagesize($_image);
$sourceWidth = $imageinfo[0];
$sourceHeight = $imageinfo[1];
$sourceImage = openImage($_image);
imagecopyresampled($newImage, $sourceImage, 0, 0, 0, 0, $_canvasWidth, $_canvasHeight, $sourceWidth, $sourceHeight);
return $newImage;
}
function openImage($file)
{
// *** Get extension
$extension = strtolower(strrchr($file, '.'));
switch($extension) {
case '.jpg': case '.jpeg':
$img = #imagecreatefromjpeg($file);
break;
case '.gif':
$img = #imagecreatefromgif($file);
break;
case '.png':
$img = #imagecreatefrompng($file);
break;
default:
$img = false;
break;
}
return $img;
}
Doesn't work and I don't know why.
$sourceWidth & $sourceHeight doesn't have a value so I presume $image is in the wrong format
Thanks!
There is no function in php called openImage so that would be a problem if you don't define it yourself.
If you do have it defined, what does it look like and are you receiving any errors?
Edit: Based on your comments the problem would seem to be that you treat the input parameter of your openImage function as a file path. However, when you call it you are feeding it an image resource, the result of imagecreatefromstring.
If you are on a linux server and ghostscript is installed - here is an easier way
shell_exec("convert in.jpg -resize 100x75 out.jpg")