Estrazione di coordinate dal file autocad dxf usando la funzione explode php

07/03/2020

La funzione explode () è abbastanza semplice, ma estremamente utile e viene utilizzata continuamente nella codifica. Se questa funzione non è applicabile, vengono utilizzate espressioni regolari, ma funzionano più lentamente, quindi è utile lavorare con le funzioni PHP necessarie per accelerare il programma.

Explode php: descrizione della funzione e sua applicazione pratica

La funzione accetta una stringa come input, la divide per divisore e assegna valori a un array con i tasti numerici.

Usando il file dxf autocad come esempio, proviamo ad estrarre le coordinate e disegnare un disegno usando GD Library php.

Il file dxf è essenzialmente un documento di testo in cui sono registrate le coordinate di tutti i disegni. Sembra questo:

File Dxf con disegno poligonale

ENTITIES sono entità, LWPOLYLINE è un poligono chiuso, il numero 10 indica la coordinata x, seguito dal valore (cioè, x = 0.0), il numero 20 indica y, il suo valore è dalla riga successiva (cioè, y = 500.0 ), niente di complicato.

Trova le informazioni necessarie nel file dxf. Per questo, usiamo la funzione strchr (), che troverà entità (ENTITIES) e poligoni (LWPOLYLINE):

 $content = file_get_contents('stairs.dxf');$entities = strchr($content, "ENTITIES");$endsec = strchr($entities, "ENDSEC", true);$entitie_string = $endsec; 

La funzione explode php aiuterà ad estrarre le coordinate. In questo esempio, usiamo solo un poligono, ma ci sono molte altre figure nei disegni, quindi li assegnamo ad un array. Consideriamo solo LWPOLYLINE:

 $entities_array = array("LWPOLYLINE"); 

L'esempio seguente trova le posizioni di tutte le LWPOLYLINE in un documento di testo dxf:

 $lastPos = 0;$positions_entities = array();foreach($entities_array as $key_needle =>$entitie){while (($lastPos = strpos($entitie_string, $entitie, $lastPos))!== false) }$array_keys_positions = array_keys($positions_entities);$sizeof_array_positions = sizeof($array_keys_positions);for($i=0; $i< $sizeof_array_positions; $i++){if(isset($array_keys_positions[$i+1])) else } {$positions_entities[$lastPos] = $entitie;$lastPos = $lastPos + strlen($entitie);} $lastPos = 0;$positions_entities = array();foreach($entities_array as $key_needle =>$entitie){while (($lastPos = strpos($entitie_string, $entitie, $lastPos))!== false) }$array_keys_positions = array_keys($positions_entities);$sizeof_array_positions = sizeof($array_keys_positions);for($i=0; $i< $sizeof_array_positions; $i++){if(isset($array_keys_positions[$i+1])) else } {$entities_string_array[$positions_entities[$array_keys_positions[$i]]][$array_keys_positions[$i]] = substr($entitie_string, $array_keys_positions[$i], $array_keys_positions[$i+1] - $array_keys_positions[$i] );} $lastPos = 0;$positions_entities = array();foreach($entities_array as $key_needle =>$entitie){while (($lastPos = strpos($entitie_string, $entitie, $lastPos))!== false) }$array_keys_positions = array_keys($positions_entities);$sizeof_array_positions = sizeof($array_keys_positions);for($i=0; $i< $sizeof_array_positions; $i++){if(isset($array_keys_positions[$i+1])) else } {$entities_string_array[$positions_entities[$array_keys_positions[$i]]][$array_keys_positions[$i]] = substr($first_entitie_string, $array_keys_positions[$i] );} $lastPos = 0;$positions_entities = array();foreach($entities_array as $key_needle =>$entitie){while (($lastPos = strpos($entitie_string, $entitie, $lastPos))!== false) }$array_keys_positions = array_keys($positions_entities);$sizeof_array_positions = sizeof($array_keys_positions);for($i=0; $i< $sizeof_array_positions; $i++){if(isset($array_keys_positions[$i+1])) else } 

