...

View Full Version : Resize breaks image when included.



mattyod
06-07-2005, 09:54 AM
Hi,

I've been messing about with some of the gd functions and scripts recently, in particular ones that allow the resizing of images on the fly to create thumbnails but having difficulty getting them to work correctly when the scripts are called by an include from another page.

Let me use an example. The following little snippet resizes an image to 50% fine when used as a stand alone php page.



<?php
$src = ImageCreateFromJPEG('test.jpg');
$width = ImageSx($src);
$height = ImageSy($src);
$x = $width/2; $y = $height/2;
$image = ImageCreateTrueColor($x,$y);
ImageCopyResampled($image,$src,0,0,0,0,$x,$y,$width,$height);
header('Content-Type: image/jpeg');
Imagejpeg($image, "", 60);
?>


However, when I try to call it from another page with say...

<? include 'test.php' ?>
I just get a long stream of jumbled text and symbols outputed by the browser.

I found a site that hinted that whitespace had something to do with it but it failed to go into much more detail than that.

Anyone able to throw a little light on this for me please?

mrruben5
06-07-2005, 10:31 AM
Yeah, you're echoing a image inside normal html. You need to use img tags in html.

whackaxe
06-07-2005, 10:32 AM
it might be that the content-type header hasn't been set in the including file. try setting it just above your include() and see what happens

mrruben5
06-07-2005, 10:36 AM
Yes he has put a header in his script. But I think you are echoing the header in the same stream as the normal html text. Why you aren't seeing a headers already sent? I don't know. Maybe your host has turned off.

mattyod
06-07-2005, 11:28 AM
Sorry guys I'm struggling with this a bit.

You are right I was also getting a message about content headers already being sent at the start of the garbled text, forgot to mention that.

I've removed the line headers line in the test.php file so it now looks like.


<?php
$src = ImageCreateFromJPEG('test.jpg');
$width = ImageSx($src);
$height = ImageSy($src);
$x = $width/2; $y = $height/2;
$image = ImageCreateTrueColor($x,$y);
ImageCopyResampled($image,$src,0,0,0,0,$x,$y,$width,$height);
Imagejpeg($image, "", 60);
?>

and tried the following


<img src="<?header('Content-Type:image/jpeg');include'test.php';?>" />

As well as just the plain include.

Now I don't get the headers already sent warning but I still get the long stream of junk text.

I've not really payed much attention to content types before, text/html in my header always did the job :) So perhaps I'm doing something dumb.

Any further help much appreciated.

delinear
06-07-2005, 11:32 AM
Put the content type header in test.php like this:


<?php
$src = ImageCreateFromJPEG('test.jpg');
$width = ImageSx($src);
$height = ImageSy($src);
$x = $width/2; $y = $height/2;
$image = ImageCreateTrueColor($x,$y);
ImageCopyResampled($image,$src,0,0,0,0,$x,$y,$width,$height);

header('Content-Type:image/jpeg');
Imagejpeg($image, "", 60);
?>

Then call the image in the main page using:


<img src="test.php" />

mattyod
06-07-2005, 11:52 AM
Aaaah so simple :) *slaps forehead*

Thanks for the help people.

Actually it seems to work regardless of whether the content type declaration is in the test.php file or not.

Maybe this is a different subject but why am I bothering to declare a MIME type for this image? You wouldn't normally bother in plain old html would you?

whackaxe
06-07-2005, 12:10 PM
you don't, your webserver probably does. Apache for example has a file where file extensions are mapped to appropriate mime types.

delinear
06-07-2005, 12:18 PM
Maybe this is a different subject but why am I bothering to declare a MIME type for this image? You wouldn't normally bother in plain old html would you?

I thought the same but I recently used a slightly modified version of this code to generate a page full of thumbnails and while it worked fine in IE, FF output garbled text instead of images. As soon as I put the header declaration in there it worked fine in both browsers, so I put it in now just to be on the safe side :)

mattyod
06-08-2005, 11:42 AM
Me again.

Well the img src solution works fine for my simple example but seems to present me with a set of problems when trying to expand upon that. Perhaps it is easiest if I explain what I am trying to do.

The idea was to make an automated/dynamic photo gallery. I want to be able to just drop a folder full of images into a main "photos" folder. I will then have my gallery.php page that will read through this photos folder, listing all the sub folders within, providing a link to an index page within those folders and displaying any folder description that happens to be in a description.txt if present. Then my final touch was to also make a random thumb nail for that folder so you might get something along the lines of this :how it might look (http://www.pbase.com/kurtteej) .

Now here's the problem by using the php file as an img src and not an include it would seem to not pick up any variables from previous scripts as you might be able to do with 2 includes in a page. It doesn't even seem to pick up any $_SESSION variables either. And I cann't merge the script that displays the link and description with the script that displays the thumbnail becasue one is outputting html and the other is an image.

I've thought of a couple of possible solutions for this but all have their drawbacks:

1: Put the thumbnail script as a function at the top of the page so I can pass variables to it.

Not sure if this would even work, I will try, but I've always been taught to try and seperate my scripts and my html wherever possible and I want to try to continue to do so.

2: Use a cookie to pass a folder path variable.

Again not sure if this will work properly and if it does it strikes me that it would create a huge amount of cookie passing as the number of sub folders increases, not sure that the processing invoved would be entirely warranted.

3: Taking the thing to a whole new level where by pictures are uploaded through the website, resized into thumbnails and saved onto the server so they can be used as normal image files.

This is perhaps a better and more complete solution but it's a lot more work, (especially introducing a whole new set of security issues for a site that's just my bit of fun) and well it's not what i set out to do :)

Well kudos to anyone who is still with me on this, I hope it made some sort of sense to you.

Is there another way?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum