Jump to content

Save image


putty

Recommended Posts

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

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.