This snippet allows you to grab a thumbnail from the center of a large image. This was used for a client photo gallery for art to give a teaser of the image to come (only a small portion). You could get dynamic with this value. I also put in a scaling factor in case you want to scale down first before chopping.

If you read your Imagedata from a Database Blob and use the functions from above to resize the image to a thumbnail improving a lot of traffic, you will have to make temporary copies of the files in order that the functions can access them

// get a temporary filename// use microtime() to get a unique filename// if you request more than one file f.e. by creating large numbers of thumbnails, the server could be not fast enough to save all these different files and you get duplicated copies and resizepics() will resize and output often the same content

If you need to delete or resize images in the filesystem (not in DB) without loosing the image quality...I commented the code as much as possible so that newbies (like myself) will understand it. ;)

<?php

/*

WRITTEN BY: Finnur Eiriksson, (http://www.centrum.is/finnsi) Based on snippets that have been posted on www.PHP.net Drop me an e-mail if you have any questions.

NOTE: This code is written to either delete or resize pictures in the file system, so if you have your pictures in a database you will have to make some changes. Also, if you are using other picture formats than .gif's or .jpg's you will have to add som code as well (Read the comments to find out where to do this).

IMPORTANT: The $_GET['resizepic'] variable only contains the NAME of the file that is going to be deleted/resized.

The internet guest account (IUSR_SERVERNAME on WINDOWS) must have read and write permissions (execution permission not needed) in your image directory (i.e. $dir_name = "FooBar"). It is a good idea to have a separate directory for images that users can upload to and manipulate the contents. Ideally, you should have one directory for the pictures that are used for the website, and another upload directory

*/

$dir_name = "FooBar"; // Enter the name of the directory that contains the images$olddir = getcwd(); // Get the Current Windows Directory to be able to switch back in the end of the script$dir = opendir($dir_name); //make a directory handle //To delete a pictureif(isset($_GET['delpic'])){ chdir('images');$delpic = $_GET['delpic']; @unlink($delpic);chdir($olddir); }//To resize a pictureif(isset($_GET['resize'])){//$_GET['resize'] contains the resize-percentage (for example 80 and 40, for 80% and 40% respectively. To double the image in size the user enters 200 etc.) // File and new size$percent = ($_GET['resize']/100);chdir('images');// change the windows directory to the image directory$filename = $_GET['resizepic'];

// Load the imageswitch($format){ case 'image/jpeg':$source = imagecreatefromjpeg($filename); break; case 'image/gif';$source = imagecreatefromgif($filename); break; }//Get the Image$thumb = imagecreatetruecolor($newwidth,$newheight);//This must be set to false in order to be able to overwrite the black //pixels in the background with transparent pixels. Otherwise the new //pixels would just be applied on top of the black background.imagealphablending($thumb, false);//Make a temporary file handle$source = @imagecreatefromjpeg($filename);// Resizeimagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);//Write the image to the destination file@imagejpeg($thumb, $filename);//Change back to the old directory... I'm not sure that this is neccessarychdir($olddir);//Specify where you want the user to go after the operation:header('Location: foobar.php'); } }

I'm not sure why nworld3d at yahoo dot com's http://php.net/manual/en/function.imagecopyresized.php#69123 was downvoted--it was exactly what I was looking for to composite several alpha channel images, just like Photoshop. I needed to start with transparency and stack a few alpha images one after another on top of it. After mucking about with the built in PHP functions, I was worried that I would need to cheat by removing all the alpha and then using a transparent color in the end, but that would have looked different depending on what background I put it on top of. nworld3d at yahoo dot's comment saved me from having to go that route, and gets a +1 from me.

I didn't find here before any script giving both GIF and PNG transparent images correct, so I offer you mine.
Here is the code for 'image.php' script to generate a resized or original sized images of any type (JPEG, GIF, PNG) with respect to possible transparency of GIF and PNG.
The following script can be used in HTML tag IMG (to display an image marked in database with ID equal to 1 and resized to max 100px) like this:
<img src="image.php?id=1&size=100" />.

The function below will resize an image based on max width and height, then it will create a thumbnail image from the center of the resized image of a width and height specified. This function will not resize the image to max_w pixels by max_h pixels, those are only the max width's and heights the image can be, it resizes the image to the first 1:1 ratio below max_w and max_h.

For example, if you have an image that is 800x600 and you specify your new image to be 400x200, it will resize based on the smallest number (in this case 200) and maintain the images 1:1 ratio. So your final image would end up at something like 262x200.

UPDATE: I have updated this function, I added the 'newdir' option in case you want the images saved in a different directory than the script. I also fixed the thumbnail slice so it is perfectly in the center now and fixed the bug that 'ob at babcom dot biz' mentioned so you can safely resize based on width or height now.

<?/********************************************************** * function resizejpeg: * * = creates a resized image based on the max width * specified as well as generates a thumbnail from * a rectangle cut from the middle of the image. * * @dir = directory image is stored in * @newdir = directory new image will be stored in * @img = the image name * @max_w = the max width of the resized image * @max_h = the max height of the resized image * @th_w = the width of the thumbnail * @th_h = the height of the thumbnail * **********************************************************/

Belows is the code snipet that allows you to resize a transparent PNG and composite it into another image. The code is tested to work with PHP5.1.2, GD2, but I think it can also work with other versions of PHP and GD.

The code has been commented to help you read through it. The idea of resizing the transparent PNG image is to create a new destination image which is completely transparent then turn off the imageAlphaBlending of this new image so that when the PNG source file is copied, its alpha channel is still retained.

/** * Resize a PNG file with transparency to given dimensions * and still retain the alpha channel information * Author: Alex Le - http://www.alexle.net */function imageResizeAlpha(&$src, $w, $h){/* create a new image with the new width and height */$temp = imagecreatetruecolor($w, $h);

/* making the new image transparent */$background = imagecolorallocate($temp, 0, 0, 0);ImageColorTransparent($temp, $background); // make the new temp image all transparentimagealphablending($temp, false); // turn off the alpha blending to keep the alpha channel

/** * Open the same PNG file then compose without resizing * As the original $watermark is passed by reference, it was resized already. * So we have to reopen it. */$watermark = imagecreatefrompng('images/watermark.png');imageComposeAlpha( $photoImage, $watermark, 80, 350);Imagepng($photoImage); // output to browser

Users of this function should be aware that this function can return false in certain circumstances! I am assuming this is an indicator of failure.

None of the example code displayed in these notes takes this into account, so all of these examples contain weaknesses that could crash a program if not used with care.

However, it is not documented what kinds situations will cause it to fail, and what side effects (if it is not atomic) occur if the operation fails, so we will have to wait for the documentation to be updated before this function can be used in rock-solid (crash-proof) code.

[[I posted this information earlier, but I phrased it as a question so it was removed by the editors]]

Function creates a thumbnail from the source image, resizes it so that it fits the desired thumb width and height or fills it grabbing maximum image part and resizing it, and lastly writes it to destination

I wrote a function not long ago that creates a thumbnail out of a large picture. Unlike other notes on this page, the code is a function so it can be used many times on the same script. The function allows the programer to set max height and width and resizes the picture proportionally.<?phpfunction saveThumbnail($saveToDir, $imagePath, $imageName, $max_x, $max_y) {preg_match("'^(.*)\.(gif|jpe?g|png)$'i", $imageName, $ext); switch (strtolower($ext[2])) { case 'jpg' : case 'jpeg': $im = imagecreatefromjpeg ($imagePath); break; case 'gif' : $im = imagecreatefromgif ($imagePath); break; case 'png' : $im = imagecreatefrompng ($imagePath); break; default : $stop = true; break; }

Neat script to create a thumbnails no larger than 150 (or user-specific) height AND width.

<?PHP$picture ="" # picture fileNAME here. not address$max=150; # maximum size of 1 side of the picture./*here you can insert any specific "if-else",or "switch" type of detector of what type of picture this is.in this example i'll use standard JPG*/

The function below will resize an image based on max width and height, then it will create a thumbnail image from the center of the resized image of a width and height specified.

<?php/********************************************************** * function resizejpeg: * * = creates a resized image based on the max width * specified as well as generates a thumbnail from * a rectangle cut from the middle of the image. * * @dir = directory image is stored in * @img = the image name * @max_w = the max width of the resized image * @max_h = the max height of the resized image * @th_w = the width of the thumbnail * @th_h = the height of the thumbnail * **********************************************************/

There are many user notes here that deal with creating a thumbnail, but I wrote a function that created a thumbnail of an image as it gets uploaded. It is simply a combination of a file uploader with a thumbnail maker, but it is very simple to use now.

Following is an index.php file that contains the upload form, the function, and an example of how to use the function. The last two values in the 'thumb' function are for the width and height limits for the resulting thumbnail. No matter what size the original image, the thumbnail will always fit within the dimensions specified.

I tried to use his function resizejpeg() for resizing images in my gallery. As far as I can tell it contains a small bug.

Resizing worked fine as long as I had the same maximum width and maximum height specified. Wanting all thumbnails to have the same height - so my images would appear in a straight line on my website both in portrait format and in landscape format - I soon encountered the problem that resizing with different values of maximum width and maximum height would not work proberly.

If you are using the script change the following 2 lines where the resized width and the resized height are calculated:

The following function is based on Kyle Florence's function. I left out the thumbnail-part and rather added the possibiliy of defining a new dir and new filename for the image. If you need to resize and then create a thumbnail just run the function twice. Here, the thumbnail will contain the full picture and not a cutout of the original image.

The function supports JPG, GIF and PNG resizing. The quality in case of JPG is given to the function as the last parameter $Quality.

I spent last night getting unsuccessful results from this function until I found this note of fluffle <<at>> gmail on the imagecopyresampled page, I have made a slight modification, so you can just cut and paste.

Just to clarify that src_w and src_h do not necessarily need to be the source image width and height, as they specify the size of the rectangle cropped from the source picture, with its top left corner situated at (src_x, src_y).

For example, the code below crops a jpeg image to be square, with the square situated in the centre of the original image, and then resizes it to a 100x100 thumbnail.

function ($image_filename, $thumb_location, $image_thumb_size){//@$image_filename - the filename of the image you want//to get a thumbnail for (relative to location of this//function).//@$thumb_location - the url (relative to location of this//function) to save the thumbnail.//@$image_thumb_size - the x-y dimension of your thumb//in pixels.