Jump to content

images to emmbeded images this src not working


antonyfal
Go to solution Solved by antonyfal,

Recommended Posts

Hi there.

I am busy creating a php imap app for my site. It was working untill Mozilla changed its policies. No i get this error:

 

Loading mixed (insecure) display content on a secure page "http://hi5.taggedmail.com/imgsrv.php?sz=1&uid=5458308947"

 

div.innerHTML = wrap[1] + elem + wrap[2];

 

 

What i am doing now is to fetch the image info such as its height width style, then convert the image to a embedded image and return it to its original spot with its original dimensions and style. However i am having problems with image src's that look like the following example:

<img src ="http://hi5.taggedmail.com/imgsrv.php?sz=1&uid=5458308947" />// my regex either
//doesn't pick it up, or cannot pull the image from this link..
 
where as a src link example like the following i have no problems, however it only does the first link
<img src ="http://x.tagstat.com/im/headers/default/hi5_logo_small_01.png" />
 
 
here is my code:
if(preg_match('/<img[^>]+>/i', $msgBody)){
    preg_match_all('/<img[^>]+>/i', $msgBody, $matches);
    $i = 1;
    $nbody ='';
$imageinfo = array();
    foreach ($matches[0] as $img)
    {
      $id = 'img_'.($i++);
    preg_match_all('/(src|style)=("[^"]*")/i',$img, $imageinfo[$img]);    
    $src= str_replace('"', '', $imageinfo[$img][2][0]);
    $style = str_replace('"', '', $imageinfo[$img][2][1]);    
    if($Style !='')
    $nstyle =' style="border: none;'.$style.'"';
    else
    $nstyle=' style="border: none;"';
    
    if(!preg_match('/width/i', $nstyle, $iswidth)){
    preg_match_all('/(width)=("[^"]*")/i',$img, $imageinfo[$img]);    
    $width= str_replace('"', '', $imageinfo[$img][2][0]);
    if($width !='')
    $nwidth = ' width="'.$width.'"';    
    }
    
    if(!preg_match('/height/i', $nstyle, $isheight)){
    preg_match_all('/(height)=("[^"]*")/i',$img, $imageinfo[$img]);    
    $height= str_replace('"', '', $imageinfo[$img][2][0]);
    if($height !='')
    $nheight = ' height="'.$height.'"';    
    }
    
    $type = pathinfo($src, PATHINFO_EXTENSION);
    $imgData = base64_encode(file_get_contents($src));
    $nnsrc = 'data:image/' . $type . ';base64,'.$imgData;
   
    $nbody .= str_replace($img, '<img id="'.$id.'" src="'.$nnsrc.'"'.$nwidth.$nheight.$nstyle.'/>', $msgBody);
    }
    } else {
    $nbody = $msgBody;
    }
 
$_SESSION['MessageHTML'] = $nbody;
 
 

Edited by antonyfal
Link to comment
Share on other sites

UPDATE:

 

if i use file get_contents on this link:  http://hi5.taggedmail.com/imgsrv.php?sz=1&uid=5458308947

 

i get this output when i echo it:

 

JFIFOcad$Rev: 14797 $ %# , #&')*)-0-(0%()( @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@xx" !"1AQa#q3r2BR$&Db1A2Qa! ?kEP6@ZC@ͻV'Iy,s"n!W]*yspo;V(6 6wsnn0̓r7nPѝ$t Dkr^8.jvMPD.0B N[$}7<ppzzF8R^)~폰~[W`RU!qďXَ`wE <eöVz+F;\ۇL]ܗe975;sP@IIfcTscx#)˧9WxvDfϏWqi/,Jc ُ̃z5g*#ҙM_NZ/wkZ\[W[pr:(6=[ n`QȍM pHM&n2=X17w_4=l qQ5:&.wR1ֹbx$4ؾ7SDxh:Q}/ᚓN8lg !flw8~zCqRs#ilQ9q!a&9kFM]G?htUuc8itR>%kaWu 7',,ƨ U4ƨUrMZjȴ[v:vB:Y(FbdƢ9š+3 pcp月=$/j:H?@\Ny-@Er ߲:.G67jtm1>9RUd]*O77)7/W6BB HzZjWd-Nr\9st ԑԐoM`P,D69!\[$t՘5$]^.-cH1PX|MO c_<V%pgsMTbQvDo` J:|\8"ː:/0QO(6{Yz!nnSyy-6Kޮ-fWӞM~:tFosͥG7?"P-r4nD>`joqR;Whˣ>I $fG5UDsk]X_ԅd~S0$aI-^ I>GBZ86F*p$ƅn@Fd{oq̑ȩC0üV睎^f(bP9t j9Ezk9T_)Kٺ%mJ":`cf8|rX*uMc~__ZV֖T05wUA,ek3,@Å~o7q)g4f<8nA[a`G#N@:M ߞS sSKuF(o;Н1$)M(GqJgFJIx?d{ u-laICqH2S2lLF+Œmssnnܪap9*ߖ6Y(9>Fj٭+f\(xs\;o?IcE0vNc`&hȠqȊN Ay޽B{fO۔a1vo 4ԭQt*ӷ}®F<Ыdu-nۼ`ԿP994 #Y
 
 
i just want to get this info <img height="" width="" style="" src="">
 
