473,416 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

create file (image) based on header

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:

Expand|Select|Wrap|Line Numbers
  1. $a_url = parse_url($url);
  2. if (!isset($a_url['port'])) $a_url['port'] = 80;
  3. $errno = 0;
  4. $errstr = '';
  5. $timeout = 30;
  6. if(isset($a_url['host']) &&
  7. $a_url['host']!=gethostbyname($a_url['host'])){
  8. $fid = fsockopen($a_url['host'], $a_url['port'], $errno, $errstr,
  9. $timeout);
  10. if (!$fid) return false;
  11. $page = isset($a_url['path'])  ?$a_url['path']:'';
  12. $page .= isset($a_url['query'])?'?'.$a_url['query']:'';
  13. fputs($fid, 'HEAD '.$page.' HTTP/1.0'."\r\n".'Host:
  14. '.$a_url['host']."\r\n\r\n");
  15. $head = fread($fid, 4096);
  16. if(!preg_match('[Content-Type: image/gif]', $head) &&
  17. preg_match('#^HTTP/.*\s+[200|302]+\s#i', $head)){
  18. $foundThumb = true;
  19. // here I should to create the file using the $fid resource
  20. }
  21. fclose($fid);
  22. }
  23.  
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

Mar 12 '06 #1
3 2650
If I use fopen($url) I'm able to copy the stream:

$buffer = fopen($url,"r");
if ($buffer)
{
$head = fread($buffer, 4096);
print("first: ".preg_match('[Content-Type: image/gif]', $head));
print("second: ".preg_match('#^HTTP/.*\s+[200|302]+\s#i', $head));
//print($head."<br>");
if(!preg_match('[Content-Type: image/gif]', $head) &&
preg_match('#^HTTP/.*\s+[200|302]+\s#i', $head)){
print("found image!<br>");
$foundThumb = true;
$content = $head;
while (!feof($buffer))
{
$content .= fread($buffer, 8192);
}
$ourFileHandle = fopen($this->thumbs_folder."/".$name, 'w');
fputs($ourFileHandle,$content);
fclose($ourFileHandle);
}
fclose($buffer);
}

but then my regex don't work anymore. I tried to create put together
the stream in my previous code:

if(!preg_match('[Content-Type: image/gif]', $head) &&
preg_match('#^HTTP/.*\s+[200|302]+\s#i', $head)){
print("found image!<br>");
$foundThumb = true;
$content = $head;
while (!feof($fid))
{
$content .= fread($fid, 8192);
}
$ourFileHandle = fopen($this->thumbs_folder."/".$name, 'w');
fputs($ourFileHandle,$fid);
fclose($ourFileHandle);
}

but the files are corrupted (ridiculous small size). Any clue?

Mar 12 '06 #2
ch********@lateral.net wrote:
but the files are corrupted (ridiculous small size). Any clue?


Send some details like where this service can be found, so we can try it for
ourselfs.
JW
Mar 12 '06 #3
$url =
"http://pthumbnails.alexa.com/image_server.cgi?size=small&url=http://www.apple.com";

thanks, chr

Mar 13 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: John | last post by:
I have over 5000 thumbnail pictures of size 5kb each. I would like to able to load all 5000 pictures and view 50 per page using mysql_data_seek(). I would like to know what are the advantages and...
2
by: StinkFinger | last post by:
All, Does anyone know of a script that already exists that would allow me to pass in the name of a JPG or GIF and spit out a PDF ? Just checking around before I buckle down and write one....
0
by: Eugene Borukhovich | last post by:
Hello, I just got a client that is running their PHP site on Apache 1.3 with PHP 4.x. The server is very weak and during peak times hangs. I am not a programmer, so having a little dificulty...
8
by: Ted Miller | last post by:
Hi folks, I'm looking at moving a large base of C++ code to .Net under tight time constraints. The code runs in mission-critical environments, and I am extremely concerned about the loader lock...
6
by: windandwaves | last post by:
Hi Folk Some of my clients asked me to create "fancy emails" for them (aka html formatted emails). I know how to make a nice html document, but I had trouble creating a simple way to provide...
5
by: Michael Sperlle | last post by:
Is it possible? Bestcrypt can supposedly be set up on linux, but it seems to need changes to the kernel before it can be installed, and I have no intention of going through whatever hell that would...
4
by: Gunnar G | last post by:
Hi. I've created an image with this code Header("Content-Type: image/png"); $image = ImageCreate($W,$H); $white = ImageColorAllocate($image,255,255,255); $black =...
7
by: shadowman | last post by:
Hello all: I have a web app that creates an image of a graph (as a png), based on user input of a combination of drop-down box items. I'm trying to add a function that allows the user to save...
12
by: Bob Bedford | last post by:
I've to build a website where the customer can buy an image. I'm thinking in the 2 ways to let people get those images: receive them by Email or send them an image link wich validity is limited in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.