【问题标题】:PHP save large parameterised image to serverPHP将大型参数化图像保存到服务器
【发布时间】:2018-04-07 19:19:53
【问题描述】:

我有一张图片的以下网址。它的分辨率约为 1500px 宽度和 300kb。 我通过 ajax 将 url 发送到服务器并尝试保存。但它节省了一半的图像。就像损坏了。

图片网址:

$url = "https://textronic.online/WEB_API_TDS/v1/img?part=SHSLIM&swatch=F1A0F99B/part=9038004B&swatch=F1A0F99B/part=7FEA98C3&pair=9038004B&swatch=F1A0F99B&pair=9038004B/part=BTBLACK&pair=9038004B&swatch=F1A0F99B&pair=9038004B/part=BHBLACK&pair=9038004B&swatch=F1A0F99B&pair=9038004B/part=EA8BD88C&swatch=F1A0F99B/part=7FEA98C3&pair=EA8BD88C&swatch=F1A0F99B&pair=EA8BD88C/part=BTBLACK&pair=EA8BD88C&swatch=F1A0F99B&pair=EA8BD88C/part=BHBLACK&pair=EA8BD88C&swatch=F1A0F99B&pair=EA8BD88C/part=E339C530&swatch=F1A0F99B/part=7FEA98C3&pair=E339C530&swatch=F1A0F99B&pair=E339C530/part=BTBLACK&pair=E339C530&swatch=F1A0F99B&pair=E339C530/part=BHBLACK&pair=E339C530&swatch=F1A0F99B&pair=E339C530/part=B5CB81BC&swatch=F1A0F99B/part=7FEA98C3&pair=B5CB81BC&swatch=F1A0F99B&pair=B5CB81BC/part=BTBLACK&pair=B5CB81BC&swatch=F1A0F99B&pair=B5CB81BC/part=BHBLACK&pair=B5CB81BC&swatch=F1A0F99B&pair=B5CB81BC/part=REGULARBOTTOM&swatch=F1A0F99B/part=7FEA98C3&swatch=F1A0F99B/part=DIAMOND&swatch=F1A0F99B/part=BOXPLEAT&swatch=F1A0F99B/part=EPNO&swatch=F1A0F99B/part=BHBLACK&swatch=F1A0F99B/part=BTBLACK&swatch=F1A0F99B/view=face";

方法-1

$contents=file_get_contents($url);
$save_path="dimage.jpg";
file_put_contents($save_path,$contents);

结果:图像应为红色。但它是白色的。原因未完全保存。


方法二

copy($url, 'image2.jpg');

结果:图像应为红色。但它是白色的。原因未完全保存。


方法3 卷曲方法。这没有奏效。保存了 0kb 的图像。


方法4

header("content-type: image/jpg");
$qr_image = imagecreatefrompng($url); 

$save = getcwd()."image4.jpg";
imagejpeg($qr_image,$save); //save the file to $save path
imagejpeg($qr_image); //display the image

结果已损坏。

基本尝试了以下发帖方式:PHP - Copy image to my server direct from URL

如果有人有解决方案,请在这里回答。

谢谢

【问题讨论】:

  • 4k 代表,你问这样的问题???
  • 是的,尝试了 4 种不同的方法。没有成功。
  • 要在服务器上保存图像,您需要文件上传而不是通过 ajax 发送链接
  • @Ferhad Othman:尝试使用 file_get_contents、curl、copy..methods。仅工作复制功能。图像保存了一半。尝试使用小图像并且在不上传图像的情况下使用复制功能效果很好。
  • @apokryfos:为您的反对票添加了尝试过的代码。

标签: php image curl


【解决方案1】:

老实说,这看起来像是服务器的一个错误。另请注意,服务器使用 http/2,因此它可能不是经过良好测试的代码。无论如何,我可以在大约 2/10 次尝试中在 curl 中重现此问题,有时,目标服务器只是在读取 content-length: 字节数之前随机关闭连接。我运行了这个命令 10 次

curl -v 'https://textronic.online/WEB_API_TDS/v1/img?part=SHSLIM&swatch=F1A0F99B/part=9038004B&swatch=F1A0F99B/part=7FEA98C3&pair=9038004B&swatch=F1A0F99B&pair=9038004B/part=BTBLACK&pair=9038004B&swatch=F1A0F99B&pair=9038004B/part=BHBLACK&pair=9038004B&swatch=F1A0F99B&pair=9038004B/part=EA8BD88C&swatch=F1A0F99B/part=7FEA98C3&pair=EA8BD88C&swatch=F1A0F99B&pair=EA8BD88C/part=BTBLACK&pair=EA8BD88C&swatch=F1A0F99B&pair=EA8BD88C/part=BHBLACK&pair=EA8BD88C&swatch=F1A0F99B&pair=EA8BD88C/part=E339C530&swatch=F1A0F99B/part=7FEA98C3&pair=E339C530&swatch=F1A0F99B&pair=E339C530/part=BTBLACK&pair=E339C530&swatch=F1A0F99B&pair=E339C530/part=BHBLACK&pair=E339C530&swatch=F1A0F99B&pair=E339C530/part=B5CB81BC&swatch=F1A0F99B/part=7FEA98C3&pair=B5CB81BC&swatch=F1A0F99B&pair=B5CB81BC/part=BTBLACK&pair=B5CB81BC&swatch=F1A0F99B&pair=B5CB81BC/part=BHBLACK&pair=B5CB81BC&swatch=F1A0F99B&pair=B5CB81BC/part=REGULARBOTTOM&swatch=F1A0F99B/part=7FEA98C3&swatch=F1A0F99B/part=DIAMOND&swatch=F1A0F99B/part=BOXPLEAT&swatch=F1A0F99B/part=EPNO&swatch=F1A0F99B/part=BHBLACK&swatch=F1A0F99B/part=BTBLACK&swatch=F1A0F99B/view=face'

在 2/10 次尝试中,curl 抱怨连接过早关闭...联系 textronic.online 开发人员并让他们知道,他们的 http/2 服务器代码似乎有问题。在他们修复它之前,使用

curl_setopt($ch,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1); 使用 curl 强制使用 http/1.1,而不是 http/2 下载它 - 这相当于在 curl 命令行中添加 --http1.1,这似乎使下载稳定。

(ps,不知道为什么这么多人对你投了反对票,也许不好的部分被删掉了?)

【讨论】:

  • 感谢您的回答。两台服务器都是https。此外,当我尝试使用 canvas.toDataURL 将画布图像保存为 jpg 时,它会显示错误“SecurityError: The operation is insecure.”。将交叉访问代码添加到 htaac​​ess fille 并且不起作用。您的答案上方的机器人也说明了一些服务器问题。我想这可能是我们服务器的问题。谢谢你,汉申里克。投票。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
相关资源
最近更新 更多