mysql - PHP - BLOB image rotation when uploading from phone -


disclaimer: understand storing images in directory vs. database better way go, please humour me sake of learning. thank in advance help.

i'm having issue rotating image when uploading database blob , displaying when user logs in.

i have tested multiple methods of upload , found following:

the image rotated when:
1) choosing file, clicking "take photo" mobile device.

rotated image

the image not rotated when:
1) choosing file, clicking "from gallery" mobile device.
2) screenshot of image taken , uploaded using desktop computer.
3) image imported using application such iphoto , uploaded.

not rotated image

upload.php - upload image database.

<?php  require_once "../config/config.php"; session_start();  if(empty($_session['id'])) {   header("location: ../login/login.html"); } else {   $id = $_session['id']; }  include "fetchavatar.php";  if(!empty($_files['avatar']['name']) && (isset($_post['upload']))) {   $name = $_files['avatar']['name'];   $temp = $_files['avatar']['tmp_name'];   $size = $_files['avatar']['size'];   $type = $_files['avatar']['type'];    $fp = fopen($temp, 'r');   $img = fread($fp, filesize($temp));   fclose($fp);    if($fetchedrows) {     $stmt = $con->prepare("update avatars set name = ?, type = ?, size = ?, data = ? user_id = ?;");   } else {     $stmt = $con->prepare("insert avatars (name, type, size, data, user_id) values(?, ?, ?, ?, ?);");   }      $stmt->bindparam(1, $name);     $stmt->bindparam(2, $type);     $stmt->bindparam(3, $size);     $stmt->bindparam(4, $img);     $stmt->bindparam(5, $id);     $stmt->execute();  }  header("location: profile.php");  ?> 

fetchavatar.php - select image database, store html + image $avatar.

<?php  require_once "../config/config.php"; session_start();  $id = $_session['id'];  $stmt = $con->prepare("select * avatars user_id = ?"); $stmt->bindparam(1, $id); $stmt->execute(); $result = $stmt->fetch(pdo::fetch_assoc);  $fetchedrows = $stmt->rowcount();  $_session['avatar'] = base64_encode($result['data']); $avatar = "<img src='data:image/jpeg;base64,". $_session['avatar']. "' width='100' height='100'>";  ?> 

profile.php - profile page.

<?php  session_start(); include "fetchavatar.php";  ?>  <html> <body>  <div id="profile_div">  <h1>profile page</h1>    <?php echo $avatar ?>    ...  </body> </html> 

here example of latest attempt @ rotating image. when attempting method, blob data uploaded blank , image no longer displayed. have tried multiple solutions offered here have not been successful in either implementing code correctly or knowing if i'm on right track.

upload.php

<?php  ...  include "fetchavatar.php";  if(!empty($_files['avatar']['name']) && (isset($_post['upload']))) {   $name = $_files['avatar']['name'];   $temp = $_files['avatar']['tmp_name'];   $size = $_files['avatar']['size'];   $type = $_files['avatar']['type'];     $image = imagecreatefromstring(file_get_contents($temp));   $exif = exif_read_data($temp);   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;      }   }    $fp = fopen($image, 'r');   $img = fread($fp, filesize($image));   fclose($fp);    if($fetchedrows) {     $stmt = $con->prepare("update avatars set name = ?, type = ?, size = ?, data = ? user_id = ?;");   } else {     $stmt = $con->prepare("insert avatars (name, type, size, data, user_id) values(?, ?, ?, ?, ?);");   }      $stmt->bindparam(1, $name);     $stmt->bindparam(2, $type);     $stmt->bindparam(3, $size);     $stmt->bindparam(4, $img);     $stmt->bindparam(5, $id);     $stmt->execute();  }  header("location: profile.php");  ?> 

in last upload.php example looks attempting fopen() image resource instead of image file on file system. try changing the

$fp = fopen($image, 'r'); $img = fread($fp, filesize($image)); fclose($fp); 

to

ob_start(); imagejpeg($image); $img = addslashes(ob_get_clean()); 

this write image resource string can inserted database.

this modified output file system instead of database, should ever change application.


Comments

Popular posts from this blog

What Are The Best Universities In The World?

Hard vs. Soft Water: What's The Difference?

The Ten Most Livable Cities In The World