anyone have any ideas?
Edited by antonyfal
Link to comment
Share on other sites

Try (not tested), it should work

<?php
$string = 'Content-Type: image/jpeg'; 
header($string);
$filename = 'http://hi5.taggedmail.com/imgsrv.php?sz=1&uid=5458308947';
$pic = file_get_contents($filename); 
echo $pic;

Link to comment
Share on other sites

OK! so the code above didn't work for my needs, its only to display the image.

I now know that what i have to use is Curl. I have this function which returns the above link as an array of the page at the end of the link.. For the email containing the link above this works fine, however i am looking for a more universal solution to my issue..

 

 

 

function get_web_page( $url )
{
    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_USERAGENT      => "spider", // who am i
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    );

    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = $content;
    return $header;
}
 
 
    $filename = 'http://hi5.taggedmail.com/imgsrv.php?sz=1&uid=5458308947';
    $pic = get_web_page($filename);
$srcs = array();
    foreach($pic as $k =>$v){
if($k == 'url')
$newsrc = $v;
    print_r($newsrc);
    }
    return;
  
 

Link to comment
Share on other sites

UPDATE:

 

i have this little function that works on any type of link... the only problem im having now is that is only changes the first image it finds, and doesnt itterate through all the images and changes them.. Can someone see the error with my code?

if(preg_match('/<img[^>]+>/i', $msgBody)){
    preg_match_all('/<img[^>]+>/i', $msgBody, $matches);    
    $i = 1;
$imageinfo = array();
    foreach ($matches[0] as $img)
    {
      $id = 'img_'.($i++);
    preg_match_all('/(src|style)=("[^"]*")/i',$img, $imageinfo[$img]);    
    $src= str_replace('"', '', $imageinfo[$img][2][0]);
    
    if (preg_match('/(\.jpg|\.png|\.bmp|\.gif|\.jpeg)$/', $src)) {
    preg_match_all('/(style)=("[^"]*")/i',$img, $imageinfo[$img]);    
    $style = str_replace('"', '', $imageinfo[$img][2][0]);    
    if($Style !=''){
    $nstyle =' style="border: none;'.$style.'"';
    } else {
    $nstyle=' style="border: none;"';
    }
    
    if(!preg_match('/width/i', $nstyle, $iswidth)){
    preg_match_all('/(width)=("[^"]*")/i',$img, $imageinfo[$img]);    
    $width= str_replace('"', '', $imageinfo[$img][2][0]);
    if($width !=''){
    $nwidth = ' width="'.$width.'"';
    }    
    }
    
    if(!preg_match('/height/i', $nstyle, $isheight)){
    preg_match_all('/(height)=("[^"]*")/i',$img, $imageinfo[$img]);    
    $height= str_replace('"', '', $imageinfo[$img][2][0]);
    if($height !=''){
    $nheight = ' height="'.$height.'"';    
    }
    }
    
    $type = pathinfo($src, PATHINFO_EXTENSION);
    $imgData = base64_encode(file_get_contents($src));
    $fname =basename($src);
    //$msgBody = str_replace($img, '<img id="'.$id.'" src="data:image/' . $type . ';base64,'.$imgData.'"'.$nwidth.$nheight.$nstyle.'/>', $msgBody);
    } else {
    $pic = get_web_page($src); //this function returns an array of the image from this type of url:
//http://hi5.taggedmail.com/imgsrv.php?sz=1&uid=5458308947
    foreach($pic as $k =>$v){  //so here its suposed to run throught the $msgBody and find all images that have this type of link.. and then replace the images in the //image body.. however it does not work, and when it does it only changes the first one it comes accross..
    $type = pathinfo($pic['url'], PATHINFO_EXTENSION);
    $imgData = base64_encode(file_get_contents($pic['url']));
    $fname =basename($pic['url']);
    $nwidth ='';
    $nheight ='';
    $nstyle='';
    }    
    }
    $msgBody = str_replace($img, '<img id="'.$id.'" src="data:image/' . $type . ';base64,'.$imgData.'"'.$nwidth.$nheight.$nstyle.'/>', $msgBody);
    }
$_SESSION['MessageHTML'] = $msgBody;
    } else {    
$_SESSION['MessageHTML'] = $msgBody;
    }
 

 

