Log in

View Full Version : Square image thumbnail



jnscollier
05-30-2007, 04:39 AM
So i found a script that does what I want but I'm having trouble "implementing" it. Basically I have all the image file names stored in my table and i want them to all display (the ones uploaded by the particular user logged in) on a page.

Of course I don't want them enormous, just small thumbnail images would do. I don't want them clickable or anything, i don't even want them to enlarge. But because of the whole portrait and landscape thing, i didn't want to hardcode the size in html. so, after searching i found this script:




function createThumb($source,$dest) {
$thumb_size = 150;
$size = getimagesize($source);
$width = $size[0];
$height = $size[1];
if($width> $height) {
$x = ceil(($width - $height) / 2 );
$width = $height;
} elseif($height> $width) {
$y = ceil(($height - $width) / 2);
$height = $width;
}
$new_im = ImageCreatetruecolor($thumb_size,$thumb_size);
$im = imagecreatefromjpeg($source);
imagecopyresampled($new_im,$im,0,0,$x,$y,$thumb_size,$thumb_size,$width,$height);
imagejpeg($new_im,$dest,100);
}


here's how I'm calling the function:



<?php
$path = "upload/$pic1";

echo
"<img src=\"'".createThumb($path)."'\" />";
?>


(that's in a while statement)

i get a page full of wingdings and it says i'm also missing an argument. can anyone help?? the directions with this script really suck and don't explain anything.

thanks,
sammy

mbrodin
05-30-2007, 07:19 AM
Hi there!

Try this:

function createThumb($source)
{
$thumb_size = 150;
$size = getimagesize($source);
$width = $size[0];
$height = $size[1];

if($width > $height)
{
$x = ceil( ( $width - $height ) / 2 );
$width = $height;
}
elseif($height > $width)
{
$y = ceil( ( $height - $width ) / 2 );
$height = $width;
}

$new_im = ImageCreatetruecolor($thumb_size,$thumb_size);
$im = imagecreatefromjpeg($source);
imagecopyresampled($new_im, $im, 0, 0, $x, $y, $thumb_size, $thumb_size, $width, $height);
imagejpeg($new_im, '', 100);
}


Make sure it's a JPEG-image you send, else you also must use imagegif() and imagepng etc.

Hopes this was usefull info!

----------------------------------
Edit:
----------------------------------
I forgoted, you can never call the createThumb()-function in a img-tag directly. You must add the the function in a own file let's say thumbnail.php,

Thumbnail.php:

<?php
function createThumb($source)
{
$thumb_size = 150;
$size = getimagesize($source);
$width = $size[0];
$height = $size[1];

if($width > $height)
{
$x = ceil( ( $width - $height ) / 2 );
$width = $height;
}
elseif($height > $width)
{
$y = ceil( ( $height - $width ) / 2 );
$height = $width;
}

$new_im = ImageCreatetruecolor($thumb_size,$thumb_size);
$im = imagecreatefromjpeg($source);
imagecopyresampled($new_im, $im, 0, 0, $x, $y, $thumb_size, $thumb_size, $width, $height);
imagejpeg($new_im, '', 100);
}


$path = "upload/$pic1";

createThumb($path);
?>


Then you have the page where youre image-tag call the thumbnail.ph, let's say it's name is getImage.php,

getImage.php


<html>
<head>
<title>Get image</title>
</head>

<body>
<img src="thumbnail.php">
</body>

</html>



I hopes you understand how I mean!

:D

Best regards,
mbrodin

jnscollier
05-30-2007, 07:05 PM
awesome! you answered my next question too about pngs and gifs.... ROCK ON! THANK YOU!

mbrodin
05-30-2007, 11:34 PM
awesome! you answered my next question too about pngs and gifs.... ROCK ON! THANK YOU!

It's always a plessure to help! ;)

Best regards,
mbrodin