Clipper On Line • Ver Tópico - Gravar imagem tabela MySQL
Página 1 de 1

Gravar imagem tabela MySQL

MensagemEnviado: 22 Nov 2017 10:41
por MSDN
Alguém já gravou uma imagem ( PNG, JPG ) com sucesso em um campo de uma tabela MySQL ?

Abraços

Gravar imagem tabela MySQL

MensagemEnviado: 22 Nov 2017 18:46
por MSDN
Encontrei uma maneira que funciona perfeitamente, segue abaixo o código fonte :
* créditos Fórum Fivewin ( www.fivewin.com.br )

Código para escolher a imagem, mostrar na tela e gerar o conteúdo que será gravado (lembrando que testei usando MySQL)
STATIC FUNCTION F_importar()

   LOCAL varios := .T.   
   LOCAL arq_cas, i, n_for, File_cas, m_rat

   PRIVATE m_ver
   PRIVATE p_GetFile
   
   p_GetFile := iif( empty( p_GetFile ) , GetMyDocumentsFolder() , p_GetFile )

   arq_cas := GetFile ( { ;
      {'Image Files' , '*.JPG;*.PNG;*.GIF;*.ICO'} ,;
      {'JPG Files' , '*.JPG'} ,;
      {'PNG Files' , '*.PNG'}  } ,;
      'Abrir arquivo' , p_GetFile , varios , .t. )

   if len( arq_cas ) = 0
      return nil
   endif

   for n_for := 1 to len( arq_cas )
   
      i = n_for + 1

      if n_for = len(arq_cas) 
         i = 1           
      endif

      File_cas := strtran( arq_cas[ i ] , '\\' , '\' )

      m_rat := rat( '\' , File_cas )
      
      if m_rat # 0
         //repl NOME   with substr( File_cas , m_rat + 1 )
      else
         //repl NOME   with File_cas
      end

      /*
         guarda o conteúdo que será gravado na tabela
         --------------------------------------------
         o tipo do campo foi definido como LongBlob
      */
      _foto_cad_emp := StrToHex( MemoRead( File_cas ) )
      /*
         mostra a imagem escolhida na tela
      */
          Setproperty('Form_Empresa','img_logotipo','picture',File_cas)
   next

   m_rat = rat('\',arq_cas[1])
   p_GetFile := left( arq_cas[1] , m_rat-1 )

   return nil   


Depois de gravada a imagem na tabela MySQL, código para recuperar a imagem e mostrar na tela :
   /*
      recuperar imagem (logotipo) gravada
   */
   IF .NOT. Empty(v_logotipo)
      cLogo := HexToStr(v_logotipo)
      nH    := fCreate('imgtmp.png')
      fWrite(nH,cLogo)
      fClose(nH)         
      v_logo := curdrive()+':\'+curdir()+'\imgtmp.png'
   ENDIF

        /*
             componente imagem (minigui)
        */
        @ 20, 720 IMAGE Img_logotipo PICTURE v_logo WIDTH 260 HEIGHT 260


Abraços

Gravar imagem tabela MySQL

MensagemEnviado: 06 Dez 2017 12:36
por rochinha
Amiguinhos,

Mais funções:

static function SalvaImagem( _imagem_ )
   RETURN str2hex( memoread( _imagem_ ) )

static function RecuperaImagem( _campo_, _imagem_ )
   RETURN memowrit( _imagem_, hex2str( _campo_ ) )


Salvar:
...
dbAppend()
_db->campoimagem := SalvaImagem( "c:\temp\foto.jpg" )
dbSkip()
...


Mostrar:
...
m->imagem := RecuperaImagem( _db->campoimagem, "c:\temp\"+alltrim(str(random(9999999)))+".jpg" )
...
@ SAY ... IMAGEM m->imagem // comando ficticio
...

Gravar imagem tabela MySQL

MensagemEnviado: 20 Jun 2018 18:39
por cjp
Teria como me informar de qual lib é a str2hex? Não estou conseguindo compilar. Será porque estou usando em modo console?

Gravar imagem tabela MySQL

MensagemEnviado: 22 Jun 2018 01:27
por rochinha
Amiguinhos,

Esta aqui no meio:
#pragma BEGINDUMP
#include "hbapi.h"
#include "hbapiitm.h" //#include "hbfast.h"
#include "hbstack.h"
#include "hbdefs.h"
#include "hbvm.h"
#include "hbapierr.h"

HB_ULONG HB_EXPORT hb_hextonum(char *cHex)
{
HB_ULONG ulNum = 0;
char c;
int iDigit;

while ( *cHex && (*cHex == ' ') ) cHex++;

while ( *cHex )
{
ulNum <<= 4;

c = *cHex;
if ( c >= '0' && c <= '9' )
{
iDigit = c - '0';
}
else if ( c >= 'A' && c <= 'F' )
{
iDigit = c - 'A' + 10;
}
else if ( c >= 'a' && c <= 'f' )
{
iDigit = c - 'a' + 10;
}
else
{
ulNum = 0;
break;
}
ulNum += iDigit;
cHex++;
}

return ulNum;
}

HB_FUNC( NUMTOHEX )
{
int iDigit;
char ret[ 33 ];
int iLen, iDefaultLen;
HB_ULONG ulNum;

if( ISNUM( 2 ) )
{
iLen = hb_parni( 2 );
iLen = ( iLen < 1 ) ? 1 : ( ( iLen > 32 ) ? 32 : iLen );
iDefaultLen = 0;
}
else
{
iLen = 32;
iDefaultLen = 1;
}

if( ISNUM( 1 ) )
{
ulNum = hb_parnint( 1 );
}
else if ( ISPOINTER( 1 ) )
{
ulNum = (HB_PTRDIFF) hb_parptr( 1 );
}
else
{
hb_errRT_BASE_SubstR( EG_ARG, 3012, NULL, "NUMTOHEX", 2, hb_paramError( 1 ), hb_paramError( 2 ) );
return;
}

ret[ iLen ] = '\0';
do {
iDigit = (int) ( ulNum & 0x0F );
ret[ --iLen ] = iDigit + ( iDigit < 10 ? '0' : 'A' - 10 );
ulNum >>= 4;
} while ( iDefaultLen ? ulNum > 0 : iLen > 0 );

hb_retc( &ret[ iLen ] );
}

HB_FUNC( HEXTONUM )
{
if( ! ISCHAR(1) )
{
hb_errRT_BASE_SubstR( EG_ARG, 3012, NULL, "HEXTONUM", 1, hb_paramError( 1 ) );
return;
}

hb_retnint( hb_hextonum( hb_parc( 1 ) ) );
}

HB_FUNC( STRTOHEX )
{
char *cOutBuf;
char *cStr;
char *c;
char *cSep = "";
unsigned char ucChar;
ULONG ul, ulLen, ulLenSep = 0;
int iDigit;

if( ! ISCHAR(1) )
{
hb_errRT_BASE_SubstR( EG_ARG, 3012, NULL, "STRTOHEX", 2, hb_paramError( 1 ), hb_paramError( 2 ) );
return;
}

if ( ISCHAR( 2 ) )
{
cSep = hb_parc( 2 );
ulLenSep = hb_parclen( 2 );
}

cStr = hb_parc( 1 );
ulLen = hb_parclen( 1 );
c = cOutBuf = (char*) hb_xgrab( ulLen * 2 + ( ulLen - 1 ) * ulLenSep + 1 );

for( ul = 0; ul < ulLen; ul++ )
{
if ( ulLenSep && ul )
{
memcpy( c, cSep, ulLenSep );
c += ulLenSep;
}

ucChar = (unsigned char) cStr[ ul ];

iDigit = (int) ( ucChar & 0x0F );
c[ 1 ] = iDigit + ( iDigit < 10 ? '0' : 'A' - 10 );

ucChar >>= 4;

iDigit = (int) ucChar;
c[ 0 ] = iDigit + ( iDigit < 10 ? '0' : 'A' - 10 );

c += 2;
}
hb_retclen( cOutBuf, c - cOutBuf );
hb_xfree( cOutBuf );
}

HB_FUNC( HEXTOSTR )
{
char *cOutBuf, *cStr;
char c;
int iByte, iFirst;
ULONG ul, ulLen, ulPos, ulAlloc;

if( ! ISCHAR(1) )
{
hb_errRT_BASE_SubstR( EG_ARG, 3012, NULL, "HEXTOSTR", 1, hb_paramError( 1 ) );
return;
}

cStr = (char *) hb_parc( 1 );
ulLen = hb_parclen( 1 );
ulAlloc = (int) ( ulLen / 2 );
cOutBuf = (char *) hb_xgrab( ulAlloc + 1 );

ulPos = 0;
iByte = 0;
iFirst = 1;

for ( ul = 0; ul < ulLen; ul++ )
{
iByte <<= 4;

c = *cStr++;
if ( c >= '0' && c <= '9' )
{
iByte += c - '0';
}
else if ( c >= 'A' && c <= 'F' )
{
iByte += c - 'A' + 10;
}
else if ( c >= 'a' && c <= 'f' )
{
iByte += c - 'a' + 10;
}
else
{
continue;
}

iFirst ^= 1;
if ( iFirst )
{
cOutBuf[ ulPos++ ] = (char) iByte;
iByte = 0;
}
}
hb_retclen( cOutBuf, ulPos );
hb_xfree( cOutBuf );
}

#pragma ENDDUMP


Sobre str2hex():
Visite str2hex

Sobre funções:
Visite Funções por Categoria