Automated storing of Image URLs in a database - php

I have been trying to create smoothly functioning image preview for each website URL, on hover, using the websnapr service. The javascript below works great, but generates a website preview on the fly, which is very slow, and seems unnecessary because I am always working with the same database entries, and this method may generate the image preview for the SAME website multiple times (on every refresh), when in actuality only once will suffice, as long as I have and can store the image path in a DB for later use.
The database I am working with contains 3,000+ website URL's. I would like to create a thumbnail image preview for each of these in an automated way, using a PHP script and by storing the websnapr image paths in the database, so they can later be used to generate website previews in a more efficient way. However, this is a bit over my skill level...
I appreciate any suggestions how to do this.
Many thanks in advance!
JavaScript
$('.co-website').each(function(){
var url = encodeURIComponent($(this).children('a').attr('href'));
var imgSRC = 'http://images.websnapr.com/?url=' + url + '&key=' + apiKey + '&hash=' + encodeURIComponent(websnapr_hash);
$(this).children('a').attr("title", "<img src='"+imgSRC+"' />");
});
PHP Script
while($row = mysqli_fetch_array($results)){
$thumbnail_url = 'http://images.websnapr.com/?url=' . $row['website'] . '&key='.$apiKey.'&hash='.;
//Not sure how to get the hash portion of the URL
}
//update this record's thumbnail mySQL field

Related

Fetching images from the server for a social media post

