Note that there are some explanatory texts on larger screens.

plurals
  1. POInserting image-url as resized image blob data into database with good performance
    primarykey
    data
    text
    <p>I have a csv file that looks like this:</p> <pre><code>"image-url","text1","text2","text3","text4" </code></pre> <p>This should be inserted into a mysql-database as</p> <pre><code>content-of-image-as-blob,"text1","text2","text3","text4" </code></pre> <p>I used to do it via php, ie</p> <ol> <li>Read image-url</li> <li>Resize to within 150 x 150 pixels while maintaining aspect ratio</li> <li>convert to blob-data, ie 0x225332525....</li> <li>Insert into mysql-database</li> </ol> <p>I found this to take a long time when the csv file has more than 1000 lines and the script to stop prematurely (even though max-execution-time is set to 2 hours, it aborted after about 10 minutes). Images are around 10 to 60KB each. So using the transmission speeds of servers for say 1000 lines this amounts to 1000 * 60KB, just 60 Megabytes max. Should be one minute with 1MB/s and much less at a server's speed.</p> <p>How do I convert</p> <pre><code>"image-url","text1","text2","text3","text4" </code></pre> <p>into</p> <pre><code>content-of-image-as-blob,"text1","text2","text3","text4" </code></pre> <p>with better performance than how I do it and without it aborting?</p> <p><strong>Edit</strong>: That's my current approach</p> <p><em>upload.php:</em></p> <pre><code>&lt;?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 &lt; $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; } } } ?&gt; </code></pre> <p><em>kleiner.php:</em></p> <pre><code>&lt;?php header('Content-Type: image/jpeg'); require('SimpleImage.php'); $image = new SimpleImage(); $image-&gt;load($_GET['link']); if ($image-&gt;getWidth() &gt; $image-&gt;getHeight()) { $ratio = $width / $image-&gt;getWidth(); $height = $image-&gt;getheight() * $ratio; } else { $ratio = $height / $image-&gt;getHeight(); $width = $image-&gt;getWidth() * $ratio; } $image-&gt;resize($width,$height); $image-&gt;output(); ?&gt; </code></pre> <p>EDIT: Measuring imageurl</p> <pre><code>$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); </code></pre> <p>results in</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload