Jump to content

Image thumbnail creation


wrathican

Recommended Posts

hey peeps

 

ive made an upload function and then a function to create a thumbnail from the uploaded file.

 

the file uploads correctly but the thumbnail doesn't seem to get created.

 

here is the upload function:

function uploadFile ($oldFile, $userFile, $imgDir) {

//extensions array
$validExt = array ('.jpg', '.jpeg', '.png', '.gif');

//get the file ext
$ext = strrchr($userFile, ".");

$ext = strtolower($ext);

//check the file ext against the array
if(in_array($ext, $validExt)) {
	//needle is in haystack
	//process upload

	//rename the file
	$newName = md5(rand() * time()) . $ext;

	//new file dest
	$destFile = $imgDir . $newName;

	//move the file to tmpDir
	$result = move_uploaded_file($oldFile, $destFile);
	if($result == false) {
		//there was an error uploading
		return false;
	}else{
		//file was uploaded
		return $destFile;
	}

}else{
	//needle is not in haystack. exit
	return false;
}

}

 

and heres the thumbnail creation function:

function createThumb($oldFile, $userFile, $imgDir, $picWidth) {

$result = uploadFile($oldFile, $userFile, $imgDir);

if ($result == false) {

	//file could not be uploaded

	return false;

}else{

	//file was uploaded. create thumbnail

	//pathinfo
	$file = pathinfo($result);

	//find the .ext
	$ext = $file['extension'];

	//strip .ext to lower
	$ext = strtolower($ext);

	//get image sizes
	$sizes = getimagesize($result);

	//ratio
	$ratio = $sizes[0]/$sizes[1];

	//create new height
	$newHeight = round($picWidth/$aspect);

	//create true colour image		
	$srcImg = imagecreatetruecolor($picWidth, $newHeight);

	if($ext == 'jpg' || $ext == 'jpeg') {

		$tmpImg = imagecreatefromjpeg($result);

	}elseif($ext == 'png') {

		$tmpImg = imagecreatefrompng($result);

	}elseif($ext == 'gif') {

		$tmpImg = imagecreatefromgif($result);

	}

	imagecopyresampled($srcImg, $tmpImg, 0, 0, 0, 0, $picWidth, $newHeight, $sizes[0], $sizes[1]);

	//save image
	if($ext == 'jpg' || $ext == 'jpeg') {

		imagejpeg($srcImg, $result, 100);

	}elseif($ext == 'png') {

		imagepng($scrImg, $result, 100);

	}elseif($ext == 'gif') {

		imagegif($srcImg, $result, 100);

	}

	imagedestroy($srcImg);
	imagedestroy($tmpImg);

}
}

 

and heres how i call the function:

$tmpFile = $_FILES['image']['tmp_name'];
$userFile = $_FILES['image']['name'];
$thumbWidth = 150;
$albumImg = '../images/album/';
$result = createThumb ($tmpFile, $userFile, $albumImg, $thumbWidth);

 

anyone have any idea why the script wont create the thumbnail?

Link to comment
https://forums.phpfreaks.com/topic/105256-image-thumbnail-creation/
Share on other sites

i cannot be bothered to look through your code because i have a headache from a heavy drinking weekend but if you would like i have made the code you are looking for before if you want me to email it to you.  it uploads the images and creates the thumbnail image.  it also stores them in to different files on the server.  it would need alittle bit of tweaking so that it works for you i.e. changing where the files are stored and things like that but at least you no it already is being used on a site and it works as intended.

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.