I have a php code that will extract and retrieve all the images in a website. How do I modify the code so that the dimensions(width and height) of the images are shown as well?
This is the php coding:
<?php
$page_title = "MiniCrawler";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title><?php print($page_title) ?></title>
</head>
<body>
<?php
ini_set('display errors',1);
error_reporting(E_ALL|E_STRICT);
Include simple_html_dom.php.
include_once ('simple_html_dom.php');
// Add the url of the site you want to scrape.
$target_url = "http://www.alibaba.com/";
// Let simple_html_dom do its magic:
$html = new simple_html_dom();
$html->load_file($target_url);
// Loop through the page and find everything in the HTML that begins with 'img'
foreach($html->find('img') as $link){
echo $link->src."<br />";
echo '<img src ="'. $link->src.'"><br />';
}
?>
</body>
</html>
Thanks
First you would have to check, if the $link->src string already has the domain name at the beginning:
<?php
if(substr($link->src, 0, 4) == "http"){
// url already complete
$path = $link->src;
}else if(substr($link->src, 0, 1) == "/"){
// path starts absolute
$path = $target_url . $link->src;
}else{
// path starts relative -> http://stackoverflow.com/questions/4444475/transfrom-relative-path-into-absolute-url-using-php
}
?>
Then: Request the files dimensions via the getimagesize() function.
<?php
list($width, $height, $type, $attr) = getimagesize($path);
echo '<img src ="'. $link->src.'" width="' . $width . '" height="' . $height . '"><br />';
?>
Related
I've been using simplexml_load_file to parse a XML URL, however, the file size is above 100mb and instead of loading only the nodes, what's happening is that the script is loading the whole XML file before the nodes are extracted and parsed, what is resulting in a page TimeOut.
I'm using the following code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Boutique</title>
</head>
<body>
<?php header ('Content-type: text/html; charset=UTF-8'); ?>
<!-- --><link rel="stylesheet" href="/va/artigos-complexos/afilio/afilio-vitrine.css" type="text/css" />
<div><p class="ofertasdotopovitrine">ConheƧa nossas super ofertas!</p>
</div>
<div class="mainproductebayfloatright-bottom">
<?php
function parse($url, $offset = 1, $limit = -1)
{
$xml = simplexml_load_file($url);
$limitCriteria = '';
if ($limit > 0) {
$limitCriteria = 'and position() <= ' . ((int)$offset + (int)$limit + 1);
}
$products = array();
$path = sprintf('//produto[position() >= %s %s]', (int)$offset, $limitCriteria);
foreach($xml->xpath($path) as $product) {
$products[] = array(
'nome' => $product->nome,
'preco_promocao' => $product->preco_promocao,
'description' => $product->descricao,
'link_imagem' => $product->link_imagem,
'link_produto' => $product->link_produto,
'preco_normal' => $product->preco_normal,
'parcelas' => $product->parcelas,
'vl_parcelas' => $product->vl_parcelas
);
}
return $products;
}
//XML GOES HERE
$products = parse('http://v2.afilio.com.br/aff/aff_get_boutique.php?boutiqueid=37930-895987&token=53e355b0a09ea0.74300807&progid=1010&format=XML', 5, 5);
?>
<?php
foreach ($products as $product) {
print '<div class="aroundebay"><div id="aroundebay2">';
/* */
print '<div class="titleebay"><a target="_blank" rel="nofollow" href="'. $product['link_produto'] . '">' . $product['nome'] . '"</a></div>';
print '<div class="mainproduct"><a target="_blank" rel="nofollow" href="' . $product['link_produto'] . '"><img style="height:120px" src="' . $product['link_imagem'] . '"/><br/>';
//print "De:; R$". $preco_normal . "<br/>";
print '<span>Apenas R$' . $product['preco_promocao'] . '<br/></a></span></div>';
//print "Em " . $parcelas . "x de : R$" . $vl_parcelas . "</a></span></div>";
print '</div></div>';
}
?>
</div>
</body>
</html>
The CSS is irrelevant.
The script works just fine when you use a smaller XML, such as this one:
http://v2.afilio.com.br/aff/aff_get_boutique.php?boutiqueid=37930-895835&token=53e355b0a09ea0.74300807&progid=1681&format=XML
Would it be possible to load only the, for exemplo, 10 first nodes of the xml without having to load the whole file first?
I'm also accepting suggestions in other languages, such as jQuery.
Thanks in advance. You can also change the file format to JSON and RSS, just change format=XML to format=JSON or format=RSS.
I have issue to redirect another location after fopen() function in php. below is my function which i m using.
<?php
function create_file($filename){
$my_file = 'folder/index.php';
$fh = fopen($my_file, "wb");
$data = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Some data</title>
</head>
<body>Here some data</body>
</html>';
fwrite($fh, $data);
fclose($fh);
return true;
ob_end_clean();
exit();
}
$file = create_file(test);
if($file == true){
$url = 'http://example.com';
return $url;
}
else{
return 0;
}
?>
If you want to redirect in PHP, you cannot send any output to the server before header("Location: http://example.com");.
This includes any HTML, text or white spaces that aren't wrapped in a PHP tag set.
The header function must be called before any page output (if HTML has already been displayed, it's too late for your headers!).
I have been racking my brains over this for hours, I need to pass some html to a function and have it replace the links and the return the html with the replaced links.
<?php
final static public function replace_links($campaign_id, $text) {
$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
if(preg_match_all("/$regexp/siU", $text, $matches, PREG_SET_ORDER)) {
foreach($matches as $match) {
if(substr($match[2], 0, 2) !== '##') { // ignore the placeholders
if(substr($match[2], 0, 6) !== 'mailto') { // ignore email addresses
// $match[2] = link address
// $match[3] = link text
$url = "http://xxx.com/click?campaign_id=$campaign_id&email=##email_address##&next=" . $match[2];
#$text .= str_replace($match[2], $url, $text);
#echo $links . "\n";
preg_replace($match[2], "<a href='$url'>{$match[3]}</a>", $match[2]);
}
}
return $text;
}
}
}
?>
When I echo the links it shows all the matched links. Question is how do i return the complete HTML with the replaced links example below.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
xxxx
xxxx
xxxx
</body>
</html>
Should become:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
xxxx
xxxx
xxxx
</body>
</html>
Hope this makes sense.
Thanks in advance,
Kyle
Don't reinvent the wheel just use something like this:
http://code.google.com/p/jquery-linkify/
It's really easy i use it as well
I don't know much about preg_replace, but i needed exatcly the same function as you.
Changing the line:
preg_replace($match[2], "<a href='$url'>{$match[3]}</a>", $match[2]);
for this:
str_replace($match[0],$url,$text);
seems to do the trick.
I just needed to get the return from this functions, so:
//$text = preg_replace($match[2], "<a href='$url'>{$match[3]}</a>", $match[2]);
$text = str_replace($match[0],$url,$text);
I need help in displaying my images from the directory images on my public_html directory. It's been uploaded, but it doesn't appear when I run the below php script. Please does anyone know why?
<?php
include_once("connect.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM authors") or die(mysql_error());
//Puts it into an array
while($info = mysql_fetch_array( $data ))
{
Echo "<img src=http://giveaway.comli.com/public_html/images/".$info['photo']."> <br>";
Echo "<b>Name:</b> ".$info['name'] . "<br> ";
Echo "<b>Title:</b> ".$info['title'] . " <br>";
Echo "<b>Phone:</b> ".$info['description'] . " <br>";
Echo "<b>Link<br>";
Echo "<b>Country:</b> ".$info['country'] . " <hr>";
}
?>
</body>
</html>
Is your public_html directory really not your site's document root? Perhaps you mean to say:
Echo "<img src=http://giveaway.comli.com/images/".$info['photo']."> <br>";
Where /images is a directory at the site document root. In a typical hosting situation, public_html would be the site document root, meaning that referenced from the web (rather than the file system), it is /.
What is the document root of your site? I'm guessing it's /home/.../public_html, which means the URL to access the images would really be http://giveaway.comli.com/images. Absolute urls of the sort you're using are really only necessary if you're using multiple different hosts/servers for your site, and/or are intending for the html to be portable. You could probably get away with using just <img src="/images/...">.
As I can see your document roo directory is public_html, so URL path to your photo will be look like this $image_url = "/images/" . $info['photo'];
I have an upload script that write a index.html file, I modified it to mail me the results, however the point to the root, since the email isn't in the root, the image doesn't load.
How do I append the code to add "http://www.home.html/uploaded" prior to the ". $value ." so that the images show up in the email.
Here is portion of PHP that assigns the images to a $result:
// Process value of Imagedata field, this is JPEG-files array
foreach ($_FILES[Imagedata][name] as $key => $value)
{
$uploadfile = $destination_dir . basename($_FILES[Imagedata][name][$key]);
if (move_uploaded_file($_FILES['Imagedata']['tmp_name'][$key], $uploadfile))
{
$result .= "File uploaded: <a href='". $value . "'>" . $value . "</a><br>";
}
}
//
$result .= "<br>";
Here is what I'm now receiving in the email, :
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Upload results</title>
</head>
<body>
AdditionalStringVariable: pass additional data here<br><a>
href='gallery_grannylovesthis_l.jpg'><img border = '0'
src='QIU_thumb_gallery_grannylovesthis_l.jpg'/></a><br><br><br>File uploaded: <a
href='gallery_grannylovesthis_l.jpg'>gallery_grannylovesthis_l.jpg</a><br><br>
GlobalControlData:
PHOTO_VISIBILITY : 2<br>
GlobalControlData:
PHOTO_DESCR : Requiredtest<br>
GlobalControlData:
PHOTO_TITLE : Requiredtest<br><br>gallery_grannylovesthis_l.png<br> control: , value: <br>
</body>
</html>
Thanks in advance for any guidance...I have a feeling it's something simple.
Like this:
<a href='http://www.home.html/uploaded/". $value . "'>"
Your code has a malformed tag:
<a>
href='gallery_grannylovesthis_l.jpg'><img border = '0'
It should be like this:
<a href='gallery_grannylovesthis_l.jpg'><img border = '0'
Notice on yours you have href (you added a > when there should not be one)