[solved] image() question (possibly more of a html question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

[solved] image() question (possibly more of a html question

Post by Coco »

im using image() on a site im building with a friend, using it to supply images from a secure folder to the browser henceforth making it impossible to remote link, and to only view images your permissions allow. (im doing this by the <img src=makeimage.php?id=xxx> method)

the problem is this... some of the images the user is supposed to be able to right-click and save-as, however, the resulting dialogue comes up with 'untitled' filename, and bmp as the only availiable file type, when the image itself is a gif or a jpeg. is there any way to get round this? i tried <object> instead but thatdoesnt seem to be compatible with retrieving images via script

any help gratefully recieved.. sorry if this should have gone in a different forum

this is my image display, used after getting details from the db

Code: Select all

<?php
	if(stristr($dataa['thumbadd'], '.jpg')&&(strlen(stristr($dataa['thumbadd'], '.jpg'))==4))
	{
		$im = imagecreatefromjpeg ('./' . $dataa['thumbadd']);
	}
	elseif(stristr($dataa['thumbadd'], '.gif')&&(strlen(stristr($dataa['thumbadd'], '.gif'))==4))
	{
		$im = imagecreatefromgif ('./' . $dataa['thumbadd']);
	}
	imagejpeg($im,'',75);
	imagedestroy ($im);
?>
Last edited by Coco on Mon Sep 06, 2004 12:20 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

are you outputting a content type header? there's no need to use the image functions, you can just [php_man]readfile[/php_man] the image needed, however, you do need to send the proper content-types..
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

im not sure how to do that?
since the script is being called by a different script, can i really output content type headers? bearing in mind that the script is run up to 30 odd times a page
the problem i had was that if i did it all in one script, i couldnt output anything BUT the image, unless i saved the image, which makes it pointless since im trying to prevent unauthorised access

ill have a look into readfile tho
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

each image call should output a content-type header. It should also specify the content-length too.

read [php_man]header[/php_man] for more details on outputing headers..
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

ok readfile didnt help, but it seems to be more efficient so im gonna stay with that anyway.

i tried the headers, it made no difference. Still, when you right click save as, it comes up as an untitled bmp.

i even tried calling the image script on its own, to see if the headers make it work, but it doesnt (my main script passes calls to the image script via <img> html)

in short, it will not output an image and maintain the filename and type when right clicking. I do not want to give the users direct access to the files.

this is my new output script. i didnt include content length on the header, as the main concern is the file type:

call using <img src="makeimage.php?id=xxx">

Code: Select all

<?php
// fetch image details from db
if(stristr($dataa['thumbadd'], '.jpg')&&(strlen(stristr($dataa['thumbadd'], '.jpg'))==4))
{
	header('Content-type: image/jpeg');
	@readfile('./'.$dataa['thumbadd']);
}
elseif(stristr($dataa['thumbadd'], '.gif')&&(strlen(stristr($dataa['thumbadd'], '.gif'))==4))
{
	header('Content-type: image/gif');
	@readfile('./'.$dataa['thumbadd']);
}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you may want to specify content-disposition.. (where you can tell the browser the filename)

can you post a link to this page, I can run a script that tells me all the headers and things they are sending...
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

ok i tried adding a content disposition header, but it didnt work...

heres my current display script, called as before... any ideas?

Code: Select all

<?php
if(stristr($dataa['thumbadd'], '.jpg')&&(strlen(stristr($dataa['thumbadd'], '.jpg'))==4))
{
	$filename = substr(strrchr($dataa['thumbadd'], "/"), 1);
	header('Content-type: image/jpeg');
	header('Content-Disposition: image; filename="' . $filename . '"');
	@readfile('./'.$dataa['thumbadd']);
}
elseif(stristr($dataa['thumbadd'], '.gif')&&(strlen(stristr($dataa['thumbadd'], '.gif'))==4))
{
	header('Content-type: image/gif');
	$filename = substr(strrchr($dataa['thumbadd'], "/"), 1);
	header('Content-Disposition: image; filename="' . $filename . '"');
	@readfile('./'.$dataa['thumbadd']);
}

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

feyd via PM wrote:it appears that content-length is what IE needs.. Firefox gets it right.
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

solved
content length seems to be what it needed
Post Reply