Quote:
|
Originally Posted by ryan_f
Im working on a website right now and i made a script in php to upload an image and then display it on a different page. It works very well. My only problem is that i want to be able to turn those images into thumbnails with small file sizes automatically once i upload the file to my server. I don't really know that much php and i have no idea how i would accomplish this
|
Here is a snippet of code i wrote for an image gallery a while back. It has extensie debug statements that will give you som insight into its operation. It was adapted from some code i found in the PHP documentation. Note your server must have the GD library installed for this to work
PHP Code:
function image_createThumb($src,$dest,$maxWidth,$maxHeight,$quality=100) {
print '$src = ' . $src . '<br>';
if (isset($dest)) {print '$dest is set<br>';} else {print '$dest is not set<br>';}
if (file_exists($src) && isset($dest)) {
print('JPEG Quality: ' . $quality .'<BR>');
// path info
$destInfo = pathInfo($dest);
// image src size
$srcSize = getImageSize($src);
// image dest size $destSize[0] = width, $destSize[1] = height
$srcRatio = $srcSize[0]/$srcSize[1]; // width/height ratio
$destRatio = $maxWidth/$maxHeight;
if ($destRatio > $srcRatio) {
$destSize[1] = $maxHeight;
$destSize[0] = $maxHeight*$srcRatio;
}
else {
$destSize[0] = $maxWidth;
$destSize[1] = $maxWidth/$srcRatio;
}
// path rectification
if ($destInfo['extension'] == "gif") {
$dest = substr_replace($dest, 'jpg', -3);
}
// true color image, with anti-aliasing
print '$maxHeight = ' . $maxHeight . '<br>';
print '$maxWidth = ' . $maxWidth . '<br>';
print 'src height = ' . $srcSize[1] . '<br>';
print 'src width = ' . $srcSize[0] . '<br>';
print 'dest height = ' . $destSize[1] . '<br>';
print 'dest width = ' . $destSize[0] . '<br>';
$destImage = imagecreatetruecolor($destSize[0],$destSize[1]);
imageAntiAlias($destImage,true);
// src image
switch ($srcSize[2]) {
case 1: //GIF
$srcImage = imageCreateFromGif($src);
break;
case 2: //JPEG
$srcImage = imageCreateFromJpeg($src);
break;
case 3: //PNG
$srcImage = imageCreateFromPng($src);
break;
default:
return false;
break;
}
// resampling
imageCopyResampled($destImage, $srcImage, 0, 0, 0, 0,$destSize[0],$destSize[1],$srcSize[0],$srcSize[1]);
// generating image
switch ($srcSize[2]) {
case 1:
case 2:
imageJpeg($destImage,$dest,$quality);
break;
case 3:
imagePng($destImage,$dest);
break;
}
return true;
}
else {
print 'file does not exist<br>';
return false;
}
}