Jump to content

Image Download - URL with parameters


wakeup1

Recommended Posts

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

Link to comment
Share on other sites

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)
}

?>


Link to comment
Share on other sites

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);
}

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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();
?>
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.