Page 1 of 1

Thumbnail generation: crop, resize, save

Posted: Sun Sep 19, 2010 9:07 pm
by phpnewbieperson
I've searching for a few hours now and haven't found anything that really suits what I need. I need to be able to create thumbnails on the fly that are the correct ratio so they don't look pixelated upon resizing. I need to be able to do the following:

- take an image from the server, crop it to a multiple of 75x70px (w x h) and then resize it to 75 x 70px
- save the thumbnail to the server
- display the thumbnail

The reason I want to save it is I want to use an IF/ELSE statement when calling the image to see if a thumbnail has already been created for this image and if so, just use the one already created.

The code I have so far is very basic and is as follows (currently running on local server only):

Code: Select all

<ul id="car">
            <?php
                $conn = mysql_connect("localhost", "root", "passwordhere");
                mysql_select_db("12345678_shop1", $conn);
                $sql = "SELECT * FROM products ORDER BY RAND() LIMIT 16";
                $result = mysql_query($sql,$conn);
                while ($newArray = mysql_fetch_array($result)) {
                    $products_id = $newArray['products_id'];
                    $products_image = $newArray['products_image'];
                    echo "<li><a href=\"product_info.php?products_id=" . $products_id . "\"><img src=\"" . HTTP_SERVER . DIR_WS_HTTP_CATALOG . DIR_WS_IMAGES . "$products_image\" width=\"75\" height=\"70\" /></a></li>";
                }
            ?>
        </ul>
Ideally looking for a solution along the lines of img src="image.php?image=foobar.jpg"

It's ok if it's just JPG but GIF support would be great also.

Any help would be great.

Re: Thumbnail generation: crop, resize, save

Posted: Sun Sep 19, 2010 9:48 pm
by Jonah Bron
By "on the fly", do you really mean make a thumbnail every time the page loads? That's terribly inefficient...

Re: Thumbnail generation: crop, resize, save

Posted: Sun Sep 19, 2010 10:09 pm
by phpnewbieperson
Jonah Bron wrote:By "on the fly", do you really mean make a thumbnail every time the page loads? That's terribly inefficient...
Yep. It only needs to occur once for every image. Bear in mind I've already run a thumbnail script for existing images so the only time this script will actually need to generate a new thumbnail is when a new product image is added - so we're not talking about many images at all.