Jump to content

Image upload and resize creating a black image


andrew_biggart

Recommended Posts

I have been wrecking my brain about this now for two weeks and cannot for the life of me figure out were I am going. I am only after some guidance, not asking someone to right my code for me.

 

I have read dozens of tutorials and did a lot of research on this top as it is one of the key elements of my website, so I have to get it working properly. I have wrote a script which uploads and image, which I have got working fine. Now I am trying to add some code to resize the image and create a resized image.

 

Once I have done that, I am going to be saving the original to an uploads folder, and saving the thumbnail to a thumbs directory.

 

The problem that I am facing is that everything is working as intended apart from the imagecopyresampled() part of the script. When ever the new image is being created and saved into the thumbs directory, it is creating a pure black image every time.

 

I cannot figure out why this is happening, can anyone point me in the right direction? Or spot something that I've missed?

 

 

<?php
session_start();

$path_thumbs = "uploads/thumbs";
$path_big        = "uploads";
$createdby       = $_SESSION["ufullname"];		

//the new width of the resized image, in pixels.

$img_thumb_width = 570; // 

$extlimit = "yes"; //Limit allowed extensions? (no for all extensions allowed)

//List of allowed extensions if extlimit = yes

$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");		

//the image -> variables

$file_type = $_FILES['pic']['type'];
$file_name = $_FILES['pic']['name'];
$file_size = $_FILES['pic']['size'];
$file_tmp = $_FILES['pic']['tmp_name'];

//check if you have selected a file.

if(!is_uploaded_file($file_tmp)){

	exit_status('Error: Please select a file to upload!');
	exit(); //exit the script and don't process the rest of it!

}

//check the file's extension

$ext = strrchr($file_name,'.');

$ext = strtolower($ext);

//uh-oh! the file extension is not allowed!

if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {

	exit_status('Wrong file extension.');
	exit();

}

//so, whats the file's extension?

$getExt = explode ('.', $file_name);

$file_ext = $getExt[count($getExt)-1];

//create a random file name

$rand_name = md5(time());

$rand_name = $rand_name . rand(0,999999999);

//the new width variable

$ThumbWidth = $img_thumb_width;



/////////////////////////////////

///// CREATE THE THUMBNAIL /////

///////////////////////////////



//keep image type

if($file_size){

	if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){

		$new_img = imagecreatefromjpeg($file_tmp);

	}elseif($file_type == "image/x-png" || $file_type == "image/png"){

		$new_img = imagecreatefrompng($file_tmp);

	}elseif($file_type == "image/gif"){

		$new_img = imagecreatefromgif($file_tmp);

	}

	//list the width and height and keep the height ratio.

	list($width, $height) = getimagesize($file_tmp);

	//calculate the image ratio

	$imgratio=$width/$height;

	if ($imgratio>1){

		$newwidth = $ThumbWidth;

		$newheight = $ThumbWidth/$imgratio;

	}else{

		$newheight = $ThumbWidth;

		$newwidth = $ThumbWidth*$imgratio;

	}

	//function for resize image.

	if (function_exists(imagecreatetruecolor)){

		$resized_img = imagecreatetruecolor($newwidth,$newheight);

	}else{

		die("Error: Please make sure you have GD library ver 2+");

	}

	//the resizing is going on here!

	imagecopyresampled($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

	//finally, save the image

	imagejpeg($resized_img,"$path_thumbs/$rand_name.$file_ext", 100);


}



//ok copy the finished file to the thumbnail directory

if(move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext")){

	include('functions.php');

	connect();

	$link   = get_option('admin_url');

	$sql    = " INSERT into mediaT ( name, url, link, createdon, createdby ) VALUES ( '$file_name', '$path_big/$rand_name.$file_ext', '$link$path_big/$rand_name.$file_ext', NOW(), '$createdby' ) ";
	$result = mysql_query($sql);

}

exit_status('Something went wrong with your upload!');


// Helper functions

function exit_status($str){
	echo json_encode(array('status'=>$str));
	exit;
}

function get_extension($file_name){
	$ext = explode('.', $file_name);
	$ext = array_pop($ext);
	return strtolower($ext);
}
?>

Link to comment
Share on other sites

Guest
This topic is now 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.