Fotoupload script met .zip ondersteuning, hulp gevraagd.

Status
Niet open voor verdere reacties.

tmind

Gebruiker
Lid geworden
18 mrt 2008
Berichten
8
Als het over het uploaden van bestanden gaat dan heb ik meestal geen zin om helemaal uit te gaan pluizen hoe alle image functies werken dus heb ik een bestaand script aangepast zodat het voor mij wel ok overkomt.

Wat ik nu wil proberen wat nog niet werkt is een .zip met .jpg bestanden uploaden en dat deze allemaal geupload gerenamed en er een thumb van gemaakt word.

Op het moment werkt dit als ik een .jpg of .jpeg upload. Ik laat even zien wat ik geprobeerd heb:

Code:
<?	
	//modules/album/album.admin.php
	//Module for OJCdewerf.nl album administrator
	//check for "hack" attempt
	if (!defined('TMIND')){header("location:../../");die;}
	class albumadmin
	{
		function start()
		{
			$loadconfig = mysql_query('select * from `album_config`');
			while ($l = mysql_fetch_assoc($loadconfig))
			{
				$_GET['album_config']['homepagetype'] = $l['homepagetype'];
				$_GET['album_config']['photoperpage'] = $l['photoperpage'];
				$_GET['album_config']['ajaxdisplay'] = $l['ajaxdisplay'];
				$_GET['album_config']['maxheight'] = $l['maxheight'];
				$_GET['album_config']['maxwidth'] = $l['maxwidth'];
				$_GET['album_config']['textrandomphoto'] = $l['textrandomphoto'];
				$_GET['album_config']['textnext'] = $l['textnext'];
				$_GET['album_config']['textprevious'] = $l['textprevious'];
				$_GET['album_config']['buttonheight'] = $l['buttonheight'];
				$_GET['album_config']['buttonwidth'] = $l['buttonwidth'];
			}
			if (!isset($_GET[3]))
			{
				$_GET['ADMIN_PAGE_TYPE'] = 'link';
				$title = 'album main';
				$dump[0] .= "{1}module_album_new{2}module_album_new{3}add{4}Add photoalbum{5}Add photoalbum{6}";
				$dump[2] .= "{1}module_album_upload{2}module_album_upload{3}upload{4}upload photo's{5}upload photo's{6}";
				$dump[2] .= "{1}module_album_browse_albums{2}module_album_browse_albums{3}search{4}Browse albums{5}Browse albums{6}";
				$dump[3] .= "{1}module_album_browse_photo{2}module_album_browse_photo{3}search{4}Browse photo's{5}Browse photo's{6}";
				$dump[4] .= "{1}module_album_config{2}module_album_config{3}config{4}Configuration{5}Configuration{6}";
			}
			elseif($_GET[3] == 'new')
			{
				$dump  = formelements::form("data",$_GET['CURRENTPAGE']."_module_album_.php");
				$dump .= formelements::create("Name :","file","",'albumname','');
				$dump .= formelements::create("Description :","text",'',"albumdescription",'');
				$dump .= formelements::create("In photoalbum :","select",'',"albumid",$albumdump);
				$dump .= formelements::create("Active :","select",'',"active",'1|Yes,0|No');
				$dump .= formelements::create("","submit","Load me up","submit","");
				$dump .= formelements::formend();
			}
			elseif($_GET[3] == 'upload')
			{
				$_GET['ADMIN_PAGE_TYPE'] = 'dump2content';
				$title = 'Add a new event';
				$i = 0;
				$albumdump = '';
				$albums = mysql_query("select `id`,`albumname` from `album`");
				while ($a = mysql_fetch_array($albums))
				{
					if (!isset($kek)){$kek=true;}else{$albumdump.=',';}
					$albumdump.= $a['id']."|".$a['albumname'];
				}
				$dump  = formelements::form("data",$_GET['CURRENTPAGE']."_module_album_newpics.php");
				$dump .= formelements::create("file(s) jpg or zip :","file","",'file','');
				$dump .= formelements::create("Description :","text",'',"description",'');
				$dump .= formelements::create("In photoalbum :","select",'',"albumid",$albumdump);
				$dump .= formelements::create("Active :","select",'',"active",'1|Yes,0|No');
				$dump .= formelements::create("","submit","Load me up","submit","");
				$dump .= formelements::formend();
			}
			elseif($_GET[3] == 'browse' AND $_GET[4] == 'albums')
			{
			}
			elseif($_GET[3] == 'browse' AND $_GET[4] == 'photo')
			{
			}
			elseif($_GET[3] == 'newpics')
			{
				if (is_uploaded_file($_FILES['file']['tmp_name']))
				{
					$getstuff = explode('.',$_FILES['file']['name']);
					$tot = count($getstuff) -1;
					$ext = strtolower($getstuff[$tot]);
					if ($ext == 'jpg' or $ext == 'jpeg')
					{
						albumadmin::upload(0);
						tmind::redirect($_GET['CURRENTPAGE']."_module_album");
					}
					elseif($ext = 'zip')
					{
						albumadmin::upload(1);
						tmind::redirect($_GET['CURRENTPAGE']."_module_album");
					}
					else
					{
						tmind::redirect($_GET['CURRENTPAGE'].'_modules_album_extension');
					}			
				}
				tmind::redirect($_GET['CURRENTPAGE'].'_modules_album_uploaderror');
			}
			tmind::tdefine("PAGETITLEFORCONTENT",$title);
			tmind::tdefine('ADMIN_DUMP',$dump);
		}
		function upload($archive)
		{
			ob_end_clean();
			$idfetch = mysql_query("select MAX(`id`) as `id` from `album_photo` ");
			while($id = mysql_fetch_assoc($idfetch))
			{
				$idd=$id['id'];
			}
			if ($archive == 1)
			{
			$zip = zip_open($_FILES['file']['tmp_name']);

				if ($zip) {
				    while ($zip_entry = zip_read($zip)) {
				        if (zip_entry_open($zip, $zip_entry, "r")) {
				            albumadmin::thumb_and_resize(zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)),$id,1);
							zip_entry_close($zip_entry);
				        }
				    }
				    zip_close($zip);
				}
			}
			else
			{
				$resized = albumadmin::thumb_and_resize($_FILES['file']['tmp_name'],$idd,0);
			}
			die;
		}
		function thumb_and_resize($file,$id,$zip)
		{
			$id=$id+1;
			// This is the temporary file created by PHP 
				$uploadedfile = $file;
			if ($zip ==0)
			{
				$src = imagecreatefromjpeg($uploadedfile);
			}
			else
			{
				$src = imagecreatefromstring($uploadedfile);
			}
			
			// Capture the original size of the uploaded image
			list($width,$height)=getimagesize($uploadedfile);
			// For our purposes, I have resized the image to be
			// 600 pixels wide, and maintain the original aspect 
			// ratio. This prevents the image from being "stretched"
			// or "squashed". If you prefer some max width other than
			// 600, simply change the $newwidth variable
			if ($width > $_GET['album_config']['buttonwidth'])
				{	$tmp = round($width / $_GET['album_config']['buttonwidth'],1);
					$dwidth = $width / $tmp;
					$dheight = $height / $tmp;
				}
				if ($dheight > $_GET['album_config']['buttonheight'])
				{
					$tmp = round($dheight / $_GET['album_config']['buttonheight'],1);
					$dwidth = $dwidth / $tmp;
					$dheight = $dheight / $tmp;
				}
				$newwidth = round($dwidth);
				$newheight= round($dheight);
				$tmp=imagecreatetruecolor($newwidth,$newheight);
			// this line actually does the image resizing, copying from the original
			// image into the $tmp image
			imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 
			// now write the resized image to disk. I have assumed that you want the
			// resized, uploaded image file to reside in the ./images subdirectory.
			$filename = "modules/album/photo/".$id."_small.jpg";
			if (!imagejpeg($tmp,$filename,100))
			{
				tmind::redirect($_GET['CURRENTPAGE']."_module_album_cantmake");
				die;
			}
			$src = imagecreatefromjpeg($uploadedfile);
			// Capture the original size of the uploaded image
			list($width,$height)=getimagesize($uploadedfile);
			if ($width > $_GET['album_config']['maxwidth'])
				{	$tmp = round($width / $_GET['album_config']['maxwidth'],1);
					$dwidth = $width / $tmp;
					$dheight = $height / $tmp;
				}
				if ($dheight > $_GET['album_config']['maxheight'])
				{
					$tmp = round($dheight / $_GET['album_config']['maxheight'],1);
					$dwidth = $dwidth / $tmp;
					$dheight = $dheight / $tmp;
				}
				$newwidth = round($dwidth);
				$newheight= round($dheight);
				$tmp=imagecreatetruecolor($newwidth,$newheight);
			// this line actually does the image resizing, copying from the original
			// image into the $tmp image
			imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 
			// now write the resized image to disk. I have assumed that you want the
			// resized, uploaded image file to reside in the ./images subdirectory.
			$filename = "modules/album/photo/".$id.".jpg";
			if(!imagejpeg($tmp,$filename,100))
			{
				tmind::redirect($_GET['CURRENTPAGE']."_module_album_cantmake");
				die;
			}
			imagedestroy($src);
			imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request
			// has completed.
			$description =  $_POST['description'];
			$filesize=filesize("modules/album/photo/".$id.".jpg");
			list($width, $height, $type, $attr) = @getimagesize("modules/album/photo/".$id.".jpg");
			$active = $_POST['active'];
			$insert = mysql_query("insert into `album_photo` (`id`,`description`,`albumid`,`filesize`,`height`,`width`,`active`) values ('".$id."','".$description."','".$albumid."','".$filesize."','".$height."','".$width."','".$active."')");
		}
	}
?>

Het belanrijkste aandachtspunt zijn de functies Upload en thumb_and_resize,

In het Upload gedeelte word er gekeken of het een .zip of .jpg is indien het een zip is word deze open gewrikt en probeer ik de source van de images door thumb_and_resize heen te slepen maar dit is mij niet gelukt.

Kan iemand mij vertellen of ik even een andere functie op php.net moet doorlezen of iets dergenlijks? ik kan zo snel niets googlen en als iemand mij hier mee vooruit kan helpen dan zou ik dat zeer op prijs stellen ;).
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan