Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Feb 2005
    Posts
    97
    Thanks
    7
    Thanked 7 Times in 7 Posts

    Resize breaks image when included.

    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.

    Code:
    <?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...
    Code:
     <? 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?

  • #2
    Regular Coder
    Join Date
    Nov 2004
    Location
    The Netherlands
    Posts
    551
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah, you're echoing a image inside normal html. You need to use img tags in html.
    CATdude about IE6: "All your box-model are belong to us"

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    paris, france
    Posts
    1,216
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    photoshop too expensive? use the GIMP! www.gimp.org

  • #4
    Regular Coder
    Join Date
    Nov 2004
    Location
    The Netherlands
    Posts
    551
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    CATdude about IE6: "All your box-model are belong to us"

  • #5
    New Coder
    Join Date
    Feb 2005
    Posts
    97
    Thanks
    7
    Thanked 7 Times in 7 Posts
    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.
    Code:
    <?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
    Code:
    <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.

  • #6
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Put the content type header in test.php like this:

    PHP Code:
    <?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:

    PHP Code:
    <img src="test.php" /> 

  • #7
    New Coder
    Join Date
    Feb 2005
    Posts
    97
    Thanks
    7
    Thanked 7 Times in 7 Posts
    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?

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    paris, france
    Posts
    1,216
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you don't, your webserver probably does. Apache for example has a file where file extensions are mapped to appropriate mime types.
    photoshop too expensive? use the GIMP! www.gimp.org

  • #9
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mattyod
    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

  • #10
    New Coder
    Join Date
    Feb 2005
    Posts
    97
    Thanks
    7
    Thanked 7 Times in 7 Posts
    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 .

    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?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •