I have a problem with the mpdf library.
When I insert a img tag the library I received error 500.
This is my code:
<?php
$tabla = $this->load->view("prueba",$data , true);
$this->load->library('mpdf');
//size
$mpdf = new mPDF('utf-8','Letter',0,0,10.1,10.1,32,29,13.2,13.2);
$mpdf->showImageErrors = true;
$stylesheet = file_get_contents('assets/css/bootstrap/css/bootstrap.min.css');
$mpdf->WriteHTML($stylesheet,1);
$mpdf->SetHTMLHeader('<div class="cabecera"> <img src="assets/img/asd.png"></div>');
$mpdf-> WriteHTML ($tabla,2);
$mpdf-> SetHTMLFooter('<div class="footer"> <span> some text!</span></div>');
$mpdf->Output('files/daniel.pdf','F');
$this->response("True", 200);
?>
If I do that even without the tag IMG everything works fine.
Help me please.
with mpdf I use base64 and it works well. Like:
<img alt="logo" width="298" height="108" src="data:image/png;base64,{base64 of the image}>" />
To get convert a picture in base64 do:
base64 filename.png
Related
I'm reading a webpage using PHP DOM/XPath and I've managed to get the text I need, but now I'm trying to get the src of the main image but I can't get it.
Also to complicate things, the source is different to the inspector.
Here is the source:
<div id="bg">
<img src="https://example.com/image.jpg" alt=""/>
</div>
And here is the element in the inspector:
<div class="media-player" id="media-player-0" style="width: 320px; height: 320px; background: url("https://example.com/image.jpg") center center / cover no-repeat rgb(208, 208, 208);" currentmouseover="16">
I've tried:
$img = $xpath->evaluate('substring-before(substring-after(//div[#id=\'bg\']/img, "\')")');
and
$img = $xpath->evaluate('substring-before(substring-after(//div[#class=\'media-player\']/#style, "background: url(\'"), "\')")');
but get nothing from either.
Here is my complete code:
$html = file_get_contents($externalurl);
$doc = new DOMDocument();
#$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$allChildNodesFromDiv = $xpath->query('//h1[#class="artist"]');
$releasetitle = $allChildNodesFromDiv->item(0)->textContent;
echo "</br>Title: " . $releasetitle;
$img = $xpath->evaluate('substring-before(substring-after(//div[#class=\'media-player\']/#style, "background: url(\'"), "\')")');
echo $image;
$img = $xpath->evaluate('substring-before(substring-after(//div[#id=\'bg\']/img, "\')")');
echo $image;
Not something I would normally suggest, but as the particular content you are after is loaded from javascript, BUT the content is in <script> tags, then it may be an easy one for a regex to extract. From your comment...
Ah yes, it appears in: poster :
'https://284fc2d5f6f33a52cd9f-ce476c3c56a27f320262daffab84f1af.ssl.cf3.rackcdn.com/artwork_5e74a44e1e004_CHAMPDL879D_5e74a44e4672b.jpg'
So this code looks the value of poster : '...',.
$html = file_get_contents($externalurl);
preg_match("/poster : '(.*)',/", $html, $matches);
echo $matches[1];
This can be prone to changes in the html, but it may work for now.
I downloaded this code:
$image = ImageClass::getImage('bg.jpeg','myTitle');
$bg_img = explode(" ",$image);
$src = substr(strpos('"',$bg_img),strlen($bg_image)-1);
echo "<div style='background-image: url(".$src.");' ></div>
<?php
/*
*** OPTIONS ***/
// TITLE OF PAGE
$title = "ARQUIVOS PROPAR";
// STYLING (light or dark)
$color = "dark";
// ADD SPECIFIC FILES YOU WANT TO IGNORE HERE
$ignore_file_list = array( ".htaccess", "Thumbs.db", ".DS_Store", "index.php", "flat.png", "error_log" );
// ADD SPECIFIC FILE EXTENSIONS YOU WANT TO IGNORE HERE, EXAMPLE: array('psd','jpg','jpeg')
$ignore_ext_list = array( );
// SORT BY
$sort_by = "name_asc"; // options: name_asc, name_desc, date_asc, date_desc
// ICON URL
//$icon_url = "https://www.dropbox.com/s/lzxi5abx2gaj84q/flat.png?dl=0"; // DIRECT LINK
$icon_url = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA+gAAAAyCAYAAADP7vEwAAAgAElEQVR4nOy9d5hdV3nv";
// TOGGLE SUB FOLDERS, SET TO false IF YOU WANT OFF
$toggle_sub_folders = true;
// FORCE DOWNLOAD ATTRIBUTE
$force_download = true;
// IGNORE EMPTY FOLDERS
$ignore_empty_folders = false;
// SET TITLE BASED ON FOLDER NAME, IF NOT SET ABOVE
if( !$title ) { $title = clean_title(basename(dirname(__FILE__))); }
?>
Th full code can be download here: https://github.com/halgatewood/file-directory-list/blob/master/index.php
I'm having problem with the start:
$image = ImageClass::getImage('bg.jpeg','myTitle');
$bg_img = explode(" ",$image);
$src = substr(strpos('"',$bg_img),strlen($bg_image)-1);
echo "<div style='background-image: url(".$src.");' ></div>
I want to put a picture as background, but it isn't happening. What's wrong?
Changed with the answer:
<?php
echo "<div style='background-image: url('/bg.jpeg');' ></div>";
?>
<?php
/*
*** OPTIONS ***/
// TITLE OF PAGE
$title = "ARQUIVOS PROPAR";
// STYLING (light or dark)
$color = "dark";
etc..
No need for all that,
What you desire to achieve is much simpler.
Assuming this code is inside index.php and your server's directory structure:
/some-folder/
/index.php
/bg.jpeg
Simply link it as its done in plain html —
<?php
echo "<div style=\"background-image: url('/bg.jpeg');\" ></div>";
?>
If you wan't it do be dynamic, i.e, image files's name is inside a variable then,
<?php
$my_image = 'bg.jpeg';
echo "<div style='background-image: url($my_image);' ></div>";
?>
Update:
Important Tip: All programming languages are executed line-by-line, this tip applies not only to PHP, but also HTML Learn More
Assume for example, your page's html structure returned to the browser is as provide below and you want to apply background to body tag
<html>
<head><head>
<body>
<nav>Some dummy navigation</nav>
<div>welcome to my website</div>
<footer>Copyright</footer>
</body>
</html>
Simply copying and pasting my code to the top of page will result in
<div style="background-image: url('bg.jpeg');" ></div>
<html>
<head><head>
<body>
<nav>Some dummy navigation</nav>
<div>welcome to my website</div>
<footer>Copyright</footer>
</body>
</html>
But that created a empty div tag at the top of html output, i wanted it to apply background to by body tag instead !!!?
— This happened because echo is used to send output to the browser as soon as it is executed. So since you copied my code to the top of your script the html output is also at the top.
But why did it echo <div style="background-image: url('bg.jpeg');" ></div> when i wanted it to apply to my page's body?
— Because the echo statements reads "<div style=\"background-image: url('bg.jpeg');\" ></div>"; as its output.
Ok, but how to apply the background-image to body then??
As mentioned earlier code is executed line-by-line, so in order to apply the style to pages's body tag you'll need to call it near your body tag and also modify it to not output the div it currently does.
So assuming your index.php is:
<?php
$my_image = 'bg.jpeg';
echo "<div style='background-image: url($my_image);' ></div>";
?>
<html>
<head><head>
<body>
<nav>Some dummy navigation</nav>
<div>welcome to my website</div>
<footer>Copyright</footer>
</body>
</html>
You'll need to change it to —
<?php
$my_image = 'bg.jpeg';
// don't echo any thing here
?>
<html>
<head><head>
<body style="background-image: url('<?php echo $my_image; ?>')">
<!-- apply the style to body -->
<nav>Some dummy navigation</nav>
<div>welcome to my website</div>
<footer>Copyright</footer>
</body>
</html>
Hopefully i explained it well :)
i try to replace all img src that not contain full url with full image url
example like this
<?php
$html_str = "<html>
<body>
Hi, this is the first image
<img src='image/example.jpg' />
this is the second image
<img src='http://sciencelakes.com/data_images/out/14/8812836-green-light-abstract.jpg' />
and this is the last image
<img src='image/last.png' />
</body>
</html>";
?>
and when replace became like this
<?php
$html_str = "<html>
<body>
Hi, this is the first image
<img src='http://example.com/image/example.jpg' />
this is the second image
<img src='http://sciencelakes.com/data_images/out/14/8812836-green-light-abstract.jpg' />
and this is the last image
<img src='http://example.com/image/last.png' />
</body>
</html>";
?>
so how to check every img src that not full link and replace it ? ( the $html_str is dynamic based on mysql )
please give me some solution for this problem
thanks
I'd do it properly using a DOM library, eg
$doc = new DOMDocument();
$doc->loadHTML($html_str);
$xp = new DOMXPath($doc);
$images = $xp->query('//img[not(starts-with(#src, "http:") or starts-with(#src, "https:") or starts-with(#src, "data:"))]');
foreach ($images as $img) {
$img->setAttribute('src',
'http://example.com/' . ltrim($img->getAttribute('src'), '/'));
}
$html = $doc->saveHTML($doc->documentElement);
Demo here - http://ideone.com/4K9pyD
Try this:
You can get image source using following code:
$xpath = new DOMXPath(#DOMDocument::loadHTML($html));
$src = $xpath->evaluate("string(//img/#src)");
After that check string contains http or not. According do the operation.
I want my IP cam streaming on my website.
Since I have this piece of code but if you look at the source code, you'll see of course the complete data as user and pass. That is not the intention, I looked at RewriteRule option in .htaccess but don't know how to formulate, or maybe another solution to protect my user and pass data. Who can help me get out the thinking circle and gives a move in the right direction (or example).
<?php
$url = '123.456.7.890:1234';
$user = 'naam';
$pass = 'wachtwoord';
$cam = "http://$url/videostream.cgi?user=$user&pwd=$pass";
?>
<html>
<body>
...
<img name="main" id="main" border="0" width="640" height="480" src="<?php echo("$cam"); ?>">
...
</body>
</html>
If camera response you with image you can get it in PHP code and then add it to image src like base64 data.
For example
<?php
$url = '123.456.7.890:1234';
$user = 'naam';
$pass = 'wachtwoord';
$cam = "http://$url/videostream.cgi?user=$user&pwd=$pass";
$image = file_get_contents($cam); // Or use CURL
// Here you need to replace $type of your camera
$image_src = 'data:image/' . $type . ';base64,' . base64_encode($image);
?>
<html>
<body>
...
<img name="main" id="main" border="0"
width="640" height="480" src="<?php echo $image_src ?>">
...
</body>
</html>
You should use your server side to create a image.
Solution can be found here: Create Image From Url Any File Type
You can create a PHP file that outputs the image or something.
If you do that, you can load the image like this: <img src="http://domain.com/image.php?id=1">
Then in image.php, you can load from your DB, or whatever you like.
However, when you have the real image info, you can make a "fake" image as descriped in the link above.
I am creating a Google chart in wordpress, chart is rendered and stored into an image. However I am not able to call the image using <img src="" />. Following is the code:
$filepath = "/wp-content/uploads/graph.png";
file_put_contents($filepath, $response);
echo $filepath;
echo "<img src=\"/wp-content/uploads/graph.png\">";
I have also tried with http://*/graph.png which is not working. If I open the same in different browser, image is showing properly.
Try this
echo '<img src="' . get_bloginfo('template_directory') . '/images/logo.gif" />';
Go for 'template_directory' or 'stylesheet_directory'.
You may try the below code;
Much better if you combine your HTML code and PHP code. Much cleaner easy to read.
PHP code
<?php
Try changing your file path to
$filepath = "../../wp-content/uploads/graph.png";
file_put_contents($filepath, $response);
?>
HTML Code
<img src="<?php echo $filepath; ?>"/>