[英]Selecting images from Flickr and storing them in a Database using PHP
我正在處理一段代碼,該代碼可以從Flickr打印圖像。 根據用戶的輸入,代碼查找相應的圖像。
將圖像打印在頁面上之后,我希望能夠進行選擇,並將選擇保存到數據庫或其他地方,我可以檢索它們以進行編輯或最終用於制作自己的拼貼畫頁。
我用來檢索Flickr圖像的代碼如下:
<?php
//choices of tags, number of pictures, tag search based on given tags
//send back in Json format and decode json
$api_key = '4eb66ae95c7e8fb8dd729ebf61541d79';
if(isset($_POST['submit']))
{
$tag1 = htmlspecialchars($_POST['tag1']);
$tag2 = htmlspecialchars($_POST['tag2']);
$tag3 = htmlspecialchars($_POST['tag3']);
$tag = $tag1.",".$tag2.",".$tag3;
$perPage = 25;
$url = 'https://api.flickr.com/services/rest/?method=flickr.photos.search';
$url.= '&api_key='.$api_key;
$url.= '&tags='.$tag;
$url.= '&per_page='.$perPage;
$url.= '&format=json';
$url.= '&nojsoncallback=1';
$response = json_decode(file_get_contents($url));
$photo_array = $response->photos->photo;
//check each photo from server, and specifiy url to print
foreach($photo_array as $single_photo)
{
$farm_id = $single_photo->farm;
$server_id = $single_photo->server;
$photo_id = $single_photo->id;
$secret_id = $single_photo->secret;
$size = 'm';
$title = $single_photo->title;
$photo_url = 'http://farm'.$farm_id.'.staticflickr.com/'.$server_id.'/'.$photo_id.'_'.$secret_id.'_'.$size.'.'.'jpg';
print "<img title='".$title."' src='".$photo_url."' />";
}
}
?>
所以我的問題是,是否有一種方法可以使用DB或其他方法使打印的圖像可點擊/選擇和/或存儲圖像?
我認為您的問題對SO來說太廣泛了,但是這里有一些技巧。
首先,在img標簽上放置一些類
print_r "<img class="clickable" title='".$title."' src='".$photo_url."' />"
使用AJAX將照片網址發送到另一個PHP頁面。 這是使用jQuery的示例。
$('.body').on('click', '.clickable', function (){
//use on because your <img> is dynamic
var photo_url = $(this).prop('src);
$.ajax({
url: 'myApp/saveImg.php',
data: {'url': photo_url},
type: 'POST',
success: function(){
alert('ok');
}
error: function(){
alert('error');
}
});
您的saveImg.php是
$img_url = $_POST['url'];
$con = new PDO("mysql:host=ip_number;port=port_number;dbname=dbname", 'user', 'pass', array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8'));
$sql = 'INSERT INTO table_name VALUES("'.$img_url.'")';
$result = $con->query($sql);
return 'URL inserted';
這樣的事情。 嘗試適應您的需求。
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.