Php : Convert a blob into an image file

前端 未结 4 629
醉酒成梦
醉酒成梦 2020-11-29 09:23

Is this possible with php and a mysql database to Convert a blob into an image file?

4条回答
  •  死守一世寂寞
    2020-11-29 10:22

    In my case i had to use base64_decode to convert blob images correctly into file.

    $path = "/tmp/images";
    $sql = "SELECT image_name, image_content FROM images";
    
    $result = mysql_query($sql, $db_con);
    if (!$result) {
       $message  = 'Invalid query: ' . mysql_error() . "\n";
       $message .= 'Whole query: ' . $sql;
       die($message);
    }
    
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
       $image = $row["image_contents"];
       $name = $row["image_name"];
    
       // option 1
       $file = fopen($path."/".$name,"w");
       echo "File name: ".$path."$name\n";
       fwrite($file, base64_decode($image));
       fclose($file);
    
       // option 2 (oneliner)
       // file_put_contents($path."/".$name, base64_decode($image));
    }
    

提交回复
热议问题