Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to upload an image using a form, everything so far in the form functions well except i get this error after i submit the form and the data gets submitted to the database table : **

Notice>: Undefined offset: 1 in D:wampwwwasserterincludescreate_thumbnail.php on line 40

this is the line 40 in the create_thumbnail() function : imagecopyresampled($thumb, $src, 0, 0, $src_pos[0], $src_pos[1], $new_size[0], $new_size[1], $size[0], $size[1]);

if comment the create_thumbnail() in my codes everything works fine and the image gets uploaded and i get no error back, but when i add create_thumbnail() line of code the original image does not get uploaded to the path but the thumbnail gets created and i still get this error ! please help !

here is my codes :

if ($_FILES["discussion_image"]["name"] != "") {
        $test = explode(".", $_FILES["discussion_image"]["name"]);
        $extension = end($test);
        $name = rand(100, 9999999999);
        $file_temp = $_FILES['discussion_image']['tmp_name'];
        $file_path = '../uploaded_pictures/uploads/' . $user_id . '/'. $name .'.'.$extension;
        //$file_path = mysqli_real_escape_string($connection, $file_path);
        move_uploaded_file($file_temp, $file_path); // move_uploaded_file() is a built in function of PHP

        $query = "INSERT INTO discussions_table (user_id, title, link, image_link, subject, discussion, discussion_timestamp) VALUES ($user_id, '$title', '$link', '$file_path', '$subject', '$discussion', $discussion_timestamp)";


        $save_path = '../uploaded_pictures/uploads/' . $user_id . '/'. $name.'.jpg';
        //$save_path_small = "../uploaded_pictures/uploads/" . $user_id . "/" . $name.'small.jpg';
        create_thumbnail($file_path, $save_path, 250, 250); // creates thumbnail for profile picture
        //create_thumbnail($file_path, $save_path_small, 50, 50); // creates thumbnail for small user picture
    }

and here is my create_thumbnail function :

function create_thumbnail($path, $save, $width, $height) {
$info = getimagesize($path);
$size = array($info[0], $info[1]); // info[0] is the width and info[1] is the height

if ($info['mime'] == 'image/png') {
    $src = imagecreatefrompng($path);
} else if ($info['mime'] == 'image/jpeg') {
    $src = imagecreatefromjpeg($path);
} else {
    return false;
}

$thumb = imagecreatetruecolor($width, $height);

$src_aspect = $size[0] / $size[1];
$thumb_aspect = $width / $height;

if ($src_aspect < $thumb_aspect) {
    // means the image is narrower so we want to crop the top and bottom
    $scale = $width / $size[0]; // width of the thumb and width of the actual image
    $new_image = array($width, $width / $src_aspect);
    $src_pos = array(0, ($size[1] * $scale - $height) / $scale / 2); // we are fixing x position to 0 and calculating the y position

} else if ($src_aspect > $thumb_aspect) {
    // means image is wider so we need to crop left and right
    $scale = $height / $size[1];
    $new_size = array($height * $src_aspect, $height);
    $src_pos = array(($size[0] * $scale - $width) / $scale / 2); // we are fixing y position to 0 and calculating the x position
} else {
    // this means it is the same shape as the thumbnail we are creating
    $new_size = array($width, $height);
    $src_pos = array(0, 0);
}

$new_size[0] = max($new_size[0], 1);
$new_size[1] = max($new_size[1], 1);

imagecopyresampled($thumb, $src, 0, 0, $src_pos[0], $src_pos[1], $new_size[0], $new_size[1], $size[0], $size[1]);

if ($save === false) {
    return imagejpeg($thumb);
} else {
    return imagejpeg($thumb, $save);
}

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
169 views
Welcome To Ask or Share your Answers For Others

1 Answer

Thanks everyone for your help, I found the bug !

it was a naming issue, i should replace this $new_image = array($width, $width / $src_aspect); with this $new_size = array($width, $width / $src_aspect);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...