Jump to content

How Can I Resize When Uploading Images?


slaterino

Recommended Posts

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

 

(edited by kenrbnsn to add


tags)

Link to comment
https://forums.phpfreaks.com/topic/114931-how-can-i-resize-when-uploading-images/
Share on other sites

hope this makes sense

 

//this defines the width of the thumbnail
/*existing code below*/
define('THUMBNAIL_WIDTH', 100);
//FullImage Width //Add line below
define('FullImage_WIDTH', 500);

 

// resize FullImage //ADD line below
$result = createThumbnail($uploadDir . $imagePath, $uploadDir . $imagePath, FullImage_WIDTH);


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

 

to lower case filename

//change
$imagePath = md5(rand() * time()) . ".$ext";
//to
$imagePath = strtolower(md5(rand() * time()) . ".$ext");

 

and

//change
$thumbnailPath=md5(rand() * time()) . ".$ext";
//to
$thumbnailPath=strtolower(md5(rand() * time()) . ".$ext");

 

 

EDIT:&#160; seams to appear everywhere

Hey,

Thanks for the help. The lower case extensions are now solved. One question though, where in the coding do I insert:

 

//

$result = createThumbnail($uploadDir . $imagePath, $uploadDir . $imagePath, FullImage_WIDTH);

 

I'm still getting to grips with PHP and anyone help is always appreciated!!

 

Thanks

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.