Tutte le linee di poligoni sono ora in una matrice, cioè abbiamo tre poligoni, che significa tre chiavi e tre valori. I valori contengono le linee con informazioni dal file dxf su questi poligoni. Ora è necessario dividere queste linee per selezionare le coordinate da loro. Per fare ciò, usa explode ():

 foreach( $entities_string_array as $key_ent =>$value_ent){foreach( $value_ent as $key_val =>$value_e ) } {$explode_value_e[$key_ent][$key_val] = explode(PHP_EOL,  $value_e);} foreach( $entities_string_array as $key_ent =>$value_ent){foreach( $value_ent as $key_val =>$value_e ) } 

Dall'array multidimensionale ottenuto, otteniamo i valori che seguono i segni 10 (x) e 20 (y):

 $lwpolyline_array = $explode_value_e["LWPOLYLINE"];$k = 0;foreach( $lwpolyline_array as $lw_key => $explode_key ){foreach( $explode_key as $key => $lwpolyline_value){if( $lwpolyline_value == '10' ) else if( $lwpolyline_value == '20' ) }$implode_coordinates_db[$k] = implode(';',$coordinates_db);$k++;}$width = max($coordinates_x) - min($coordinates_x);$height = max($coordinates_y) - min($coordinates_y); {$coordinates[$k][] = $coordinates_db[] = $coordinates_x[] = (float)$explode_key[$key+1];} $lwpolyline_array = $explode_value_e["LWPOLYLINE"];$k = 0;foreach( $lwpolyline_array as $lw_key => $explode_key ){foreach( $explode_key as $key => $lwpolyline_value){if( $lwpolyline_value == '10' ) else if( $lwpolyline_value == '20' ) }$implode_coordinates_db[$k] = implode(';',$coordinates_db);$k++;}$width = max($coordinates_x) - min($coordinates_x);$height = max($coordinates_y) - min($coordinates_y); {$coordinates[$k][] = $coordinates_db[] = $coordinates_y[] = (float)$explode_key[$key+1];} $lwpolyline_array = $explode_value_e["LWPOLYLINE"];$k = 0;foreach( $lwpolyline_array as $lw_key => $explode_key ){foreach( $explode_key as $key => $lwpolyline_value){if( $lwpolyline_value == '10' ) else if( $lwpolyline_value == '20' ) }$implode_coordinates_db[$k] = implode(';',$coordinates_db);$k++;}$width = max($coordinates_x) - min($coordinates_x);$height = max($coordinates_y) - min($coordinates_y); 

L'array $ coordinates contiene le coordinate dei poligoni, $ coordinates_db sarà usato per scrivere i valori xey nel database, e $ coordinates_x e $ coordinates_y sono necessari per determinare la larghezza e l'altezza del nostro disegno nella libreria php GD.

Combinare i dati in una stringa usando implode () per la memorizzazione compatta delle informazioni in MySQL

Per l'archiviazione nel database, raccogliamo le coordinate in una stringa usando implode (), t. A. Se memorizzi ciascun punto in una cella separata, il database crescerà gradualmente fino a raggiungere dimensioni enormi. Creare una tabella in MySQL non lo farà, in modo da non complicare il codice.

 $implode_coordinates_db[$k] = implode(';',$coordinates_db); 

Resta da scrivere una funzione che disegnerà un'immagine nella GD Library:

 $image_array = array($implode_coordinates_db, $width, $height); 

Per creare un'immagine, abbiamo bisogno di coordinate (useremo una stringa con coordinate, che può essere recuperata dal database $ implode_coordinates_db), larghezza e altezza.

Chiamata di funzione:

 $image_base64 = image_create($image_array); 

