我有一个csv文件,如下所示:
"image-url","text1","text2","text3","text4"
这应该作为
插入到mysql数据库中content-of-image-as-blob,"text1","text2","text3","text4"
我曾经通过php做到这一点,即
我发现这需要很长时间,当csv文件超过1000行并且脚本过早停止时(即使最大执行时间设置为2小时,它在大约10分钟后中止)。图像大约为10到60KB。因此,使用服务器的传输速度(例如1000行),这相当于1000 * 60KB,最大仅为60兆字节。应该是1分钟/秒的一分钟,而服务器的速度要小得多。
如何转换
"image-url","text1","text2","text3","text4"
到
content-of-image-as-blob,"text1","text2","text3","text4"
比我的表现更好,没有中止?
修改:这是我目前的做法
upload.php的:
<?php
function BildInBLOB($linkadresse)
{
$maxsize = 65536;
$data = file_get_contents('kleiner.php?link='.urlencode($linkadresse)); //kleiner.php further down
$serialized_data = serialize($data);
$size = strlen($serialized_data);
if ($size < $maxsize) {
$imgData = mysql_escape_string($data);
return $imgData;
}
else
{
return false;
}
}
//$felder[0] = image-url
$kopfzeilenfelder = get_headers($felder[0]);
if (isset($kopfzeilenfelder[7]))
{
if ($kopfzeilenfelder[7] == "Content-Type: image/jpeg" || $kopfzeilenfelder[7] == "Content-Type: image/gif" || $kopfzeilenfelder[7] == "Content-Type: image/png")
{
$imageurl = BildInBLOB($felder[0]);
if ($imageurl == false)
mail('test@test.de','Error',$felder[0]);
$imageurl_typ = returnMIMEType($felder[0]);
//Einfügen in Datenbank
$abfragetext = 'INSERT INTO `table` (`content-of-image-as-blob`, `text1`,`text2`,`text3`,`text4`) VALUES (\''.$imageurl.'\', \''.$text1.'\', \''.$text2.'\', \''.$text3.'\', \''.$text4.'\');';
$abfrage_ergebnis = mysql_query($abfragetext);
}
else
{
$fehler = true;
}
}
else
{
$fehler = true;
break;
}
}
}
?>
kleiner.php:
<?php
header('Content-Type: image/jpeg');
require('SimpleImage.php');
$image = new SimpleImage();
$image->load($_GET['link']);
if ($image->getWidth() > $image->getHeight())
{
$ratio = $width / $image->getWidth();
$height = $image->getheight() * $ratio;
}
else
{
$ratio = $height / $image->getHeight();
$width = $image->getWidth() * $ratio;
}
$image->resize($width,$height);
$image->output();
?>
编辑:测量图像
$time_start = microtime(true);
$imageurl = BildInBLOB($felder[0]);
$time_end = microtime(true);
$time = $time_end - $time_start;
$datei_handle=fopen('loeschen.txt',"a");
fwrite($datei_handle,$time."\n");
fclose($datei_handle);
结果
4.0833919048309
0.49023699760437
1.4829730987549
0.46650505065918
1.6500248908997
1.6888198852539
1.7747900485992
1.7654030323029
1.7827639579773
1.6984438896179
1.7959661483765
0.5819149017334
1.5812880992889
0.477952003479
0.48433899879456
0.49926495552063
2.6636629104614
1.6903870105743
0.47167086601257
0.59404015541077
0.4657621383667
0.48750114440918
1.5725650787354
0.67707896232605
0.57791996002197
0.69522404670715
3.727931022644
0.59935402870178
0.47536182403564
0.5656099319458
0.74236917495728
0.57420802116394
0.58336305618286
2.6896998882294
2.6945810317993
0.55907297134399
1.1201891899109
0.60288882255554
0.55426001548767
5.7161459922791
4.6411039829254
0.61310982704163
0.63013315200806
2.6628720760345
0.59778094291687
0.58457708358765
0.45639109611511
0.6767098903656
0.67303490638733
0.61607003211975
0.69722104072571
1.5636711120605
0.66522693634033
3.5140080451965
0.58035182952881
0.575355052948
0.60047006607056
0.59423995018005
2.5612559318542
3.721363067627
3.5471968650818
3.5297789573669
0.56819891929626
1.667160987854
0.70830678939819
0.55341696739197
0.76275014877319
0.88022589683533
1.9490652084351
0.96493005752563
1.0359950065613
2.3565669059753
1.7734251022339
1.8485751152039
0.51054096221924
0.77539587020874
2.6769950389862
1.6807489395142
0.47317314147949
0.45396709442139
0.5827260017395
0.67431807518005
0.45959377288818
0.58339309692383
1.4438741207123
0.58361291885376
1.5400068759918
1.639084815979
1.6939740180969
0.56597709655762
0.70935010910034
0.5993390083313
0.47545599937439
0.61747407913208
0.60248994827271
0.61786508560181
0.58388590812683
0.47097396850586
0.60158681869507
0.58294200897217
0.60332798957825
1.7215020656586
0.67375087738037
0.60588788986206
0.68769884109497
0.7059919834137
0.45914888381958
0.56948590278625
0.57627296447754
0.46779298782349
0.46954989433289
0.57487106323242
答案 0 :(得分:0)
使用file_get_contents('image')
获取图像的二进制数据。执行mysqli_real_escape_string()
以获取转义权并在查询中使用此数据。
$data = file_get_contents($image);
$data_sql = mysqli_real_escape_string($data);
确保使用正确的命令进行转义,具体取决于mysql / mysqli / pdo / etc.
修改强>
由于我忘了对调整大小部分做出反应:使用imagecopyresampled,手册页上有很多例子。 要获取原始数据,您必须使用output buffering:
ob_start();
imagejpeg($resource, null, 90);
$raw_image_data = ob_get_contents();
ob_end_clean();
请记住,从数据库输出图像时,您必须发送与图像类型对应的正确标题。
header('image/jpeg');
echo $blob_data;