如何在函数/类中将图像作为参数传递,或者这是不可能的,如果可以的话我该如何解决?例如:
session_start();
require_once("twitteroauth/twitteroauth.php"); //Path to twitteroauth library
$twitteruser = "xxx";
$notweets = 30;
$consumerkey = "xxxx";
$consumersecret = "xxx";
$accesstoken = "xxx";
$accesstokensecret = "xxx";
//use the php similar_text function to remove similar tweets
function removeSimilar ($array, $minSimilarity = 60) {
$result = [];
foreach ($array as $outerValue) {
$append = true;
foreach ($result as $key => $innerValue) {
$similarity = null;
similar_text($innerValue, $outerValue, $similarity);
if ($similarity >= $minSimilarity) {
if (strlen($outerValue) > strlen($innerValue)) {
// always keep the longer one
$result[$key] = $outerValue;
}
$append = false;
break;
}
}
if ($append) {
$result[] = $outerValue;
}
}
return $result;
}
function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
$connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
return $connection;
}
$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
$tweets = $connection->get("https://api.twitter.com/1.1/search/tweets.json?q=london+exclude:retweets+exclude:replies&since=2019-03-01&count=100");
$encode = json_encode($tweets);
$decode = json_decode($encode,true);
$decode = removeSimilar($decode);
foreach($decode['statuses'] as $tweets) {
echo '<p>' . $tweets['text'] . '</p>';
}
我已经尝试过dimage.src和其他方法,但我使用的任何方法似乎都不起作用:/
答案 0 :(得分:2)
像其他任何方式一样传递它,但是要显示它,请使用appendChild
而不是innerHTML
:
var tree = new Image();
tree.src = "https://upload.wikimedia.org/wikipedia/commons/thumb/e/eb/Ash_Tree_-_geograph.org.uk_-_590710.jpg/220px-Ash_Tree_-_geograph.org.uk_-_590710.jpg";
function showTree(dimage) {
document.getElementById("div").appendChild(dimage);
console.log(dimage);
}
showTree(tree);
<div id="div"></div>
如果您确实想使用innerHTML
,请制作一个<img>
为src
的{{1}}:
dimage.src
var tree = new Image();
tree.src = "https://upload.wikimedia.org/wikipedia/commons/thumb/e/eb/Ash_Tree_-_geograph.org.uk_-_590710.jpg/220px-Ash_Tree_-_geograph.org.uk_-_590710.jpg";
function showTree(dimage) {
document.getElementById("div").innerHTML = "<img src='" + dimage.src + "'>";
console.log(dimage);
}
showTree(tree);
答案 1 :(得分:1)
您必须命名函数并通过将图像作为参数传递来调用它。如果您使用的是innerHTML
,则必须创建图像标签,并将接收到的图像源作为参数添加到函数中
var tree = new Image();
tree.src = "img/statobj/tree.png"
function a(dimage){
document.getElementById("myitems").innerHTML ='<div id="invetoryitem" ><img src="'+dimage.src+ '"></div>'
console.log(dimage) //gets undefined
}
a(tree);
<body id="myitems"></body>