You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

create_thumbnail函数报Undefined offset:1及图片上传异常求助

Fixing "Undefined offset: 1" and Original Image Upload Issues

Hey there! Let's tackle your problems one by one:

1. The "Undefined offset: 1" Error

This error pops up because in your create_thumbnail function, when handling wider images ($src_aspect > $thumb_aspect), you only define one element in the $src_pos array, but later try to access $src_pos[1] (the y-coordinate) in the imagecopyresampled call.

Fix the $src_pos Array

In the $src_aspect > $thumb_aspect branch, update the $src_pos line to include the y-coordinate (which should be 0 since we're cropping left/right, not top/bottom):

} 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);
    // Add the y-coordinate (0) to the $src_pos array
    $src_pos = array(($size[0] * $scale - $width) / $scale / 2, 0); 
}

This ensures $src_pos always has two elements (x and y), so accessing $src_pos[1] won't throw an undefined offset error.

2. Original Image Not Uploading

When you enable create_thumbnail, the original image fails to upload—this is likely due to missing folder creation or unvalidated upload success. Here's how to fix it:

Add Folder Creation & Upload Validation

Before moving the uploaded file, make sure the target directory exists (PHP can't upload to a folder that doesn't exist). Also, check if move_uploaded_file actually succeeds:

if ($_FILES["discussion_image"]["name"] != "") { 
    $test = explode(".", $_FILES["discussion_image"]["name"]); 
    $extension = end($test); 
    $name = rand(100, 9999999999); 
    $upload_dir = '../uploaded_pictures/uploads/' . $user_id . '/';
    
    // Create directory if it doesn't exist
    if (!is_dir($upload_dir)) {
        mkdir($upload_dir, 0755, true); // Recursively create parent folders if needed
    }

    $file_temp = $_FILES['discussion_image']['tmp_name']; 
    $file_path = $upload_dir . $name . '.' . $extension; 

    // Validate upload success
    if (!move_uploaded_file($file_temp, $file_path)) {
        die("Error: Failed to upload original image. Check folder permissions.");
    }

    // Rest of your code (database insert, thumbnail creation)
    $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)"; 
    // Execute your query here (use prepared statements to prevent SQL injection!)
    
    $save_path = $upload_dir . $name . '.jpg'; 
    create_thumbnail($file_path, $save_path, 250, 250); 
}

Important Notes:

  • Always use prepared statements for database inserts instead of directly inserting variables—this prevents SQL injection attacks.
  • The original image is stored at $file_path (e.g., xxx.png if you uploaded a PNG), while the thumbnail is saved as $save_path (e.g., xxx.jpg). Double-check you're looking in the correct path for the original image.

Full Fixed create_thumbnail Function

Here's the updated function with the $src_pos fix:

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

    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) { 
        $scale = $width / $size[0]; 
        $new_image = array($width, $width / $src_aspect); 
        $src_pos = array(0, ($size[1] * $scale - $height) / $scale / 2); 
    } else if ($src_aspect > $thumb_aspect) { 
        $scale = $height / $size[1]; 
        $new_size = array($height * $src_aspect, $height); 
        // Fixed: added y-coordinate (0) to $src_pos
        $src_pos = array(($size[0] * $scale - $width) / $scale / 2, 0); 
    } else { 
        $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); 
    } 
}

内容的提问来源于stack exchange,提问作者firashelou

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:09:57