Reputation: 1
function generateRandomString($length = 10)
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString . '.jpg';
}
if(isset($_FILES["studentProfile"]))
{
$uploadDir = '../../vendor/img/students/';
$fileName = $_FILES['studentProfile']['name'];
$fileTmpName = $_FILES['studentProfile']['tmp_name'];
$fileType = $_FILES['studentProfile']['type'];
if ($_FILES['studentProfile']['error'] === UPLOAD_ERR_NO_FILE)
{
if($stuGender === "male")
{
$StuImgId = "male.jpg";
}
elseif($stuGender === "female")
{
$StuImgId = "female.jpg";
}
else
{
$StuImgId = "default.jpg";
}
}
else
{
$allowedTypes = ['image/jpeg', 'image/png'];
if (!in_array($fileType, $allowedTypes))
{
$message = "Error: Only JPG or PNG files are allowed.";
exit;
}
if ($fileType == 'image/png')
{
$source = imagecreatefrompng($fileTmpName);
$fileName = $uploadDir . generateRandomString();
imagejpeg($source, $fileName);
imagedestroy($source);
}
else
{
$fileName = $uploadDir . generateRandomString();
move_uploaded_file($fileTmpName, $fileName);
}
$newWidth = 413;
$newHeight = 531;
list($width, $height) = getimagesize($fileName);
$imageResized = imagecreatetruecolor($newWidth, $newHeight);
$source = imagecreatefromjpeg($fileName);
imagecopyresized($imageResized, $source, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
imagejpeg($imageResized, $fileName);
imagedestroy($source);
imagedestroy($imageResized);
$compressionQuality = 75;
$source = imagecreatefromjpeg($fileName);
imagejpeg($source, $fileName, $compressionQuality);
imagedestroy($source);
$upath = parse_url($fileName, PHP_URL_PATH);
$StuImgId = basename($upath);
}
}
When I upload this code using type=file
everything is working properly except that the image is rotated, like this:
Can anyone suggest how to fix this issue? Specifically it is happening when I upload large size photos.
Upvotes: 0
Views: 54
Reputation: 293
If I am not misunderstanding the question, it sounds similar to a problem I had a while ago with images taken by mobile devices.
The images were showing up as rotated. In my case it was related to the EXIF information for image orientation. Basically, the mobile device camera would write orientation info in EXIF which may or may not be interpreted correctly by a web browser.
Long story short, I came up with a PHP function, using PHP's exif_read_data() function, to check the EXIF and correct the image rotation if necessary. You can read more about PHP's exif_read_data() function here.
This is my function. Give it a try.
function correct_image_orientation($filename)
{
if (function_exists('exif_read_data')) {
$exif = exif_read_data($filename);
if ($exif && isset($exif['Orientation'])) {
$orientation = $exif['Orientation'];
if ($orientation != 1) {
$img = imagecreatefromjpeg($filename);
$deg = 0;
switch ($orientation) {
case 3:
$deg = 180;
break;
case 6:
$deg = 270;
break;
case 8:
$deg = 90;
break;
}
if ($deg) {
$img = imagerotate($img, $deg, 0);
}
/**
* Rewrite the rotated image back to the disk as $filename
*/
imagejpeg($img, $filename, 95);
} // if rotation is necessary
} // if exif orientation info exists
}
}
Upvotes: 1