I have a small problem of needed to check which of 3 possible file paths relates to the correct image. I've always used get_headers for this in the past - but there are up to 100 images to load on any given pagination - so thats a potential of 300 get_headers in a worst case scenario and its painfully slow on my university server space.
However for any given steam_id there is the potential for images to exist at 2 of the possible file paths. Which means if I just include all three of urls and use Jquery to hide the on errors, for around half the results I end up with 2 images (which is in all fairness faster then what I'm doing currently, but at least the get_headers method looks fine.
Here's what I'm using so far:
//Use Header Response to determine which img to use
$url1 = 'http://cdn4.steampowered.com/v/gfx/apps/' . $row -> steam_id_clean . 'header_292x136.jpg';
$url2 = 'http://cdn4.steampowered.com/v/gfx/apps/' . $row -> steam_id_clean . 'header.jpg';
$header_response = get_headers($url1, 1);
if (strpos($header_response[0], "404") !== false) {
// File does not exist at url1 - recurse.
$header_response = get_headers($url2, 1);
if (strpos($header_response[0], "404") !== false) {
//File does not exist at URL - Therefore must be at URL3
echo '<img src = "http://cdn4.steampowered.com/v/gfx/subs/' . $row -> steam_id_clean . 'header.jpg" style="width: 200px; height: auto;" >';
} else {
//File exists at URL2
echo '<img src = "http://cdn4.steampowered.com/v/gfx/apps/' . $row -> steam_id_clean . 'header.jpg" style="width: 200px; height: auto;" >';
}
} else {
// File exists at URL1
echo '<img src = "http://cdn4.steampowered.com/v/gfx/apps/' . $row -> steam_id_clean . 'header_292x136.jpg" style="width: 200px; height: auto;" >';
}
This data is too dynamic - so I can't write a scrape to grab the images. Any Suggestions in a better method than get_headers would be appreciated. I don't have access to curl (unfortunately) on my university server either!
Related
I work for a voluntary fire brigade and years ago I created an operational status code using file_get_contents. Now it has no function anymore and I have no idea how to fix it.
<?php
$word = array ('T1','T2','T3','B1','B2','B3','B4','S1','S2','S3');
$word2 = array ('keine Einsätze');
$quelltext = file_get_contents("https://www.feuerwehr-huettendorf.at/einsatz.html");
foreach ($word as $einsatz) {
$pos = strpos($quelltext, $einsatz);
if($pos !== FALSE)
{
echo '<img title="Feuerwehr im Einsatz" alt="Feuerwehr im Einsatz" width="160" height="50" src="/images/content/icons/einsatz.gif" />';}
}
foreach ($word2 as $bereit) {
$pos2 = strpos($quelltext, $bereit);
if($pos2 !== FALSE)
{
echo '<img title="Einsatzbereit" alt="Einsatzbereit" width="160" height="50" src="/images/content/icons/einsatzbereit.gif" />';}
}
?>
The source code of that URL you've mentioned is (at the time of writing) simply this:
<iframe frameborder="0" style="height: 300px; width: 100%; frameborder: 0px" width="100%" height="680" src="https://www.feuerwehr-krems.at/Dokumente/Bezirk/Die%20Feuerwehren/Die%20Feuerwehren/FFInfo_Allgemein.asp?EldisID=222201&Select=1" scrolling="no"></iframe>
It doesn't contain any of the content your code is looking for. I wonder if someone changed the way the server-side application is set up.
You need to directly get the source code of the URL mentioned in that iframe instead, e.g.
$quelltext = file_get_contents("https://www.feuerwehr-krems.at/Dokumente/Bezirk/Die%20Feuerwehren/Die%20Feuerwehren/FFInfo_Allgemein.asp?EldisID=222201&Select=1");
This contains the real contents of the page.
I have a css media query that resizes and repositions a div. In my html that div has 2 lines but on the smaller screen I would like it displayed as 1 line.
Is there a way to ensure I have:
Website Visitors: <?php echo $data; ?>
<div class="fb-like"></div>
Instead of:
Website Visitors: <?php echo $data; ?>
<br/>
<div class="fb-like"></div>
When a screen is less than a fixed amount (in this case 460px)?
I would like to use php or css as they're what I'm using for styling in my css.php file.
Remove the <br/> add this in your css (values can be anything you want):
.fb-like {
margin-top: 10px;
}
and in your media query:
.fb-like {
margin-top: 0px;
}
Not possible with complete PHP you might need to use Javascript too !!
You can try following code to fetch screen width and height!!!
<?php
session_start();
if(isset($_SESSION['screen_width']) AND isset($_SESSION['screen_height'])){
echo 'Screen resolution: ' . $_SESSION['screen_width'] . 'x' . $_SESSION['screen_height'];
} else if(isset($_REQUEST['width']) AND isset($_REQUEST['height'])) {
$_SESSION['screen_width'] = $_REQUEST['width'];
$_SESSION['screen_height'] = $_REQUEST['height'];
header('Location: ' . $_SERVER['PHP_SELF']);
} else {
echo '<script type="text/javascript">window.location = "' . $_SERVER['PHP_SELF'] . '?width="+screen.width+"&height="+screen.height;</script>';
}
?>
Is there a gallery that have three features:
displays pictures in a lightbox
allows to import tens or hundreds of images at once from a folder
takes descriptions from file names
I have to make it on yesterday :)
I've made my own script together with original Lightbox
http://lokeshdhakar.com/projects/lightbox2/
Decided it will be much faster that way:
<?php
echo str_replace(array('<','>'), array('<','>'),'<table id="galx"><tr>');
$source_dir = 'images/taken/from/here';
$mini_dir = 'mini';
$target_dir = 'imagies/copied/to/there';
$i=0;
$images = array_diff(scandir($source_dir), array('..', '.',$mini_dir));
foreach($images as $image)
{
$filename = pathinfo($image, PATHINFO_FILENAME);
$title = mb_strtoupper(trim(str_replace('_',' ',$filename)));
$s = "<td><a href='$target_dir/$image' data-lightbox='galx' data-title='$title' >"
. "<img src='$target_dir/$mini_dir/$filename.jpg' /><br/>"
. "$title<a/></td>"; //gallery with titles
/*$s = "<td><a href='$target_dir/$image' data-lightbox='galx' >"
. "<img src='$target_dir/$mini_dir/$filename.jpg' />"
. "<a/></td>";*/ //gallery without titles
if (++$i % 5 == 0)
$s .= '</tr><tr>';
$s = str_replace(array('<','>'), array('<','>'), $s); //comment this line if want to paste it as php code
echo $s;
}
echo str_replace(array('<','>'), array('<','>'),'</tr></table>');
?>
Some basic css:
#galx {
width: 650px;
}
#galx td {
text-align: center;
}
#galx a {
display: block;
width: 120px;
font-size: 0.8em;
margin:1px auto;
text-decoration: none;
color: black;
text-align: center;
}
I've used FastStone Image Viewer to batch resize images at once and create miniatures. In app just press F3 to open advanced processing tool.
It just won't work with non-english letters on Windows - PHP bug. It still spoils first non-english letter of file name on Linux, so need to prefix '_' in such cases.
Overall good result - i've hided script somewhere on website and in 1,5 hours generated 6 galleries with ~1,5k pictures, and most of the time was consumed by file processing and copying. Not elegant but effective.
I am using the php google drive sdk, and programatically loading a document from Drive into a div. The document loads correctly and I can edit and save without issue. The problem occurs when I attempt to use the insert image dialog via the document menu, it just loads a blank white box.
What do I need to do in order to get the correct insert image dialog to load as if I was using the document directly from within drive? Below is the code I use to load the document:
function loadDocumentForView()
{
$service = buildServiceAccountService("Google Drive account");
$file = $service->files->get($_SESSION['selectedDocument']);
$name = $file->getTitle();
$ext = strtolower(substr(strrchr($name, "."), 1));
if ($ext == 'doc' || $ext == 'docx')
{
if ($_SESSION['editSelectedDocument'] == 1)
{
$path = "https://docs.google.com/document/d/" . $_SESSION['selectedDocument'] . "/edit?usp=sharing&embedded=true";
}
else
{
$path = "https://docs.google.com/a/googleAccount/document/d/" . $_SESSION['selectedDocument'] . "/preview";
}
}
else {
echo "Incorrect Format";
return;
}
$html = "";
if ($_SESSION['editSelectedDocument'] == 1)
{
$html = $html . "<div style='text-align: left; margin-right: auto; margin-left: auto; margin-bottom: 15px; width:95%;'><input driveFile='" . $_SESSION['selectedDocument'] . "' type='button' value='Save' onclick='saveChangesAndDownload(this)' style='margin-right: 5px;' fileName='" . $_SESSION['selectedDocumentName'] ."'><input currentFile='" . $_SESSION['selectedDocument'] . "' type='button' value='Exit' onclick='exitWithoutSaving(this)'>";
$html = $html . "<span id='saveFeedbackSpan' class='serverCallResult'></span></div>";
$html = $html . "<object data='" . $path . "' style='text-align: center; margin-right: auto; margin-left: auto; width:95%; height: 99%;' id='documentViewerObject' doc='" . $_SESSION['selectedDocument'] . "'></object>";
}
echo $html;
}
The problem I think is that the insert image dialog uses a google PickerBuilder that uses Google's domain as the origin and if you look at the javascript console you will see a message like
Invalid 'X-Frame-Options' header encountered when loading 'https://docs.google.com/picker?protocol=gadgets&origin=https://docs.google.…NnhLX2lIv83439cgDPmc%22%7D))&rpctoken=hyerknnixivf&rpcService=bgxw5kefbo32': 'ALLOW-FROM https://docs.google.com' is not a recognized directive. The header will be ignored.
4080223570-picker_modularized_i18n_opc__iw.js:1008 Uncaught Error: Incorrect origin value. Please set it to - (window.location.protocol + '//' + window.location.host) of the top-most page
The problem is that you need to find a way to tell the picker what the origin of the document is like specifiend in the
Picker API. I have the same issue and am still looking for a way to do that as well.
I'm trying to make profile photos show up against a list of reviews on a site I'm working on. If they don't have a profile photo I have a standard image to show instead, unfortunately the image always goes to the standard image rather than the profile even if it exists. Heres the code:
$reviewerPic = 'http://www.[URL].co.uk/images/members/' . $reviewPosterId . '/profilePic.jpg';
$default_pic = 'http://www.[URL].co.uk/images/background.jpg';
if(file_exists($reviewerPic)){
$reviewer_pic = '<img src="' . $reviewerPic . '" width="100px" style="float: left; margin: 20px;" />';
}else {
$reviewer_pic = '<img src="' . $default_pic . '" width="100px" style="float: left; margin: 20px;" />';
}
Pretty generic code but it doesn't seem to work! It just keeps showing the background image...
Any ideas on why file_exists wouldn't be working?
The function you are using, file_exists, uses physical paths, the parameter you need to provide should be the address on that server where the file can be found, and not an url
Sou you should have something like
/home/var/www/images/
instead of
http://www.[URL].co.uk/images/
So you need to check if the file exists on the server locally and after that you can use an url to make it available to the public (in img src)
You can see on the man page that this function only works with some URL wrappers, so it is better to use paths and not urls (I guess it depends on allow url fopen setting)
file_exists does not work with HTTP,
use : $_SERVER["DOCUMENT_ROOT"] to access root in your host
so
$reviewerPic = $_SERVER["DOCUMENT_ROOT"].'/images/members/' . $reviewPosterId . '/profilePic.jpg';
$default_pic = $_SERVER["DOCUMENT_ROOT"].'/images/background.jpg';
File_exists uses the /home/www/username/public_html/ format. You may want to retrieve this from your host.
So it would be something like /home/www/jack/public_html/[URL].co.uk/profilepic/id/profilePic.jpg.
By the way, no need to use . in your echo '<img src=... . You can use variables inside of strings, you should only use the . when you need to modify it using a function or something, like "string0".function($str....)."string1";
According to the PHP manual for file_exists not all protocols supports the function:
this function can also be used with some URL wrappers. Refer to
Supported Protocols and Wrappers to determine which wrappers support
stat() family of functionality.
The HTTP wrapper does not support stat:
Supports stat() No
the file_exists PHP function works only on physical files like /var/www/file.ext or C:\WWW\File.ext and not for HTTP Files. It doesn't even know how to handle HTTP files.
In this case, if you want to know if the file is there or not, since it is going to be an HTTP Response, you can try using curl, which if it responds with 404, then the file is not found.
For more information, check out these: http://php.net/manual/en/function.file-exists.php and
http://php.net/manual/en/book.curl.php
file_exists doesn't support remote URLs.
You can try this:
if (fopen($reviewerPic, "r"))
echo "File exists!";
Try simple one.
$reviewerPic = 'http://www.[URL].co.uk/images/members/' . $reviewPosterId . '/profilePic.jpg';
$default_pic = 'http://www.[URL].co.uk/images/background.jpg';
if($fileopen = #fopen($reviewerPic)){
$reviewer_pic = '<img src="' . $reviewerPic . '" width="100px" style="float: left; margin: 20px;" />';
fclose($fileopen);
}else {
$reviewer_pic = '<img src="' . $default_pic . '" width="100px" style="float: left; margin: 20px;" />';
}
A great way to handle it is here ~
<?php
$imgok="1.webp"; // Your actual image
$imgerr="template.webp"; // If image is missing
$dir="/assets/image/products/"; // Location of the image directory
$path = $_SERVER['DOCUMENT_ROOT'].$dir. $imgok; // Full Paths for processing
echo file_exists($path) ? $dir.$imgok : $dir.$imgerr; // Actual paths for domain to get the image
?>