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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String to image 2

Status
Not open for further replies.

jasc2k

Programmer
Joined
Nov 2, 2005
Messages
113
Location
GB
Hi all,
I have written a simple function that turns a string passed to the function into an image (for email address protection) only I want to be able to call it from other pages but when I do it just returns garbage (binary possibly?)

Code:
	/**
    * imageCreatex - this little function will
    * turn any given string into an image.
    */
	function imageCreatex($string){
		//header("Content-type: image/png");
		//*******MAKE IT SEND AN ERROR
		
		$image = @imagecreate(110, 20)
			or die("Cannot Initialize new GD image stream");
			
		$background_color = imagecolorallocate($image, 255, 255, 255);
		$text_color = imagecolorallocate($image, 233, 14, 91);
		imagestring($image, 1, 5, 5,  $string, $text_color);
		imagepng($image);
		return $image;
		imagedestroy($image);
	}

I cannot use header("Content-type: image/png"); because I have allready changed the headers becasue I use PHP includes to update page content?

Please anyone with any ideas?
Thanks, James
 
Hi

As far as I understand, you are doing the good old server-side beginner's mistake : trying to deliver more than one content for a request. Which is not possible with HTTP 1.1.

Of course, you could show us how and from where are you calling that function, so we can be sure...

But in meantime one thing is absolutely sure : the [tt]Content-type: image/png[/tt] HTTP header must be sent.

Ok, there is one more absolutely sure thing there : the [tt]imagedestroy()[/tt] ( or anything else ) is useless after the [tt]return[/tt] statement.

Feherke.
 
ok good guess I am a beginner lol but thanks for the reply

the function is in a file called database.php I am calling it via $database->imageCreatex("test")

as for the destroy I thought that was the case but moved it for testing - I will put it back

If I put the header back in I then get an error telling me that I have allready sent the headers?

Any ideas?
 
Hi

If you want relevant help on this, you will really have to show us more code.

For a generic solution :
Code:
[b]function[/b] [COLOR=darkgoldenrod]imageCreatex[/color][teal]([/teal][navy]$string[/navy][teal])[/teal]
[teal]{[/teal]
  [navy]$filename[/navy][teal]=[/teal][COLOR=darkgoldenrod]md5[/color][teal]([/teal][green][i]'jasc2k'[/i][/green][teal].[/teal][navy]$string[/navy][teal]).[/teal][green][i]'.png'[/i][/green][teal];[/teal]

  [b]if[/b] [teal]([/teal][COLOR=darkgoldenrod]file_exists[/color][teal]([/teal][navy]$filename[/navy][teal]))[/teal] [b]return[/b] [navy]$filename[/navy][teal];[/teal]

  [navy]$image[/navy] [teal]=[/teal] [navy]@imagecreate[/navy][teal]([/teal][purple]110[/purple][teal],[/teal] [purple]20[/purple][teal])[/teal] [b]or[/b] [b]die[/b][teal]([/teal][green][i]"Cannot Initialize new GD image stream"[/i][/green][teal]);[/teal]

  [navy]$background_color[/navy] [teal]=[/teal] [COLOR=darkgoldenrod]imagecolorallocate[/color][teal]([/teal][navy]$image[/navy][teal],[/teal] [purple]255[/purple][teal],[/teal] [purple]255[/purple][teal],[/teal] [purple]255[/purple][teal]);[/teal]
  [navy]$text_color[/navy] [teal]=[/teal] [COLOR=darkgoldenrod]imagecolorallocate[/color][teal]([/teal][navy]$image[/navy][teal],[/teal] [purple]233[/purple][teal],[/teal] [purple]14[/purple][teal],[/teal] [purple]91[/purple][teal]);[/teal]
  [COLOR=darkgoldenrod]imagestring[/color][teal]([/teal][navy]$image[/navy][teal],[/teal] [purple]1[/purple][teal],[/teal] [purple]5[/purple][teal],[/teal] [purple]5[/purple][teal],[/teal]  [navy]$string[/navy][teal],[/teal] [navy]$text_color[/navy][teal]);[/teal]
  [COLOR=darkgoldenrod]imagepng[/color][teal]([/teal][navy]$image[/navy][teal],[/teal][navy]$filename[/navy][teal]);[/teal]
  [COLOR=darkgoldenrod]imagedestroy[/color][teal]([/teal][navy]$image[/navy][teal]);[/teal]

  [b]return[/b] [navy]$filename[/navy][teal];[/teal]
[teal]}[/teal]
Code:
Contact us [teal]:[/teal] [teal]<[/teal]img src[teal]=[/teal][green][i]"[/i][/green][teal]<?php[/teal] [b]echo[/b] [navy]$database[/navy][teal]->[/teal][COLOR=darkgoldenrod]imageCreatex[/color][teal]([/teal][green][i]'user@example.com'[/i][/green][teal]);[/teal] [teal]?>[/teal][green][i]"[/i][/green] alt[teal]=[/teal][green][i]""[/i][/green][teal]>[/teal]
[small][maroon]Warning[/maroon] The above code was not tested[/small]

The point is, you will need a separate request for the image, so put the browser to work : output an [tt]img[/tt] tag and the browser will make the request for the image. Just make sure the image file's name contains no relevant information. This way the image is generated only once, then reused.

Feherke.
 
awsome, even though you did not test the code it works beautifully!

One question though I like the idea of reusing the image but how would you suggest I should delete the image so I dont get too many piling up?

Thanks again
 
Hi

That depends on what kind of site is. They should be 300-400 bytes each, so 2500-3500 of them would be around 1 Mbyte.

So deleting them would be necessary only for a huge site. And such site probably is powered by some robust engine. Which may have some kind of archiving functionality, where additional user defined tasks can be also executed. I would look for such possibility first.

If not, I would set a [tt]cron[/tt] job to run once a month and remove the image files older then 3 months. Of course, your mileage and operating system may vary.


Feherke.
 
Hi,

Again you are correct they are super small in size so I think I can afford to store quite a few before having any issues.

Again thanks for your advice - I'm hapy now!
I can only hope to be as knowledgable one day :o)
 
I'm not even trying to do anything related to this (just browsing the forum to learn things and see if I can answer anyone's questions), but I think Feherke deserves a big fat star for help like that - jasc2k didn't do it, so I did! [thumbsup2]
 
sorry did not realise you could even do that - thanks again for your help though you deserve a star :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top