regoch Posted June 30, 2011 Share Posted June 30, 2011 Hi! I got this script for upload images and working fine. Got images width always 700px * 460px or 700px * 1050px. But for new project I need always same height, not weight. Anybody have some idea what to change in my code? <?php require_once('zastita.php'); //Start session session_start(); //Podaci za spajanje na bazu podataka require_once('config.php'); //Array za provjeru jesu li ispunjena sva polja $errmsg_arr = array(); //Isključivanje provjere jesu li ispunjena sva polja $errflag = false; //Spajanje na mysql server $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Greška prilikom spajanja na server: ' . mysql_error()); } //Odabir baze podataka $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Greška prilikom spajanja na bazu podataka"); } ?> <?php include ("include/head.php"); ?> <body> <div id="main_container"> <div class="header"> <div class="logo"><a href="index.php"><img src="images/logo.gif" alt="" title="" border="0" /></a></div> <div class="right_header">Administrator: <?php echo $_SESSION['SESS_FIRST_NAME'];?> | <a href="odjava.php" class="logout">Odjavi se</a></div> <div class="jclock"></div> </div> <div class="main_content"> <?php include ("include/menu.php");?> <div class="center_content"> <div class="right_content"> <h2>Admin Panel - Unos slika</h2> <div class="form"> <?php error_reporting(0); $change=""; $abc=""; define ("MAX_SIZE","4096"); function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } $errors=0; $slike = array(file, file1, file2, file3, file4); if($_SERVER["REQUEST_METHOD"] == "POST") { // $image =$_FILES["file"]["name"]; // $uploadedfile = $_FILES['file']['tmp_name']; $filearray = array(); $filearray1 = array(); $k=0; foreach($slike as $slika){ $image =$_FILES[$slika]["name"]; $uploadedfile = $_FILES[$slika]['tmp_name']; if ($image) { $filename = stripslashes($_FILES[$slika]['name']); $extension = getExtension($filename); $extension = strtolower($extension); if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { $change='<div class="msgdiv">Unknown Image extension </div> '; $errors=1; } else { $size=filesize($_FILES[$slika]['tmp_name']); if ($size > MAX_SIZE*4096) { $change='<div class="msgdiv">Slika je veća od 4096kb!</div> '; $errors=1; } if($extension=="jpg" || $extension=="jpeg" ) { $uploadedfile = $_FILES[$slika]['tmp_name']; $src = imagecreatefromjpeg($uploadedfile); } else if($extension=="png") { $uploadedfile = $_FILES[$slika]['tmp_name']; $src = imagecreatefrompng($uploadedfile); } else { $src = imagecreatefromgif($uploadedfile); } echo $scr; list($width,$height)=getimagesize($uploadedfile); $newwidth=700; $newheight=($height/$width)*$newwidth; $tmp=imagecreatetruecolor($newwidth,$newheight); $newwidth1=190; $newheight1=140; $tmp1=imagecreatetruecolor($newwidth1,$newheight1); imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height); //$filename = "images/". $_FILES['file']['name']; //$filename1 = "images/small". $_FILES['file']['name']; $rand = rand(0000000000, 9999999999); $image_name=(date("dmY")+$k).'_'.$rand.'.'.$extension; $filename = "../slike/".$image_name; $filename1 = "../slike/thumbnails/".$image_name; $filename3 = $image_name; $filename4 = $image_name; $id_galerije = $_GET['id_galerije']; imagejpeg($tmp,$filename,100); imagejpeg($tmp1,$filename1,100); $filearray[$k]= $filename3; $filearray1[$k]= $filename4; $filearray2[$k]= $id_galerije; $k++; // 3. Perform database query mysql_query("INSERT INTO slike set picture_name='$filename3', picture_name1='$filename4', id_galerije='$id_galerije'"); echo '<br /><img src="../slike/thumbnails/'.$filename4.'"><br />'; imagedestroy($src); imagedestroy($tmp); imagedestroy($tmp1); }} } } // If no errors registred, print the success message if(isset($_POST['Submit']) && !$errors) { if ($filename3==""){ echo " <table width='400px' align='center' cellspacing='0'> <tr bgcolor='#E6e6e6' height='26'> <td align='center' width='100%' colspan='2'><strong>Potvrda</strong></td> </tr> <tr bgcolor='#F6f6f6'> <td align='center' style='border-left:#E6e6e6 1px solid;border-bottom:#E6e6e6 1px solid;'><img src='images/warning.png'></td> <td align='center' width='80'% style='border-right:#E6e6e6 1px solid;border-bottom:#E6e6e6 1px solid;'><strong>Slike nisu unešene.</strong></td> </tr> </table><br />"; } else{ echo " <table width='400px' align='center' cellspacing='0'> <tr bgcolor='#E6e6e6' height='26'> <td align='center' width='100%' colspan='2'><strong>Potvrda</strong></td> </tr> <tr bgcolor='#F6f6f6'> <td align='center' style='border-left:#E6e6e6 1px solid;border-bottom:#E6e6e6 1px solid;'><img src='images/valid.png'></td> <td align='center' width='80'% style='border-right:#E6e6e6 1px solid;border-bottom:#E6e6e6 1px solid;'><strong>Slike uspješno unesene.</strong></td> </tr> </table><br />"; } } /*If no errors registred, print the success message if(isset($_POST['Submit']) && !$errors) { // mysql_query("update {$prefix}users set img='$big',img_small='$small' where user_id='$user'"); $change=' <div class="msgdiv">Image Uploaded Successfully!</div>'; }*/ ?> <div align="center" id="err"> <?php echo $change; ?> </div> <form method="post" action="" enctype="multipart/form-data" name="form1"> <?php foreach($slike as $c){ ?> <input size="25" name="<?php echo $c?>" type="file" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10pt" class="box"/><br /> <?php } ?> Dozvoljen je unos slika veličine do 4 mb! <br /> <input type="submit" id="mybut" value="Unesi slike" name="Submit"/> </form> <?php // 5. Close connection mysql_close($connection); ?> </div> </div><!-- end of right content--> </div> <!--end of center content --> <div class="clear"></div> </div> <!--end of main content--> <div class="footer"><br /> <?php include ("include/footer.php"); ?> </div> </div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/240834-image-upload-size/ Share on other sites More sharing options...
xyph Posted June 30, 2011 Share Posted June 30, 2011 http://www.sitepoint.com/image-resizing-php/ might help Link to comment https://forums.phpfreaks.com/topic/240834-image-upload-size/#findComment-1236997 Share on other sites More sharing options...
michaellunsford Posted June 30, 2011 Share Posted June 30, 2011 simple math - you just need to specify the width instead of the height. Swap your newwidth and newheight variables. Carefully adjusting the calculation to use the other variable. <?php $newheight=700; $newwidth=($height/$width)*$newheight; $tmp=imagecreatetruecolor($newwidth,$newheight); ?> edit - make sure to switch the order -- put the "newheight" variable above the "newwidth" variable. Link to comment https://forums.phpfreaks.com/topic/240834-image-upload-size/#findComment-1237009 Share on other sites More sharing options...
regoch Posted July 1, 2011 Author Share Posted July 1, 2011 Thanks! Link to comment https://forums.phpfreaks.com/topic/240834-image-upload-size/#findComment-1237363 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.