PHPQuery... trying to get all links of all images from page - php

I am trying to get all links of all images on a given page using PHPQuery. I am using the PHP support syntax of PHPQuery.
This is the code I have so far:
include('phpQuery-onefile.php');
$all = phpQuery::newDocumentFileHTML("http://www.mysite.com", $charset = 'utf-8');
// in theory this gives me all image sources
$images = $all->find('img')->attr('src');
// but if I do `echo $images;` what I get is the src to the first image
Out of curiosity I have tried
$images = $all->find('img:first')->attr('src');
and
$images = $all->find('img:last')->attr('src');
and it prints correctly the first and the last image's addresses, respectively, but how in hell can I get an array of all links?

Within your foreach loop, you need to wrap the $a with a pq().
For example:
$all = phpQuery::newDocumentFileHTML("http://www.mysite.com", $charset = 'utf-8');
$imgs = $all['img'];
foreach ($imgs as $img) {
// Note: $img must be used like "pq($img)"
echo pq($img)->attr('src');
}

Related

Imagick: Combine 7 images on top of each other, all are same size so no positioning needed

I am using PHP imagick, how can I combine 7 images on top of each other? They are all same size so there is no need for positioning or adjusting.
I have all images in array like this:
$images = explode("c", $character);
$elements = array();
$elements[0] = $skins[$images[0]];
$elements[1] = $eyes[$images[1]];
$elements[2] = $hair[$images[2]];
$elements[3] = $mouth[$images[3]];
$elements[4] = $pants[$images[4]];
$elements[5] = $shoes[$images[5]];
$elements[6] = $torso[$images[6]];
try{
$pdf = new Imagick($elements);
$pdf->setImageFormat('png');
$pdf->writeImages('public/images/avatars/new.png', true);
}
catch(ImagickException $e){
echo($e);
}
I tried the code above, but it outputs 7 different images instead of one combined.
http://php.net/manual/en/imagick.combineimages.php
$pdf->combineImages($elements);
I think that should to work, but i cannot prove it.
Regards,
$pdf->compositeImage($elements[$i], $elements[$i]->getImageCompose(), 0,0,imagick::MONTAGEMODE_FRAME);
this will do (y)

How do I get the h1 inside a loop in simple html dom?

How do I get the h1 inside a loop in simple html dom?
This is the code that I'm using to fetch images and save them in a folder.
It works fine.
But I don't have any idea on how to save h1 as file name
include("simple_html_dom.php");
$files = file_get_contents("http://picjumbo.com/page/2/");
$html = new simple_html_dom();
$html->load($files);
//Get all the item-wrap divs
foreach($html->find('div[class=item_wrap]') as $item_wrap){
//fetch each image and save it
foreach($item_wrap->find('img[class=image]') as $img){
$img_src = explode('/',$img->src);
$img_src = explode('-',$img_src[5]);
$img = $img_src[0];
$url = 'http://picjumbo.com/wp-content/themes/picjumbofree/run.php?download&d='.$img.'.jpg';
copy($url, 'images/'.$img.'.jpg');
}
}
I tried this but it is not working.I can't get my head around it
foreach($html->find('div[class=item_wrap]') as $item_wrap){
foreach($item_wrap->find('img[class=image]') as $img){
$img_src = explode('/',$img->src);
$img_src = explode('-',$img_src[5]);
$img = $img_src[0];
//this throws an error (I know a foreach is required but how do I save h1 as file name with it?)
$h1 = $item_wrap->find('h1')->plaintext;
$url = 'http://picjumbo.com/wp-content/themes/picjumbofree/run.php?download&d='.$h1.'.jpg';
copy($url, 'images/'.$img.'.jpg');
}
}
You get the idea right?I just want to save the image as h1.
Need your help!!!
Find('...', $index) returns the Nth element specified by $index (zero based), and If $index is not set it returns an array...
So, since you know there's only one h1 in each block, then it willl correspond to the first element in the array returnd by find():
$h1 = $item_wrap->find('h1', 0)->plaintext;
You can also try:
$h1Temp = $item_wrap->find('h1');
$h1 = $h1Temp[0]->plaintext;
Edit: Damn, I see you already got similar answer in the comments, i'll leave it anyway here, it may help others..

