我一直致力于一个项目,我必须获取用户提供的网址输入中的所有图像,并保存用户找到并选择的所有图像的副本到他的帐户。 我只想在这里寻找更好的解决方案。除了从iframe中打开的当前页面保存图像之外,我甚至无法尝试任何操作。 请提出解决方案。
答案 0 :(得分:1)
对不起,我在问题中看到*保存用户找到并选择的所有图像的副本到他的帐户,所以我看到你只想保存用户选择的所选图像我发布的内容不正确下面有更多代码。 。
<?php
$allowed_formats = array("png", "jpg", "gif");//Allowed image formats to save image as
$users_directory = "image-directory";//directory to save the images when user selects image to save
if ( isset( $_POST['view'] ) ) {//If url form submitted
$users_submitted_url = $_POST['url'];//users submitted url
$ch = curl_init();//Curl the page
curl_setopt($ch, CURLOPT_URL , $users_submitted_url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$web_images_response = curl_exec ($ch);
curl_close($ch);
preg_match_all('@<img src="http://(.*?)"@', $web_images_response, $imgs);//match only full image links
$image_divs .= '';
foreach($imgs['1'] as $image_url){
$image_divs .= "<div><a href='".$PHP_SELF."?url=".$image_url."' target='_blank'><img height='200px' height='200px' src='http://".$image_url."'></a></div>";//make images if clicked use get method to save the image
}
}
if ($_GET["url"] == ""){//If url to save is blank do nothing else save image
}
else{
$split_image = pathinfo($_GET["url"]);
if (in_array($split_image['extension'], $allowed_formats)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL , "http://".$_GET["url"]);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response= curl_exec ($ch);
curl_close($ch);
$file_name = $users_directory."/".$split_image['filename'].".".$split_image['extension'];//save image as file name and extension
$file = fopen($file_name , 'w') or die("X_x");
fwrite($file, $response);
fclose($file);
}
}
?>
<form method="post">
URL:<input name="url" value="http://piic.us/gallery.php" onclick="this.value='';"/>
<input name="view" value="Submit Query" type="submit" />
</form>
<?php echo $image_divs;?>
如果用户点击图像,它会保存到那个目录,我希望这次能正确使用。