简体   繁体   English

使用PHP复制图像

[英]Copying an image with PHP

With permission from the other site, I am supposed to periodically download an image hosted on another site and include it in a collection of webcam photos on our site with an external link to the contributing site. 在获得其他网站的许可后,我应该定期下载托管在其他网站上的图片,并将其包含在我们网站上的网络摄像头照片集中,并带有指向贡献网站的外部链接。

This hasn't been any issue for any of the other sites, but with this particular one, I can't open the image to resize it and save it to our server. 这对任何其他网站都没有任何问题,但是对于这个特定的网站,我无法打开图像来调整大小并将其保存到我们的服务器。 It's not a hotlinking issue because I can create a plain ole' <img src="http://THEIRIMAGE /> on a page on our site and it works fine. 这不是一个热门链接问题,因为我可以在我们网站的页面上创建一个简单的' <img src="http://THEIRIMAGE /> ,它运行正常。

I've tried using $img = new Imagick($sourceFilePath) directly as with all the others, as well as trying to use PHP's copy and also trying to copy the image using cURL but when doing so, the page just times out with no results at all. 我已经尝试直接使用$img = new Imagick($sourceFilePath) ,以及尝试使用PHP的副本,并尝试使用cURL复制图像但是这样做时,页面只是超时而没有结果。

Here's the image in question: http://island-alpaca.selfip.com:10202/SnapshotJPEG?Resolution=640x480&Quality=Standard 这是有问题的图片: http//island-alpaca.selfip.com10202/SnapshotJPEG?Resolution = 640x480&Quality = Standard

Like I've said, I'm able to do this sort of thing with several other webcams, but it isn't working with this one, and I am stuck as to why it isn't. 就像我说过的那样,我可以用其他几个网络摄像头来做这类事情,但它不适用于这个,我不知道为什么它不是。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Thank you. 谢谢。

In order to reduce bandwidth and server load some sites block certain bots from accessing their content. 为了减少带宽和服务器负载,一些站点阻止某些机器人访问其内容。 Your cURL request needs to more closely mimic an actual browser, which would include a referrer (usually), user agent, etc. It could also be that there is a redirect and you haven't told cURL to follow redirects. 您的cURL请求需要更接近地模仿实际的浏览器,其中包括引荐来源(通常),用户代理等。也可能是有重定向而您没有告诉cURL遵循重定向。

Try setting more opts like this: 尝试设置更多opts,如下所示:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_ENCODING , "gzip");
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

If that doesn't work, get the user agent from an actual browser and put it in there and see if that makes a difference. 如果这不起作用,请从实际的浏览器中获取用户代理并将其放在那里,看看是否有所作为。

Try using file_get_contents() . 尝试使用file_get_contents()

for example: 例如:

$url = 'http://island-alpaca.selfip.com:10202/SnapshotJPEG?Resolution=640x480&Quality=Standard';
$outputfile = "tmp/image" . date("Y-m-d_H.i.s");
$cmd = "wget -q \"$url\" -O $outputfile";
exec($cmd);
$temp_img = file_get_contents($outputfile);
$img = new Imagick($temp_img);

Can you try this and get back to me? 你可以尝试一下并回复我吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM