Page 1 of 1

How Can I Resize When Uploading Images?

Posted: Tue Jul 15, 2008 5:54 pm
by slaterino
Hi,
Basically, there's two parts to my query. First off, I have used a tutorial to create an image gallery which automatically creates thumbnails of each image with a certain width but I also want to know how I can resize each full-size image to a specific size.

Secondly, is there any way that I can ensure that all files are saved in lower-case file formats? I have had some problems uploading files that have upper-case file formats which are then appearing in the gallery.

I have included code from the 3 pages which relate to uploading images:

config.php
<?php
define('ALBUM_IMG_DIR', '/home/sites/thedaffodilsociety.com/public_html/gallery/images/album/');

define('GALLERY_IMG_DIR', '/home/sites/thedaffodilsociety.com/public_html/gallery/images/gallery/');

define('THUMBNAIL_WIDTH', 100);
?>

functions.php
<?php
function uploadImage($inputName, $uploadDir)
{
$image = $_FILES[$inputName];
$imagePath = '';
$thumbnailPath = '';

if (trim($image['tmp_name']) != '') {
$ext = substr(strrchr($image['name'], "."), 1);

// generate a random new file name to avoid name conflict
// then save the image under the new file name
$imagePath = md5(rand() * time()) . ".$ext";
$result = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath);

if ($result) {
// create thumbnail
$thumbnailPath = md5(rand() * time()) . ".$ext";
$result = createThumbnail($uploadDir . $imagePath, $uploadDir . 'thumbnail/' . $thumbnailPath, THUMBNAIL_WIDTH);

// create thumbnail failed, delete the image
if (!$result) {
unlink($uploadDir . $imagePath);
$imagePath = $thumbnailPath = '';
} else {
$thumbnailPath = $result;
}
} else {
// the image cannot be uploaded
$imagePath = $thumbnailPath = '';
}

}


return array('image' => $imagePath, 'thumbnail' => $thumbnailPath);
}

function createThumbnail($srcFile, $destFile, $width, $quality = 75)
{
$thumbnail = '';

if (file_exists($srcFile) && isset($destFile))
{
$size = getimagesize($srcFile);
$w = number_format($width, 0, ',', '');
$h = number_format(($size[1] / $size[0]) * $width, 0, ',', '');

$thumbnail = copyImage($srcFile, $destFile, $w, $h, $quality);
}

}

function copyImage($srcFile, $destFile, $w, $h, $quality = 75)
{
$tmpSrc = pathinfo(strtolower($srcFile));
$tmpDest = pathinfo(strtolower($destFile));
$size = getimagesize($srcFile);

if ($tmpDest['extension'] == "gif" || $tmpDest['extension'] == "jpg")
{
$destFile = substr_replace($destFile, 'jpg', -3);
$dest = imagecreatetruecolor($w, $h);
//imageantialias($dest, TRUE);
} elseif ($tmpDest['extension'] == "png") {
$dest = imagecreatetruecolor($w, $h);
//imageantialias($dest, TRUE);
} else {
return false;
}

switch($size[2])
{
case 1: //GIF
$src = imagecreatefromgif($srcFile);
break;
case 2: //JPEG
$src = imagecreatefromjpeg($srcFile);
break;
case 3: //PNG
$src = imagecreatefrompng($srcFile);
break;
default:
return false;
break;
}

imagecopyresampled($dest, $src, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);

switch($size[2])
{
case 1:
case 2:
imagejpeg($dest,$destFile, $quality);
break;
case 3:
imagepng($dest,$destFile);
}
return $destFile;

}
?>

add-image.php
<?php
if(isset($_POST['txtTitle']))
{
$albumId = $_POST['cboAlbum'];
$imgTitle = $_POST['txtTitle'];
$imgDesc = $_POST['mtxDesc'];
$imgDiv = $_POST['mtxDiv'];
$imgEx = $_POST['mtxEx'];

$images = uploadImage('fleImage', GALLERY_IMG_DIR);

if ($images['image'] == '' && $images['thumbnail'] == '') {
echo "Error uploading file";
exit;
}

$image = $images['image'];
$thumbnail = $images['thumbnail'];

if (!get_magic_quotes_gpc()) {
$albumName = addslashes($albumName);
$albumDesc = addslashes($albumDesc);
$imgPath = addslashes($imgPath);
}

$sql = "INSERT INTO tbl_image (im_album_id, im_title, im_bloom, im_division, im_exhibit, im_image, im_thumbnail, im_date)
VALUES ($albumId, '$imgTitle', '$imgDesc', '$imgDiv', '$imgEx', '$image', '$thumbnail', NOW())";

mysql_query($sql) or die('Error, add image failed : ' . mysql_error());

echo "<script>window.location.href='index.php?page=list-image&album=$albumId';</script>";
exit;
}
?>

If anyone can help with either of these issues or could point me in the direction of a tutorial that could help I would be amazingly grateful!!!

Thanks,
Russ

Re: How Can I Resize When Uploading Images?

Posted: Tue Jul 15, 2008 8:18 pm
by Benjamin
I see you've got image resize code up there, but we aren't going to debug it. Where are you stuck?

When saving the file you can use strtolower to change the case to lowercase.

Re: How Can I Resize When Uploading Images?

Posted: Wed Jul 16, 2008 3:53 am
by slaterino
The resize code works well for creating the thumbnails but I took this code from a tutorial and was hoping that someone might be able to suggest how I could recreate it for all full size images. Sorry to ask so much, it's just i'm a relative newbie and have been struggling getting my head round this. If anyone can just offer me some pointers, maybe some of the code that I could duplicate and just change Thumbnail to FullsizeImage or something like that, that would be amazing!!

The lower case problem is now sorted. strtolower did the job on that one!

Thanks!