Jump to content

On the fly thumbnail creation


sford999

Recommended Posts

Hi all,

 

I`m using the following function to create a thumbnail of an image when its uploaded:

 

<?php

function create_thumbnail($source, $destination, $thumb_width)
{
$size = getimagesize($source);
$width = $size[0];
$height = $size[1];
$x = 0;
$y = 0;
if($width > $height)
{
	$x = ceil(($width - $height) / 2);
	$width = $height;
}
elseif($height > $width)
{
	$y = ceil(($height - $width) / 2);
	$height = $width;
}
$new_image = imagecreatetruecolor($thumb_width, $thumb_width) or die('Cannot Initialize new GD image stream');
$extension = get_image_extension($source);

if($extension == 'jpg' || $extension == 'jpeg')
{
	$image = imagecreatefromjpeg($source);
}
if($extension == 'gif')
{
	$image = imagecreatefromgif($source);
}
if($extension == 'png')
{
	$image = imagecreatefrompng($source);
}

imagecopyresampled($new_image, $image, 0, 0, $x, $y, $thumb_width, $thumb_width, $width, $height);
if($extension == 'jpg' || $extension == 'jpeg')
{
	imagejpeg($new_image, $destination);
}
if($extension == 'gif')
{
	imagegif($new_image, $destination);
}
if($extension == 'png')
{
	imagepng($new_image, $destination);
}
}
?>

 

However, Whenever I upload an image that is say 1920px x 1080px the "create_thumbnail()" function is creating a square thumbnail, eg 600x600 and not what it should be at 600x338 or 300x169.

 

How can I change it so that it does work by keeping the file proportions?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/167970-on-the-fly-thumbnail-creation/
Share on other sites

Hi, thumbnails are cropped images, which means that the width and height need to be the same size, for formatting reasons. What happens here is that the image is resized down until it reaches the thumb height and then any extra is cropped/cut out. Look for a function that resizes images.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.