0

I want to dynamically generate thumbnails, and I found from one of posts here a recommendation that uses SimpleImage class, it is usage like this:

header('Content-Type: image/jpeg');
include('SimpleImage.php');
$image = new SimpleImage();
$image->load('picture.jpg');
$image->resizeToWidth(150);
$image->output();

and the output method is as following:

function output($image_type=IMAGETYPE_JPEG) {

    if( $image_type == IMAGETYPE_JPEG ) {
       imagejpeg($this->image);

    } elseif( $image_type == IMAGETYPE_GIF ) {
       imagegif($this->image);

    } elseif( $image_type == IMAGETYPE_PNG ) {
       imagepng($this->image);

    }
}

this works fine with outputing images, because I received something like binary code on client side. but what I want is a thumbnail slider, so I need all image to be in a <ul></ul>, I tried this using ajax:

updated

function thumb($files){
    echo '<ul>';
    foreach($files as $file){
        $thumb = new Thumbnail();
        $thumb->load($file->getFilePath());
        $thumb->resizeToWidth(80);
        echo '<li>'.$thumb->output().'</li>';
    };
    echo '</ul>';
}

but this does not seem to work. how can I get output something like this so I can do a slider gallery.

<ul>
  <li><img src="path here"></li>
  <li><img src="path here"></li>
</ul>
bingjie2680
  • 7,643
  • 8
  • 45
  • 72
  • What you need to do is wrap all the image generation code into a script that just serves one single image. Then your HTML calls that script in the ``. See my answer here: http://stackoverflow.com/questions/6625427/how-to-serve-multiple-images-which-reside-above-the-www-root-within-a-single-page/6625590#6625590 – Michael Berkowski Jul 12 '11 at 13:15
  • This is because you cannot output multiple images with one `Content-type: image/jpeg` header. – Michael Berkowski Jul 12 '11 at 13:16
  • @Kthompson,,,means I dont get the image list..only binary like code – bingjie2680 Jul 12 '11 at 13:18

1 Answers1

0

You're confusing two things: html layout of images and the scaling of images. You need to keep your scaling code - which is perfectly fine - in a separate php file, say, scale.php - like this:

<?php
    header('Content-Type: image/jpeg');
    include('SimpleImage.php');
    $image = new SimpleImage();
    $image->load($_GET['image']);
    $image->resizeToWidth(150);
    $image->output();
?>

And in your main php, you'll have a code like this:

<?php
    echo '<ul>';
    foreach($files as $file){
        echo '<li><img src="scale.php?image=' . $file . '"/></li>';
    }
    echo '</ul>';
?>
Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • 1
    It is not good idea to use directly image name from GET parameter. Try using mysql table for example with data like "image_id => file_name.jpg" and than pass numeric image_id to your script. – Ostin Jul 12 '11 at 13:19
  • hi, thanks for you answer, should I save the generated thumnail somewhere? – bingjie2680 Jul 12 '11 at 13:19
  • @Ostin,i kind of not understand you. if we use numeric id, where do we get the file path? – bingjie2680 Jul 12 '11 at 13:26
  • 1
    @bingjie2680 it can be mysql or other data source of your choice. If you can't do this, you have to check carefully every parameter passed to your script in GET. – Ostin Jul 12 '11 at 13:32
  • Saving thumbnails to separate place can protect you from later problems of high server load. – Ostin Jul 12 '11 at 13:34
  • @Ostin let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1370/discussion-between-bingjie2680-and-ostin) – bingjie2680 Jul 12 '11 at 13:52