Home>

I want to resize, bind the image and display it in the browser.

I was able to confirm the resizing of the image, but the displayed image appeared in the middle of the black background and could not be bound.
When I check the database, it is displayed as 1 byte.

Corresponding source code
public function add ($arr) {
    if (is_uploaded_file ($_FILES ["img"] ["tmp_name"])) {
         if (! file_exists ('upload')) {
            mkdir ('upload');
       }
       $file ='upload /'. basename ($_FILES ['img'] ['name']);
       if (move_uploaded_file ($_FILES ["img"] ["tmp_name"], $file)) {
        $img ='';
// Specify copy destination image size
$dst_w = 384;
$dst_h = 256;
// Create copy destination image
$dst_image = imagecreate ($dst_w, $dst_h);
// Read the copy source image
$src_image = imagecreatefromjpeg ($file);
// Get the size of the copy source image
$imagesize = getimagesize ($file);
$src_w = $imagesize [0];
$src_h = $imagesize [1];
// Resize and copy
imagecopyresampled (
    $dst_image, // image to copy to
    $src_image, // Copy source image
    0, // x coordinate of copy destination
    0, // y coordinate of the copy destination.
    0, // x coordinate of copy source
    20, // copy source y coordinate
    $dst_w, // Copy destination width
    $dst_h, // Height of copy destination
    $src_w, // Copy source width
    $src_h);// Copy source height
// Output image to file
imagejpeg ($dst_image);
       }
    } else {
        echo "No file selected.";
    }
    $sql = "INSERT INTO fish (user_id, catch_who, fish, catch_at, catch_time, catch_weather, img) VALUES (: user_id,: catch_who,: fish,: catch_at, now (),: catch_weather,: img)";
    $stmt = $this->con->prepare ($sql);
    $params = array (': user_id' =>$_SESSION ['User'] ['id'],': catch_who' =>$_SESSION ['User'] ['name'],': fish'=>$arr ['fish'],': catch_at' =>$arr ['catch_at'],': catch_weather' =>$arr ['catch_weather'],': img' =>$dst_image);
    $stmt->execute ($params);
}

table definition

CREATE TABLEuser(
idint (11) NOT NULL,
user_idint (11) DEFAULT NULL,
namevarchar DEFAULT NULL,
addressvarchar DEFAULT NULL,
telint (11) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;

CREATE TABLEfish(
idint (11) NOT NULL,
user_idint (11) DEFAULT NULL,
catch_whovarchar DEFAULT NULL,
catch_atvarchar DEFAULT NULL,
catch_timedatetime DEFAULT NULL,
imgmediumblob DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;

  • Answer # 1

    I don't know what the table definition looks like, but imagecreate () returns resource and I think it can't be saved in the DB as it is.
    Try converting to binary.
    However, it depends on the table definition.