1

Currently I am using this code to upload an image:

elseif ($_post['password'] == ''){
    if($my_form->checkEmail($_POST['email'])) { // check for good mail

$allowedExts = array("gif", "jpeg", "jpg", "png");

        $extension = end(explode(".", $_FILES["img"]["name"]));

        if ((($_FILES["img"]["type"] == "image/gif")
        || ($_FILES["img"]["type"] == "image/jpeg")
        || ($_FILES["img"]["type"] == "image/jpg")
        || ($_FILES["img"]["type"] == "image/pjpeg")
        || ($_FILES["img"]["type"] == "image/x-png")
        || ($_FILES["img"]["type"] == "image/png"))
        && ($_FILES["img"]["size"] < 3145728)
        && in_array($extension, $allowedExts))
        {
           if ($_FILES["img"]["error"] <= 0)
           {
              move_uploaded_file($_FILES["img"]["tmp_name"], "upload/" . $user->userID.'.'.$extension);
           }
        }

I am using this code to show the image (it grabs the last modified file):

 $avatars = glob("upload/$user->userID.*");
 if(!empty($avatars)) {
 $avatars = array_combine($avatars, array_map("filemtime", $avatars));
 arsort($avatars);
 $latest_avatar = key($avatars);

 echo '<img style="width:20%;" src="'.$latest_avatar.'">';
 echo '<p>**Note Image must be smaller than 3MB.</p>';
 echo '<input type="submit" name="submit" value="Submit"></td></tr>';

I have seen some code around and have tried to use it, but I cannot get it to work... something like this:

$avatars = glob("upload/$user->userID.*");
if(!empty($avatars)) {
$avatars = array_combine($avatars, array_map("filemtime", $avatars));
arsort($avatars);
$latest_avatar = key($avatars);

function resample($latest_avatar, $thumbFile, $width, $orientation) {
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($latest_avatar);
$height = (int) (($width / $width_orig) * $height_orig);
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image   = imagecreatefromjpeg($latest_avatar);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Fix Orientation
switch($orientation) {
    case 3:
        $image_p = imagerotate($image_p, 180, 0);
        break;
    case 6:
        $image_p = imagerotate($image_p, -90, 0);
        break;
    case 8:
        $image_p = imagerotate($image_p, 90, 0);
        break;
}
// Output
imagejpeg($image_p, $thumbFile, 90);
}

echo '<img style="width:20%;" src="'.$image_p.'">';
echo '<p>**Note Image must be smaller than 3MB.</p>';
echo '<input type="submit" name="submit" value="Submit"></td></tr>';

It comes out with nothing, img src is just blank. I get no errors... error reporting is enabled. If I take out the function and use echo '<img style="width:20%;" src="'.$latest_avatar.'">'; it works fine and shows the image, but of course it isn't rotated.

Can anyone help me with this?

Mark Jones
  • 193
  • 1
  • 9
  • this will fail `$_post` for one thing (see what you have for email now?). Fix that and try it again. You should be getting an undefined variable notice, but you're not checking for errors. http://php.net/manual/en/function.error-reporting.php – Funk Forty Niner Jul 28 '15 at 18:21
  • plus, hard to tell if your form's kosher or not – Funk Forty Niner Jul 28 '15 at 18:23
  • The form works fine... I can currently upload an image and everything else. I just need to rotate the image based on EXIF data, thanks. – Mark Jones Jul 28 '15 at 18:29
  • ok, well technically `$_post['password']` should be `$_POST['password']` but that's probably irrelevant here. Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner Jul 28 '15 at 18:31
  • This Q&A should help http://stackoverflow.com/q/7489742/ not sure if that's where you got your code from though. – Funk Forty Niner Jul 28 '15 at 18:36
  • Th problem is, I'm confused as to where to put the function (I assume I could put it anywhere) and where to call the function. I'm not even sure if it is the right function that I should be using. I have error reporting enabled... I get no errors, it just never rotates the image. – Mark Jones Jul 28 '15 at 18:38
  • Look at this answer http://stackoverflow.com/a/13963783/ and seems there's an example on its usage. Seems to be the similar code as yours. May have to "echo" the function too. – Funk Forty Niner Jul 28 '15 at 18:40
  • I have read those for the past 2 days and I just don't understand. I will edit original post with how I have put it together... – Mark Jones Jul 28 '15 at 19:13

0 Answers0