Page 1 of 1

Help needed

Posted: Wed May 20, 2009 7:42 am
by umang40
I am trying to display image thumbnail.Here i attach my code.But problem is there it needed <html> <body> tag to view in browser.But i can not add those tags because header is there.
And without <html> <body> tag i can notshow image in browser.
So, how camn I mix up <html> <body> tag with headers. I am new at php.

Code: Select all

<?php
$thumb = createThumbnail("Winter.jpg","phpimages","-thumb", 120, 100, 100);
function createThumbnail($img, $imgPath, $suffix, $newWidth, $newHeight, $quality)
{
  // Open the original image.
  $original = imagecreatefromjpeg("$imgPath/$img") or die("Error Opening original");
  list($width, $height, $type, $attr) = getimagesize("$imgPath/$img");
 
  // Resample the image.
  $tempImg = imagecreatetruecolor($newWidth, $newHeight) or die("Cant create temp image");
  imagecopyresized($tempImg, $original, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height) or die("Cant resize copy");
// Save the image.
 imagejpeg($tempImg, "$imgPath/def.jpg", $quality) or die("Cant save image");
$image1=imagecreatefromjpeg("$imgPath/def.jpg");
header('Content-Type: image/jpeg');
imagejpeg($image1);
imagedestroy($original);
    imagedestroy($tempImg);
    return true;
}
 
 
?>

Re: Help needed

Posted: Wed May 20, 2009 8:07 am
by crazycoders
Please remember to use

Code: Select all

 tags for your code thank you.

Ok so first of all, you need a thumbnail, remember that thumbnail are files. If you wish to simply show a thumbnail of an existing file, you have 2 options. Use a thumbnailing script that creates a thumbnail of your pictures on the fly and sends them back directly to the output buffer. THAT is partly what the script you sent us does or almost... You'd need to change it a little. Another option is to create a script that is not executed on each image get but on the page get.

Example 1: Create images on the fly from the page
[syntax=php] 
<html>
    <body>
        <table>
            <tr>
                <td><?php echo getThumbnail('image1.jpg'); ?></td>
                <td><?php echo getThumbnail('image2.jpg'); ?></td>
                <td><?php echo getThumbnail('image3.jpg'); ?></td>
                <td><?php echo getThumbnail('image4.jpg'); ?></td>
            </tr>
        </table>
    </body>
</html>
<?php
//Try to create thumbnail
function getThumbnail($image){
    $imagepath = 'imagespath\\';
    $image = str_replace(array('..', '.'), '', $image); //Remove .. and . to prevent navigation to other folders from hackers
    if(file_exists($imagepath.$image)){
        echo '<img src="'.createThumbnail($image, $imagepath, "-thumb", 120, 100, 100).'">'; //Create a thumbnail and flush it to the buffer
    }else{
        echo '<img src="'.$imagespath.'notfound.jpg">'; //Return the not found image
    }
}
 
//Creates a thumbnail and returns it through the buffer
function createThumbnail($img, $imgPath, $suffix, $newWidth, $newHeight, $quality){
 
    // Open the original image.
    $original = imagecreatefromjpeg("$imgPath/$img") or die("Error Opening original");
    list($width, $height, $type, $attr) = getimagesize("$imgPath/$img");
 
    // Resample the image.
    $tempImg = imagecreatetruecolor($newWidth, $newHeight) or die("Cant create temp image");
    imagecopyresized($tempImg, $original, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height) or die("Cant resize copy");
 
    // Save the image.
    imagejpeg($tempImg, "$imgPath/$img$suffix.jpg", $quality) or die("Cant save resized image");
    imagedestroy($original);
    imagedestroy($tempImg);
    return "$imgPath/$img$suffix.jpg";
    
}
?>
 [/syntax]

Example 2: Create the thumbnails on each get of an image
file1: browser.php
[syntax=php] 
<html>
    <body>
        <table>
            <tr>
                <td><img src="thumbnail.php?thumbof=image1.jpg"></td>
                <td><img src="thumbnail.php?thumbof=image2.jpg"></td>
                <td><img src="thumbnail.php?thumbof=image3.jpg"></td>
                <td><img src="thumbnail.php?thumbof=image4.jpg"></td>
            </tr>
        </table>
    </body>
</html>
 [/syntax]
File2: Thumbnail.php (thumbnail generator)
[syntax=php] 
<?php
//Return the content type
header('content-type: image/jpeg');
 
//Try to create thumbnail
$imagepath = 'imagespath\\';
if(isset($_GET['thumbof'])){
    $_GET['thumbof'] = str_replace(array('..', '.'), '', $_GET['thumbof']); //Remove .. and . to prevent navigation to other folders from hackers
    if(file_exists($imagepath.$_GET['thumbof'])){
        createThumbnail($_GET['thumbof'], $imagepath, "-thumb", 120, 100, 100); //Create a thumbnail and flush it to the buffer
    }else{
        readfile($imagespath.'notfound.jpg'); //Return the not found image
    }
}else{
    readfile($imagespath.'notfound.jpg'); //Return the not found image
}
 
//Creates a thumbnail and returns it through the buffer
function createThumbnail($img, $imgPath, $suffix, $newWidth, $newHeight, $quality){
 
    // Open the original image.
    $original = imagecreatefromjpeg("$imgPath/$img") or die("Error Opening original");
    list($width, $height, $type, $attr) = getimagesize("$imgPath/$img");
 
    // Resample the image.
    $tempImg = imagecreatetruecolor($newWidth, $newHeight) or die("Cant create temp image");
    imagecopyresized($tempImg, $original, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height) or die("Cant resize copy");
 
    // Save the image.
    imagejpeg($tempImg);
    imagedestroy($original);
    imagedestroy($tempImg);
    return true;
    
}
?>
 [/syntax]