E la funzione stessa:

 function image_create( $image_array ) ob_start();imagepng($im);$buffer = ob_get_clean();//ob_end_clean();$img = base64_encode($buffer);imagedestroy($im);return $img;} {$implode_coordinates = $image_array[0];$width = $image_array[1];$height = $image_array[2];$im = imagecreatetruecolor($width+1, $height+1);$white=imagecolorallocate($im,255,255,255);$black = imagecolorallocate($im, 0, 0, 0);imagefilledrectangle($im, 0, 0, $width, $height, $white);foreach( $implode_coordinates as $key =>$coordinates_string ){$coordinates[$key] = explode(';', $coordinates_string);$polygon_arrays_count[$key]=count($coordinates[$key])/2;imagepolygon($im, $coordinates[$key], $polygon_arrays_count[$key] , $black);} function image_create( $image_array ) ob_start();imagepng($im);$buffer = ob_get_clean();//ob_end_clean();$img = base64_encode($buffer);imagedestroy($im);return $img;} 

Divisione di una stringa ottenuta da MySQL usando explode ()

La funzione riceve le coordinate di stringa dal database, quindi la linea viene divisa usando explode php, dopo di che imagepolygon () disegna poligoni su di noi.

 $coordinates[$key] = explode(';', $coordinates_string);imagepolygon($im, $coordinates[$key], $polygon_arrays_count[$key] , $black); 

Per questo esempio, l'immagine è stata creata nel formato base64_encode di imagepng, ma è possibile salvare l'immagine in jpg o png.

Ora puoi visualizzare l'immagine nel browser:

 $image_array = array($implode_coordinates_db, $width, $height);$image_base64 = image_create($image_array);$image_src="'data:image/png;base64,'.$image_base64;echo" "  "; 

Le immagini di sfondo avranno questo aspetto:

Immagine di sfondo immagine png base64_encode

Se è possibile utilizzare le funzioni integrate invece delle espressioni regolari, il processo di esecuzione del programma richiederà un po 'meno di tempo, quindi è sempre consigliabile cercare di raggiungere l'obiettivo con l'aiuto di soluzioni pronte in php.

