PHP - How Do I Adjust The Size With Gd ??
Hello:
Hoping someone can help me figure this out... I am resizing a photo/thumbnails with GD (I think that's the correct term) upon uploading. Works fine but the one thing I can't figure out is how to scale the height proportionate with the width. Meaning, I have the width set to 690px, and the height set to 500px. It is making the images look squished. What I would like to do is have a set width (like 690px), but have the height scale down proportionalety. Or, can I just remove the height (although I tried that and it did not work). This is the code: Code: [Select] // Let's get the Thumbnail size $size = GetImageSize( $images_dir."/".$filename ); if($size[0] > $size[1]) //{ //$thumbnail_width = 100; //$thumbnail_height = (int)(100 * $size[1] / $size[0]); //} //else //{ //$thumbnail_width = (int)(100 * $size[0] / $size[1]); //$thumbnail_height = 100; //} { //$thumbnail_width = 690; //$thumbnail_height = (int)(500 * $size[1] / $size[0]); $old_width = $size[0]; $old_height = $size[1]; $thumbnail_width = 690; $thumbnail_height = ($old_height * $thumbnail_width / $old_width); } else { $thumbnail_width = (int)(690 * $size[0] / $size[1]); $thumbnail_height = 500; } // Build Thumbnail with GD 1.x.x, you can use the other described methods too $function_suffix = $gd_function_suffix[$filetype]; $function_to_read = "ImageCreateFrom".$function_suffix; $function_to_write = "Image".$function_suffix; // Read the source file $source_handle = $function_to_read ( $images_dir."/".$filename ); if($source_handle) { // Let's create an blank image for the thumbnail //$destination_handle = ImageCreate ( $thumbnail_width, $thumbnail_height ); $destination_handle = imagecreatetruecolor( $thumbnail_width, $thumbnail_height ); // Now we resize it ImageCopyResized( $destination_handle, $source_handle, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1] ); } // Let's save the thumbnail $function_to_write( $destination_handle, $images_dir."/tb_".$filename ); ImageDestroy($destination_handle ); // $result_final .= "<img src='".$images_dir. "/tb_".$filename."' style='margin-right: 20px; width: 100px;' />"; Anyone have any ideas on this? Thanks much. Similar TutorialsI need to adjust mod rewrites from an admin panel. Any ideas on how to do this? could someone help in how to resize a uploaded image size, say if someone is uploading a 1mb photo to my server, i wish for it to become 100kb and also resize its width and height? here is my code Code: [Select] //////////////////////////////////////uploader else if($action=="uploader") { echo "Upload your picture and copy the link <br/>after uploading to user it at gallery.<br/><br/>"; echo "<form method=\"post\" enctype=\"multipart/form-data\" action=\"index.php?action=uploaded&sid=$sid\">"; echo "Choose Pictu <br />"; echo "<input name=\"uploaded\" type=\"file\" /><br /><br />"; echo "<input type=\"submit\" value=\"Upload\" />"; echo "</form><br/>"; echo "<p align=\"center\">"; echo "<a href=\"index.php?action=main&sid=$sid\">Home</a>"; echo "</p>"; } //////////////////////////////////////uploader else if($action=="uploaded") { $blacklist = array(".php", ".php.jpg", ".php.jpeg", ".php.gif", ".php.png", ".phtml", ".php3", ".php4"); foreach ($blacklist as $item) { if(preg_match("/$item\$/i", $_FILES['uploaded']['name'])) { echo "<p align=\"center\">"; echo "Oops sorry we do not allow those files.<br/>"; echo "<a href=\"index.php?action=main&sid=$sid\">Home</a>"; echo "</p>"; exit; } } $target = "../images/"; $target = $target . basename( $_FILES['uploaded']['name']) ; $ok=1; if (file_exists("../images/" . $_FILES["uploaded"]["name"])) { echo "<p align=\"center\">"; echo $_FILES["file"]["name"] . "Oops file name already exists<br/> kindly rename your picture and upload again. <br/>"; echo "<a href=\"index.php?action=main&sid=$sid\">Home</a>"; echo "</p>"; }else{ //This is our size condition if ($uploaded_size > 25600){ echo "Your file is too large. We have a 25kb limit.<br/>"; $ok=0; } $types = array('image/jpeg', 'image/gif', 'image/png'); if (in_array($_FILES['uploaded']['type'], $types)) { // file is okay continue } else { $ok=0; } //Here we check that $ok was not set to 0 by an error if ($ok==0){ echo "<p align=\"center\">"; Echo "Sorry your file was not uploaded.<br/> It may be the wrong filetype. <br/>We only allow JPG, GIF, and PNG filetypes.<br/>"; echo "<a href=\"index.php?action=main&sid=$sid\">Home</a>"; echo "</p>"; } //If everything is ok we try to upload it else{ if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)){ echo "<p align=\"center\">"; echo "The file ". basename( $_FILES['uploadedfile']['name']). " Picture uploaded successfully.<br/><br/><b>$target <br/>"; echo "<a href=\"index.php?action=main&sid=$sid\">Home</a>"; echo "</p>"; } else{ echo "<p align=\"center\">"; echo "Sorry, there was a problem uploading your file.<br/>"; echo "<a href=\"index.php?action=main&sid=$sid\">Home</a>"; echo "</p>"; } } } } Hello can anyone help with my code, it bombs out if I dont add in a file for attachment but i want to be able to add to the database with just comments (sometimes without a file) Any help appreciated!!! $timestamp = time(); $tbl_name="guestbook"; // Table name $email = $_REQUEST["email"]; $name = $_REQUEST["name"]; $comment = $_REQUEST["comment"]; $datetime = date("dmy"); $uploaddir = "upload/"; $filename = $timestamp.$file['file']['name']; $filename = strtolower($filename); $final_location = "$uploaddir$filename"; $pathinfo = pathinfo($_FILES['userfile1']['name']); if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 2000000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $filename . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists($final_location)) { echo $filename . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], $final_location); echo "Stored in: " . $final_location . "<br>"; mysql_connect("$host", "$username", "$password")or die("cannot connect server "); mysql_select_db("$db_name")or die("cannot select DB"); $sql="INSERT INTO $tbl_name(name, email, comment, datetime, upload)VALUES('$name', '$email', '$comment', '$datetime', '$final_location')"; $result=mysql_query($sql); } } //} else { echo "Invalid file"; } if($result){ echo "Successful added update to the Board"; echo "<BR>"; echo "<a href='HomePage4.php'>View Bulletin Board</a>"; } else { echo "ERROR"; } mysql_close(); ?> hello dear php-experts i run opensuse 13-2 and i am very very happy the time does not get displayed correct. it falls back to UTC all day. What can i do. i have had look at the documents here https://en.opensuse....uring_the_clock https://tr.opensuse....T_Date_and_Time how to set up correct to the local time - note i live in GERMANY near francfurt Code: [Select] <?php if(date('w') == 0) $str = 'today'; else $str = 'last sunday'; $timestamp = strtotime($str); //echo $timestamp; echo "<img src='brn_masthead.gif' width='500' /><br /><table border='1'><tr>"; echo "<tr><td align='center' width='140'><b>Sunday</b></td><td align='center' width='140'><b>Monday</b></td><td align='center' width='140'><b>Tuesday</b></td><td align='center' width='140'><b>Wednesday</b></td> <td align='center' width='140'><b>Thursday</b></td><td align='center' width='140'><b>Friday</b></td><td align='center' width='140'><b>Saturday</b></td></tr><tr>"; $j = 0; for($i = 0; $i < 14; $i++) { $thisdate = date('Y-m-d', $timestamp + ($i * (60 * 60 * 24))); ?> I've looked into mktime and checked the manual, but can't find anything that seems to work inside the loop. Thanks! Hey guys I have a small issue, i have an upload that resizes the image into thumbnail by max width and ratios the width based on that. Here is my code
What I am wanting to do is instead upload the image with a max height and ratio the width proportionally. What variables do I have to reverse? Hi there, In a string for a shopping cart I am storing both the item id and the item size selected in a number like this 12s1 where 12 is the id and 1 is the size. How can I separate these from one another again? I'm guessing it's to do with expand() ? Thanks, Jack Hello again How to convert size from taken from sql as the numbers and converted to MB in php? I am able to get directory size of folder inside folders owned by me but I need to get folder size of a root owned folder. Is it possible to do this? Thanks in advance! i need help gettig the size of a directory Is there a way I can get the .jpg file in kilobytes from a remote url and check it to make sure it does not exceed 40 kilobytes before it saves the remote url into my database? Font size GD Library Hi All I'm trying to create a font tester - http://www.ttmt.org.uk/fonts/index.php (this demo doesn't use the font I'm using.). I'm using the GD library to create images of the text. I'm not happy with the quality of the fonts at small sizes. I've tried everything to fix it but nothing is working. One suggestion I had was to create the fonts larger then reduce the size of the image. Can anyone offer any advise how I might do this? Code: [Select] <?php header('Content-Type: image/png'); $im = imagecreatetruecolor(1000, 200); $gray = imagecolorallocate($im, 240, 240, 240); $black = imagecolorallocate($im, 0, 0, 0); imagefilledrectangle($im, 0, 0, 1000, 199, $gray); $text = $_GET['text']; $textSize = $_GET['size']; $font = $_GET['font']; imagefttext($im, $textSize, 0, 15, 160, $black, $font, $text); imagepng($im); imagedestroy($im); ?> Code: [Select] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>untitled</title> <style type="text/css"> *{margin:0;padding:0;} #wrap{margin:20px 0 0 20px;} h1{margin:0px 0 20px 0;clear:both;} h2{font-size:1.1em;margin:15px 0 5px 0;} #top{padding:0 0 20px 0;} form{margin:0 0 20px 0;} </style> </head> <body> <div id="wrap"> <form action="index.php" method="post"> <!--<input type="text" name="text" value="<?php echo $_POST['text'];?>" />--> <select name="text" > <option value="<?php echo $_POST['text'];?>">Text</option> <option value="ABCDEFGHIJKLMNOPQRSTUVWXYZ">ABCDEFGHIJKLMNOPQRSTUVWXYZ</option> <option value="abcdefghijklmnopqrstuvwxyz">abcdefghijklmnopqrstuvwxyz</option> <option value="0123456789">0123456789</option> </select> <select name="size"> <option value="<?php echo $_POST['size'];?>">Size</option> <option value="10">10</option> <option value="12">12</option> <option value="14">14</option> <option value="18">18</option> <option value="24">24</option> <option value="30">30</option> <option value="38">38</option> <option value="42">42</option> <option value="50">50</option> <option value="60">60</option> <option value="80">80</option> <option value="100">100</option> </select> <input type="submit" name="submit" value="Set →" /> </form> <div id="top"> <?php $theFont="corbelb.ttf"; if(!empty($_POST['submit'])){ $myText = $_POST['text']; $mySize = $_POST['size']; echo '<img src="imageftt.php?text='.$myText.'&size='.$mySize.'&font='.$theFont.'">'; }else{ $myText = "Handgloves"; $mySize = 24; echo '<img src="imageftt.php?text='.$myText.'&size='.$mySize.'&font='.$theFont.'">'; } ?> </div> </div> </body> </html> This topic has been moved to CSS Help. http://www.phpfreaks.com/forums/index.php?topic=347531.0 I use a simple php code (taken from php.net example) to resize an image Code: [Select] list($width, $height) = getimagesize($filename); $new_height = $height / $width * 400; $image_p = imagecreatetruecolor(400, $new_height); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, 400, $new_height, $width, $height); imagejpeg($image_p, "new.jpg"); Here, we create a blank image and merge it with the resized image. I wonder if it is the simplest way to resize and save an image. Is it really necessary to create a blank background image? hi, i want to resize the font-size in my website ussing php butt it doenst work why ?? can someone help me to get it working?? Code: [Select] <html> <head> <title>Een website naam.</title> </head> <!-- this doent work, this was a test to see ik ik works!! --> <style type='text/css'> html{ font-size: 300%; } </style> <!-- this is my php code for working with resize!! This must got to work !!! --> <!-- Start varible font-size --> <?php //start session Session_start(); $_SESSION['FontSize']='300'; // check default fontsize if($_SESSION['FontSize']=='') { ?> <style type='text/css'> body { font-size: 100%; } </style> <?php } else { echo "<style type='text/css'>body { font-size: ".$_SESSION['FontSize']."%; } </style>"; } ?> <!-- end varible font-size --> how can tell my wath i do wrong??? thnx, $file="../myclients/clients/".$company_name."/Download/".$file_name; $size = filesize($path); // not working i try with function also function formatbytes($file, $type) { switch($type){ case "KB": $filesize = filesize($file) * .0009765625; // bytes to KB break; case "MB": $filesize = (filesize($file) * .0009765625) * .0009765625; // bytes to MB break; case "GB": $filesize = ((filesize($file) * .0009765625) * .0009765625) * .0009765625; // bytes to GB break; } if($filesize <= 0){ return $filesize = 'unknown file size';} else{return round($filesize, 2).' '.$type;} } echo formatbytes($file, "KB"); uploaded file size is 488 kb but it showing "unknown file size" please help me. Hi Im building a file hosting script and need to do two things. I have two sets of users FREE and PAID. Both users can upload files to thier own folder, but free users can upload only 2gb of files and paid users can upload 10gb. Is there anyone who could give some pointers on how i can: Allow different folder quotas/sizes for both users Stop both users exceeding their quota. inform them when thier quota is full. Hope someone can give me help on this thanks Lee i want to display the size of a file but i round it off to the nearest kb. I am using... ($_FILES["file"]["size"]/1024/1024)."mb" i am geting it return something like this.... 1.0313243865967mb and i want it to be more like 1.03mb any idears??? Hey there, need some help badly. I'm trying to get the size of the BLOB data in mysql. Did a search and there are like no tutorial on this. Only relevant info was from this page, at the far bottom, it just wrote this (below code). But the writer did not explain how to use it. SELECT OCTET_LENGTH(content) FROM BloBTest WHERE filename='myimage.png' I just give it a trial an error, wrote this, but did not work. $LENGTH = mysql_query("SELECT OCTET_LENGTH(blobdata) FROM thedatabase WHERE id = 'selected_id'"); echo $LENGTH; Please help.... Hi, I'm using the script below to that allows a user to upload a photo, which then appears on the page as a thumbnail, the image can be clicked and opens up in a lightbox image pop up. All this works fine except the images are not resizing. I.e if a a picture is 1024px wide, before upload - it stays that size on the server. I need it to compress down to around 600px wide and have the KB of the file reduced. I know that I should be using GD library to do this but not sure how to implement it. The script I am using is he ############################################################################################## function upload($user_id,$fileid) ############################################################################################## { global $db; $maxsize = $_POST['MAX_FILE_SIZE']; if(is_uploaded_file($_FILES['userfile']['tmp_name'][$fileid])) { if($_FILES['userfile']['size'][$fileid] < $maxsize) { $imgData = addslashes(file_get_contents($_FILES['userfile']['tmp_name'][$fileid])); $size = getimagesize($_FILES['userfile']['tmp_name'][$fileid]); $sql_query = "SELECT * FROM user_images WHERE image_id = '999999999999999999'"; if(mysqli_num_rows(mysqli_query($db, $sql_query)) > 0) { $sql_query = "UPDATE user_images SET image_type = '{$size['mime']}', image = '{$imgData}', image_size = '{$size[3]}', image_name = '{$_FILES['userfile']['name'][$fileid]}' WHERE image_id = '$user_id'"; } else { $sql_query = "INSERT INTO user_images ( image_type ,image, image_size, image_name) VALUES ('{$size['mime']}', '{$imgData}', '{$size[3]}', '{$_FILES['userfile']['name'][$fileid]}')"; } if(!mysqli_query($db,$sql_query)) { echo '<div class="error">Unable to upload file</div>'; } else { $return = mysqli_insert_id($db); } } else { echo '<div>File exceeds the Maximum File limit. Skipping uploading of image.</div> <div>Maximum File limit is '.$maxsize.'</div> <div>File '.$_FILES['userfile']['name'][$fileid].' is '.$_FILES['userfile']['size'][$fileid].' bytes</div> <hr />'; } } return $return; }//END - upload() ############################################################################################## function imageResize($width, $height, $target) ############################################################################################## { //protect function if image sizes not set (i.e. image doesn't exist in database) if((!isset($width)) || (!isset($height))) { $width = '1'; $height = '1'; } //if trying to resize an image that is much smaller than target...essentially do nothing and keep the original sizing elseif(($width < $target) && ($height < $target)) { } else { //takes the larger size of the width and height and applies the formula accordingly...this is so this script will work dynamically with any size image if ($width > $height) { $percentage = ($target / $width); } else { $percentage = ($target / $height); } //gets the new value and applies the percentage, then rounds the value $width = round($width * $percentage); $height = round($height * $percentage); } //returns the new sizes in html image tag format... return "width=\"$width\" height=\"$height\""; }//END - imageResize ############################################################################################## |