You are here: create file (image) based on header « PHP Programming Language « IT news, forums, messages
create file (image) based on header

Posted by christiang on 03/12/06 23:28

Hi guys, I'd like to use Alexia thumbnail service. It's an interesting
service, you pass an url to its cgi and you get a thumbnail of that
url. Of course they don't have the screenshots of all the websites so I
have to check if the image returned is fine or not. This is not that
problematic, I can read the headers and understand if it is a jpg or a
gif (the error one). The problem that I have is that sometimes although
they have the screenshot in the database they return the error one. For
different reasons I thought to backup once on a while the thumbs that I
need.

So the procedure should be something like this:

- query the cgi
- check if it is a correct image
- if it is, backup it

I'm having problem on backup it basically. To read and check the header
I do:

[code]
$a_url = parse_url($url);
if (!isset($a_url['port'])) $a_url['port'] = 80;
$errno = 0;
$errstr = '';
$timeout = 30;
if(isset($a_url['host']) &&
$a_url['host']!=gethostbyname($a_url['host'])){
$fid = fsockopen($a_url['host'], $a_url['port'], $errno, $errstr,
$timeout);
if (!$fid) return false;
$page = isset($a_url['path']) ?$a_url['path']:'';
$page .= isset($a_url['query'])?'?'.$a_url['query']:'';
fputs($fid, 'HEAD '.$page.' HTTP/1.0'."\r\n".'Host:
'.$a_url['host']."\r\n\r\n");
$head = fread($fid, 4096);
if(!preg_match('[Content-Type: image/gif]', $head) &&
preg_match('#^HTTP/.*\s+[200|302]+\s#i', $head)){
$foundThumb = true;
// here I should to create the file using the $fid resource
}
fclose($fid);
}
[/code]

Since sometimes the service fails I have to use the same stream I check
when creating the new file and I tried in many way to read the bites
and "fput" them in the new file but without success. How could I reuse
$fid to create the new file? I don't know so much fsockopen and I
didn't understand in which way I'm handling the file, it's quite weird
to me to open a socket to a server and then get the headers setting the
header in the socket. If it is like that how to make me pass the stream
and create the image without doing another call to the cgi with the
risk of having the error one?

Any help would be welcome and let me know if something is not clear.
Thanks, chr

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация