putty Posted November 2, 2007 Share Posted November 2, 2007 I need to be able to save an image generated at this address http://onearth.jpl.nasa.gov/wms.cgi?request=GetMap&layers=global_mosaic&styles=&srs=EPSG:4326&bbox=110,-45.5,160,-8&WIDTH=1280&HEIGHT=960&format=image/jpeg to my local server, what would be the best way to go about this? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/75714-save-image/ Share on other sites More sharing options...
cooldude832 Posted November 2, 2007 Share Posted November 2, 2007 you are in luck because this is already an image, a few options. I am sure you can do it with gd, but try this <?php $url = "that big url you had"; $file = file_get_contents($url); $path = "Path to save to"; if(file_put_contents($path,$file)){ echo "It worked"; } else{ echo "it didn't"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/75714-save-image/#findComment-383181 Share on other sites More sharing options...
putty Posted November 2, 2007 Author Share Posted November 2, 2007 I get an error tyring to read the file Warning: file_get_contents(http://onearth.jpl.nasa.gov/wms.cgi?request=GetMap&layers=global_mosaic&styles=&srs=EPSG:4326&bbox=110,-45.5,160,-8&WIDTH=1280&HEIGHT=960&format=image/jpeg ) [function.file-get-contents]: failed to open stream: Connection refused Quote Link to comment https://forums.phpfreaks.com/topic/75714-save-image/#findComment-383185 Share on other sites More sharing options...
cooldude832 Posted November 2, 2007 Share Posted November 2, 2007 let me see what you wrote. Quote Link to comment https://forums.phpfreaks.com/topic/75714-save-image/#findComment-383187 Share on other sites More sharing options...
putty Posted November 2, 2007 Author Share Posted November 2, 2007 <?php $url = "http://onearth.jpl.nasa.gov/wms.cgi?request=GetMap&layers=global_mosaic&styles=&srs=EPSG:4326&bbox=110,-45.5,160,-8&WIDTH=1280&HEIGHT=960&format=image/jpeg "; $file = file_get_contents($url); $path = "./map"; if(file_put_contents($path,$file)){ echo "Yahoooooo"; } else{ echo "it didn't Work"; } ?> and i have chmod ./map to 777 Quote Link to comment https://forums.phpfreaks.com/topic/75714-save-image/#findComment-383196 Share on other sites More sharing options...
cooldude832 Posted November 2, 2007 Share Posted November 2, 2007 well the chmod won't matter since we aren't getting that far, its refusing the connection meaning there is some issue opening that file, let me try it on my server see if I get the same issue. Quote Link to comment https://forums.phpfreaks.com/topic/75714-save-image/#findComment-383199 Share on other sites More sharing options...
cooldude832 Posted November 2, 2007 Share Posted November 2, 2007 I did this as I don't have 5 so I need to use the reach around for files_put_contents <?php <?php define('FILE_APPEND', 1); function file_put_contents($n, $d, $flag = false) { $mode = ($flag == FILE_APPEND || strtoupper($flag) == 'FILE_APPEND') ? 'a' : 'w'; $f = @fopen($n, $mode); if ($f === false) { return 0; } else { if (is_array($d)) $d = implode($d); $bytes_written = fwrite($f, $d); fclose($f); return $bytes_written; } } $url = "http://onearth.jpl.nasa.gov/wms.cgi?request=GetMap&layers=global_mosaic&styles=&srs=EPSG:4326&bbox=110,-45.5,160,-8&WIDTH=1280&HEIGHT=960&format=image/jpeg "; $file = file_get_contents($url); $path = "./image.jpg"; $test = file_put_contents($path,$file); echo $test; ?> however it reads the content fine (I echoed out $file and it read it), but putting it didn't work try it and see Quote Link to comment https://forums.phpfreaks.com/topic/75714-save-image/#findComment-383214 Share on other sites More sharing options...
putty Posted November 2, 2007 Author Share Posted November 2, 2007 I seem to be having the same problem with connecting. The images are coming from NASA Blue Marble maps, they are generated dynamically using some unknown GD library. Could this be what is causing the problem with file_get_contents() function? I have also tried to create the image using GD library, but with little success. $im = imagecreatefromjpeg($url1); // Attempt to open if (!$im) { // if it failes to creat image, create a black image $im = imagecreatetruecolor($Width, $Hight); $bgc = imagecolorallocate($im, 135, 206, 250); $tc = imagecolorallocate($im, 255, 255, 255); imagefilledrectangle($im, 0, 0, $Width, $Hight, $bgc); } imagejpeg($im,$fname,100); imagedestroy($im); Quote Link to comment https://forums.phpfreaks.com/topic/75714-save-image/#findComment-383250 Share on other sites More sharing options...
cooldude832 Posted November 2, 2007 Share Posted November 2, 2007 I could read the file fine on my server, saving it was an issue, let me revisit recreating it on my server using gd. What versino of php u on Quote Link to comment https://forums.phpfreaks.com/topic/75714-save-image/#findComment-383251 Share on other sites More sharing options...
cooldude832 Posted November 2, 2007 Share Posted November 2, 2007 this http://pira00.worldispnetwork.com/getimg.php is <?php $url = "http://onearth.jpl.nasa.gov/wms.cgi?request=GetMap&layers=global_mosaic&styles=&srs=EPSG:4326&bbox=110,-45.5,160,-8&WIDTH=1280&HEIGHT=960&format=image/jpeg "; $data = file_get_contents($url); $im = imagecreatefromstring($data); if ($im !== false) { header('Content-Type: image/jpg'); imagejpeg($im); } else { echo 'An error occurred.'; } ?> and it works so if it doesn't its your version of php issue Quote Link to comment https://forums.phpfreaks.com/topic/75714-save-image/#findComment-383252 Share on other sites More sharing options...
putty Posted November 2, 2007 Author Share Posted November 2, 2007 PHP Version 5.1.6 and GD enabled Quote Link to comment https://forums.phpfreaks.com/topic/75714-save-image/#findComment-383254 Share on other sites More sharing options...
putty Posted November 2, 2007 Author Share Posted November 2, 2007 I am operating from behind a fire wall with heavy security, this potentially could be causing the problem. Think its time to go have a chat with the networking guys. Thanks for the help ill let you know how it goes. Quote Link to comment https://forums.phpfreaks.com/topic/75714-save-image/#findComment-383256 Share on other sites More sharing options...
putty Posted November 2, 2007 Author Share Posted November 2, 2007 Ok got the file_get_contents($url) working, needed to change a few setting on our server. Now the problem is that file_get_contents($url) doesn’t return anything. Did you get any return data from file_get_contents($url)? Quote Link to comment https://forums.phpfreaks.com/topic/75714-save-image/#findComment-383279 Share on other sites More sharing options...
atlanta Posted November 2, 2007 Share Posted November 2, 2007 Ok got the file_get_contents($url) working, needed to change a few setting on our server. Now the problem is that file_get_contents($url) doesn’t return anything. Did you get any return data from file_get_contents($url)? The above makes no sense read it again to yourself lol Quote Link to comment https://forums.phpfreaks.com/topic/75714-save-image/#findComment-383283 Share on other sites More sharing options...
cooldude832 Posted November 2, 2007 Share Posted November 2, 2007 it does mean something, file_get_contents is a function like any other developing a given return, you must assign this return by calling the function through a variable i.e $data = file_get_contents($url); some functions lack a return (generally user-defined), and thus they can be called by saying user_Defined_function($var1,$var2...); and since the return isn't important it is not needed to assign via a variable, but it can never hurt you. Quote Link to comment https://forums.phpfreaks.com/topic/75714-save-image/#findComment-383299 Share on other sites More sharing options...
putty Posted November 2, 2007 Author Share Posted November 2, 2007 sorry I did know that I was talking about this bit of code, <?php $url = "http://onearth.jpl.nasa.gov/wms.cgi?request=GetMap&layers=global_mosaic&styles=&srs=EPSG:4326&bbox=110,-45.5,160,-8&WIDTH=1280&HEIGHT=960&format=image/jpeg "; $file = file_get_contents($url); $path = "./map"; if(file_put_contents($path,$file)){ echo "Yahoooooo"; } else{ echo "it didn't Work"; } ?> $file is empty after running $file = file_get_contents($url); Quote Link to comment https://forums.phpfreaks.com/topic/75714-save-image/#findComment-383313 Share on other sites More sharing options...
cooldude832 Posted November 2, 2007 Share Posted November 2, 2007 well i showed this isn't true on my 4.1 server so your server is having issues on it Quote Link to comment https://forums.phpfreaks.com/topic/75714-save-image/#findComment-383532 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.