Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/75714-save-image/
Share on other sites

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";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/75714-save-image/#findComment-383181
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/75714-save-image/#findComment-383185
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/75714-save-image/#findComment-383196
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/75714-save-image/#findComment-383214
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/75714-save-image/#findComment-383250
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/75714-save-image/#findComment-383252
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/75714-save-image/#findComment-383283
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/75714-save-image/#findComment-383299
Share on other sites

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/75714-save-image/#findComment-383313
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.