Hello,
I have a script I've been planning on using to create temporary thumbnails for a picture gallery. I've adapted a code that just resizies an image into one that automatically adjusts the image size based on its orientation. This script is working. However, I would like to then crop that image into a square with a side length of the shortest side of the thumbnail (Kind of like Flickr.com thumbnails).
Here's the first script, the one that just detects the orientation and adjusts the size accordingly.
<?
function image_resize($image_file, $width, $height, $download)
{
$src = "jpg-resize.php?" .
"image_file=" . urlencode($image_file) . "&" .
"width=" . $width . "&" .
"height=" . $height;
$img = "<img " .
"border=\"0\" " .
"width=\"" . $width . "\" " .
"height=\"" . $height . "\" " .
"src=\"" . $src . "\" />";
if($download == true)
$img = "<a href=\"" . $image_file . "\">\n" .
$img . "</a>";
echo($img);
}
function image_horizontal_resize($image_file, $width, $download)
{
$original_size = getImageSize($image_file);
$original_width = $original_size[0];
$original_height = $original_size[1];
$height = round($original_height * ($width / $original_width));
image_resize($image_file, $width, $height, $download);
}
function image_vertical_resize($image_file, $height, $download)
{
$original_size = getImageSize($image_file);
$original_width = $original_size[0];
$original_height = $original_size[1];
$width = round($original_width * ($height / $original_height));
image_resize($image_file, $width, $height, $download);
}
if(isset($_GET))
{
$original_size = getImageSize($image_file);
$original_width = $original_size[0];
$original_height = $original_size[1];
if($original_height > $original_width)
{
header("Content-type: image/jpeg");
$image_file = $HTTP_GET_VARS["image_file"];
$width = $HTTP_GET_VARS["height"];
$height = $HTTP_GET_VARS["width"];
$original_jpg = imageCreateFromJPEG($image_file);
$resized_png = imageCreateTrueColor($width, $height);
imageCopyResampled($resized_png, $original_jpg, 0, 0, 0, 0,
$width, $height, $original_width, $original_height);
imagePNG($resized_png);
imageDestroy($resized_png);
}
else if($original_width > $original_height)
{
header("Content-type: image/jpeg");
$image_file = $HTTP_GET_VARS["image_file"];
$width = $HTTP_GET_VARS["width"];
$height = $HTTP_GET_VARS["height"];
$original_jpg = imageCreateFromJPEG($image_file);
$resized_png = imageCreateTrueColor($width, $height);
imageCopyResampled($resized_png, $original_jpg, 0, 0, 0, 0,
$width, $height, $original_width, $original_height);
imagePNG($resized_png);
imageDestroy($resized_png);
}
}
?>
And here's the script that crop them into squares (based on the previous script) that I can't seem to get working.
<?
function image_resize($image_file, $width, $height, $download)
{
$src = "jpg-resize.php?" .
"image_file=" . urlencode($image_file) . "&" .
"width=" . $width . "&" .
"height=" . $height;
$img = "<img " .
"border=\"0\" " .
"width=\"" . $width . "\" " .
"height=\"" . $height . "\" " .
"src=\"" . $src . "\" />";
if($download == true)
$img = "<a href=\"" . $image_file . "\">\n" .
$img . "</a>";
echo($img);
}
function image_horizontal_resize($image_file, $width, $download)
{
$original_size = getImageSize($image_file);
$original_width = $original_size[0];
$original_height = $original_size[1];
$height = round($original_height * ($width / $original_width));
image_resize($image_file, $width, $height, $download);
}
function image_vertical_resize($image_file, $height, $download)
{
$original_size = getImageSize($image_file);
$original_width = $original_size[0];
$original_height = $original_size[1];
$width = round($original_width * ($height / $original_height));
image_resize($image_file, $width, $height, $download);
}
function orientation_create($image_file, $height, $download)
{
if(isset($_GET))
{
$original_size = getImageSize($image_file);
$original_width = $original_size[0];
$original_height = $original_size[1];
if($original_height > $original_width)
{
$image_file = $HTTP_GET_VARS["image_file"];
$width = $HTTP_GET_VARS["height"];
$height = $HTTP_GET_VARS["width"];
$original_jpg = imageCreateFromJPEG($image_file);
$resized_png = imagecreatetruecolor($width, $height);
imageCopyResampled($resized_png, $original_jpg, 0, 0, 0, 0, $width, $height, $original_width, $original_height);
}
else if($original_width > $original_height)
{
$image_file = $HTTP_GET_VARS["image_file"];
$width = $HTTP_GET_VARS["width"];
$height = $HTTP_GET_VARS["height"];
$original_jpg = imageCreateFromJPEG($image_file);
$resized_png = imagecreatetruecolor($width, $height);
imageCopyResampled($resized_png, $original_jpg, 0, 0, 0, 0, $width, $height, $original_width, $original_height);
}
if($width >= $height)
{
$crop = $height;
$offset = round(($width - $height) / 2);
$final_png = imagecreatetruecolor($crop,$crop);
header("Content-type: image/png");
ImageCopy($final_png, $resized_png, 0, 0, $offset, 0, $crop, $crop);
}
else if($height > $width)
{
$crop = $width;
$offset = round(($height - $width) / 2);
$final_png = imagecreatetruecolor($crop,$crop);
header("Content-type: image/jpeg");
ImageCopy($final_png, $resized_png, 0, 0, 0, $offset, $crop, $crop);
}
imagepng($final_png);
imageDestroy($final_png);
}
}
?>
I'm running an Apache 2 server on a FreeBSD system. I have PHP v4.4.2, with GD library preinstalled. Any ideas?