I need to give my images an extension, depending on their type and also create a completely new name for them. Can I get the extension before I send the file to the server?
$_POST['image'] is a URL of an image. $new_name is supposed to be added while running the switch.
$image = $_POST['image'];
$ch = curl_init($image);
curl_setopt($ch, CURLOPT_HEADER, 0);
$imagedata = curl_exec($ch);
list($width, $height, $type) = getimagesize($filename);
switch ($type) {
case 1: ....... //Give the filename a .gif extension and so on for every filetype
}
$fp = fopen('../images/' . $new_name, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_close($ch);
fclose($fp);
!FOUND ANSWER! (sorry if I'm answering my own question wrong (in means of simply "editing" the question), comment on how to do it correctly, if I'm wrong ;)
I simply need to run the cURL function 2 times, but with different names. The first one simply gets me the info about the file, and the second one saves it wit the $new_name
$image = $_POST['image'];
$ch = curl_init($image); curl_setopt($ch, CURLOPT_HEADER, 0);
$imagedata = curl_exec($ch); $info = curl_getinfo($ch,
CURLINFO_CONTENT_TYPE); curl_close($ch);
if($info == "image/jpeg") //Running a code that will give the image an
appropriate extension...
$cf = curl_init($image); $fp = fopen('../images/' . "image.jpg",
'wb'); curl_setopt($cf, CURLOPT_FILE, $fp); curl_exec($cf);
curl_close($cf); fclose($fp);
Related
I'm keen to understand the difference between saving a .gif or .jpg (which seem to work fine using the code below), but does not work if I change it to .png
So this code works fine for .jpg or .gif images (links ending in .jpg or .gif)
$img = "http://www.anylocation.com/image.gif";
$ch = curl_init($img);
$fp = fopen($currentdir . 'image.gif', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
echo "<b>Image stored...<br>";
But if I do this:
$img = "http://www.anylocation.com/image.png";
$ch = curl_init($img);
$fp = fopen($currentdir . 'image.png', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
echo "<b>Image stored...<br>";
It doesn't save the image, well, it tries to, but it just writes image.png with 0 byte size and no content.
Ideally I'd like to modify the above code so that it will work for any scenario or type of image. If not, and the code has to be different for .png, I could write in a 'check the extension of $img' and direct it to the correct piece of code.
Could anyone assist with the explanation of why this doesn't work with .png and if possible, any changes that I could make to make it work?
Your code can reduce to ...
$img = "http://www.anylocation.com/image.png";
file_put_contents($currentdir . 'image.png', file_get_contents($img));
I've been trying to figure out how to upload a file from a URL using the following code without using a form. In a form I can enter in a URL instead of a local file on the computer and it uploads it, So im guessing its possible?:
example location: http://www.bubblews.com/assets/images/news/521013543_1385596410.jpg
if ((isset($_FILES['upload']['name'])) && (substr_count($_FILES['upload']['type'],'image/'))) {
$prefix = "market/".$market->guid;
$filehandler = new ElggFile();
$filehandler->owner_guid = $market->owner_guid;
$filehandler->setFilename($prefix . ".jpg");
$filehandler->open("write");
$filehandler->write(get_uploaded_file('upload'));
$filehandler->close();
Try this example:
Saving image from PHP URL
$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);
or this
$url = 'http://example.com/image.php';
$img = '/my/folder/flower.gif';
file_put_contents($img, file_get_contents($url));
In linux hosts you can try
exec("wget ".$_POST['url']);
I have the follow code:
$image = imagecreatefromstring(file_get_contents('http://externaldomain.com/image.png'));
And want to make a curl request:
$files = array();
$files[] = '#' . $image['tmp_name'] . ';filename=' . $image['name'] . ';type=' . $image['type'];
$ch = curl_init('index.php?route=upload');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $files);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
The main problem is, how to get the file name and extension from $image variable?
I can replace $image['tmp_name'] with tempnam(), but other things?
Have another way to do?
imagecreatefromstring returns a GD handle. There is no tmp_name, name, OR type associated with it. You're trying to treat that GD handle as if it was the result of a standard POST-based $_FILES data structure.
a GD handle cannot be passed to curl for upload, because it's not really an image, and won't be until you use imagejpeg/imagepng/imagewhatever to save it out to a file, which you can then point curl at, e.g.
$image = imagecreatefromstring(...);
imagejpeg($image, 'tempfile.jpg');
$files[] = '#tempfile.jpg;filename=tempfile.jpg;type=image/jpeg';
curl...
I was looking for a way to perform a number of tasks in PHP
Get a file from another server
Change the file name and extention
Download the new file to the end user
I would prefer a method that acts as a proxy server type thing, but a file download would be fine
Thanks in advance
Try this
<?php
$url = 'http://www.example.com/a-large-file.zip';
$path = '/path-to-file/a-large-file.zip';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
file_put_contents($path, $data);
?>
After you save rename the file with whatever name you need
Refer this
http://www.php.net/manual/en/ref.curl.php
See the example at http://www.php.net/manual/en/function.curl-init.php
This grabs the data and outputs it straight to the browser, headers and all.
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);
I use something like this:
<?php
$url = 'http://www.some_url.com/some_file.zip';
$path = '/path-to-your-file/your_filename.your_ext';
function get_some_file($url, $path){
if(!file_exists ( $path )){
$fp = fopen($path, 'w+');
fwrite($fp, file_get_contents($url));
fclose($fp);
}
}
?>
I wrote a PHP script that allows me to get the dimensions (width and height) of a remotely hosted JPG without having to download it in full (just the first 10K).
The problem with this is I write the partial download to a file, then read that file to extract the information I need (using getImageSize).
I know this can be down without writing to disk, but I do not know how.
Anyone have suggestions/solutions?
Here is my original code:
function remoteImage($url){
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
curl_setopt($ch, CURLOPT_RANGE, "0-10240");
$fn = "partial.jpg";
$raw = curl_exec($ch);
$result = array();
if(file_exists($fn)){
unlink($fn);
}
if ($raw !== false) {
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($status == 200 || $status == 206) {
$result["w"] = 0;
$result["h"] = 0;
$fp = fopen($fn, 'x');
fwrite($fp, $raw);
fclose($fp);
$size = getImageSize($fn);
if ($size===false) {
// Cannot get file size information
} else {
// Return width and height
list($result["w"], $result["h"]) = $size;
}
}
}
curl_close ($ch);
return $result;
}
My original question, which lead to this, is here - and might be helpful.
It may be possible to use a memory file stream.
$fn = 'php://memory';
See: http://php.net/manual/en/wrappers.php.php