Help with curl

software development

#1

I’ll admit, I barely know anything about PHP, but I’m trying to convert some code from using fopen_url to using curl.

This chunk of code is what’s throwing errors for me:

[code]$img_res = imagecreatetruecolor(300, 300); //temp image used for resizing that will be deleted later

switch ($ext) {

case 'png':
	$img[$i]= imagecreatefrompng($img_url);
break;

case 'jpg':
	$img[$i]= imagecreatefromjpeg($img_url);
break;

case 'gif':
	$img[$i]= imagecreatefromgif($img_url);
break;

}[/code]

I’ve been Googling around but haven’t really found much that I can use. Any tips?


#2

The imagecreate functions require a filepath as input, so you would need to curl the remote image to a local file and then feed that local file into the function.

You could try something like this.

[php]
$local_image_filepath = ‘google.png’;
$remote_image_url = ‘http://www.google.com/images/logos/ps_logo2.png’;

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $remote_image_url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
$image_source_code = curl_exec($ch);
curl_close($ch);

file_put_contents($local_image_filepath, $image_source_code);
[/php]

Then you would just use the $local_image_filepath as the parameter in the imagecreate functions.