-1

I have the below url of a image. It's resolution about 1500px width and 300kb. I sent the url to server via ajax and tried to save. But it's saving half of the image. Like corrupted.

Image 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";

Method -1

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

Result: image should be red color. but it's white color. the reason is not fully saved.


Method-2

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

Result: image should be red color. but it's white color. the reason is not fully saved.


Method-3 curl method. This wasn't worked. saved 0kb image.


Method-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

Result is corrupted.

Basically tried the below post methods:PHP - Copy image to my server direct from URL

If anyone have a solution please answer here.

Thank you

8
  • 3
    4k rep and you ask a question like this??? Commented Oct 26, 2017 at 12:01
  • yes, tried 4 different methods. wasn't success. Commented Oct 26, 2017 at 12:03
  • To save an image on the server you need file upload not to send a link via ajax Commented Oct 26, 2017 at 12:04
  • @ Ferhad Othman: tried with file_get_contents, curl,copy..methods. worked only copy function. image saved but half. tried with small image and worked well with copy function without uploading the image. Commented Oct 26, 2017 at 12:07
  • @apokryfos: Tried code added for your down vote. Commented Oct 26, 2017 at 12:22

1 Answer 1

2

honestly just looks like a bug with the server. also note that the server uses http/2, so it's probably not very well-tested code. in any case, i can reproduce this problem in curl in approximately 2/10 attempts, the target server just randomly close the connection before the content-length: number of bytes are read, sometimes. i ran this command 10 times

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'

and in 2/10 attempts, curl complained that the connection was closed prematurely... contact the textronic.online devs and let them know, they seemingly have problems with their http/2 server code. and until they fix it, use

curl_setopt($ch,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1); with curl to force download it with http/1.1, not http/2 - this is the equivalent of adding --http1.1 to the curl command line, which seemingly made the download stable.

(ps, no idea why so many people downvoted you, maybe the bad parts were edited away?)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. Both servers are https. Also when I try to save the canvas image to jpg using canvas.toDataURL it shows error "SecurityError: The operation is insecure.". added cross access code to htaacess fille and not worked. bot above your answer is also telling some server issue. I guess that might be a problem in our server. Thank you hanshenrik. voted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.