MySQL Forums
Forum List  »  PHP

Re: how to show an image from mysql table
Posted by: Peter Brawley
Date: December 17, 2015 11:19AM

The mysql API is deprecated and will go away. Use the mysqli API.

We often use this general code to make url & image links clickable ...

function urlactivate( $text ) {
  $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
  $text = preg_replace( "#(?<!://)www.#", "http://www.";, $text); 
  preg_match_all( $reg_exUrl, $text, $matches );
  $usedPatterns = array();
  foreach( $matches[0] as $pattern ) {
    if( !array_key_exists( $pattern, $usedPatterns ) ) {
      $usedPatterns[$pattern] = true;
      if( isImage($pattern) )
        $text = str_replace($pattern, '<img width=100% src="'.$pattern.'">', $text);   
      else 
        $text = str_replace($pattern, '<a href="'.$pattern.'">'.$pattern.'</a>', $text);
    }
  }
  return $text;
}
  
function isImage( $url ) {
  return stripos( "|.gif|.jpg|.png|.tif|", substr($url,-4) ) 
         || stripos( "|.jpeg|.tiff|", substr($url,-5) );
}

function isImage_AllCases( $url ) {
  $params = array( 'http' => array( 'method' => 'HEAD' ) );
  $ctx = stream_context_create( $params );
  $fp = @fopen( $url, 'rb', false, $ctx );
  if( !$fp ) return false;  // Unavailable url
  $meta = stream_get_meta_data($fp);
  if( $meta === false ) {
    fclose($fp);
    return false;  // Problem reading data from url
  }
  $wrapper_data = $meta["wrapper_data"];
  if( is_array($wrapper_data) ) {
    foreach(array_keys($wrapper_data) as $hh){
      if( substr($wrapper_data[$hh], 0, 19) == "Content-Type: image" ) {
        fclose($fp);
        return true;
      }
    }
  }
  fclose($fp);
  return false;
}

IsImage_AllCases() is more correct but too slow for more than a couple of items.



Edited 1 time(s). Last edit at 12/17/2015 11:24AM by Peter Brawley.

Options: ReplyQuote


Subject
Written By
Posted
Re: how to show an image from mysql table
December 17, 2015 11:19AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.