Jump to content

PHP - Upload image, store it, add mysql entry + create thumbnail.


llamaultima

Recommended Posts

Hello,

I'm currently using this: http://pastebin.com/h1jMkf7J - it's working great, but I'm noticing that giant images are increasing loading times drastically, so I would like to create a thumbnail of the image as well, somewhere around 400px width and maintaining the aspect ratio. So, it'd be; uploads/theimage.png and uploads/theimage-thumbnail.png

Can anyone help with this please? I've been trying different methods, such as http://www.9lessons.info/2009/03/upload-and-resize-image-with-php.html but I haven't been able to combine the two.

Thanks.

Link to comment
Share on other sites

  • 1 month later...

ok i have this function i wrote for you should work ....

/*	create thumb
*
*	@$path	path to the origional image
*	@$src	sorce image to resize
*	@$name	the name with out extention
*	@$ext	the extention
*	@$w	(default null) set the new width no required
*	@$h	(default null) set the new height not required
*	!!! $width or $hight one must be set
*/
function _createThumb($path, $src, $name, $ext, $w=null, $h=null)
	{
		$mime = getimagesize($src);

		switch($mime['mime'])
		{
			case 'image/png':
				$src_img = imagecreatefrompng($src);
			break;
			case 'image/jpg':
				$src_img = imagecreatefromjpeg($src);
			break;
			case 'image/jpeg':
				$src_img = imagecreatefromjpeg($src);
			break;
			case 'image/pjpeg':
				$src_img = imagecreatefromjpeg($src);
			break;
			default : return "Unsupported picture type!"; break;
		}
		
		$width = imageSX($src_img);
		$height = imageSY($src_img);
		
		if (isset($w) && !isset($h))
		{
			$ratio = $width/$w;
		}
		elseif(!isset($w) && isset($h))
		{
			$ratio = $height/$h;
		}
		else
		{
			$ratio = '1';
		}
		// allways make sure ratio is 1 or more less is bad
		if ($ratio < 1){$ratio = 1;}
		
		$nwidth = floor($width/$ratio);
		$nheight = floor($height/$ratio);
		
		$dst_img        =   ImageCreateTrueColor($nwidth,$nheight);

		imagecopyresampled($dst_img,$src_img,0,0,0,0,$nwidth,$nheight,$width,$height);
		
		$new_thumb_loc = $path.$name.'-thumbnail.'.$ext;

		if($mime['mime']=='image/png'){ $result = imagepng($dst_img,$new_thumb_loc,; }
		if($mime['mime']=='image/jpg'){ $result = imagejpeg($dst_img,$new_thumb_loc,80); }
		if($mime['mime']=='image/jpeg'){ $result = imagejpeg($dst_img,$new_thumb_loc,80); }
		if($mime['mime']=='image/pjpeg'){ $result = imagejpeg($dst_img,$new_thumb_loc,80); }

		imagedestroy($dst_img);
		imagedestroy($src_img);
		return true;
	}

then use it like this notice the changes in your script

$image_name = time().substr(str_replace(" ", "_", $ext), 5);
$actual_image_name = $image_name.".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
	{
	mysqli_query($db,"INSERT INTO `media` (title, description, image) VALUES ('$imgtitle', '$imgdesc', '$actual_image_name')");
		//	create thumb
		_createThumb($path, $path.$actual_image_name, $image_name, $ext, '400', null);
		// end thumb
		header('Location: media.php');
	}
Link to comment
Share on other sites

$mime = getimagesize($src);

		switch($mime['mime'])
		{

Is unreliable

 

Look into File Uploads

<?php

if(isset($_REQUEST['upload']))
{

$dir = "./";
$file = $dir . basename($_FILES['userfile']['name']);
$ext = pathinfo($file, PATHINFO_EXTENSION);
$allowed = array('jpg', 'bmp', 'png', 'gif');

if($_FILES['userfile']['size'] > 500000)

{

die("File size is too large!");

} if(!in_array($ext != $allowed))
{

die("The selected file is not allowed to be uploaded. You may upload files with the following
extensions: jpg, png, bmp, gif");

} if(move_uploaded_file($_FILES['userfile']['tmp_name'], $file))
{

echo "Your file: ".$file." has been uploaded!";

} else 
{

echo "Upload failed";

echo "Here is some debugging information:";

print_r($_FILES);

}

if(is_uploaded_file($file))
{
die("A file with the same name has already been uploaded. Please re-name your file and try again.");
}

}

?>

hasn't been tested just coded it purely as an example.

 

blacknight's code looks ok apart from mime to check the file type so you can do it which ever way just add a sql record like blacknight has when the file is uploading.

Edited by Tom10
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.