473,772 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2678
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_matc h('[Content-Type: image/gif]', $head));
print("second: ".preg_match('# ^HTTP/.*\s+[200|302]+\s#i', $head));
//print($head."<b r>");
if(!preg_match( '[Content-Type: image/gif]', $head) &&
preg_match('#^H TTP/.*\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($ourFileH andle,$content) ;
fclose($ourFile Handle);
}
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('#^H TTP/.*\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($ourFileH andle,$fid);
fclose($ourFile Handle);
}

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

Mar 12 '06 #2
ch********@late ral.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.ale xa.com/image_server.cg i?size=small&ur l=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
7100
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 disadvantages of using a MySQL blob field rather than reading the images directly from the file? How does one insert an image into a blob field? Can it be done dynamically? Thank you John
2
3228
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. Thanks.
0
1719
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 figuring these out.... I am in a process of moving it over to another server, however running into a few things: 1. One of the links is to upload your own logo in a BMP format and the
8
4525
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 problem and the potential for deadlocks. After pouring over the available information, and trying a few experiments, I am still left with a few questions and issues I hope someone out there can shed some light on.
6
3691
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 the document to my clients so that they could use it to. I know most of them use Outlook XP or Outlook 2003, so what I created was a page that creates a Visual Basic script that, when saved to the desktop and
5
6777
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 cause. If I could create a large file that could be encrypted, and maybe add files to it by appending them and putting in some kind of delimiter between files, maybe a homemade version of truecrypt could be constructed. Any idea what it...
4
1904
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 = ImageColorAllocate($image,0,0,0); ImageLine($image,0,0,$W,$H,$black); ImagePng($image);
7
4136
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 the graph image to his hard drive, just like right-clicking on the image and selecting 'save image as...' There's a link next to the graph that should open the save image dialog. The link calls the following php script:
12
4019
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 time. The second may help me to know if an image has been downloaded and how many times. How to build such download link ? The informations I'd like is the userid, the datetime limit and the imagefile, off course everything must be encrypted...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9911
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7460
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6713
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.