Hi, I am trying to create a thumbnail of an image and show it on the browser without creating an actual file of the thumbnail...rather show it through a function. I am doing it in a smarty library using site..(i dont know if thats the problem)

I got a profile.tpl, a profile.php and a functions.php where i have the function.

In the profile.php i have the function calling . ex:
$pic = createThumb("1.jpg"); and at the end
$smarty->assign('pic', $pic);

In the profile.tpl i have:
{$pic} to show the picture
and in the function.php

function createThumb($source) {

$thumb_size = 25;
$size = getimagesize($source);
$width = $size[0];
$height = $size[1];
$y = NULL;
$x = NULL;
if($width> $height) {
	$x = ceil(($width - $height) / 2 );
	$width = $height;
} elseif($height> $width) {
	$y = ceil(($height - $width) / 2);
	$height = $width;
}
$new_im = ImageCreatetruecolor($thumb_size,$thumb_size);
$im = imagecreatefromjpeg($source);
imagecopyresampled($new_im,$im,0,0,$x,$y,$thumb_size,$thumb_size,$width,$height);

  header('Content-type: image/jpeg');
  imagejpeg($new_im,"",100);
  imagedestroy($im);
}

The problem is that when i check the page on the browser i get alot of garbage on the top like :

&#65533;&#65533;&#65533;&#65533;&#65533;JFIF&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality &#65533;&#65533;&#65533;C&#65533;    $.' ",#(7),01444'9=82<.342&#65533;&#65533;&#65533;C  2!!22222222222222222222222222222222222222222222222222&#65533;&#65533;&#65533;&#65533;&#65533;"&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533; &#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;}&#65533;!1AQa"q2&#65533;&#65533;&#65533;#B&#65533;&#65533;R&#65533;&#65533;$3br&#65533; %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;

and no image of course where it should be.
I realise its because its not sending the header('Content-type: image/jpeg'); to the browser first so it displays the image.

Can anyone help me out here ? What am i doing wrong ?
Thanks

....EDIT.....

Btw this image is just a part of an html page...I dont want to just show this image on the page....but inside a <td> so i cant just put {header('Content-type: image/jpeg');} at the top of the page....cause it will just show the image and nothing else.

I am doing all this because i dont want to take extra space on a temp folder by making thumb images.
If there is a way to delete the images after they are shown to the user then i can go with that solution also...I can make it work by making tmp images, i just dont know how to delete them after.

    The problem is that you are trying to output the jpeg file in the middle of the HTML.

    What you need to do is save the file to disc and then reference it. May I suggest creating a thumbnails folder in your image folder, and then changing the imagejpeg line to:

    imagejpeg($new_im,"./thumbnails/" . $source,100);

    And then to display you'd do:
    $pic = createThumb("1.jpg"); and at the end
    $smarty->assign('pic', '<img src="./thumbnails/1.jpg" />');

    Obviously you'll have to mess with the paths to get it to save and recall from the same place (especially if you're using paths with more levels than just 1.jpg, e.g. ./img/data/1.jpg)

    The alternative is to move your thumbnail script to another page, and then call it like so:
    <img src="thumbnail.php?file=1.jpg" />
    with the thumbnail.php file generating the thumbnail on the fly using the function you have there, and outputting it to the browser with header('Content-type: image/jpeg');

    I hope you'll be able to gain at least some help from that, it's at least a point in the right direction.

    Just read your edit, if you are using the second method then you will not need to store a temporary file.
    In regard to the first method, disk space is cheap, and unless you have thousands to millions of thumbnails being generating, storing them is easy and leaves the advantage of them only needing to be generated once.

      Thanks for your respornse 🙂
      Up and down we are on the same path...I figured out that if i do it in an external file and call it like you did it works..then i though i have to mask that output cause i dont want people to see thumbs.php?..... and thats gonna be a hassle to....
      About the space thing i thought about that too....2.3K per thumbnail is nothing...So i am gonna follow that way..if i get million thumbnails so be it 😛
      thanks again 🙂

        What's wrong with people seeing "thumbnail.php?file=1.jpg" or something similar?

        Granted, in this case it sounds like saving to an actual file would be good, since you could implement some image caching (i.e. the image doesn't necessarily need to be generated on each request).

          Write a Reply...