Get 3 URLs from XML, randomly select one to save to variable

I'm in over my head at this point - but what i have currently looks like this:
<?php
$request_url = "http://aethereverywhere.tumblr.com/api/read?type=photo&tagged=ae&start=0&num=1";
$xml = simplexml_load_file($request_url);
$img = $xml->posts->post->{'photo-url'};
?>
If increase &num to 3, lets say - it'll pull three files, and simplexml_load_file will parse them out - and save them to $img - but what i'd like to is have only one URL saved to $img, selected at random.
Thanks for the help
New code: select a random image from 0 to 118 (total 119), then outputing, choose the highest resolution.
<?php
$request_url = "http://aethereverywhere.tumblr.com/api/read?type=photo&start=".rand(0,118);
$xml = simplexml_load_file($request_url);
$img = $xml->posts->post->{'photo-url'};
$img=(array)$img;
echo '<img src="'.$img[0].'">';
?>
Add a random number between 0 and the total photos -1. ie, get it in one line randomly.
$img = $xml->posts->post->{'photo-url'}[$random] // if that's the right syntax.
Or while iterating through the tags, do a random check for an even/odd number.
$img = (empty($img) || !$img) ? (rand(10)%2==0) ? $PHOTO_URL : FALSE : $img;
Do it like this:
$xml = simplexml_load_string($x); // assuming XML in $x, or use simplexml_load_file
$urls = $xml->xpath("//photo-url"); // get all <photo-url> nodes
echo $urls[rand(0,count($urls)-1)]; // echo a random url
see it working: http://codepad.viper-7.com/WDq0ha

In PHP, how can I get an XML attribute based on a variable?

