Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Anyone know whether this is possible? 1

Status
Not open for further replies.

JohnnyT

Programmer
Jul 18, 2001
167
GB
Hi,
I have a form on my site that allows people to upload images from their hard drive to my site. I wondered if it was possible to allow people to enter an URL into the form, but instead of "hotlinking" the image that they enter, could a script automatically get the image and upload it to my server??

I have no idea whether this is even possible so thought I'd ask and see if anyone knows whether this could be done?

Cheers

John ;-)

I don't make mistakes, I'm merely beta-testing life.
 
Hi

It may depend on your PHP settings, but this works for me :
PHP:
[url=http://php.net/file_put_contents/][COLOR=darkgoldenrod]file_put_contents[/color][/url][teal]([/teal][COLOR=darkgoldenrod]basename[/color][teal]([/teal][navy]$imageurl[/navy][teal]),[/teal][url=http://php.net/file_get_contents/][COLOR=darkgoldenrod]file_get_contents[/color][/url][teal]([/teal][navy]$imageurl[/navy][teal]));[/teal]

Feherke.
 
Brilliant stuff...!

I'll give it a go and get back to you and let you know whether it works.

Many thanks feherke

Cheers

John ;-)

I don't make mistakes, I'm merely beta-testing life.
 
Can't do file_get_contents because it is not allowed on my server. I'm using cURL instead but I'm stuck.

I can read an image into a string from an URL but then I need to convert that string back to an "image".

So, if someone normally uploads an image from their hard drive my code checks its size and resizes it etc... with the following code:
$info = getimagesize($image);
switch($info[2]):
case 1 : $img =imagecreatefromgif($image); break;
case 2 : $img =imagecreatefromjpeg($image); break;
case 3 : $img =imagecreatefrompng($image); break;
endswitch;

But if I read the image using cURL and just end up with a string that represents the jpg or whatever how can I turn that back into an image that I can send to my "resize" function ?

Many thanks for your help

Cheers

John ;-)

Spend a few minutes remembering your loved ones,
Create a permanent memorial to commemorate their life,

 
Hi feherke,

Basically after the following code I end up with a string:

$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $imageurl);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);

Now $file_contents is a string of gobble-de-gook that basically represents an image that I've downloaded from an URL such as
If someone "uploads" an image then I have a function which updates the database. Here is the code snippet:

$memorial->update_image($ID, $_FILES['person_image']['tmp_name']);

Because it is a File upload it has extra information such as a 'tmp_name' and a 'size' etc.

However, if someone just enters an URL I just end up with a string that represents the image.

I want to take the string and make it into a valid image that I can then pass through my existing function to update the database etc.

I've just been experimenting with "imagecreatefromstring":

$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $imageurl);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);


$data = $file_contents;
$ported_image = imagecreatefromstring($data);
$memorial->update_image($ID, $ported_image);

But this doesn't seem to work....

Hope that has made things clearer

Cheers

John ;-)

Spend a few minutes remembering your loved ones,
Create a permanent memorial to commemorate their life,

 
Hi

Well, then depends on [tt]$memorial->update_image()[/tt]. Probably you will have to rewrite it to accept as parameter image data too, not only file name. Or you just write the data into a file and pass the name to the method. Something like this :
Code:
[navy]$data[/navy] [teal]=[/teal] [navy]$file_contents[/navy][teal];[/teal]
[navy]$ported_image[/navy] [teal]=[/teal] [COLOR=darkgoldenrod]imagecreatefromstring[/color][teal]([/teal][navy]$data[/navy][teal]);[/teal]
[highlight][navy]$ported_image_file[/navy][teal]=[/teal][navy]$tempdir[/navy][teal].[/teal][COLOR=darkgoldenrod]basename[/color][teal]([/teal][navy]$imageurl[/navy][teal]);[/teal][/highlight]
[highlight][COLOR=darkgoldenrod]file_put_contents[/color][teal]([/teal][navy]$ported_image_file[/navy][teal],[/teal][navy]$data[/navy][teal]);[/teal][/highlight]
[navy]$memorial[/navy][teal]->[/teal][COLOR=darkgoldenrod]update_image[/color][teal]([/teal][navy]$ID[/navy][teal],[/teal] [highlight][navy]$ported_image_file[/navy][/highlight][teal]);[/teal]

Feherke.
 
feherke,

You're a genius!!

It worked a treat. I put the image string into another file with file_put_contents and then read that into my function. Just as you have written in your post.

A great many thanks for all your efforts. They are much appreciated!


Many thanks again

John ;-)

Spend a few minutes remembering your loved ones,
Create a permanent memorial to commemorate their life,

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top