Jump to content

Image Resizing + Putting into Database


ColdEdge

Recommended Posts

Alright, so I have made a simple GD Image Thumbing script. It uploads image as well as creates a copy of it in separate directory.The script is composed of 3 parts, config file, function file and index file. Here is the problem that I am facing. If user A, uploads say file named A.jpg and we have user B upload a file thats titled A.jpg as well the system will overwrite it. I tried to solve this by giving the file random 6 character name (something like is done by tinypic) so image A.jpg would become x7uHJk.kp.jpg for example. And if image A.jpg is uploaded again it will have a new name lets say, i4LKm2.kp.jpg. The problem is in the fact that the functions dose not create a thumb of this new file. The other problem is that I have no idea what I can do to do few of the following things.1 - Limit file size. To about 5 Mb2 - Move filepath to corresponding user avatar field in MySQL.3 - Have the maximum height set to 302 so if the image has greater hight then width it will be scaled to 200 x 302 px.Here are the files coding- config.php

<?php$final_width_of_image = 200;  $path_to_image_directory = '.Pictures/full/';   $path_to_thumbs_directory = '.Pictures/thumbs/'; ?>

- functions.php

<?phpfunction createThumbnail($filename) {   	   	require 'config.php';   	   	if(preg_match('/[.](jpg)$/', $filename)) {   		$im = imagecreatefromjpeg($path_to_image_directory . $filename);   	} else if (preg_match('/[.](gif)$/', $filename)) {   		$im = imagecreatefromgif($path_to_image_directory . $filename);   	} else if (preg_match('/[.](png)$/', $filename)) {   		$im = imagecreatefrompng($path_to_image_directory . $filename);   	}   	   	$ox = imagesx($im);   	$oy = imagesy($im);   	   	$nx = $final_width_of_image;   	$ny = floor($oy * ($final_width_of_image / $ox));	$nm = imagecreatetruecolor($nx, $ny);   			   	imagecopyresampled($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);   	   	if(!file_exists($path_to_thumbs_directory)) {   	  if(!mkdir($path_to_thumbs_directory)) {   		   die("There was a problem. Please try again!");   	  }	 }   		imagejpeg($nm, $path_to_thumbs_directory . "th_" . $filename, 100);   	$tn = '<img src="' . $path_to_thumbs_directory . "th_" . $filename .'" alt="image" style="width:250px;padding: 2px;border: 1px solid #cecece;"/>'; 	$tn .= '<br><a href="' . $path_to_image_directory . $filename .'">View Fullsize ?!</a>';		$tn .= '<br />Upload Successfull!';   	echo $tn;   	}  ?>

and the last piece is the index.php file.

<?php $final_width_of_image = $_GET['size'];?>  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   <html>   <title>Kyonko Media .Inc - We dont need a fancy slogan !</title><style type="text/css" media="all">html, body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, fieldset, textarea, p, blockquote, th, td { margin: 0;padding: 0; } body { background: #f7f7f7;repeat-x scroll center top;font: 11px/150% Arial; }#sub-menu { background-color: #fcfcfc;width: 100%;height: 220px; }#sub-break { background-color: #cecece;width: 100%;height: 11px;border-top:1px solid #dcdcdd;border-bottom:1px solid #ffffff; }#hs { width: 680px;height: 200px;margin: 0 auto; }#hs-fo { width: 680px;height: 50px;margin: 0 auto; }#hs-re { width: 630px;margin: 0 auto; }#sub-search { width: 520px;height: 200px;float: left; }.search-field { width: 308px;height: 30px;padding: 3px; }.search-go { width: 120px;height: 42px; padding: 3px;cursor: pointer; }a { color: #010000;text-decoration: underline; }#error { color: red; }  hr { margin:0 auto; }.sh_img { padding: 2px;border: 1px solid #cecece; }</style>  <head>   	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />   	<meta name="author" content="" />   	<title>ZEN Portal - PALMTOP Development .INC</title>   </head>     <body>  	<div id="sub-menu"></div>	<div id="sub-break"></div>	<div style="width: 600px;height: 500px;margin: 0 auto;">	<?php     require 'config.php';   require 'functions.php';     if(isset($_FILES['fupload'])) {   	   	if(preg_match('/[.](jpg)|(gif)|(png)$/', $_FILES['fupload']['name'])) {   		   		$filename = $_FILES['fupload']['name'];   		$source = $_FILES['fupload']['tmp_name'];	  		$target = $path_to_image_directory . $filename;   		   		move_uploaded_file($source, $target);	  		   		createThumbnail($filename);			}   }   		?>  	<form enctype="multipart/form-data" action="<?php print $_SERVER['PHP_SELF'] ?>" method="post">   		<input type="file" name="fupload" />   		<input type="submit" value="Upload!" />   	</form>   	</div></body>   </html>

- Thanks in advance.

Link to comment
Share on other sites

The problem is in the fact that the functions dose not create a thumb of this new file.
I don't see where you're generating a random filename, but it should be obvious that you need to use that name when you save the file, and then when you create the thumbnail. You obviously wouldn't want to save the file with one name and then try to create a thumbnail from a file with a different name.
1 - Limit file size. To about 5 Mb
Check the PHP file upload tutorial, that shows one method. You can also limit the file size through php.ini. Your code is not checking for any file upload errors though, so you wouldn't know if they exceeded the limit set in php.ini.http://www.php.net/manual/en/features.file-upload.php
2 - Move filepath to corresponding user avatar field in MySQL.
Wouldn't you just look up the path from the database and then pass that information to whatever needs the path?
3 - Have the maximum height set to 302 so if the image has greater hight then width it will be scaled to 200 x 302 px.
You're already checking the width, you can do the same with the height.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...