Regards Antony..

Edited by antonyfal
Link to comment
Share on other sites

  • Solution

RESOLVED IT: if anybody want a function to retrieve all images in a imap html message  and convert them to embedded images before they display, so as to avoid Mozilla's warnings regarding http pages on a https domain, here is the code. This code will retrieve any image src, even if the src does not contain a mime type.. IE: http://hi5.taggedmail.com/imgsrv.php?sz=1&uid=5458308947

 

// here is the curl function to collect the info on the image src url:
function get_web_page($url)
{
    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_USERAGENT      => $_SERVER["HTTP_USER_AGENT"], // who am i
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    );

    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = $content;
    return $header;
}
 
 
 
 
// here is the complete use of the function that will take all the src of the images regardless weather the image has a basename or is a php or html link, it will get the file source and convert it to a base64 embedded image and put it back in its original place.. NOTE: i use output not echo you can change that to your needs..
 
if(preg_match_all('/<img[^>]+>/i', $msgBody, $matches)){
    $i = 1;
$imageinfo = array();
    foreach ($matches[0] as $img)
    {
      $id = 'img_'.($i++);
      
     preg_match('/src="(.*?)"/', $img, $m);
     $src1 = str_replace('src=','',$m[0]);
     $src = str_replace('"','',$src1);
     if (preg_match('/(\.jpg|\.png|\.bmp|\.gif|\.jpeg)$/', $src)) { // style and height width attributes are only necessary
//for already displayed inline images with correct or direct url's
    preg_match_all('/(style)=("[^"]*")/i',$img, $imageinfo[$img]);    
    $style = str_replace('"', '', $imageinfo[$img][2][0]);    
    if($Style !='')
    $nstyle =' style="border: none;'.$style.'"';
    else
    $nstyle=' style="border: none;"';    
    
    if(strpos($nstyle,'width')===0 ){
    preg_match('/(width)=("[^"]*")/i',$img, $imageinfo[$img]);    
    $width= str_replace('"', '', $imageinfo[$img][2][0]);
    if($width !='')
    $nwidth = ' width="'.$width.'"';    
    }    
    if(strpos($nstyle,'height')===0 ){
    preg_match('/(height)=("[^"]*")/i',$img, $imageinfo[$img]);    
    $height= str_replace('"', '', $imageinfo[$img][2][0]);
    if($height !='')
    $nheight = ' height="'.$height.'"';    
    }    
    
    $type = pathinfo($src, PATHINFO_EXTENSION);
    $imgData = base64_encode(file_get_contents($src));
    $fname = basename($src);
    $msgBody = str_replace($img, '<img id="'.$id.'" src="data:image/' . $type . ';base64,'.$imgData.'"'.$nwidth.$nheight.$nstyle.'/>', $msgBody);
    } else {    
    $pic = get_web_page($src);
    if(!preg_match('/(\.jpg|\.png|\.bmp|\.gif|\.jpeg)$/', $pic['url'])) {
    $pic = get_web_page($pic['url']);
    }
    $type = pathinfo($pic['url'], PATHINFO_EXTENSION);
    $imgData = base64_encode(file_get_contents($pic['url']));
    $fname = basename($pic['url']);
    $msgBody = str_replace($img, '<img id="'.$id.'" src="data:image/' . $type . ';base64,'.$imgData.'"/>', $msgBody);
    }    
    }
$_SESSION['MessageHTML'] = $msgBody;
    } else {    
$_SESSION['MessageHTML'] = $msgBody;
    }
 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.