I'm retrieving files like so (from the Internet Archive):
<files>
<file name="Checkmate-theHumanTouch.gif" source="derivative">
<format>Animated GIF</format>
<original>Checkmate-theHumanTouch.mp4</original>
<md5>72ec7fcf240969921e58eabfb3b9d9df</md5>
<mtime>1274063536</mtime>
<size>377534</size>
<crc32>b2df3fc1</crc32>
<sha1>211a61068db844c44e79a9f71aa9f9d13ff68f1f</sha1>
</file>
<file name="CheckmateTheHumanTouch1961.thumbs/Checkmate-theHumanTouch_000001.jpg" source="derivative">
<format>Thumbnail</format>
<original>Checkmate-theHumanTouch.mp4</original>
<md5>6f6b3f8a779ff09f24ee4cd15d4bacd6</md5>
<mtime>1274063133</mtime>
<size>1169</size>
<crc32>657dc153</crc32>
<sha1>2242516f2dd9fe15c24b86d67f734e5236b05901</sha1>
</file>
</files>
They can have any number of <file>s, and I'm solely looking for the ones that are thumbnails. When I find them, I want to increase a counter. When I've gone through the whole file, I want to find the middle Thumbnail and return the name attribute.
Here's what I've got so far:
//pop previously retrieved XML file into a variable
$elem = new SimpleXMLElement($xml_file);
//establish variable
$i = 0;
// Look through each parent element in the file
foreach ($elem as $file) {
if ($file->format == "Thumbnail"){$i++;}
}
//find the middle thumbnail.
$chosenThumb = ceil(($i/2)-1);
//Gloriously announce the name of the chosen thumbnail.
echo($elem->file[$chosenThumb]['name']);`
The final echo doesn't work because it doesn't like have a variable choosing the XML element. It works fine when I hardcode it in. Can you guess that I'm new to handling XML files?
Edit:
Francis Avila's answer from below sorted me right out!:
$sxe = simplexml_load_file($url);
$thumbs = $sxe->xpath('/files/file[format="Thumbnail"]');
$n_thumbs = count($thumbs);
$middlethumb = $thumbs[(int) ($n_thumbs/2)];
$happy_string = (string)$middlethumb[name];
echo $happy_string;
Use XPath.
$sxe = simplexml_load_file($url);
$thumbs = $sxe->xpath('/files/file[format="Thumbnail"]');
$n_thumbs = count($thumbs);
$middlethumb = $thumbs[(int) ($n_thumbs/2)];
$middlethumbname = (string) $middlethumb['name'];
You can also accomplish this with a single XPath expression if you don't need the total count:
$thumbs = $sxe->xpath('/files/file[format="Thumbnail"][position() = floor(count(*) div 2)]/#name');
$middlethumbname = (count($thumbs)) ? $thumbs[0]['name'] : '';
A limitation of SimpleXML's xpath method is that it can only return nodes and not simple types. This is why you need to use $thumbs[0]['name']. If you use DOMXPath::evaluate(), you can do this instead:
$doc = new DOMDocument();
$doc->loadXMLFile($url);
$xp = new DOMXPath($doc);
$middlethumbname = $xp->evaluate('string(/files/file[format="Thumbnail"][position() = floor(count(*) div 2)]/#name)');
$elem->file[$chosenThumb] will give the $chosenThumb'th element from the main file[] not the filtered(for Thumbnail) file[], right?
foreach ($elem as $file) {
if ($file->format == "Thumbnail"){
$i++;
//add this item to a new array($filteredFiles)
}
}
$chosenThumb = ceil(($i/2)-1);
//echo($elem->file[$chosenThumb]['name']);
echo($filteredFiles[$chosenThumb]['name']);
Some problems:
Middle thumbnail is incorrectly calculated. You'll have to keep a separate array for those thumbs and get the middle one using count.
file might need to be {'file'}, I'm not sure how PHP sees this.
you don't have a default thumbnail
Code you should use is this one:
$files = new SimpleXMLElement($xml_file);
$thumbs = array();
foreach($files as $file)
if($file->format == "Thumbnail")
$thumbs[] = $file;
$chosenThumb = ceil((count($thumbs)/2)-1);
echo (count($thumbs)===0) ? 'default-thumbnail.png' : $thumbs[$chosenThumb]['name'];
/edit: but I recommend that guy's solution, to use XPath. Way easier.

php web image search

I want to do a picture search engine. I use simple_html_dom and preg_match_all to get all the images, then use getimagesize to get all the image sizes.
Here is one part of my code.
<?php
header('Content-type:text/html; charset=utf-8');
require_once 'simple_html_dom.php';
$v = 'http://www.jqueryimage.com/';
$html = file_get_html($v);
foreach($html->find('img') as $element) {
if( preg_match('#^http:\/\/(.*)\.(jpg|gif|png)$#i',$element->src)){
$image = $element->src;
//$arr = getimagesize($image); //get image width and height
//$imagesize = $arr[0] * $arr[1];
echo $image.'<hr />';
}
}
?>
First question, how to add a judgement so that I can echo the biggest size image? (only one image).
Second question, I can get the image real url in these two possibilities, first where image is as a 'http' began, second where image is as a / began.
But how to get the image real url in the situation where image is as a './' or ../ or ../../ began? it is difficulty for me to judge how many ../ in a image, then cut the site url to complement a image real url?
Thanks.
Insert this before foreach loop:
$maxsize = -1;
$the_biggest_image = false;
Insert this below $image = $element->src; line
$arr = getimagesize($image);
if (($arr[0] * $arr[1]) > $maxsize) {
$maxsize = $arr[0] * $arr[1];
$the_biggest_image = $image;
}
After foreach loop you'll have $the_biggest_image variable set or false if nothing found. This will take first one if more than one 'biggest image' found.
For second question, I don't know!
Edit: fixed something!

Categories