PHP Save Image File

PHP save image file

No need to create a GD resource, as someone else suggested.

$input = 'http://images.websnapr.com/?size=size&key=Y64Q44QLt12u&url=http://google.com';
$output = 'google.com.jpg';
file_put_contents($output, file_get_contents($input));

Note: this solution only works if you're setup to allow fopen access to URLs. If the solution above doesn't work, you'll have to use cURL.

Saving image from PHP URL

If you have allow_url_fopen set to true:

$url = 'http://example.com/image.php';
$img = '/my/folder/flower.gif';
file_put_contents($img, file_get_contents($url));

Else use cURL:

$ch = curl_init('http://example.com/image.php');
$fp = fopen('/my/folder/flower.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

Save image from url to local System in php

The problem is that the "images" you are using in the file_put_contents is not getting the path that where to save the file. try to make the images folder along with php file which having this code and see its working.

<?php
$imageUrl = 'https://cwsimages.ingramtest.com/cdsImages/imageloader?id=pBbFysOWRLJoSy4l4lbc+yLblU6JMuhKpze3XsQNO+njA3/XYRYbXSEYYsSqKXoiGD07duAyOSVXNUVLvxDqlMx15WtRQWJn3xC/twmM2s62tw+XgriCmEXBHawun03pQLBHXLuEQhNmCb8MC3ZMNH7pe5O76s18u/mgplf8YtU=';
@$rawImage = file_get_contents($imageUrl);
if($rawImage)
{
file_put_contents("images/".'dummy1.png',$rawImage);
echo 'Image Saved';
}
else
{
echo 'Error Occured';
}
?>

How to save uploaded image to file using php

Use $fullpath_2 in your move_uploaded_file(). change upload directiry to $uploaddir = $_SERVER['DOCUMENT_ROOT'] . '/Halpper/';

if (isset($_FILES['image']['name'])) {
/***********************************************************
* 1 - Upload Original Image To Server
***********************************************************/
//Get Name | Size | Temp Location
$ImageName = $_FILES['image']['name'];
$ImageSize = $_FILES['image']['size'];
$ImageTempName = $_FILES['image']['tmp_name'];

//Get File Ext
$ImageType = @explode('/', $_FILES['image']['type']);
$type = $ImageType[1]; //file type
//Set Upload directory
$uploaddir = $_SERVER['DOCUMENT_ROOT'] . '/Halpper/';
//Set File name
$file_temp_name = $profile_id . '_original.' . md5(time()) . 'n' . $type; //the temp file name
$fullpath = $uploaddir . "/" . $file_temp_name; // the temp file path
$file_name = $profile_id . '_temp.jpeg'; //$profile_id.'_temp.'.$type; // for the final resized image
$finalname = $profile_id . md5(time());
$fullpath_2 = "assets/images/profile_pics/" . $finalname . "n.jpg"; //for the final resized image
//Move the file to correct location
if (move_uploaded_file($ImageTempName, $uploaddir . $fullpath_2)) {
chmod($uploaddir . $fullpath_2, 0777);
}
//Check for valid uplaod
if (!$move) {
die ('File didnt upload');
} else {
$imgSrc = "assets/images/profile_pics/" . $file_name; // the image to display in crop area
$msg = "Upload Complete!"; //message to page
$src = $file_name; //the file name to post from cropping form to the resize
}
}

Saving a remote image programmatically with PHP

You can use PHP's copy function to copy remote files to a location on your server:

copy("https://example.com/some_image.jpg", "/path/to/file.jpg");

http://php.net/manual/en/function.copy.php

Saving image straight to directory in PHP?

Yes, it is very simple. Here is a little cURL script to do just that:

$image_link = "http://images5.fanpop.com/image/photos/31100000/random-random-31108109-500-502.jpg";//Direct link to image
$split_image = pathinfo($image_link);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL , $image_link);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response= curl_exec ($ch);
curl_close($ch);
$file_name = "images-folder/".$split_image['filename'].".".$split_image['extension'];
$file = fopen($file_name , 'w') or die("X_x");
fwrite($file, $response);
fclose($file);

This should do what you want. It will save the image to the directory and then name the image as random-random-31108109-500-502<-filename .jpg<-extension.

Saving image file name exactly as URL image file into folder in PHP

I would do this way

$url = "http://www.domain.com/logo.png";

$file = file_get_contents($url);
$path = "C:/xampp/htdocs/proj1/download/". basename($url);

return !file_exists($path) ? file_put_contents($path, $file) : false;

PHP image upload function, save in a dir and then return save image url

Here's a simple one.

HTML form to upload image

<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>

Your PHP file that does the Upload

<?php

$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo "<p>";

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}

echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";

?>

Source



Related Topics



Leave a reply



Submit