-1

我有一个如下所示的 csv 文件:

"image-url","text1","text2","text3","text4"

这应该作为

content-of-image-as-blob,"text1","text2","text3","text4"

我以前是通过php来做的,即

  1. 读取图片网址
  2. 在保持纵横比的同时将大小调整到 150 x 150 像素以内
  3. 转换为 blob 数据,即 0x225332525....
  4. 插入mysql数据库

当 csv 文件有超过 1000 行并且脚本过早停止时,我发现这需要很长时间(即使 max-execution-time 设置为 2 小时,它在大约 10 分钟后中止)。每个图像大约 10 到 60KB。所以使用服务器的传输速度,比如 1000 行,这相当于 1000 * 60KB,最大只有 60 兆字节。以 1MB/s 的速度应该是一分钟,而以服务器的速度则要少得多。

我如何转换

"image-url","text1","text2","text3","text4"

进入

content-of-image-as-blob,"text1","text2","text3","text4"

性能比我如何做而且没有中止?

编辑:这是我目前的方法

上传.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();
?>  

编辑:测量 imageurl

$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
4

1 回答 1

0

用于file_get_contents('image')获取图像的二进制数据。执行 amysqli_real_escape_string()以获得转义权并在查询中使用此数据。

$data = file_get_contents($image);
$data_sql = mysqli_real_escape_string($data);

确保使用正确的命令进行转义,具体取决于 mysql/mysqli/pdo/etc。

编辑

由于我忘记对调整大小部分做出反应:使用imagecopyresampled,手册页上有很多示例。要获取原始数据,您必须使用输出缓冲

ob_start();
imagejpeg($resource, null, 90);
$raw_image_data = ob_get_contents();
ob_end_clean();

请记住,从数据库中输出图像时,您必须发送与图像类型相对应的正确标题。

header('image/jpeg');
echo $blob_data;
于 2013-04-19T14:51:13.903 回答