简体   繁体   中英

Get image from SQL Table

I'm using SobiPro on my Joomla 3.x website and I have a custom field, in my entries, which is an image, it is stored in my table named XXXX_sobipro_field_data I have a fid (field id) which is equal to 23 for the Image field. But the data looks like this for every entries: jVFNT8JAEL37KzZ70kQp/US34WCgIEQ+AjUeyULXdgjtVrqgaPrf3d1WRA/E27438+a9maXEJZ8FcQiOqKDYp8SsMXuHF4XtGvcGj4HCnsK3EsOGjWnKsF8Qs0mway5gxRvrPNaMVXV0qWAhqC4gpmO2POuu5dj+ccIcPqqarPyw4SHXbE2N5ICKqqwgpTEz1jmrrGyC52wlgGdFj++ySJEtgjuT0SgYh9gv9RAJp09h0FU7OPVOiUg3qtuSad8gEkkbezZGCYM4Efqtqh7BD5pRkTwV3iX4WbUfCWk3KDp8w7d6GWV5mkBfFUhTSRU/C+7DyYygOLpRW6C92Wiiy10BWYwGwz4aToM+2nvW1TV63dENiANqo5Z7IXcpS22vT6DCOd/3KIyCLyHfcoNlYgsSu64Babz4/TFSK5JdutRa65xWt/1Ry1NL9A/fFf+jlD/AtxBDRvXFbe+c/FRafgE=
But I need to display the image, how am I supposed to deal with such a thing ?

EDIT

I did this : (It works but only with .png images)

<?php
$sid = $_GET["id"];
$db = mysql_connect("localhost", "admin", "root")
    or die("Impossible de se connecter : " . mysql_error());
mysql_select_db('database_name',$db); 

$sql = 'SELECT baseData, sid, fid FROM XXXX_sobipro_field_data WHERE sid = ' . $sid; 
$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()); 

while($data = mysql_fetch_assoc($req)) 
{ 
    if ($data["fid"] == 22) {
        $title = $data["baseData"];
    }
    if ($data["fid"] == 19) {
        $desc = $data["baseData"];
    }
    if ($data["fid"] == 17) {
        $activity = $data["baseData"];
    }
    if ($data["fid"] == 23) {
        $enc_image = $data["baseData"];
        $dec_image = unserialize(base64_decode($enc_image));
    }
}
?>
<p>
  <?php 
    echo "<span class=\"img\"><img src=\" ".$dec_image['original']." \" alt=\"\"> </img>  </span>";
  ?>
    <span class="title"><?php echo $title; ?></span>
    <br />
    <span class="activity"><?php echo $activity; ?></span>
    <br />
    <span class="desc"><?php echo $desc; ?></span>
</p>
<?php 
  mysql_close($db);
?>

I was also looking for the same answer... eventually had to figure out by myself.

In short there are 2 options you have to check:

  1. for some shorter strings (which get encoded) just use base64_decode($str)
  2. for longer strings use gzinflate(base64_decode($str))

A longer version is available in file "/com_sobipro/lib/base/config.php" in function:

public static function unserialize( $var, $name = null )
{
    $r = null;
    if ( is_string( $var ) && strlen( $var ) > 2 ) {
        if ( ( $var2 = base64_decode( $var, true ) ) ) {
            if ( function_exists( 'gzinflate' ) ) {
                if ( ( $r = @gzinflate( $var2 ) ) ) {
                    if ( !$r = @unserialize( $r ) ) {
                        throw new SPException( sprintf( 'Cannot unserialize compressed variable %s', $name ) );
                    }
                }
                else {
                    if ( !( $r = @unserialize( $var2 ) ) ) {
                        throw new SPException( sprintf( 'Cannot unserialize raw (?) encoded variable %s', $name ) );
                    }
                }
            }
            else {
                if ( !( $r = @unserialize( $var2 ) ) ) {
                    throw new SPException( sprintf( 'Cannot unserialize raw encoded variable %s', $name ) );
                }
            }
        }
        else {
            if ( !( $r = @unserialize( $var ) ) ) {
                throw new SPException( sprintf( 'Cannot unserialize raw variable %s', $name ) );
            }
        }
    }
    return $r;
}

Following is the function which does the encoding:

public static function serialize( $var )
{
    if ( !( is_string( $var ) ) && ( is_array( $var ) && count( $var ) ) || is_object( $var ) ) {
        $var = serialize( $var );
    }
    if ( is_string( $var ) && function_exists( 'gzdeflate' ) && ( strlen( $var ) > 500 ) ) {
        $var = gzdeflate( $var, 9 );
    }
    if ( is_string( $var ) && strlen( $var ) > 2 ) {
        $var = base64_encode( $var );
    }
    return is_string( $var ) ? $var : null;
}

Hope this helps.

Your string decoded looks like this:

a:5:{s:4:"data";a:1:{s:4:"exif";a:3:{s:4:"FILE";a:6:{s:8:"FileName";s:10:"51_ico.jpg";s:12:"FileDateTime";i:1417629743;s:8:"FileSize";i:1762;s:8:"FileType";i:2;s:8:"MimeType";s:10:"image/jpeg";s:13:"SectionsFound";s:7:"COMMENT";}s:8:"COMPUTED";a:4:{s:4:"html";s:22:"width="63" height="63"";s:6:"Height";i:63;s:5:"Width";i:63;s:7:"IsColor";i:1;}s:7:"COMMENT";a:1:{i:0;s:57:"CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), quality = 75 ";}}}s:5:"image";s:40:"images/sobipro/entries/55/img_51_ico.jpg";s:5:"thumb";s:42:"images/sobipro/entries/55/thumb_51_ico.jpg";s:3:"ico";s:40:"images/sobipro/entries/55/ico_51_ico.jpg";s:8:"original";s:36:"images/sobipro/entries/55/51_ico.jpg";}

Fixed this problem.

$entries[$key]['field_avatar'] = $entry->get('field_avatar');

// Encrypt Image
$enc_image = $entries[ $key ]['field_avatar'];

// Decrypt image 
**$dec_image = SPConfig::unserialize( $enc_image );**

Use this for image type field data:

    $img_data = unserialize(gzinflate(base64_decode( $sql_result)));

You'll get an array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM