Greetings there,
Could someone please tell me how to create a thumbnail of an image. Let’s say I have a db table called “pictures”. Its columns are:
picture_id
picture_name
user_id
… where picture_id is: INT NOT NULL AUTO_INCREMENT PRIMARY KEY
picture_name (file name) is: VARCHAR(64)
and user_id is a simple foreign key that references id column of a user table.
Let’s say a user has 5 pictures and I would like to output them on a page, as thumbs. Images are quite big in size, so I would need to make them smaller, somehow. I don’t know many images-related functions in PHP and how they work.
I do know though how to create a simple CATPCHA image, using these functions:
imagecreatetruecolor(), imagecolorallocate(), imageline(), imagesetpixel() and imagefilledrectangle().
I also know getimagesize() function.
I am afraid that is all the image related functions that I know.
Could someone help me out by telling me how to do the thumbnail gallery, or at least give me a link to a decent online tutorial… or reference a good book with a step by step example so I can learn?
Thank you all.
Alex.
Thank you. I guess I got it. For all who is looking for the same thing I was looking, here is a solution:
function get_image_size($large_image)
{
//retrieve original width and height
list($origin_width, $origin_height) = getimagesize($large_image);
//create resized width and heights variables
$resized_width = $origin_width;
$resized_height = $origin_height;
//see if image is wider than allowed
if ($resized_width > MAX_THUMB_WIDTH)
{
//we need to make it smaller;
//calculate the degree to which we need to descrease image to accommodate the allowed image width
$aspect_ratio = MAX_THUMB_WIDTH/$resized_width;
//descrease width and heights equally
$resized_width = round($aspect_ratio*$resized_width);
$resized_height = round($aspect_ratio*$resized_height);
}
//see if large image is taller than allowed
if ($resized_height > MAX_THUMB_HEIGHT)
{
//see by how much we need to descrease its heights
$aspect_ratio = MAX_THUMB_HEIGHT/$resized_height;
//descrease width and heights equally
$resized_width = round($aspect_ratio*$resized_width);
$resized_height = round($aspect_ratio*$resized_height);
}
//return calculated values
return array($origin_width, $origin_height, $resized_width, $resized_height);
}
$image_dimensions = get_image_size($target);
$origin_width = $image_dimensions[0];
$origin_height = $image_dimensions[1];
$resized_width = $image_dimensions[2];
$resized_height = $image_dimensions[3];
//create an empty image of the right size
$output_image = imagecreatetruecolor($resized_width, $resized_height);
//copy source image file depending on its file type
if ($image_type == "image/jpeg" || $image_type == "image/pjpeg")
{
//we have a jpeg file
$source_image = imagecreatefromjpeg($target);
}
else if ($image_type == "image/gif")
{
//we have a gif file
$source_image = imagecreatefromgif($target);
}
else
{
//we have a png file
$source_image = imagecreatefrompng($target);
}
//load the source file into the output file
imagecopyresampled($output_image, $source_image, 0, 0, 0, 0, $resized_width, $resized_height, $origin_width, $origin_height);
//depending on the image type, output it to a file inthe specified place
if ($image_type == "image/jpeg" || $image_type == "image/pjpeg")
{
//we have a jpeg file
imagejpeg($output_image, THUMB_PATH . $new_file_name, 100);
}
else if ($image_type == "image/gif")
{
//we have a gif file
imagegif($output_image, THUMB_PATH . $new_file_name, 100);
}
else
{
//we have a png file
imagepng($output_image, THUMB_PATH . $new_file_name, 100);
}
It works.
You’d probably be interested in either imagecopyresized() (http://php.net/manual/en/function.imagecopyresized.php) or imagecopyresampled() (http://php.net/manual/en/function.imagecopyresampled.php). Both manual pages have pretty good examples on how to resize images, though you would have to play around with them a little bit.