wakeup1 Posted August 7, 2017 Share Posted August 7, 2017 Hi there, i am new to php and started a few months to learn it. I am trying to download images and i noticed that it is not working proberly. I tried to download a few images in loop from a site. ex. http://timesofindia.indiatimes.com/thumb.cms?msid=59470732&width=218&height=310 That link contains two parameters with the image link. The problem is that i just get back the basic image which has 100px to 100px. Which i normally get without parameters. But i am trying to get the image size which is in the link. How can i get the images? I already tried the following things: - Download with CURL- Download with file_get_contents & file_put_contents- copy() As i noticed sometimes it downloads the image with 218px to 310px and then overrides it with the 100px image, again without triggering anything. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/304543-image-download-url-with-parameters/ Share on other sites More sharing options...
Jacques1 Posted August 7, 2017 Share Posted August 7, 2017 If you want help with code, then we need to actually see that code. None of us can read your mind. Generally speaking, the recommended approach is to use cURL and generate the URL query string with http_build_query(). Quote Link to comment https://forums.phpfreaks.com/topic/304543-image-download-url-with-parameters/#findComment-1549409 Share on other sites More sharing options...
wakeup1 Posted August 8, 2017 Author Share Posted August 8, 2017 I have a array with an object that contains the link and a file name. See the code below. I have also a function which fills the array with the links, but i didn't include that here. <?php class imgLinkObj { __construct2($name,$url) { $this->filename = $name; $tihs->link = $url; } } $imgArray = array(); function getIMG($filename, $url){ $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); $rawdata=curl_exec ($ch); curl_close ($ch); $fp = fopen($filename,'w'); fwrite($fp, $rawdata); fclose($fp); } foreach($imgArray as $item){ getIMG($item->filename, $item->link) } ?> Quote Link to comment https://forums.phpfreaks.com/topic/304543-image-download-url-with-parameters/#findComment-1549469 Share on other sites More sharing options...
wakeup1 Posted August 8, 2017 Author Share Posted August 8, 2017 The 2nd version which i tried is: <?php class imgLinkObj { __construct2($name,$url) { $this->filename = $name; $tihs->link = $url; } } $imgArray = array(); foreach($imgArray as $item){ copy($item->link, $item->filename) } ?> The 3rd version is: <?php class imgLinkObj { __construct2($name,$url) { $this->filename = $name; $tihs->link = $url; } } $imgArray = array(); foreach($imgArray as $item){ $content = file_get_contents($item->link); file_put_contents($item->filename, $content); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/304543-image-download-url-with-parameters/#findComment-1549470 Share on other sites More sharing options...
Jacques1 Posted August 8, 2017 Share Posted August 8, 2017 The code you've posted isn't even syntactically valid, so that's certainly not what you're using to download the images. I have also a function which fills the array with the links, but i didn't include that here. That's not very clever, because the link code is the relevant part here. The cURL code itself is valid (except for the syntax problems and lack of error handling) and downloads the right image when used with the above URL. Quote Link to comment https://forums.phpfreaks.com/topic/304543-image-download-url-with-parameters/#findComment-1549476 Share on other sites More sharing options...
wakeup1 Posted August 8, 2017 Author Share Posted August 8, 2017 This the code how fill the array. I copied the urls by hand. <?php function fillImgArray(){ $imgLinkObj = new imgLinkObj("Atomic_Blonde.jpg", "http://timesofindia.indiatimes.com/thumb.cms?msid=59930959&width=218&height=310"); $imgArray[] = $imgLinkObj; $imgLinkObj = new imgLinkObj("Valerian_The_City.jpg", "http://timesofindia.indiatimes.com/thumb.cms?msid=59795512&width=218&height=310"); $imgArray[] = $imgLinkObj; $imgLinkObj = new imgLinkObj("Berlin_Syndrome", "http://timesofindia.indiatimes.com/thumb.cms?msid=59779421&width=218&height=310"); $imgArray[] = $imgLinkObj; $imgLinkObj = new imgLinkObj("The_Black_Prince.jpg", "http://timesofindia.indiatimes.com/thumb.cms?msid=59697033&width=218&height=310"); $imgArray[] = $imgLinkObj; $imgLinkObj = new imgLinkObj("Dunkirk.jpg", "http://timesofindia.indiatimes.com/thumb.cms?msid=59652567&width=218&height=310"); $imgArray[] = $imgLinkObj; $imgLinkObj = new imgLinkObj("War_For_The_Planet.jpg", "http://timesofindia.indiatimes.com/thumb.cms?msid=59576139&width=218&height=310"); $imgArray[] = $imgLinkObj; $imgLinkObj = new imgLinkObj("Spider_Man_Homecoming.jpg", "http://timesofindia.indiatimes.com/thumb.cms?msid=59470732&width=218&height=310"); $imgArray[] = $imgLinkObj; } fillImgArray(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/304543-image-download-url-with-parameters/#findComment-1549478 Share on other sites More sharing options...
Jacques1 Posted August 8, 2017 Share Posted August 8, 2017 That function doesn't do anything. It creates a local array and then simply discards it, so the URLs cannot possibly be processed by cURL. Something is absolutely wrong here. Either you aren't showing me your real code. Or your description of how you get the wrong image size is nonsense. With the above code, you aren't getting any images. Last chance: Delete all old images, create a single download script, test it and post the real code here. No fantasy code. Quote Link to comment https://forums.phpfreaks.com/topic/304543-image-download-url-with-parameters/#findComment-1549482 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.