Tutto il codice:

 $content = file_get_contents('stairs.dxf');$entities = strchr($content, "ENTITIES");$endsec = strchr($entities, "ENDSEC", true);$entitie_string = $endsec;$entities_array = array("LWPOLYLINE");$lastPos = 0;$positions_entities = array();foreach($entities_array as $key_needle =>$entitie){while (($lastPos = strpos($entitie_string, $entitie, $lastPos))!== false) }$array_keys_positions = array_keys($positions_entities);$sizeof_array_positions = sizeof($array_keys_positions);for($i=0; $i< $sizeof_array_positions; $i++){if(isset($array_keys_positions[$i+1])) else }foreach( $entities_string_array as $key_ent =>$value_ent){foreach( $value_ent as $key_val =>$value_e ) }$lwpolyline_array = $explode_value_e["LWPOLYLINE"];$k = 0;foreach( $lwpolyline_array as $lw_key => $explode_key ){foreach( $explode_key as $key => $lwpolyline_value){if( $lwpolyline_value == '10' ) else if( $lwpolyline_value == '20' ) }$implode_coordinates_db[$k] = implode(';',$coordinates_db);$k++;}$width = max($coordinates_x) - min($coordinates_x);$height = max($coordinates_y) - min($coordinates_y);function image_create( $image_array ) ob_start();imagepng($im);$buffer = ob_get_clean();//ob_end_clean();$img = base64_encode($buffer);imagedestroy($im);return $img;}$image_array = array($implode_coordinates_db, $width, $height);$image_base64 = image_create($image_array);$image_src="'data:image/png;base64,'.$image_base64;echo" "  "; {$positions_entities[$lastPos] = $entitie;$lastPos = $lastPos + strlen($entitie);} $content = file_get_contents('stairs.dxf');$entities = strchr($content, "ENTITIES");$endsec = strchr($entities, "ENDSEC", true);$entitie_string = $endsec;$entities_array = array("LWPOLYLINE");$lastPos = 0;$positions_entities = array();foreach($entities_array as $key_needle =>$entitie){while (($lastPos = strpos($entitie_string, $entitie, $lastPos))!== false) }$array_keys_positions = array_keys($positions_entities);$sizeof_array_positions = sizeof($array_keys_positions);for($i=0; $i< $sizeof_array_positions; $i++){if(isset($array_keys_positions[$i+1])) else }foreach( $entities_string_array as $key_ent =>$value_ent){foreach( $value_ent as $key_val =>$value_e ) }$lwpolyline_array = $explode_value_e["LWPOLYLINE"];$k = 0;foreach( $lwpolyline_array as $lw_key => $explode_key ){foreach( $explode_key as $key => $lwpolyline_value){if( $lwpolyline_value == '10' ) else if( $lwpolyline_value == '20' ) }$implode_coordinates_db[$k] = implode(';',$coordinates_db);$k++;}$width = max($coordinates_x) - min($coordinates_x);$height = max($coordinates_y) - min($coordinates_y);function image_create( $image_array ) ob_start();imagepng($im);$buffer = ob_get_clean();//ob_end_clean();$img = base64_encode($buffer);imagedestroy($im);return $img;}$image_array = array($implode_coordinates_db, $width, $height);$image_base64 = image_create($image_array);$image_src='data:image/png;base64,'.$image_base64;echo "  "; {$entities_string_array[$positions_entities[$array_keys_positions[$i]]][$array_keys_positions[$i]] = substr($entitie_string, $array_keys_positions[$i], $array_keys_positions[$i+1] - $array_keys_positions[$i] );} $content = file_get_contents('stairs.dxf');$entities = strchr($content, "ENTITIES");$endsec = strchr($entities, "ENDSEC", true);$entitie_string = $endsec;$entities_array = array("LWPOLYLINE");$lastPos = 0;$positions_entities = array();foreach($entities_array as $key_needle =>$entitie){while (($lastPos = strpos($entitie_string, $entitie, $lastPos))!== false) }$array_keys_positions = array_keys($positions_entities);$sizeof_array_positions = sizeof($array_keys_positions);for($i=0; $i< $sizeof_array_positions; $i++){if(isset($array_keys_positions[$i+1])) else }foreach( $entities_string_array as $key_ent =>$value_ent){foreach( $value_ent as $key_val =>$value_e ) }$lwpolyline_array = $explode_value_e["LWPOLYLINE"];$k = 0;foreach( $lwpolyline_array as $lw_key => $explode_key ){foreach( $explode_key as $key => $lwpolyline_value){if( $lwpolyline_value == '10' ) else if( $lwpolyline_value == '20' ) }$implode_coordinates_db[$k] = implode(';',$coordinates_db);$k++;}$width = max($coordinates_x) - min($coordinates_x);$height = max($coordinates_y) - min($coordinates_y);function image_create( $image_array ) ob_start();imagepng($im);$buffer = ob_get_clean();//ob_end_clean();$img = base64_encode($buffer);imagedestroy($im);return $img;}$image_array = array($implode_coordinates_db, $width, $height);$image_base64 = image_create($image_array);$image_src='data:image/png;base64,'.$image_base64;echo "  "; {$entities_string_array[$positions_entities[$array_keys_positions[$i]]][$array_keys_positions[$i]] = substr($entitie_string, $array_keys_positions[$i] );} $content = file_get_contents('stairs.dxf');$entities = strchr($content, "ENTITIES");$endsec = strchr($entities, "ENDSEC", true);$entitie_string = $endsec;$entities_array = array("LWPOLYLINE");$lastPos = 0;$positions_entities = array();foreach($entities_array as $key_needle =>$entitie){while (($lastPos = strpos($entitie_string, $entitie, $lastPos))!== false) }$array_keys_positions = array_keys($positions_entities);$sizeof_array_positions = sizeof($array_keys_positions);for($i=0; $i< $sizeof_array_positions; $i++){if(isset($array_keys_positions[$i+1])) else }foreach( $entities_string_array as $key_ent =>$value_ent){foreach( $value_ent as $key_val =>$value_e ) }$lwpolyline_array = $explode_value_e["LWPOLYLINE"];$k = 0;foreach( $lwpolyline_array as $lw_key => $explode_key ){foreach( $explode_key as $key => $lwpolyline_value){if( $lwpolyline_value == '10' ) else if( $lwpolyline_value == '20' ) }$implode_coordinates_db[$k] = implode(';',$coordinates_db);$k++;}$width = max($coordinates_x) - min($coordinates_x);$height = max($coordinates_y) - min($coordinates_y);function image_create( $image_array ) ob_start();imagepng($im);$buffer = ob_get_clean();//ob_end_clean();$img = base64_encode($buffer);imagedestroy($im);return $img;}$image_array = array($implode_coordinates_db, $width, $height);$image_base64 = image_create($image_array);$image_src='data:image/png;base64,'.$image_base64;echo "  "; {$explode_value_e[$key_ent][$key_val] = explode(PHP_EOL,  $value_e);} $content = file_get_contents('stairs.dxf');$entities = strchr($content, "ENTITIES");$endsec = strchr($entities, "ENDSEC", true);$entitie_string = $endsec;$entities_array = array("LWPOLYLINE");$lastPos = 0;$positions_entities = array();foreach($entities_array as $key_needle =>$entitie){while (($lastPos = strpos($entitie_string, $entitie, $lastPos))!== false) }$array_keys_positions = array_keys($positions_entities);$sizeof_array_positions = sizeof($array_keys_positions);for($i=0; $i< $sizeof_array_positions; $i++){if(isset($array_keys_positions[$i+1])) else }foreach( $entities_string_array as $key_ent =>$value_ent){foreach( $value_ent as $key_val =>$value_e ) }$lwpolyline_array = $explode_value_e["LWPOLYLINE"];$k = 0;foreach( $lwpolyline_array as $lw_key => $explode_key ){foreach( $explode_key as $key => $lwpolyline_value){if( $lwpolyline_value == '10' ) else if( $lwpolyline_value == '20' ) }$implode_coordinates_db[$k] = implode(';',$coordinates_db);$k++;}$width = max($coordinates_x) - min($coordinates_x);$height = max($coordinates_y) - min($coordinates_y);function image_create( $image_array ) ob_start();imagepng($im);$buffer = ob_get_clean();//ob_end_clean();$img = base64_encode($buffer);imagedestroy($im);return $img;}$image_array = array($implode_coordinates_db, $width, $height);$image_base64 = image_create($image_array);$image_src='data:image/png;base64,'.$image_base64;echo "  "; {$coordinates[$k][] = $coordinates_db[] = $coordinates_x[] = (float)$explode_key[$key+1];} $content = file_get_contents('stairs.dxf');$entities = strchr($content, "ENTITIES");$endsec = strchr($entities, "ENDSEC", true);$entitie_string = $endsec;$entities_array = array("LWPOLYLINE");$lastPos = 0;$positions_entities = array();foreach($entities_array as $key_needle =>$entitie){while (($lastPos = strpos($entitie_string, $entitie, $lastPos))!== false) }$array_keys_positions = array_keys($positions_entities);$sizeof_array_positions = sizeof($array_keys_positions);for($i=0; $i< $sizeof_array_positions; $i++){if(isset($array_keys_positions[$i+1])) else }foreach( $entities_string_array as $key_ent =>$value_ent){foreach( $value_ent as $key_val =>$value_e ) }$lwpolyline_array = $explode_value_e["LWPOLYLINE"];$k = 0;foreach( $lwpolyline_array as $lw_key => $explode_key ){foreach( $explode_key as $key => $lwpolyline_value){if( $lwpolyline_value == '10' ) else if( $lwpolyline_value == '20' ) }$implode_coordinates_db[$k] = implode(';',$coordinates_db);$k++;}$width = max($coordinates_x) - min($coordinates_x);$height = max($coordinates_y) - min($coordinates_y);function image_create( $image_array ) ob_start();imagepng($im);$buffer = ob_get_clean();//ob_end_clean();$img = base64_encode($buffer);imagedestroy($im);return $img;}$image_array = array($implode_coordinates_db, $width, $height);$image_base64 = image_create($image_array);$image_src='data:image/png;base64,'.$image_base64;echo "  "; {$coordinates[$k][] = $coordinates_db[] = $coordinates_y[] = (float)$explode_key[$key+1];} $content = file_get_contents('stairs.dxf');$entities = strchr($content, "ENTITIES");$endsec = strchr($entities, "ENDSEC", true);$entitie_string = $endsec;$entities_array = array("LWPOLYLINE");$lastPos = 0;$positions_entities = array();foreach($entities_array as $key_needle =>$entitie){while (($lastPos = strpos($entitie_string, $entitie, $lastPos))!== false) }$array_keys_positions = array_keys($positions_entities);$sizeof_array_positions = sizeof($array_keys_positions);for($i=0; $i< $sizeof_array_positions; $i++){if(isset($array_keys_positions[$i+1])) else }foreach( $entities_string_array as $key_ent =>$value_ent){foreach( $value_ent as $key_val =>$value_e ) }$lwpolyline_array = $explode_value_e["LWPOLYLINE"];$k = 0;foreach( $lwpolyline_array as $lw_key => $explode_key ){foreach( $explode_key as $key => $lwpolyline_value){if( $lwpolyline_value == '10' ) else if( $lwpolyline_value == '20' ) }$implode_coordinates_db[$k] = implode(';',$coordinates_db);$k++;}$width = max($coordinates_x) - min($coordinates_x);$height = max($coordinates_y) - min($coordinates_y);function image_create( $image_array ) ob_start();imagepng($im);$buffer = ob_get_clean();//ob_end_clean();$img = base64_encode($buffer);imagedestroy($im);return $img;}$image_array = array($implode_coordinates_db, $width, $height);$image_base64 = image_create($image_array);$image_src='data:image/png;base64,'.$image_base64;echo "  "; {$implode_coordinates = $image_array[0];$width = $image_array[1];$height = $image_array[2];$im = imagecreatetruecolor($width+1, $height+1);$white=imagecolorallocate($im,255,255,255);$black = imagecolorallocate($im, 0, 0, 0);imagefilledrectangle($im, 0, 0, $width, $height, $white);foreach( $implode_coordinates as $key =>$coordinates_string ){$coordinates[$key] = explode(';', $coordinates_string);$polygon_arrays_count[$key]=count($coordinates[$key])/2;imagepolygon($im, $coordinates[$key], $polygon_arrays_count[$key] , $black);} $content = file_get_contents('stairs.dxf');$entities = strchr($content, "ENTITIES");$endsec = strchr($entities, "ENDSEC", true);$entitie_string = $endsec;$entities_array = array("LWPOLYLINE");$lastPos = 0;$positions_entities = array();foreach($entities_array as $key_needle =>$entitie){while (($lastPos = strpos($entitie_string, $entitie, $lastPos))!== false) }$array_keys_positions = array_keys($positions_entities);$sizeof_array_positions = sizeof($array_keys_positions);for($i=0; $i< $sizeof_array_positions; $i++){if(isset($array_keys_positions[$i+1])) else }foreach( $entities_string_array as $key_ent =>$value_ent){foreach( $value_ent as $key_val =>$value_e ) }$lwpolyline_array = $explode_value_e["LWPOLYLINE"];$k = 0;foreach( $lwpolyline_array as $lw_key => $explode_key ){foreach( $explode_key as $key => $lwpolyline_value){if( $lwpolyline_value == '10' ) else if( $lwpolyline_value == '20' ) }$implode_coordinates_db[$k] = implode(';',$coordinates_db);$k++;}$width = max($coordinates_x) - min($coordinates_x);$height = max($coordinates_y) - min($coordinates_y);function image_create( $image_array ) ob_start();imagepng($im);$buffer = ob_get_clean();//ob_end_clean();$img = base64_encode($buffer);imagedestroy($im);return $img;}$image_array = array($implode_coordinates_db, $width, $height);$image_base64 = image_create($image_array);$image_src='data:image/png;base64,'.$image_base64;echo "  ";