I am building a basic social media site for a university project and I am at the stage of displaying posts. Currently the posts display using a foreach which creates a series of div tags containing the information about each post from the db. I am now looking to add the images uploaded with these posts onto the page but I am not sure how to go about fetching them.
When the images are uploaded they are placed in a folder in the server called postImages, and are renamed to fit this format:
postID + image number
where image number is determined by how many images the user uploads, starting at 0.
My initial idea was to use scandir() to list all posts and explode their names to fetch the ID but this is much more complex than it had to be as there is another table in the DB called postImages which contains an ID for each post as well as the postID of the post it belongs to, so fetching which images are needed is no problem. However, I am not sure how to go from having the required IDs to actually fetching the image from the folder.
The current code for building the posts:
include 'config.php';
$postsSQL = "SELECT * FROM Post ORDER BY postTime DESC" ;
$result = mysqli_query($connection, $postsSQL);
foreach ($result as $row) {
echo "<div class = 'postContainer' id =".$row['postID'].">";
echo "<div id = 'postTitle' class = 'postTitle'>". "Post Title: ".$row['postTitle']. "</div>";
echo "<div id = 'postDesc' class = 'postDesc'>" . "Post Description: ".$row['postDescription']. "</div>";
echo "<div id = 'postLocation' class = 'postLocation'>" . "Post Location: ".$row['postLocation']. "</div>";
echo "<div id = 'postTime' class = 'postTime'>" . "Posted at: ".$row['postTime']. "</div>";
echo "<div id = 'UserID' class = 'userID'>".$row['UserID']. "</div>";
echo "</div>";
echo "<br>";
}
Thanks for taking a look, and sorry in advance if I've missed any details or this is a simple question.
Serve those images to users as static objects, not directly from PHP.
The first thing you do is make sure your images can be viewed in browsers. In other words, you should be able to use a URL something like
https://oddity.example.edu/uploads/postImages/72_0.png
You need to make that /uploads folder (or what ever you name it) visible to your system's web server.
Next, store the "slug" -- the partial URL -- for your image in your database. For the example above you might store 72_0.png.
Then, when writing out the HTML for your post page, include a tag like this:
<img src="https://oddity.example.edu/uploads/postImages/72_0.png" />
and the browser will retrieve and show the image.
You might be able to use PHP code like this to do that.
$imageRoot = 'https://oddity.example.edu/uploads/postImages/";
...
$imageurl = $imageRoot . $row['imageslug'];
echo '<img class="image" src="$imageurl" />;
Finally, people who store uploaded images for later viewing often use random hard-to-guess filenames for them to slow down web-scraping cybercreeps. That means assigning random file names upon uploads rather than using the naming scheme you mentioned.
Pro tip: avoid mixed-case table, column, and object names. Various file systems (including Windows's NTFS) do various different things with case-sensitivity, and you don't want to see your stuff stop working if you put in on a new server.

Quill - JSON > PHP > MYSQL - How to parse data from quill to php for processing?

Wizzards :)
I really want to use Quill richtext editor but I have no real knowledge of JSON or how is parses the so called "Deltas".
Basically, I need to collect the page from the editor and process it with php before storing it in the mysql database, and, from the database back to the editor if user wishes to update an existing post. It needs to be able to parse text and multiple images...
I don't have 3 months a piece to learn js/json, can anyone help me out with this one? Thank youuu!
So acording to the Quill documentation (https://quilljs.com/docs/api/) you can use var delta = quill.getContents(); to collect the data. Would it be something like this with an onclick() function? I dont even know with js if it has to be within a form? remember I don't know js :)
<script>
onclick(does this); // or can it be updated as the user creates the document?
var delta = quill.getContents();
// what then?
</script>";
The php would require a different variable if each image to be processed, perhaps in an array?
<?php
$images = array();
$images[0] = 'json/delta/quill image?';
$images[1] = 'etc...';
$images[2] = 'etc...';
$images[2] = 'wtf...';
?>
Why on earth they don't have a simple example of how to do this I don't know, its an all too common problem with these things.

How to create URLs similar to Pastebin, Imgur, Youtube etc.?

I'm creating a site (with PHP & MySQL) that takes a user input, generates a random link, and uses that users input on the generated link to show content.
The nearest real-life example would be http://pastebin.com/ - you enter a text input on the homepage, it generates a random link (such as http://pastebin.com/2hf4Dtv7 and then the text you input is permanently displayed on that generated link.
I've been stuck on this problem for what seems like forever. I've managed to get different parts of the problem solved (I think), but I can't seem to get it to work altogether.
Key facts
The user input needs to be available to use on the generated link
A PHP file run.php is what I will use to display content / manipulate the user's input, so somehow needs to be available on the generated link (unless there's another way to do this).
I have a MySQL database that currently has / stores 3 columns: ID (integer, which automatically increments), userinput (varchar, which is set to unique) and pageurl (varchar, which is also set to unique)
The site is using WordPress, but that's not too important as I'm dealing with the PHP / htaccess file(s) directly.
Things I've attempted / managed to do:
I can generate a random string for the url, via <?php echo $randGen ?>. This can be put into <form action="<?php echo $randGen ?>" method="POST"> - This works in the sense that it will open a tab to domain.com/xyz, but that 404s & run.php isn't assigned to the generated link (I'm not sure if the input value is either).
Tried both POST and GET methods in the form. The input data isn't sensitive, so it isn't a huge issue which is used.
Read up on & attempted to use .htaccess rewrite rules / pretty urls, such as RewriteRule ^([0-9a-zA-Z]+)$ run.php?userinput=$1 [NC,L], as well as trying to use [QSA] & &%{QUERY} to keep the user input, but found myself to be way out of my depth.
Attempted to covert the MySQL (auto-incrementing) ID to base64, but aren't sure where to run the PHP script to get it working & also don't understand how to append that random string to the URL, while still showing the users input.
Watched numerous tutorials on how to create pastebin-esque sites / URL shortener sites, to see if I could get any relevant ideas from that - to no avail.
You are almost there, RewriteRule is the way to go.
With this in your apache configuration:
RewriteRule ^([0-9a-zA-Z]+)$ run.php?randurl=$1 [L]
This, in your html page:
<form action="/run.php" method="POST">
Userinput: <input type="text" name="userinput">
You then just need to use the variables $_GET['randurl'] and $_POST['userinput'] in run.php:
if (isset($_POST['userinput'])) { // user adds a new input
// insert into database (userinput, randurl) values ('".$_POST['userinput']."', '".$randGen."')
// redirect the user to the new short URL page:
header('Location: domain.com/'.$randGen);
}
else if (isset($_GET['randurl'])) { // user wants to access the short URL
// retrieve userinput from database:
// select userinput from mydatabasename where randurl = 'domain.com/'.$_GET['randurl']
$sql = "SELECT userinput FROM articles WHERE randurl = '".$_GET[randurl]."'";
$result = $conn->query($sql);
if ($result) {
if ($row = $result->fetch_object()) {
// echo select result in the page
echo($row['userinput']);
}
else
echo("error: fetch_object");
$result->close();
}
else
echo("error: query");
}

Actionscript 2.0, BitmapData and Local vs Remote images

