Forum Moderators: coopster

Message Too Old, No Replies

getting images remotely

getimagesize, fopen

         

vasile_radu

11:36 am on Mar 27, 2007 (gmt 0)

10+ Year Member



Hi, I have a problem concerning the time for getimagesize remotely.
I've searched with google and found here a post ( of ianevans ) on this topic whith a solution to the problem, that is, to copy the file on local machine and then try getimagesize function.


$asin = "0788815709";
$url = http://images.example.com/images/P/".$asin.".01.MZZZZZZZ.jpg";
$filedata = "";
$remoteimage = fopen($url, 'rb');
if ($remoteimage) {
while(!feof($remoteimage)) {
$filedata.= fread($remoteimage,1024);
}
}
fclose($remoteimage);
$localimage = fopen("/tmp/".$asin.".jpg", 'wb');
fwrite($localimage,$filedata);
fclose($localimage);
$size=getimagesize($localimage);

But I get the following warnings


Warning: fopen(/tmp/0788815709.jpg) [function.fopen]: failed to open stream: No such file or directory in C:\web\lightBT\test.php on line 13
Warning: fwrite(): supplied argument is not a valid stream resource in C:\web\lightBT\test.php on line 14

Thanks,
Radu Vasile

[edited by: coopster at 4:09 pm (utc) on Mar. 27, 2007]
[edit reason] exemplified url [/edit]

joelgreen

12:27 pm on Mar 27, 2007 (gmt 0)

10+ Year Member



Welcome to WebmasterWorld!

It is creating file in "/tmp/" folder. I assume that folder does not exist, and you have to point to existing folder, like "C:/web/tmp/"

$localimage = fopen("C:/web/".$asin.".jpg", 'wb');

vasile_radu

12:52 pm on Mar 27, 2007 (gmt 0)

10+ Year Member



Thanks for the greetings and for the fast reply,

But :) , here is the code. The directory "tmp" exists and moreover the following code succes to copy the image in tmp directory.


<?php
$asin = "0788815709";
$url = "http://images.example.com/images/P/".$asin.".01.MZZZZZZZ.jpg";
$filedata = "";
$remoteimage = fopen($url, 'rb');
if ($remoteimage) {
while(!feof($remoteimage)) {
$filedata.= fread($remoteimage,1024);
}
}
fclose($remoteimage);
$localimage = fopen("c:/web/lightBT/tmp/".$asin.".jpg", 'wb');
fwrite($localimage,$filedata);
fclose($localimage);
$size=getimagesize($localimage);
?>

But I get the following worning:


Warning: getimagesize(Resource id #2) [function.getimagesize]: failed to open stream: No such file or directory in C:\web\lightBT\test.php on line 16

Thanks again,
Radu Vasile

[edited by: coopster at 4:10 pm (utc) on Mar. 27, 2007]
[edit reason] exemplified url [/edit]

whoisgregg

1:44 pm on Mar 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld [webmasterworld.com], vasile_radu!

getimagesize [php.net] expects a string filename.

$localimage
is defined by what
fopen
returns, which is a file stream handle -- not a string filename. Change your last line:

$size=getimagesize("c:/web/lightBT/tmp/".$asin.".jpg");
print_r($size);

vasile_radu

6:58 pm on Mar 27, 2007 (gmt 0)

10+ Year Member



Thanks a lot, I've put in practice what you told me and it works. :)