I need to fetch some images using google Reverse image search which is not supported by the API, but thankfully, you can query google with a direct link to the image and it still shows results, so:
$googleURL = "https://www.google.com/searchbyimage?&image_url=".$imageURL;
echo $googleURL;
Output:
https://www.google.com.au/search?tbs=sbi:AMhZZiu9rNRW4ETWGjN9XYQKsa21UHM7j_1TjMjXvYyNH1knVTyMGZGNmS2yme4CsQb0T7UViTyNrG4e8u_1xLY-dZCU16wkfdUakeY7idDwyMge78nT--Grpll4t9_1fp4YPTsJyKRUANzw1Iyctsko7OZbkYES3VUHtyNy9l9RJf12YOdEvVOxSZCO6-JPxO0PpZ5p79Rr-eDUrqENWYVbk4qojafKMTVfuXvoACQ9iykI-DMVbP9n_1o0YkdKTdUeK2r30wg4Oe2BqspoXlI_11rxySuK6TolPM6z58E6erTT0bnYfXTlyDMBfOwgSfhbn2ipLrNHgNdqyk-YhmMP0_1ZzqVyZrgMz-I5cfH9N65nX6bhZfos0lgr8_15V6ZHtX0_1p8s5r229JDrwzlwnjwOBLgP1inmEORCaKOlcfHbyPnU3n04pIfLGu5fWYpbmFJwtK_1vaJvS0uFb6Pkh_1uv0wvz_10yf4O6E1IvBSoMudcYy4cmJ1zegJJ9L50C0bzXFIRUb62lcPJWbkZNR44Tz378nOSXd-PND0JfKQ-TujT3KfC_1O241knvr9Eb3LbuvncGiCMoPgxlUY4r9B_1KWchNWhJVTJz9omeiygwz5K_13YkjuLg52UF6YWvLedCxgRoUpuj9kFdmYt-b9Tn2VEZG8yfiLm3OTkZnlVYtPF87LLQAHH24VpLMoV0oDllHDK3xOXhvusl_1K2Me9tTdK15PPG7oreeWfYRztQwTpG4iB5GAnaj687OQukvxX5hNFIqXx_1QSuNooDhIP1eJl-6QYfuI4MPasj6flSMom7HYTSjyjcsQKw0Prj1bBsJY6qH1qyLrF1f1_1Ql0COERnbOV7O5mTOuTkNWarmR5wzE06qbgsrtT95ENqafd81ppHbA0Jyg-xQ8TLV-dSp1QDAtiYAHI_11tCwsDtrak4jDS4qAfEJCw_1lb9urJqqajvp25jLH2_1mN3u0eeW7xNF-PljofyhI0iIWYSg6ghyOVRIaT_1c6klKUPvOrquZy8hMCZWHb3CYZNGJeKTnACCyYW1MNVUsYnoFWORN6hvkVlUk0beFXvA_1W2vaoedLjj-fN1y8_1dPOiBROLYtv85nq01csCKk7Eib6p2b_131wEeQBYocoYU0sGTv2_1dhOvSXRPGTnrbZlNDbJFUtH4pF9tMQj5-Fh_1lw9TTXGCjQ9UjOSLD5q7tNjCQU1As1uCQBvmZvxo7J3gZSAcj_19wXfHZCOsA8g-WA97V-2b62ia4RFOehQ38hoXoK7MCSDLnVtJTsKQz9HuEreXm8qGQlbDzfr7JFuHHe2MOyChwnL_1gzRnZd8uv2OIM0nzKh_1wg4T1KCXv3NSGNkSyNxpYXFJ161Sv3NpQQI3epBMiYA_1AcQDiCxOTQvWj00e5EXaXN22CDRWRq3uk4HWj2eXcR6-TGmsYEfSGX9nyQwK1DHp9yaNjk9Bal7rNHUAe_1eMDsCWW9htaLyiMTio0eXyTumVrlt7ShZVd8oSPOj8U0ilY9owH95jz7LsI8vUnzF-FC2m_1yNt3xe4ZAcsRTbYQXTN3Ga76vTQBPu8oz0gkYmDTA&gws_rd=cr&ei=wAHVVJOVLIeeugSZ64A4
.. now on this page, I need to follow the link to the actual results page, so my condition would look like:
if a.text == 'Large'
elseif a.text == 'Medium'
elseif a.text == 'Visually similar images'{
// crawl the link
// get direct links of top 10 results
}
But I'm not sure how to:
get the href if the condition a.text == 'Large' is met since Simple HTML DOM Parser or PHPQuery neither have this like jQuery.
On fetching the results page, how to trigger a mousedown even to get the full-size image URLS because this is what I see in the source: jsaction="mousedown:irc.rl;keydown:irc.rlk"
Here's quick screencast of what I'm looking to do: https://www.dropbox.com/s/c8g7fs5m5zqcegb/2015-02-07_08-56-23.mp4?dl=0 (5.9mb)
You can use Regular Expresssions to find the matching link.
If you check the HTML Code of the Google Image Search a bit more closely, you see there is actually also a href param in the Link (Example) which you can follow with just another crawl. There you can parse out the large image again with Regular Expressions.
Related
We have several pages generated using PHP on our website with the following titles (for example):
http://www.mysite.com/project/category/1
http://www.mysite.com/project/category/2
http://www.mysite.com/project/category/3
Each one is created dynamically with the same page layout with each showing a different database result depending on the predefined conditions.
I would like an image to be displayed at the top of the page for just one of the results, let's say for http://www.mysite.com/project/category/2 - how can I go about this?
The relevant code on our page is this:
$category=mysql_fetch_array(mysql_query("select * from project_category where project_category_id='".$project_category_id."'"));?>
If we go down the if statement route can you show an example of how to display an example image by modifying the above code to get me started?
I would probably make it a property (can be a as simple yes/no) in the database, and use the existing db-result to determine if the category has to display a page. Although this might seem overkill - I'd definitely pick this dynamic solution over a if ($categoryId == 2) { } solution any day. Keeps it dynamic and your code clean and generic.
In the end I opted for an if statement (as found here http://www.tizag.com/phpT/if.php).
The original code above was modified in the following way:
$category=mysql_fetch_array(mysql_query("select * from project_category where project_category_id='".$project_category_id."'"));
if ( $project_category_id == "2" ) {
echo '<img src="http://www.mywebsite.com/image.jpg" width="675" height="75" border="0" />';
}?>
In PHP
I'm converting a large number of blog posts into something more mobile friendly. As these blogs posts seem to have alot of large images in them, I would like to use some regex or something to go thorugh the article's HTML and replace any images that aren't currently linked with a link to that image. This will allow the mobile browser to display the article without any images, but a link to the image inplace of where the image would be thus downsizing the page download size.
Alternatively, if anyone knows any php classes/functions that can make the job of formatting these posts easier, please suggest.
Any help would be brilliant!
To parse HTML, use an HTML parser. Regex is not the correct tool to parse HTML or XML documents. PHP's DOM offers the loadHTML() function. See http://in2.php.net/manual/en/domdocument.loadhtml.php. Use the DOM's functions to access and modify img elements.
How about doing it in JQuery instead of PHP. That way, it will work across different blogging software.
You can do something like...
$(document).ready( function () {
$('#content img').each(function () {
var imageUrl = $(this).attr('src');
//* Now write codes to delete off the image and put in a link instead. :)
});
});
With Regexp something like this:
$c = 0;
while(preg_match('/<img src="(.*?)">/', $html, $match) && $c++<100) {
$html = str_replace($match[0], 'Image '.$c.'');
}
You can also use preg_replace (saves the loop), but the loop allows for easy extensions, e.g. using the image functions to create thumbnails.
I've built a blog similar to wordpress. On my home page, I take the entire blog post, throw it through a function, and only display an excerpt of it. I want to go through and shrink my videos to a specific width/height. The code in the post could look like:
[vimeo width="700" height="400"] // (the 700 & 400 could be any values).
I basically want to find that, then change it to:
[vimeo width="300" height="200"] // this will be preset/hard coded.
You can use regular expressions through preg_replace() to do the filtering. Just load your whole blog post into $BlogPost. The RegEx pattern may need to be altered to allow for variations in syntax and spacing (i.e. width = '700', etc.)
<?php
$FilteredBlogPost = preg_replace('/(.*vimeo width=")\d+(" height=")\d+(".*)/im', '${1}300${2}200${3}', $BlogPost);
?>
unless you want to scrape the video, put it on your server, resize it with some video dedicated libraries and then stream it from your server, the solution is client related (ie: HTML. not PHP)
Here is a nice tutorial on how to achieve that with youtube videos - i think exactly the same applies in your case too
Right now I have a variable: $blogbody which contains the entire contents of a blog.
I'm using the following to convert URLS to clickable links:
$blogbody = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","\\0", $blogbody);
And the following to resize embedded video:
$blogbody = preg_replace('/(width)=("[^"]*")/i', 'width="495"', $blogbody);
The problem I'm running into is the embedded video not working, comes back with an Access Forbidden error (403). If I remove the line to convert URLS to links, the embedded video works fine. Not sure how to get these two working together. If anyone else has a better solution to converting URLS to clickable links and resizing embedded video let me know!
This might be happening because the link which you use to embed the video also gets his <a href=''> tags added. So instead of just converting all links, check that they don't have ' or " directly behind or in front of them - this will make sure that the embedded videos' links won't get anchor tags.
Im building my own forum where you can use diffrent types of BBcode and one of them is [youtube][/youtube] to embed youtube videos,
The idea is to make it idiot-proof, the user can enter the full lenght url or just the code to the video like this
[youtube]http://www.youtube.com/watch?v=AJ3_kndmeCg[/youtube]
[youtube]http://www.youtube.com/watch?v=AJ3_kndmeCg&feature=related[/youtube]
[youtube]AJ3_kndmeCg[/youtube]
Here is my code that get the code out of a full lenght url,
<?php
$getpost=$_POST['post'];
$getpost=preg_replace("'\[youtube\].*?=(.*?)&.*?\[/youtube\]'is",'yt link is \\1',$getpost);
$getpost=preg_replace("'\[youtube\].*?=(.*?)\[/youtube\]'is",'yt link is \\1',$getpost);
$getpost=preg_replace("'\[youtube\](.*?)\[/youtube\]'is",'yt link is \\1',$getpost);
?>
The results are:
yt link is AJ3_kndmeCg
yt link is AJ3_kndmeCg
yt link is AJ3_kndmeCg
It works perfectly if I only want to embed 3 youtube videos,
But if i wanted to embed 6 youtube videos like this:
[youtube]http://www.youtube.com/watch?v=AJ3_kndmeCg[/youtube]
[youtube]http://www.youtube.com/watch?v=AJ3_kndmeCg&feature=related[/youtube] [youtube]AJ3_kndmeCg[/youtube]
[youtube]http://www.youtube.com/watch?v=AJ3_kndmeCg[/youtube]
[youtube]http://www.youtube.com/watch?v=AJ3_kndmeCg&feature=related[/youtube]
[youtube]AJ3_kndmeCg[/youtube]
The results are:
yt link is AJ3_kndmeCg
yt link is AJ3_kndmeCg[/youtube]
yt link is AJ3_kndmeCg
yt link is http://www.youtube.com/watch?v=AJ3_kndmeCg
[youtube]AJ3_kndmeCg
Do you see that^^ the tags still remain on two lines and one of the lines show a full lenght URL,
I'd rather use an existing BBCode parsing library like http://www.christian-seiler.de/projekte/php/bbcode/index_en.html which also allows you to define your own bb tags, and turn them into the HTML you want.
;)
Wow, this was a real toughie of a regex to write.
Got it for you in one:
$getpost=preg_replace('#\[youtube\].*?(?:v=)?([^?&[]+)(&[^[]*)?\[/youtube\]#is',
'yt link is \\1', $getpost);
This will work for all the cases you supplied. It will not work if the link begins with a part other than 'v=' (for example, 'http://www.youtube.com/watch?foo=bar&v=AJ3_kndmeCg').
Enjoy!