Good Morning Justice League of Stackoverflow,
I have here a problem that may stump the panel.
I am creating an interactive post-it for an upcoming event that allows for us to tap into a sql database and post tweets, survey answers and images. We've already tapped into the Twitter API and the survey, so those are A-OK.
The problem lies within loading the images from a location other than the local interactive board's server.
If the image itself is locally hosted, it loads just fine.
If the image is hosted elsewhere, the image will not load, even though I have a trace on the URL of said image.
I'm loading all tweets, surveys and images through an XML load and all the data is loading properly.
I AM loading the image through a smoothing filter so that when the "post-its" are slightly rotated, they are not jagged. Here is THAT code:
import flash.display.*;
var srcImg = _parent._parent.varContent;
urlText.text = srcImg;
var mainHolder = this.createEmptyMovieClip("main", this.getNextHighestDepth());
var original = mainHolder.createEmptyMovieClip("original", mainHolder.getNextHighestDepth());
var smooth = mainHolder.createEmptyMovieClip("smooth", mainHolder.getNextHighestDepth());
var mclListener:Object = new Object();
mclListener.onLoadInit = function() {
var w = original._width;
var h = original._height;
var bmpData1:BitmapData = new BitmapData(w, h, true, 0x000000);//true and 0 color allows for transparency
bmpData1.draw(original);
smooth.attachBitmap(bmpData1,2,"auto",true);//true for SMOOTHING, ;)
reSize(smooth);
original.removeMovieClip();
mainHolder._x = -(smooth._width / 2);
mainHolder._y = -(smooth._height / 2);
};
var image_mcl:MovieClipLoader = new MovieClipLoader();
image_mcl.addListener(mclListener);
image_mcl.loadClip(srcImg,original);
function reSize(target) {
if (target._width > target._height) {
s = Math.floor((300.85 / target._height) * 100);
}
if (target._width < target._height) {
s = Math.floor((320.90 / target._width) * 100);
}
target._xscale = s;
target._yscale = s;
}
This is a two part script where the bulk loads in the image and places it into an empty movieclip, then adds the smoothing filter. The second part is a resizer that automatically resizes the image and keeps the aspect ratio
Here's the kicker. When I test the flash piece (not embedded in HTML) the thing works 100%.
As soon as I put the swf into an html and view it on a web page, the remote images will not load.
I'm a bit stumped on why this is, could this be a firewall or security issue? Because I work in a high security firewall environment.
Any guidance in this would be most appreciated.
Thank you for your time.
by default flash does not allow cross domain loading of data as a security feature, but it can be overridden.
this may help:
allowDomain (security.allowDomain method) if you can get a swf running on the image server
http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00002104.html
A cross domain policy file may also be used on the server to grant access to the swf:
http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000470.html

Counter in ActionScript 3.0 with...PHP or?

I am doing this flash banners for multiple clients and one major request is to have some sort of counter so they know how many times the banner has been clicked.
I know how to do it in ActionScript 3.0, I make a simple var:int and i increase it +1 when a click is made on the banner. What do I do with the value of this var(say its 121) where do I store it online so its safe and can be changed by multiple flash banners(as3).
But how do I save this information so next time when the banner is loaded(on diffrent webpages) the number of clicks is whatever it was last time it was loaded.
Should I look into PHP for that ? I have no clue how to do this... some examples, tutorials, whatever works... would be much appreciated.(I am a designer, not programmer...please dont speak php-ish, or you know... :D)
I've googled a bit, and found some help, but i am still confused, and much of it its not AS3, I'm thinking maybe stuff has evolved a bit since the stuff that I found(2008)...
Thank you very much.
You'd have to store (and fetch) the value somewhere - either in the DB, in a text-file, ...
I'd go search for a tutorial on PHP+MySQL. If you don't like PHP-ish, you're probably better of finding another solution though :p
Example tutorial: http://www.freewebmasterhelp.com/tutorials/phpmysql
You need to store the data you want be retrievable/update-able from multiple clients, to be stored on a server.
You can use any server side language with a database.
Server Languages : PHP, ASP.net, JSP, ColdFusion
Database : MySQL, MSSQL, PostgreSQL, Oracle, DB2 etc..
Use whatever combination you are comfortable with.
In general:
You have a web app that increments the counter in the database
call the page using URLLoader from your AS3 banner.
Database
counter_table
-------------
counter INT
PHP File
$db = mysql_connect('localhost', 'mysql_user', 'mysql_password');
mysql_select_db('database_name');
mysql_query('UPDATE counter_table SET counter = counter + 1');
AS3 Banner
// url request with your php page address
var scriptRequest:URLRequest = new URLRequest("http://www.example.com/script.php");
// loader
var scriptLoader:URLLoader = new URLLoader();
// load page to trigger database update
scriptLoader.load(scriptRequest);
Do you also want to retrieve the value of the number of clicks in Banner ?
Easy solution (really not the best :) You should use one of the other answers.. anyways, make a php file that reads txt file containing the count of visits.. and in your flashbanner just call the php file. It'll add one hit per call..
PHP:
<?php
/**
* Create an empty text file called counterlog.txt and
* upload to the same directory as the page you want to
* count hits for.
*
*
* #Flavius Frantz: YOU DONT NEED THESE:
* Add this line of code on your page:
* <?php include "text_file_hit_counter.php"; ?>
*/
// Open the file for reading
$fp = fopen("counterlog.txt", "r");
// Get the existing count
$count = fread($fp, 1024);
// Close the file
fclose($fp);
// Add 1 to the existing count
$count = $count + 1;
// Display the number of hits
// If you don't want to display it, comment out this line
//echo "<p>Page views:" . $count . "</p>";
// Reopen the file and erase the contents
$fp = fopen("counterlog.txt", "w");
// Write the new count to the file
fwrite($fp, $count);
// Close the file
fclose($fp);
?>
Example code from: (google: php counter file) http://www.totallyphp.co.uk/text-file-hit-counter
Code is not tested, but looks ok. I only commented just a little..

Categories