PHP - Upload Help. Creating Thumbnail Puts Black Background On Every Picture...
Hi All,
Everything is working pretty good, its just the thumbnail portion that adds a black background to every picture it converts. When the user uploads a picture, I take the original picture and then make a thumbnail, so I have 2 images in my fileroot. The original picture looks just fine, its just when I open the thumbnail. Attached is a sample of the black background picture. Below is just the thumbnail portion of the code. Code: [Select] include('uploads/image.php'); $image = $path; $thumb = new SimpleImage(); $thumb->load($image); $width = 150; $height = 95; $thumb->resize($width,$height); $thumb->save('uploads/thumbnails/'.$filename); image.php Code: [Select] class SimpleImage { var $image; var $image_type; function load($filename) { $image_info = getimagesize($filename); $this->image_type = $image_info[2]; if( $this->image_type == IMAGETYPE_JPEG ) { $this->image = imagecreatefromjpeg($filename); } elseif( $this->image_type == IMAGETYPE_GIF ) { $this->image = imagecreatefromgif($filename); } elseif( $this->image_type == IMAGETYPE_PNG ) { $this->image = imagecreatefrompng($filename); } } function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) { if( $image_type == IMAGETYPE_JPEG ) { imagejpeg($this->image,$filename,$compression); } elseif( $image_type == IMAGETYPE_GIF ) { imagegif($this->image,$filename); } elseif( $image_type == IMAGETYPE_PNG ) { imagepng($this->image,$filename); } if( $permissions != null) { chmod($filename,$permissions); } } function output($image_type=IMAGETYPE_JPEG) { if( $image_type == IMAGETYPE_JPEG ) { imagejpeg($this->image); } elseif( $image_type == IMAGETYPE_GIF ) { imagegif($this->image); } elseif( $image_type == IMAGETYPE_PNG ) { imagepng($this->image); } } function getWidth() { return imagesx($this->image); } function getHeight() { return imagesy($this->image); } function resizeToHeight($height) { $ratio = $height / $this->getHeight(); $width = $this->getWidth() * $ratio; $this->resize($width,$height); } function resizeToWidth($width) { $ratio = $width / $this->getWidth(); $height = $this->getheight() * $ratio; $this->resize($width,$height); } function scale($scale) { $width = $this->getWidth() * $scale/100; $height = $this->getheight() * $scale/100; $this->resize($width,$height); } function resize($width,$height) { $new_image = imagecreatetruecolor($width, $height); imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); $this->image = $new_image; } } Similar Tutorialshi, i am trying to generate image gallery. but the problem is that when i upload the png files its thumbnail is generated with black background. JPEG an GIF thumbnail are generated successfully. i want to make background transparent. is there any solution that how i can get rid of it. i am using imagecreatetruecolor($thumbwidth, $thumbheight ); to generate thumbnail So, I'm learning how to upload pictures into a system from my awesome PHP book. I've looked and looked through the script but I can't figure out whats wrong with it. Goal: The script is meant to save a full version of the image in the images folder and a thumbnail in the thumbnail folder. Bug: The full image does not appear in any folder, and the thumbnail is created but its put in the images folder. I've checked the GD library, and everything is supported. image_effect.php <?php //change this path to match your images directory $dir ='C:/x/xampp/htdocs/images'; //change this path to match your fonts directory and the desired font putenv('GDFONTPATH=' . 'C:/Windows/Fonts'); $font = 'arial'; // make sure the requested image is valid if (isset($_GET['id']) && ctype_digit($_GET['id']) && file_exists($dir . '/' . $_GET['id'] . '.jpg')) { $image = imagecreatefromjpeg($dir . '/' . $_GET['id'] . '.jpg'); } else { die('invalid image specified'); } // apply the filter $effect = (isset($_GET['e'])) ? $_GET['e'] : -1; switch ($effect) { case IMG_FILTER_NEGATE: imagefilter($image, IMG_FILTER_NEGATE); break; case IMG_FILTER_GRAYSCALE: imagefilter($image, IMG_FILTER_GRAYSCALE); break; case IMG_FILTER_EMBOSS: imagefilter($image, IMG_FILTER_EMBOSS); break; case IMG_FILTER_GAUSSIAN_BLUR: imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR); break; } // add the caption if requested if (isset($_GET['capt'])) { imagettftext($image, 12, 0, 20, 20, 0, $font, $_GET['capt']); } //add the logo watermark if requested if (isset($_GET['logo'])) { // determine x and y position to center watermark list($width, $height) = getimagesize($dir . '/' . $_GET['id'] . '.jpg'); list($wmk_width, $wmk_height) = getimagesize('images/logo.png'); $x = ($width - $wmk_width) / 2; $y = ($height - $wmk_height) / 2; $wmk = imagecreatefrompng('images/logo.png'); imagecopymerge($image, $wmk, $x, $y, 0, 0, $wmk_width, $wmk_height, 20); imagedestroy($wmk); } // show the image header('Content-Type: image/jpeg'); imagejpeg($image, '', 100); ?> check_image.php <?php include 'db.inc.php'; //connect to MySQL $db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or die ('Unable to connect. Check your connection parameters.'); mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db)); //change this path to match your images directory $dir ='C:/x/xampp/htdocs/images'; //change this path to match your thumbnail directory $thumbdir = $dir . '/thumbs'; //change this path to match your fonts directory and the desired font putenv('GDFONTPATH=' . 'C:/Windows/Fonts'); $font = 'arial'; // handle the uploaded image if ($_POST['submit'] == 'Upload') { //make sure the uploaded file transfer was successful if ($_FILES['uploadfile']['error'] != UPLOAD_ERR_OK) { switch ($_FILES['uploadfile']['error']) { case UPLOAD_ERR_INI_SIZE: die('The uploaded file exceeds the upload_max_filesize directive ' . 'in php.ini.'); break; case UPLOAD_ERR_FORM_SIZE: die('The uploaded file exceeds the MAX_FILE_SIZE directive that ' . 'was specified in the HTML form.'); break; case UPLOAD_ERR_PARTIAL: die('The uploaded file was only partially uploaded.'); break; case UPLOAD_ERR_NO_FILE: die('No file was uploaded.'); break; case UPLOAD_ERR_NO_TMP_DIR: die('The server is missing a temporary folder.'); break; case UPLOAD_ERR_CANT_WRITE: die('The server failed to write the uploaded file to disk.'); break; case UPLOAD_ERR_EXTENSION: die('File upload stopped by extension.'); break; } } //get info about the image being uploaded $image_caption = $_POST['caption']; $image_username = $_POST['username']; $image_date = @date('Y-m-d'); list($width, $height, $type, $attr) = getimagesize($_FILES['uploadfile']['tmp_name']); // make sure the uploaded file is really a supported image $error = 'The file you uploaded was not a supported filetype.'; switch ($type) { case IMAGETYPE_GIF: $image = imagecreatefromgif($_FILES['uploadfile']['tmp_name']) or die($error); break; case IMAGETYPE_JPEG: $image = imagecreatefromjpeg($_FILES['uploadfile']['tmp_name']) or die($error); break; case IMAGETYPE_PNG: $image = imagecreatefrompng($_FILES['uploadfile']['tmp_name']) or die($error); break; default: die($error); } //insert information into image table $query = 'INSERT INTO images (image_caption, image_username, image_date) VALUES ("' . $image_caption . '", "' . $image_username . '", "' . $image_date . '")'; $result = mysql_query($query, $db) or die (mysql_error($db)); //retrieve the image_id that MySQL generated automatically when we inserted //the new record $last_id = mysql_insert_id(); // save the image to its final destination $image_id = $last_id; imagejpeg($image, $dir . '/' . $image_id . '.jpg'); imagedestroy($image); } else { // retrieve image information $query = 'SELECT image_id, image_caption, image_username, image_date FROM images WHERE image_id = ' . $_POST['id']; $result = mysql_query($query, $db) or die (mysql_error($db)); extract(mysql_fetch_assoc($result)); list($width, $height, $type, $attr) = getimagesize($dir . '/' . $image_id . '.jpg'); } if ($_POST['submit'] == 'Save') { // make sure the requested image is valid if (isset($_POST['id']) && ctype_digit($_POST['id']) && file_exists($dir . '/' . $_POST['id'] . '.jpg')) { $image = imagecreatefromjpeg($dir . '/' . $_POST['id'] . '.jpg'); } else { die('invalid image specified'); } // apply the filter $effect = (isset($_POST['effect'])) ? $_POST['effect'] : -1; switch ($effect) { case IMG_FILTER_NEGATE: imagefilter($image, IMG_FILTER_NEGATE); break; case IMG_FILTER_GRAYSCALE: imagefilter($image, IMG_FILTER_GRAYSCALE); break; case IMG_FILTER_EMBOSS: imagefilter($image, IMG_FILTER_EMBOSS); break; case IMG_FILTER_GAUSSIAN_BLUR: imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR); break; } // add the caption if requested if (isset($_POST['emb_caption'])) { imagettftext($image, 12, 0, 20, 20, 0, $font, $image_caption); } //add the logo watermark if requested if (isset($_POST['emb_logo'])) { // determine x and y position to center watermark list($wmk_width, $wmk_height) = getimagesize('images/logo.png'); $x = ($width - $wmk_width) / 2; $y = ($height - $wmk_height) / 2; $wmk = imagecreatefrompng('images/logo.png'); imagecopymerge($image, $wmk, $x, $y, 0, 0, $wmk_width, $wmk_height, 20); imagedestroy($wmk); } // save the image with the filter applied imagejpeg($image, $dir . '/' . $_POST['id'] . '.jpg', 100); //set the dimensions for the thumbnail $thumb_width = $width * 0.10; $thumb_height = $height * 0.10; //create the thumbnail $thumb = imagecreatetruecolor($thumb_width, $thumb_height); imagecopyresampled($thumb, $image, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height); imagejpeg($thumb, $dir . '/' . $_POST['id'] . '.jpg', 100); imagedestroy($thumb); ?> <html> <head> <title>Here is your pic!</title> </head> <body> <h1>Your image has been saved!</h1> <img src="images/<?php echo $_POST['id']; ?>.jpg" /> </body> </html> <?php } else { ?> <html> <head> <title>Here is your pic!</title> </head> <body> <h1>So how does it feel to be famous?</h1> <p>Here is the picture you just uploaded to our servers:</p> <?php if ($_POST['submit'] == 'Upload') { $imagename = 'images/' . $image_id . '.jpg'; } else { $imagename = 'image_effect.php?id=' . $image_id . '&e=' . $_POST['effect']; if (isset($_POST['emb_caption'])) { $imagename .= '&capt=' . urlencode($image_caption); } if (isset($_POST['emb_logo'])) { $imagename .= '&logo=1'; } } ?> <img src="<?php echo $imagename; ?>" style="float:left;"> <table> <tr><td>Image Saved as: </td><td><?php echo $image_id . '.jpg'; ?></td></tr> <tr><td>Height: </td><td><?php echo $height; ?></td></tr> <tr><td>Width: </td><td><?php echo $width; ?></td></tr> <tr><td>Upload Date: </td><td><?php echo $image_date; ?></td></tr> </table> <p>You may apply special options to your image below. Note: saving an image with any of the options applied <em>cannot be undone</em>.</p> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <div> <input type="hidden" name="id" value="<?php echo $image_id;?>"/> Filter: <select name="effect"> <option value="-1">None</option> <?php echo '<option value="' . IMG_FILTER_GRAYSCALE . '"'; if (isset($_POST['effect']) && $_POST['effect'] == IMG_FILTER_GRAYSCALE) { echo ' selected="selected"'; } echo '>Black and White</option>'; echo '<option value="' . IMG_FILTER_GAUSSIAN_BLUR . '"'; if (isset($_POST['effect']) && $_POST['effect'] == IMG_FILTER_GAUSSIAN_BLUR) { echo ' selected="selected"'; } echo '>Blur</option>'; echo '<option value="' . IMG_FILTER_EMBOSS . '"'; if (isset($_POST['effect']) && $_POST['effect'] == IMG_FILTER_EMBOSS) { echo ' selected="selected"'; } echo '>Emboss</option>'; echo '<option value="' . IMG_FILTER_NEGATE . '"'; if (isset($_POST['effect']) && $_POST['effect'] == IMG_FILTER_NEGATE) { echo ' selected="selected"'; } echo '>Negative</option>'; ?> </select> <br/><br/> <?php echo '<input type="checkbox" name="emb_caption"'; if (isset($_POST['emb_caption'])) { echo ' checked="checked"'; } echo '>Embed caption in image?'; echo '<br/><br/><input type="checkbox" name="emb_logo"'; if (isset($_POST['emb_logo'])) { echo ' checked="checked"'; } echo '>Embed watermarked logo in image?'; ?> <br/><br/> <input type="submit" value="Preview" name="submit" /> <input type="submit" value="Save" name="submit" /> </div> </form> </body> </html> <?php } ?> Any help appreciated. Hi! I'm looking for a script that can do the following: upload one or preferably fx. five image files(jpg) resizes them to a specific size if they exceeds that size. create a thumbnail No files above 100kb can be uploaded. uploaded and/or processed files must be renamed with a unique file name. Work with php5 I have searched the internet for a script like that, but I can't find one that matches my needs. Also, the code must include good comments and nott be to hard to understand. So as basic code as possible. Hi all, I have been looking at thumbnail scripts and they are all very advanced for what I want. I just want to take 3 uploadeed jpg images, save a copy in original format and save a copy in images/thumbs/ with a size of 200px x 133px. Could someone just show me who to do this? Thanks The script I have for uploading the actual image is: <?php $destination='aircraft/'.$reg."1.jpg"; $temp_file = $_FILES['image']['tmp_name']; move_uploaded_file($temp_file,$destination); $destination2='aircraft/'.$reg."2.jpg"; $temp_file2 = $_FILES['image2']['tmp_name']; move_uploaded_file($temp_file2,$destination2); $destination3='aircraft/'.$reg."3.jpg"; $temp_file3 = $_FILES['image3']['tmp_name']; move_uploaded_file($temp_file3,$destination3); ?> Still working on the same project. I had a function to upload a photo and you could use it as a profile picture. Now I wanted to expand on it and decided you can upload more than one photo, creating a gallery. I figured I could use the same script to create two images (one original size and one thumbnail for browsing) and went about it in two different ways, both failed miserably. First I figured my upload code could take the indata and make TWO photos at once with different name. I guess it can only keep on image in mind at once - or I did something else wrong. I used the same code to create $user.jpg and $user_tn.jpg and move them to the user's image folder. So try number two was to make two folders for the photo, one named after the user (for thumb) and another in a folder named big. That isn't working for me either. Anyone who likes to chime in with ideas or facepalms is very welcome. Here is the generic code I set out with: Code: [Select] if ($view == $user) { mkdir("grafik/users/$user"); mkdir("grafik/users/$user/big/"); if (isset($_FILES['image']['name'])) { $saveto = "grafik/users/$user/big/$user.jpg"; move_uploaded_file($_FILES['image']['tmp_name'], $saveto); $typeok = TRUE; switch($_FILES['image']['type']) { case "image/gif": $src = imagecreatefromgif($saveto); break; case "image/jpeg": case "image/pjpeg": $src = imagecreatefromjpeg($saveto); break; case "image/png": $src = imagecreatefrompng($saveto); break; default: $typeok = FALSE; break; } if ($typeok) { list($w, $h) = getimagesize($saveto); $max = 200; $tw = $w; $th = $h; if ($w > $h && $max < $w) { $th = $max / $w * $h; $tw = $max; } elseif ($h > $w && $max < $h) { $tw = $max / $h *$w; $th = $max;} elseif ($max < $w) { $tw = $th = $max;} $tmp = imagecreatetruecolor($tw,$th); imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h); imageconvolution($tmp, array( array(-1, -1, -1), array(-1, 16, -1), array(-1, -1, -1) ), 8, 0); imagejpeg($tmp, $saveto); imagedestroy($tmp); imagedestroy($src); } } echo <<<_END <div id='main'> <form method='post' action='profile.php' enctype='multipart/form-data'> Image: <input type='file' name='image' size='14' maxlength='32' /> <input type='submit' value='Save Profile' /> </form> </div> _END; } It is the page in minimalist form and it still contains the big folder version. Thanks for putting up with my nooby questions. Im trying to write an image upload script which checks various elements of the image and then saves it and and a thumbnail into a folder. At the moment I am getting a white screen which makes me think I have a syntax error so I have come here to find some fresh eyes to help me out. Can anyone see any reason why this wouldn't be working? Form Code: [Select] <form enctype='multipart/form-data' action='upload_image.php' method='POST'> <table class='uploads-form'> <tr> <td><input name='image' type='file' /><input type='hidden' name'MAX_FILE_SIZE' value='5000000' /></td> </tr> <tr> <td><input type='submit' name='upload_image' value='Upload' /></td> </tr> </table> </form> php <?php error_reporting(E_ALL); ini_set("display_errors", 1); $images_location = "project_files/"; $thumbs_location = "project_thumbs/"; $thumb_width = "100"; $maximum_size = "5000000"; //Auto Thumbnail Function function create_thumbnail($source,$destination, $thumb_width) { $size = getimagesize($source); $width = $size[0]; $height = $size[1]; $x = 0; $y = 0; if($width> $height) { $x = ceil(($width - $height) / 2 ); $width = $height; } elseif($height> $width) { $y = ceil(($height - $width) / 2); $height = $width; } $new_image = imagecreatetruecolor($thumb_width,$thumb_width) or die('Cannot initialize new GD image stream'); $extension = get_image_extension($source); if($extension=='jpg' || $extension=='jpeg') $image = imagecreatefromjpeg($source); if($extension=='gif') $image = imagecreatefromjpeg($source); if($extension=='png') $image = imagecreatefromjpeg($source); imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height); if($extension=='jpg' || $extension=='jpeg') imagejpeg($new_image,$destination); if($extension=='gif') $imagegif($new_image,$destination); if($extension=='png') imagepng($new_image,$destination); } //Get Extension Function function get_image_extension($name) { $name = strtolower($name); $i = strrpos($name,"."); if (!$i) { return ""; } $l = strlen($name) - $i; $extension = substr($name,$i+1,$l); return $extension; } //Random Name Function function random_name($length) { $characters = "abcdefghijklmnopqrstuvwxyz01234567890"; $name = ""; for ($i = 0; $i < $length; $i++) { $name .= $character[mt_rand(0, strlen($characters) - 1)]; } return "image-".$name; } //Check And Save Image if(isset($_POST['upload_image'])) { if($_FILES['image']['name'] == ""){ echo = "Please select the image you would like to upload by clicking browse"; } else{ $size=filesize($_FILES['image']['tap_name']); $filename = stripslashes($_FILES['image']['name']); $extension = get_image_extension($filename); if($size > $maximum_size) { echo = "Your file size exceeds the maximum file size!"; } else if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { echo = "Only the following extensions are allowed. 'jpg', 'jpeg', 'png', 'gif'."; } else { $image_random_name=random_name(15).".".$extension; $copy = @copy($_FILES['image']['tmp_name'], $images_location.$image_random_name); if (!$copy) { echo = "Error while uploadin your image! Please try again!"; } else{ create_thumbnail($images_location.$image_random_name,$thumbs_location.$image_random_name, $thumb_width); echo = "Your image has been uploaded!"; } } } } ?> Hey all. I'm getting down the basics of the GD Library's image creation processes. I'm trying to test out creating png images. However, where there should be transparency there is a black background. Here's my code mostly based on the image tutorial found here at phpfreaks. Code: [Select] <?php // font $font = 'c:\windows\fonts\arial.ttf'; $fontsize = 12; // array of quotes $quotes = array( "I like pie.", "Surf's Up.", "Smoke em if you got em.", "Game on.", "I need TP for my bunghole."); // select quote $pos = rand(0,count($quotes)-1); $quote = $quotes[$pos]; // image $image = imagecreatefrompng('quote.png'); $white = imagecolorallocate($image, 255, 255, 255); $black = imagecolorallocate($image, 0, 0, 0); imagettftext($image, 11, 0, 107, 32, $black, $font, $quote); imagettftext($image, 11, 0, 105, 30, $white, $font, $quote); // tell the browser that the content is an image header('Content-type: image/png'); // output image to the browser imagepng($image); // delete the image resource imagedestroy($image); ?> Cheers all, thanks! I am working on an uploader and slowly getting it working, I am uploading 3 images at once, and setting arrays for each one as keys, with an increment of ++1. I am wanting to resize the image before it gets copied to the thumbnail folder. I have this code.
Everything works with it. As you see, I started on getting the file info, but after that I am totally stuck on what to do after to resize the image proportionally with a maximum width of xPX and height to match it without looking distorted. Any help would be really appreciated. Thank You. I am trying to copy an image into another image. Code: [Select] $im = imagecreatefrompng("/oldimg.png"); //this is a semi-transparent image where I want the new image to be copied imagealphablending($im, 1); //this seems to remove the black background header('Content-type: image/png'); //outputs old image with a transparent background (YAY!) imagepng($im); imagecopy($im, $newimage, 32, 0, 0, 0, 32, 32); //I want to replace a part of the old image with a new semi-transparent image header('Content-type: image/png'); //outputs image with a black background (noooooooooo!) imagepng($im); Can anyone help me? Goal: To have a gallery that downloads images from the folder I previously uploaded to in a previous script. Bug: When I load the page the thumbnail comes up as broken and when I click on the thumbnail to get the bigger picture it comes up with the following error message: "Firefox doesn't know how to open this address, because the protocol (c) isn't associated with any program." <?php include 'db.inc.php'; //connect to MySQL $db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or die ('Unable to connect. Check your connection parameters.'); mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db)); //change this path to match your images directory $dir ='C:/x/xampp/htdocs/images'; //change this path to match your thumbnail directory $thumbdir = $dir . '/thumbs'; ?> <html> <head> <title>Welcome to our Photo Gallery</title> <style type="text/css"> th { background-color: #999;} .odd_row { background-color: #EEE; } .even_row { background-color: #FFF; } </style> </head> <body> <p>Click on any image to see it full sized.</p> <table style="width:100%;"> <tr> <th>Image</th> <th>Caption</th> <th>Uploaded By</th> <th>Date Uploaded</th> </tr> <?php //get the thumbs $result = mysql_query('SELECT * FROM images') or die(mysql_error()); $odd = true; while ($rows = mysql_fetch_array($result)) { echo ($odd == true) ? '<tr class="odd_row">' : '<tr class="even_row">'; $odd = !$odd; extract($rows); echo '<td><a href="' . $dir . '/' . $image_id . '.jpg">'; echo '<img src="' . $thumbdir . '/' . $image_id . '.jpg">'; echo '</a></td>'; echo '<td>' . $image_caption . '</td>'; echo '<td>' . $image_username . '</td>'; echo '<td>' . $image_date . '</td>'; echo '</tr>'; } ?> </table> </body> </html> Any help appreciated. Greetings, What I'm trying to do is have users upload their event information into a database which would include a flyer. I don't want the image file to go into the database (other than the filename) rather I'd like it to be dropped into a directory. In the same script I'd like to dynamically generate a thumbnail. I have the two scripts and separately they work fine, but I can't get them to work together. I'm guessing the conflict because the thumbnail script is using $_POST and the mysql script is using $_SESSION. If so how can I modify them to both use $_SESSION? The thumbnail script is goes from line 1 - 146 and the mysql portion is the rest. The results of processing this look something like this. QUERY TEXT: INSERT INTO td_events (eventgenre_sel, eventname, eventvenue, eventdate, eventgenre, eventprice, eventpromoter, eventflyer) VALUES ('12', 'spooky times', 'Ironwood Stage & Grill', '2010-12-17 22:36:00', 'DNB', '5000', 'me', '174366-1.jpg') <?php $debug = FALSE; /********************************************************************************************** CREATES THUMBNAIL **********************************************************************************************/ //define a maxim size for the uploaded images define ("MAX_SIZE","1024"); // define the width and height for the thumbnail // note that theese dimmensions are considered the maximum dimmension and are not fixed, // because we have to keep the image ratio intact or it will be deformed define ("WIDTH","500"); define ("HEIGHT","650"); // this is the function that will create the thumbnail image from the uploaded image // the resize will be done considering the width and height defined, but without deforming the image function make_thumb($img_name,$filename,$new_w,$new_h) { //get image extension. $ext=getExtension($img_name); //creates the new image using the appropriate function from gd library if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext) || !strcmp("JPG",$ext)) $src_img=imagecreatefromjpeg($img_name); if(!strcmp("png",$ext) || !strcmp("PNG",$ext)) $src_img=imagecreatefrompng($img_name); //gets the dimmensions of the image $old_x=imageSX($src_img); $old_y=imageSY($src_img); // next we will calculate the new dimmensions for the thumbnail image // the next steps will be taken: // 1. calculate the ratio by dividing the old dimmensions with the new ones // 2. if the ratio for the width is higher, the width will remain the one define in WIDTH variable // and the height will be calculated so the image ratio will not change // 3. otherwise we will use the height ratio for the image // as a result, only one of the dimmensions will be from the fixed ones $ratio1=$old_x/$new_w; $ratio2=$old_y/$new_h; if($ratio1>$ratio2) { $thumb_w=$new_w; $thumb_h=$old_y/$ratio1; } else { $thumb_h=$new_h; $thumb_w=$old_x/$ratio2; } // we create a new image with the new dimmensions $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h); // resize the big image to the new created one imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); // output the created image to the file. Now we will have the thumbnail into the file named by $filename if(!strcmp("png",$ext)) imagepng($dst_img,$filename); else imagejpeg($dst_img,$filename); //destroys source and destination images. imagedestroy($dst_img); imagedestroy($src_img); } // This function reads the extension of the file. // It is used to determine if the file is an image by checking the extension. function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; } // This variable is used as a flag. The value is initialized with 0 (meaning no error found) // and it will be changed to 1 if an error occurs. If the error occurs the file will not be uploaded. $errors=0; // checks if the form has been submitted if(isset($_POST['Submit'])) { //reads the name of the file the user submitted for uploading $image=$_FILES['eventflyer']['name']; // if it is not empty if ($image) { // get the original name of the file from the clients machine $filename = stripslashes($_FILES['eventflyer']['name']); // get the extension of the file in a lower case format $extension = getExtension($filename); $extension = strtolower($extension); // if it is not a known extension, we will suppose it is an error, print an error message // and will not upload the file, otherwise we continue if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "JPG") && ($extension != "PNG") && ($extension != "png")) { echo '<h1>Unknown extension!</h1>'; $errors=1; } else { // get the size of the image in bytes // $_FILES[\'image\'][\'tmp_name\'] is the temporary filename of the file in which // the uploaded file was stored on the server $size=getimagesize($_FILES['eventflyer']['tmp_name']); $sizekb=filesize($_FILES['eventflyer']['tmp_name']); //compare the size with the maxim size we defined and print error if bigger if ($sizekb > MAX_SIZE*500) { echo '<h1>You have exceeded the size limit!</h1>'; $errors=1; } //we will give an unique name, for example the time in unix time format $image_name=$filename; //the new name will be containing the full path where will be stored (images folder) $newname="flyers/".$image_name; $copied = copy($_FILES['eventflyer']['tmp_name'], $newname); //we verify if the image has been uploaded, and print error instead if (!$copied) { echo '<h1>Copy unsuccessfull!</h1>'; $errors=1; } else { // the new thumbnail image will be placed in images/thumbs/ folder $thumb_name='flyers/thumb_'.$image_name; // call the function that will create the thumbnail. The function will get as parameters // the image name, the thumbnail name and the width and height desired for the thumbnail $thumb=make_thumb($newname,$thumb_name,WIDTH,HEIGHT); }} }} //If no errors registred, print the success message and show the thumbnail image created if(isset($_POST['Submit']) && !$errors) { echo "<h1>Thumbnail created Successfully!</h1>"; echo '<img src="'.$thumb_name.'">'; } /************************************************************ Adjust the headers... ************************************************************/ header("Expires: Thu, 17 May 2001 10:17:17 GMT"); // Date in the past header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 header ("Pragma: no-cache"); // HTTP/1.0 /***************************************************************************** Check the session details. we will store all the post variables in session variables this will make it easier to work with the verification routines *****************************************************************************/ session_start(); if (!isset($_SESSION['SESSION'])) require_once( "../include/session_init.php"); $arVal = array(); require_once("../include/session_funcs1.php"); reset ($_POST); while (list ($key, $val) = each ($_POST)) { if ($val == "") $val = "NULL"; $arVals[$key] = (get_magic_quotes_gpc()) ? $val : addslashes($val); if ($val == "NULL") $_SESSION[$key] = NULL; else $_SESSION[$key] = $val; if ($debug) echo $key . " : " . $arVals[$key] . "<br>"; } /********************************************************************************************** Make sure session variables have been set and then check for required fields otherwise return to the registration form to fix the errors. **********************************************************************************************/ // check to see if these variables have been set... if ((!isset($_SESSION["eventname"])) || (!isset($_SESSION["eventvenue"])) || (!isset($_SESSION["eventdate"])) || (!isset($_SESSION["eventgenre"])) || (!isset($_SESSION["eventprice"])) || (!isset($_SESSION["eventpromoter"])) || (!isset($_SESSION["eventflyer"]))) { resendToForm("?flg=red"); } // form variables must have something in them... if ($_SESSION['eventname'] == "" || $_SESSION['eventvenue'] == "" || $_SESSION['eventdate'] == "" || $_SESSION['eventgenre'] == "" || $_SESSION['eventprice'] == "" || $_SESSION['eventpromoter'] == "" || $_SESSION['eventflyer'] == "") { resendToForm("?flg=red"); } /********************************************************************************************** Insert into the database... **********************************************************************************************/ $conn = mysql_connect($_SESSION['MYSQL_SERVER1'],$_SESSION['MYSQL_LOGIN1'],$_SESSION['MYSQL_PASS1']) or die ('Error connecting to mysql'); mysql_select_db($_SESSION['MYSQL_DB1']) or die("Unable to select database"); $eventgenre_sel = addslashes($_REQUEST['eventgenre_sel']); $eventname = addslashes($_REQUEST['eventname']); $eventvenue = addslashes($_REQUEST['eventvenue']); $eventdate = addslashes($_REQUEST['eventdate']); $eventgenre = addslashes($_REQUEST['eventgenre']); $eventprice = addslashes($_REQUEST['eventprice']); $eventpromoter = addslashes($_REQUEST['eventpromoter']); $eventflyer = addslashes($_REQUEST['eventflyer']); $sqlquery = "INSERT INTO td_events (eventgenre_sel, eventname, eventvenue, eventdate, eventgenre, eventprice, eventpromoter, eventflyer) " ."VALUES ('$eventgenre_sel', '$eventname', '$eventvenue', '$eventdate', '$eventgenre', '$eventprice', '$eventpromoter', '$eventflyer')"; echo 'QUERY TEXT:<br />'.$sqlquery; $result = MYSQL_QUERY($sqlquery); $insertid = mysql_insert_id(); /*** This following function will update session variables and resend to the form so the user can fix errors ***/ function resendToForm($flags) { reset ($_POST); // store variables in session... while (list ($key, $val) = each ($_POST)) { $_SESSION[$key] = $val; } // go back to the form... //echo $flags; header("Location: /user_registration.php".$flags); exit; } mysql_close($conn); ?> Hi all, creating a MYSQL, PHP & XHTML site designed to support local rugby clubs. Just putting the final touches to the functionality now so thanks for your help so far. I would like to provide site administrators with the ability to assign photos to a members profile when initially registering their account, I have no experience of dealing with what presumably will be a function that will upload a photo to a location and then linking it some how to data in the database. Thanks for your help, Tom Hi, I have a basic form that lets people enter details Code: [Select] <form action="welcome.php" method="post"> Name: <input type="text" name="fname" /> Second name: <input type="text" name="sname" /> <input type="submit" /> </form> And the standard insert in sql command. Im now looking to add a image uplad field the the same form. I have tried several methods off the internet but i cant get any to work. I would like the form to upload the image into the directory '../images' and then save the path to the file in the database, e.g. 'images/picture.jpg'. Can anyone point me in the right direction please. Im guessing i will have to use the file input type but i cant get any of it to work. Thanks Hello all, im fairly new, but i have a question that i cant answer. is it possible to use the contents of a folder as a website background as in my pictures folder would be a tile of the same size images as my website background? and if i upload some more photos it automatically updates the background? and it would get gradually smaller as the more pictures came into the folder like 2 pics would be side by side 4 would be one in each corner and 6 and 8 and so forth so basically it sizes them equally so that it fills the page. any ideas ? cheers I have the following I use for uploading images, but I am having an issue with transparent background png images, they all get changed from transparent background to a black background. After doing some hunting around I see I neet to use imagealphablending and imagesavealpha to keep the trransparency, but it doesnt seem to be working I still get the black backgrounds, any ideas? Here is the script Code: [Select] <?php // Process new logo image if ($_FILES['logo']['tmp_name']&&$_FILES['logo']['tmp_name']!="") { // set memory limit for image ini_set("memory_limit","32M"); // Check file extension $checkfile=$_FILES['logo']['name']; $ext=substr($checkfile, -3); // Redirect if not correct image type if (!in_array($ext, array('jpg', 'JPG', 'gif', 'GIF', 'png', 'PNG'))) { header("Location: ../control_index.php?sponsors=error1"); // Close database connection mysql_close($link); exit (); } // Create logo image (300px wide) $file=$_FILES['logo']['tmp_name']; list($width, $height)=getimagesize($file); // Scale to width $main_width=300; $main_height=$height*($main_width/$width); if ($ext=="jpg"||$ext=="JPG") { $image=imagecreatefromjpeg($file); $newname="sponsor".time().".jpg"; } if ($ext=="gif"||$ext=="GIF") { $image=imagecreatefromgif($file); $newname="sponsor".time().".gif"; } if ($ext=="png"||$ext=="PNG") { $image=imagecreatefrompng($file); $newname="sponsor".time().".png"; } // Create main image file $main_image=imagecreatetruecolor($main_width, $main_height); imagecopyresampled($main_image, $image, 0, 0, 0, 0, $main_width, $main_height, $width, $height); $cp_file="../images/sponsors/".$newname; $site_file="../../images/sponsors/".$newname; if ($ext=="jpg"||$ext=="JPG") { imagejpeg($main_image, $cp_file, 100); imagejpeg($main_image, $site_file, 100); } if ($ext=="gif"||$ext=="GIF") { imagegif($main_image, $cp_file, 100); imagegif($main_image, $site_file, 100); } if ($ext=="png"||$ext=="PNG") { // Turn off alpha blending and set alpha flag imagealphablending($main_image, false); imagesavealpha($main_image, true); imagepng($main_image, $cp_file, 9); imagepng($main_image, $site_file, 9); } ?> How do I do I prevent a broken image icon if there is no image? Here is my code: Code: [Select] <?php if ($_POST){ $county = $_POST['county']; } $con = mysql_connect("localhost","",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("", $con); $imageLocation = $row['imageurl1']; $result = mysql_query("SELECT * FROM places WHERE `county` = '".mysql_real_escape_string($county)."' order by `date_created` DESC"); if ( mysql_num_rows($result) > 0 ) { echo "<strong>Click Headers to Sort</strong>"; echo "<table border='0' align='center' bgcolor='#999969' cellpadding='3' bordercolor='#000000' table class='sortable' table id='results'> <tr> <th> Title </th> <th> Borough </th> <th> Town </th> <th> Phone </th> <th> Rooms </th> <th> Bath </th> <th> Fees </th> <th> Rent </th> <th> Image </th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr> <td bgcolor='#FFFFFF' style='color: #000' align='center'> <a href='classified/places/index.php?id=".$row['id']."'>" . $row['title'] . "</a></td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['county'] . "</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['town'] . "</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['phone'] . "</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['rooms'] . "</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['bath'] . "</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['feeornofee'] . "</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'>" . $row['rent'] . "</td> <td bgcolor='#FFFFFF' style='color: #000' align='center'><img src=user/". $row['imageurl1'] ." width='50'></td> </tr>"; } echo "</table>"; print_r($apts); } else { echo "<p> </p><p> </p> No Results <br /><p> </p><FORM><INPUT TYPE='button' VALUE='Go Back' onClick='history.go(-1);return true;'></FORM> and Refine Your Search <p> </p><p> </p>"; } ?> Thanks in advance hi guys i need some help i am trying to create a resized image with a watermark, from an uploaded image.. the image is getting uploaded and resized but the watermark doesnt appear correct. instead of a transparent watermark, there's a black square in it's place. this is my code Code: [Select] $tempfile = $_FILES['filename']['tmp_name']; $src = imagecreatefromjpeg($tempfile); list($origWidth, $origHeight) = getimagesize($tempfile); // creating png image of watermark $watermark = imagecreatefrompng('../imgs/watermark.png'); // getting dimensions of watermark image $watermark_width = imagesx($watermark); $watermark_height = imagesy($watermark); // placing the watermark $dest_x = $origWidth / 2 - $watermark_width / 2; $dest_y = $origHeight / 2 - $watermark_height / 2; imagecopyresampled($src, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $origWidth, $origHeight); imagealphablending($src, true); imagealphablending($watermark, true); imagejpeg($src,$galleryPhotoLocation,100); // $galleryPhotoLocation is correct. the image gets uploaded successfully.. hey im new to this web site im looking for some help with my code its suppose to be a black jack game im trying to make an array that can hold the value of the cards once the person presses the hit me button please help me i havent fully develop code yet so you can ignore the comments code if you like thanks for looking at it. Code: [Select] <html> <head> <title>poker dice</title> <style type = "text/css"> body { background: green; color: tan; } </style> </head> <body> <center> <h1>Poker Dice</h1> <form> <? $number = $_REQUEST["number"]; $statue = $_REQUEST["statue"]; $number2 = unserialize(urldecode($number)); //check to see if this is first time on the page. if ($statue == null){ if ((!$cash) && (!$card_counter)){ Print "You will start out with a value of 100 dollars and points of 0."; $cash = 100; $card_counter = 0; } // end if Cards($number); printPage(); }else if($statue == 1){ Cards2($number); printPage(); // print "this is the second page"; }else if($statue == 2){ Cards3($number); printPage(); // print "this is the second page"; }else if($statue == 3){ Cards4($number); printPage(); // print "this is the second page"; }else if($statue == 4){ Cards5($number); printPage(); // print "this is the second page"; }else if($statue == 5){ Cards6($number); printPage(); // print "this is the second page"; }else if($statue == 6){ Cards7($number); printPage(); // print "this is the second page"; }else if($statue == 7){ Cards8($number); printPage(); // print "this is the second page"; }else{ Cards9($number); printPage(); // print "this is the second page"; } //rollDice(); if ($secondRoll == TRUE){ print "<h2>Second roll</h2>\n"; $secondRoll = FALSE; evaluate(); } else { print "<h2>First roll</h2>\n"; $secondRoll = TRUE; } // end if printStuff(); function Cards($number){ $Diamonds = array( "Ace of Diamonds", "Two of Diamonds", "Three of Diamonds", "Four of Diamonds", "Five of Diamonds", "Six of Diamonds", "Seven of Diamonds", "Eight of Diamonds", "Nine of Diamonds", "Ten of Diamonds", "Jack of Diamonds", "Queen of Diamonds", "King of Diamonds"); $Clubs = array( "Ace of Clubs", "Two of Clubs", "Three of Clubs", "Four of Clubs", "Five of Clubs", "Six of Clubs", "Seven of Clubs", "Eight of Clubs", "Nine of Clubs", "Ten of Clubs", "Jack of Clubs", "Queen of Clubs", "King of Clubs"); $Hearts = array( "Ace of Hearts", "Two of Hearts", "Three of Hearts", "Four of Hearts", "Five of Hearts", "Six of Hearts", "Seven of Hearts", "Eight of Hearts", "Nine of Hearts", "Ten of Hearts", "Jack of Hearts", "Queen of Hearts", "King of Hearts"); $Spades = array( "Ace of Spades", "Two of Spades", "Three of Spades", "Four of Spades", "Five of Spades", "Six of Spades", "Seven of Spades", "Eight of Spades", "Nine of Spades", "Ten of Spades", "Jack of Spades", "Queen of Spades", "King of Spades"); $Cards = array_merge($Diamonds, $Clubs); $Cards = array_merge($Cards, $Hearts); $Cards = array_merge($Cards, $Spades); //$Deck = shuffle($Cards); $num1 = rand(1,52); $num2 = rand(1,52); echo $Cards[$num1]; //echo " ____ "; echo $Cards[$num2]; // }else{ // echo "There is no cards being displayed."; // }//close second if statement // }//close first if statement $cards_array[0] = $Cards[$num1]; $cards_array[1] = $Cards[$num2]; //it take the object of a string and pass it as a array. $serialCards = urlencode(serialize($cards_array)); print_r($cards_array); print "<br>"; print $serialCards; print $cards_array[0]; print $cards_array[1]; print <<<HERE </tr></td> <tr> <td colspan = "5"> <center> <form methods = "post" action = "PokerFinal.php"> <input type = "submit" value = "Hit Me"> <input type = "hidden" name = "number" value = "$serialCards"> <input type ="hidden" name ="statue" value = "1"> </form> </center> </td> </tr> </table> HERE; print <<<HERE </tr></td> <tr> <td colspan = "5"> <center> <input type = "submit" value = "roll again"> </center> </td> </tr> </table> HERE; // by using the number and requestinh it will store the value of that array. //it good using a hidden box for this method. //if ($Deck[$num1] == TRUE || $Deck[$num2] == TRUE) { //} //$CurrentHand = $Deck[$num1]; //$Deck = explode(",",$CurrentHand[0]); //$Hit = array_shift($Deck); //$Cards = implode(",",$Deck); //if (isset($CurrentHand[1])) // $PlayersHand = explode("~", $CurrentHand[1]); //$PlayersHand[] = $Hit; //$CardCount = 0; /*$Aces = 0; for ($i=0; $i<count($PlayersHand); ++$i) { if (strpos($PlayersHand[$i], "Ace") !== FALSE) ++$Aces; else if (strpos($PlayersHand[$i], "Two") !== FALSE) $CardCount += 2; else if (strpos($PlayersHand[$i], "Three") !== FALSE) $CardCount += 3; else if (strpos($PlayersHand[$i], "Four") !== FALSE) $CardCount += 4; else if (strpos($PlayersHand[$i], "Five") !== FALSE) $CardCount += 5; else if (strpos($PlayersHand[$i], "Six") !== FALSE) $CardCount += 6; else if (strpos($PlayersHand[$i], "Seven") !== FALSE) $CardCount += 7; else if (strpos($PlayersHand[$i], "Eight") !== FALSE) $CardCount += 8; else if (strpos($PlayersHand[$i], "Nine") !== FALSE) $CardCount += 9; else if (strpos($PlayersHand[$i], "Ten") !== FALSE) $CardCount += 10; else if (strpos($PlayersHand[$i], "Jack") !== FALSE) $CardCount += 10; else if (strpos($PlayersHand[$i], "Queen") !== FALSE) $CardCount += 10; else if (strpos($PlayersHand[$i], "King") !== FALSE) $CardCount += 10; } $Cards = $Cards .implode("~", $PlayersHand); if ($Aces > 0) { for ($i=1; $i<=$Aces;++$i) { if ($CardCount+11 <= 21) $CardCount += 11; else $CardCount += 1; } } if ($CardCount == 21) $Cards = $Cards . "\nYou win!"; else if ($CardCount> 21) $Cards = $Cards . "\nYou lose!"; //print $Deck; print <<<HERE </tr></td> <tr> <td colspan = "7"> <center> <input type = "submit" value = "play hand"> </center> </td> </tr> </table> HERE;*/ }//end function function Cards2($number){ $Diamonds = array( "Ace of Diamonds", "Two of Diamonds", "Three of Diamonds", "Four of Diamonds", "Five of Diamonds", "Six of Diamonds", "Seven of Diamonds", "Eight of Diamonds", "Nine of Diamonds", "Ten of Diamonds", "Jack of Diamonds", "Queen of Diamonds", "King of Diamonds"); $Clubs = array( "Ace of Clubs", "Two of Clubs", "Three of Clubs", "Four of Clubs", "Five of Clubs", "Six of Clubs", "Seven of Clubs", "Eight of Clubs", "Nine of Clubs", "Ten of Clubs", "Jack of Clubs", "Queen of Clubs", "King of Clubs"); $Hearts = array( "Ace of Hearts", "Two of Hearts", "Three of Hearts", "Four of Hearts", "Five of Hearts", "Six of Hearts", "Seven of Hearts", "Eight of Hearts", "Nine of Hearts", "Ten of Hearts", "Jack of Hearts", "Queen of Hearts", "King of Hearts"); $Spades = array( "Ace of Spades", "Two of Spades", "Three of Spades", "Four of Spades", "Five of Spades", "Six of Spades", "Seven of Spades", "Eight of Spades", "Nine of Spades", "Ten of Spades", "Jack of Spades", "Queen of Spades", "King of Spades"); $Cards = array_merge($Diamonds, $Clubs); $Cards = array_merge($Cards, $Hearts); $Cards = array_merge($Cards, $Spades); //$Deck = shuffle($Cards); $num1 = rand(1,52); echo $Cards[$num1]; //echo " ____ "; // }else{ // echo "There is no cards being displayed."; // }//close second if statement // }//close first if statement $cards_array[2] = $Cards[$num1]; //it take the object of a string and pass it as a array. $serialCards = urlencode(serialize($cards_array)); print_r($cards_array); print "<br>"; print $serialCards; print $cards_array[0]; print $cards_array[1]; print $cards_array[2]; print <<<HERE </tr></td> <tr> <td colspan = "5"> <center> <form methods = "post" action = "PokerFinal.php"> <input type = "submit" value = "Hit Me"> <input type = "hidden" name = "number" value = "$serialCards"> <input type ="hidden" name ="statue" value = "2"> </form> </center> </td> </tr> </table> HERE; print <<<HERE </tr></td> <tr> <td colspan = "5"> <center> <input type = "submit" value = "roll again"> </center> </td> </tr> </table> HERE; } function Cards3($number){ $Diamonds = array( "Ace of Diamonds", "Two of Diamonds", "Three of Diamonds", "Four of Diamonds", "Five of Diamonds", "Six of Diamonds", "Seven of Diamonds", "Eight of Diamonds", "Nine of Diamonds", "Ten of Diamonds", "Jack of Diamonds", "Queen of Diamonds", "King of Diamonds"); $Clubs = array( "Ace of Clubs", "Two of Clubs", "Three of Clubs", "Four of Clubs", "Five of Clubs", "Six of Clubs", "Seven of Clubs", "Eight of Clubs", "Nine of Clubs", "Ten of Clubs", "Jack of Clubs", "Queen of Clubs", "King of Clubs"); $Hearts = array( "Ace of Hearts", "Two of Hearts", "Three of Hearts", "Four of Hearts", "Five of Hearts", "Six of Hearts", "Seven of Hearts", "Eight of Hearts", "Nine of Hearts", "Ten of Hearts", "Jack of Hearts", "Queen of Hearts", "King of Hearts"); $Spades = array( "Ace of Spades", "Two of Spades", "Three of Spades", "Four of Spades", "Five of Spades", "Six of Spades", "Seven of Spades", "Eight of Spades", "Nine of Spades", "Ten of Spades", "Jack of Spades", "Queen of Spades", "King of Spades"); $Cards = array_merge($Diamonds, $Clubs); $Cards = array_merge($Cards, $Hearts); $Cards = array_merge($Cards, $Spades); //$Deck = shuffle($Cards); $num1 = rand(1,52); echo $Cards[$num1]; //echo " ____ "; // }else{ // echo "There is no cards being displayed."; // }//close second if statement // }//close first if statement $cards_array[3] = $Cards[$num1]; //it take the object of a string and pass it as a array. $serialCards = urlencode(serialize($cards_array)); print_r($cards_array); print "<br>"; print $serialCards; print $cards_array[0]; print $cards_array[1]; print $cards_array[2]; print $cards_array[3]; print <<<HERE </tr></td> <tr> <td colspan = "5"> <center> <form methods = "post" action = "PokerFinal.php"> <input type = "submit" value = "Hit Me"> <input type = "hidden" name = "number" value = "$serialCards"> <input type ="hidden" name ="statue" value = "3"> </form> </center> </td> </tr> </table> HERE; print <<<HERE </tr></td> <tr> <td colspan = "5"> <center> <input type = "submit" value = "roll again"> </center> </td> </tr> </table> HERE; } function Cards4($number){ $Diamonds = array( "Ace of Diamonds", "Two of Diamonds", "Three of Diamonds", "Four of Diamonds", "Five of Diamonds", "Six of Diamonds", "Seven of Diamonds", "Eight of Diamonds", "Nine of Diamonds", "Ten of Diamonds", "Jack of Diamonds", "Queen of Diamonds", "King of Diamonds"); $Clubs = array( "Ace of Clubs", "Two of Clubs", "Three of Clubs", "Four of Clubs", "Five of Clubs", "Six of Clubs", "Seven of Clubs", "Eight of Clubs", "Nine of Clubs", "Ten of Clubs", "Jack of Clubs", "Queen of Clubs", "King of Clubs"); $Hearts = array( "Ace of Hearts", "Two of Hearts", "Three of Hearts", "Four of Hearts", "Five of Hearts", "Six of Hearts", "Seven of Hearts", "Eight of Hearts", "Nine of Hearts", "Ten of Hearts", "Jack of Hearts", "Queen of Hearts", "King of Hearts"); $Spades = array( "Ace of Spades", "Two of Spades", "Three of Spades", "Four of Spades", "Five of Spades", "Six of Spades", "Seven of Spades", "Eight of Spades", "Nine of Spades", "Ten of Spades", "Jack of Spades", "Queen of Spades", "King of Spades"); $Cards = array_merge($Diamonds, $Clubs); $Cards = array_merge($Cards, $Hearts); $Cards = array_merge($Cards, $Spades); //$Deck = shuffle($Cards); $num1 = rand(1,52); echo $Cards[$num1]; //echo " ____ "; // }else{ // echo "There is no cards being displayed."; // }//close second if statement // }//close first if statement $cards_array[4] = $Cards[$num1]; //it take the object of a string and pass it as a array. $serialCards = urlencode(serialize($cards_array)); print_r($cards_array); print "<br>"; print $serialCards; print <<<HERE </tr></td> <tr> <td colspan = "5"> <center> <form methods = "post" action = "PokerFinal.php"> <input type = "submit" value = "Hit Me"> <input type = "hidden" name = "number" value = "$serialCards"> <input type ="hidden" name ="statue" value = "4"> </form> </center> </td> </tr> </table> HERE; print <<<HERE </tr></td> <tr> <td colspan = "5"> <center> <input type = "submit" value = "roll again"> </center> </td> </tr> </table> HERE; } function Cards5($number){ $Diamonds = array( "Ace of Diamonds", "Two of Diamonds", "Three of Diamonds", "Four of Diamonds", "Five of Diamonds", "Six of Diamonds", "Seven of Diamonds", "Eight of Diamonds", "Nine of Diamonds", "Ten of Diamonds", "Jack of Diamonds", "Queen of Diamonds", "King of Diamonds"); $Clubs = array( "Ace of Clubs", "Two of Clubs", "Three of Clubs", "Four of Clubs", "Five of Clubs", "Six of Clubs", "Seven of Clubs", "Eight of Clubs", "Nine of Clubs", "Ten of Clubs", "Jack of Clubs", "Queen of Clubs", "King of Clubs"); $Hearts = array( "Ace of Hearts", "Two of Hearts", "Three of Hearts", "Four of Hearts", "Five of Hearts", "Six of Hearts", "Seven of Hearts", "Eight of Hearts", "Nine of Hearts", "Ten of Hearts", "Jack of Hearts", "Queen of Hearts", "King of Hearts"); $Spades = array( "Ace of Spades", "Two of Spades", "Three of Spades", "Four of Spades", "Five of Spades", "Six of Spades", "Seven of Spades", "Eight of Spades", "Nine of Spades", "Ten of Spades", "Jack of Spades", "Queen of Spades", "King of Spades"); $Cards = array_merge($Diamonds, $Clubs); $Cards = array_merge($Cards, $Hearts); $Cards = array_merge($Cards, $Spades); //$Deck = shuffle($Cards); $num1 = rand(1,52); $num2 = rand(1,52); echo $Cards[$num1]; //echo " ____ "; // }else{ // echo "There is no cards being displayed."; // }//close second if statement // }//close first if statement $cards_array[5] = $Cards[$num1]; //it take the object of a string and pass it as a array. $serialCards = urlencode(serialize($cards_array)); print_r($cards_array); print "<br>"; print $serialCards; print <<<HERE </tr></td> <tr> <td colspan = "5"> <center> <form methods = "post" action = "PokerFinal.php"> <input type = "submit" value = "Hit Me"> <input type = "hidden" name = "number" value = "$serialCards"> <input type ="hidden" name ="statue" value = "5"> </form> </center> </td> </tr> </table> HERE; print <<<HERE </tr></td> <tr> <td colspan = "5"> <center> <input type = "submit" value = "roll again"> </center> </td> </tr> </table> HERE; } function Cards6($number){ $Diamonds = array( "Ace of Diamonds", "Two of Diamonds", "Three of Diamonds", "Four of Diamonds", "Five of Diamonds", "Six of Diamonds", "Seven of Diamonds", "Eight of Diamonds", "Nine of Diamonds", "Ten of Diamonds", "Jack of Diamonds", "Queen of Diamonds", "King of Diamonds"); $Clubs = array( "Ace of Clubs", "Two of Clubs", "Three of Clubs", "Four of Clubs", "Five of Clubs", "Six of Clubs", "Seven of Clubs", "Eight of Clubs", "Nine of Clubs", "Ten of Clubs", "Jack of Clubs", "Queen of Clubs", "King of Clubs"); $Hearts = array( "Ace of Hearts", "Two of Hearts", "Three of Hearts", "Four of Hearts", "Five of Hearts", "Six of Hearts", "Seven of Hearts", "Eight of Hearts", "Nine of Hearts", "Ten of Hearts", "Jack of Hearts", "Queen of Hearts", "King of Hearts"); $Spades = array( "Ace of Spades", "Two of Spades", "Three of Spades", "Four of Spades", "Five of Spades", "Six of Spades", "Seven of Spades", "Eight of Spades", "Nine of Spades", "Ten of Spades", "Jack of Spades", "Queen of Spades", "King of Spades"); $Cards = array_merge($Diamonds, $Clubs); $Cards = array_merge($Cards, $Hearts); $Cards = array_merge($Cards, $Spades); //$Deck = shuffle($Cards); $num1 = rand(1,52); echo $Cards[$num1]; //echo " ____ "; // }else{ // echo "There is no cards being displayed."; // }//close second if statement // }//close first if statement $cards_array[6] = $Cards[$num1]; //it take the object of a string and pass it as a array. $serialCards = urlencode(serialize($cards_array)); print_r($cards_array); print "<br>"; print $serialCards; print <<<HERE </tr></td> <tr> <td colspan = "5"> <center> <form methods = "post" action = "PokerFinal.php"> <input type = "submit" value = "Hit Me"> <input type = "hidden" name = "number" value = "$serialCards"> <input type ="hidden" name ="statue" value = "6"> </form> </center> </td> </tr> </table> HERE; print <<<HERE </tr></td> <tr> <td colspan = "5"> <center> <input type = "submit" value = "roll again"> </center> </td> </tr> </table> HERE; } function Cards7($number){ $Diamonds = array( "Ace of Diamonds", "Two of Diamonds", "Three of Diamonds", "Four of Diamonds", "Five of Diamonds", "Six of Diamonds", "Seven of Diamonds", "Eight of Diamonds", "Nine of Diamonds", "Ten of Diamonds", "Jack of Diamonds", "Queen of Diamonds", "King of Diamonds"); $Clubs = array( "Ace of Clubs", "Two of Clubs", "Three of Clubs", "Four of Clubs", "Five of Clubs", "Six of Clubs", "Seven of Clubs", "Eight of Clubs", "Nine of Clubs", "Ten of Clubs", "Jack of Clubs", "Queen of Clubs", "King of Clubs"); $Hearts = array( "Ace of Hearts", "Two of Hearts", "Three of Hearts", "Four of Hearts", "Five of Hearts", "Six of Hearts", "Seven of Hearts", "Eight of Hearts", "Nine of Hearts", "Ten of Hearts", "Jack of Hearts", "Queen of Hearts", "King of Hearts"); $Spades = array( "Ace of Spades", "Two of Spades", "Three of Spades", "Four of Spades", "Five of Spades", "Six of Spades", "Seven of Spades", "Eight of Spades", "Nine of Spades", "Ten of Spades", "Jack of Spades", "Queen of Spades", "King of Spades"); $Cards = array_merge($Diamonds, $Clubs); $Cards = array_merge($Cards, $Hearts); $Cards = array_merge($Cards, $Spades); //$Deck = shuffle($Cards); $num1 = rand(1,52); $num2 = rand(1,52); echo $Cards[$num1]; //echo " ____ "; // }else{ // echo "There is no cards being displayed."; // }//close second if statement // }//close first if statement $cards_array[7] = $Cards[$num1]; //it take the object of a string and pass it as a array. $serialCards = urlencode(serialize($cards_array)); print_r($cards_array); print "<br>"; print $serialCards; print <<<HERE </tr></td> <tr> <td colspan = "5"> <center> <form methods = "post" action = "PokerFinal.php"> <input type = "submit" value = "Hit Me"> <input type = "hidden" name = "number" value = "$serialCards"> <input type ="hidden" name ="statue" value = "7"> </form> </center> </td> </tr> </table> HERE; print <<<HERE </tr></td> <tr> <td colspan = "5"> <center> <input type = "submit" value = "roll again"> </center> </td> </tr> </table> HERE; } function Cards8($number){ $Diamonds = array( "Ace of Diamonds", "Two of Diamonds", "Three of Diamonds", "Four of Diamonds", "Five of Diamonds", "Six of Diamonds", "Seven of Diamonds", "Eight of Diamonds", "Nine of Diamonds", "Ten of Diamonds", "Jack of Diamonds", "Queen of Diamonds", "King of Diamonds"); $Clubs = array( "Ace of Clubs", "Two of Clubs", "Three of Clubs", "Four of Clubs", "Five of Clubs", "Six of Clubs", "Seven of Clubs", "Eight of Clubs", "Nine of Clubs", "Ten of Clubs", "Jack of Clubs", "Queen of Clubs", "King of Clubs"); $Hearts = array( "Ace of Hearts", "Two of Hearts", "Three of Hearts", "Four of Hearts", "Five of Hearts", "Six of Hearts", "Seven of Hearts", "Eight of Hearts", "Nine of Hearts", "Ten of Hearts", "Jack of Hearts", "Queen of Hearts", "King of Hearts"); $Spades = array( "Ace of Spades", "Two of Spades", "Three of Spades", "Four of Spades", "Five of Spades", "Six of Spades", "Seven of Spades", "Eight of Spades", "Nine of Spades", "Ten of Spades", "Jack of Spades", "Queen of Spades", "King of Spades"); $Cards = array_merge($Diamonds, $Clubs); $Cards = array_merge($Cards, $Hearts); $Cards = array_merge($Cards, $Spades); //$Deck = shuffle($Cards); $num1 = rand(1,52); echo $Cards[$num1]; //echo " ____ "; // }else{ // echo "There is no cards being displayed."; // }//close second if statement // }//close first if statement $cards_array[8] = $Cards[$num1]; //it take the object of a string and pass it as a array. $serialCards = urlencode(serialize($cards_array)); print_r($cards_array); print "<br>"; print $serialCards; print <<<HERE </tr></td> <tr> <td colspan = "5"> <center> <form methods = "post" action = "PokerFinal.php"> <input type = "submit" value = "Hit Me"> <input type = "hidden" name = "number" value = "$serialCards"> <input type ="hidden" name ="statue" value = "8"> </form> </center> </td> </tr> </table> HERE; print <<<HERE </tr></td> <tr> <td colspan = "5"> <center> <input type = "submit" value = "roll again"> </center> </td> </tr> </table> HERE; } function Cards9($number){ $Diamonds = array( "Ace of Diamonds", "Two of Diamonds", "Three of Diamonds", "Four of Diamonds", "Five of Diamonds", "Six of Diamonds", "Seven of Diamonds", "Eight of Diamonds", "Nine of Diamonds", "Ten of Diamonds", "Jack of Diamonds", "Queen of Diamonds", "King of Diamonds"); $Clubs = array( "Ace of Clubs", "Two of Clubs", "Three of Clubs", "Four of Clubs", "Five of Clubs", "Six of Clubs", "Seven of Clubs", "Eight of Clubs", "Nine of Clubs", "Ten of Clubs", "Jack of Clubs", "Queen of Clubs", "King of Clubs"); $Hearts = array( "Ace of Hearts", "Two of Hearts", "Three of Hearts", "Four of Hearts", "Five of Hearts", "Six of Hearts", "Seven of Hearts", "Eight of Hearts", "Nine of Hearts", "Ten of Hearts", "Jack of Hearts", "Queen of Hearts", "King of Hearts"); $Spades = array( "Ace of Spades", "Two of Spades", "Three of Spades", "Four of Spades", "Five of Spades", "Six of Spades", "Seven of Spades", "Eight of Spades", "Nine of Spades", "Ten of Spades", "Jack of Spades", "Queen of Spades", "King of Spades"); $Cards = array_merge($Diamonds, $Clubs); $Cards = array_merge($Cards, $Hearts); $Cards = array_merge($Cards, $Spades); //$Deck = shuffle($Cards); $num1 = rand(1,52); echo $Cards[$num1]; //echo " ____ "; // }else{ // echo "There is no cards being displayed."; // }//close second if statement // }//close first if statement $cards_array[9] = $Cards[$num1]; //it take the object of a string and pass it as a array. $serialCards = urlencode(serialize($cards_array)); print_r($cards_array); print "<br>"; print $serialCards; // print <<<HERE // </tr></td> // <tr> // <td colspan = "5"> // <center> //<form methods = "post" // action = "PokerFinal.php"> //<input type = "submit" // value = "Hit Me"> // <input type = "hidden" // name = "number" // value = "$serialCards"> // <input type ="hidden" // name ="statue" // value = "9"> //</form> //</center> // </td> // </tr> // </table> //HERE; print "No more HIT ME button"; print <<<HERE </tr></td> <tr> <td colspan = "5"> <center> <input type = "submit" value = "roll again"> </center> </td> </tr> </table> HERE; } function printPage(){ }//end functon //function rollDice(){ // global $die, $secondRoll, $keepIt; //print "<table border = 1><td><tr>"; //for ($i = 0; $i < 5; $i++){ //if ($keepIt[$i] == ""){ //$die[$i] = rand(1, 6); //} else { //$die[$i] = $keepIt[$i]; //} // end if //$theFile = "die" . $die[$i] . ".jpg"; //print out dice images // print <<<HERE // <td> // <img src = "$theFile" // height = 50 // width = 50><br> //HERE; //print out a checkbox on first roll only // if ($secondRoll == FALSE){ // print <<<HERE // <input type = "checkbox" // name = "keepIt[$i]" //value = $die[$i]> //</td> //HERE; // } // end if // } // end for loop //print out submit button and end of table //print <<<HERE //</tr></td> //<tr> // <td colspan = "5"> // <center> //<input type = "submit" // value = "roll again"> //</center> // </td> //</tr> //</table> //HERE; //} // end rollDice function evaluate(){ global $die, $cash; //set up payoff $payoff = 0; //subtract some money for this roll $cash -= 2; //count the dice for ($theVal = 1; $theVal <= 6; $theVal++){ for ($dieNum = 0; $dieNum < 5; $dieNum++){ if ($die[$dieNum] == $theVal){ $numVals[$theVal]++; } // end if } // end dieNum for loop } // end theVal for loop //print out results // for ($i = 1; $i <= 6; $i++){ // print "$i: $numVals[$i]<br>\n"; // } // end for loop //count how many pairs, threes, fours, fives $numPairs = 0; $numThrees = 0; $numFours = 0; $numFives = 0; for ($i = 1; $i <= 6; $i++){ switch ($numVals[$i]){ case 2: $numPairs++; break; case 3: $numThrees++; break; case 4: $numFours++; break; case 5: $numFives++; break; } // end switch } // end for loop //check for two pairs if ($numPairs == 2){ print "You have two pairs!<br>\n"; $payoff = 1; } // end if //check for three of a kind and full house if ($numThrees == 1){ if ($numPairs == 1){ //three of a kind and a pair is a full house print "You have a full house!<br>\n"; $payoff = 5; } else { print "You have three of a kind!<br>\n"; $payoff = 2; } // end 'pair' if } // end 'three' if //check for four of a kind if ($numFours == 1){ print "You have four of a kind!<br>\n"; $payoff = 5; } // end if //check for five of a kind if ($numFives == 1){ print "You got five of a kind!<br>\n"; $payoff = 10; } // end if //check for flushes if (($numVals[1] == 1) && ($numVals[2] == 1) && ($numVals[3] == 1) && ($numVals[4] == 1) && ($numVals[5] == 1)){ print "You have a flush!<br>\n"; $payoff = 10; } // end if if (($numVals[2] == 1) && ($numVals[3] == 1) && ($numVals[4] == 1) && ($numVals[5] == 1) && ($numVals[6] == 1)){ print "You have a flush!<br>\n"; $payoff = 10; } // end if print "You bet 2<br>\n"; print "Payoff is $payoff<br>\n"; $cash += $payoff; } // end evaluate function printStuff(){ global $cash, $secondRoll; print "Cash: $cash\n"; //store variables in hidden fields print <<<HERE <input type = "hidden" name = "secondRoll" value = "$secondRoll"> <input type = "hidden" name = "cash" value = "$cash"> HERE; } // end printStuff ?> </form> </center> </html> Hi all, can anyone see why my function is not running? im not getting the form output now. Code: [Select] <?php /* NEW.PHP Allows user to create a new entry in the database */ // creates the new record form // since this form is used multiple times in this file, I have made it a function that is easily reusable function renderForm($Name, $Rank, $error) { ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>New Record</title> <link rel="stylesheet" href="style.css" type="text/css" /> </head> <body> <div id="wrapper"> <p> <?php // if there are any errors, display them if ($error != '') { echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>'; } ?> </p> <p> </p> <form action="" method="post"> <div> <table width="842" border="0" class="styletable" style="border: 1px solid #000000"> <tr> <td width="255"><strong>Name: *</strong></td> <td width="577"><input type="text" name="Name" value="<?php echo $Name; ?>" /></td> </tr> <tr> <td><strong>Rank:</strong></td> <td><input type="text" name="Rank" value="<?php echo $Rank; ?>" /></td> </tr> <tr> <td><strong>Contact Email: *</strong></td> <td><input type="text" name="ContactEmail" value="<?php echo $ContactEmail; ?>" /></td> </tr> <tr> <td><strong>Website: (omitting http://)</strong></td> <td><input type="text" name="Website" value="<?php echo $Website; ?>" /></td> </tr> <tr> <td><strong>Location:</strong></td> <td><select name="Location" id="Location"> <option value="East Midlands">East Midlands</option> <option value="East Of England">East Of England</option> <option value="Greater London">Greater London</option> <option value="North East England">North East England</option> <option value="North West England">North West England</option> <option value="South East England">South East England</option> <option value="South West England">Soth West England</option> <option value="West Midlands">West Midlands</option> <option value="Yorkshire And The Humber">Yorkshire And The Humber</option> </select> </td> </tr> <tr> <td><strong>Bio:</strong></td> <td><textarea name="BIO" id="BIO" cols="60" rows="5"></textarea></td> </tr> <tr> <td> </td> <td> </td> </tr> </table> <p>* required</p> <input type="submit" name="submit" value="Submit"> <a href="view.php">Cancel - View Records</a> </div> </form> </div> </body> </html> <?php } // connect to the database include('../connect-db.php'); // check if the form has been submitted. If it has, start to process the form and save it to the database if (isset($_POST['submit'])) { // get form data, making sure it is valid $Name = mysql_real_escape_string(htmlspecialchars($_POST['Name'])); $Rank = mysql_real_escape_string(htmlspecialchars($_POST['Rank'])); $ContactEmail = mysql_real_escape_string(htmlspecialchars($_POST['ContactEmail'])); $Website = mysql_real_escape_string(htmlspecialchars($_POST['Website'])); $Location = mysql_real_escape_string(htmlspecialchars($_POST['Location'])); $BIO = mysql_real_escape_string(htmlspecialchars($_POST['BIO'])); // check to make sure both fields are entered if ($Name == '' || $ContactEmail == '') { // generate error message $error = 'ERROR: Please fill in all required fields!'; // if either field is blank, display the form again renderForm($Name, $Rank, $error); } else { // save the data to the database mysql_query("INSERT members SET Name='$Name', Rank='$Rank',ContactEmail='$ContactEmail', Website='$Website', Location='$Location', BIO='$BIO'") or die(mysql_error()); // once saved, redirect back to the view page header("Location: view.php"); // if the form hasn't been submitted, display the form } renderForm('','',''); } ?> I'm importing a CSV using the fgetcsv() function, which is working all good. However, when I take a look at the data in the database, I see black diamonds with question marks. This isn't too much of an issue when echoing the data back out again, as they don't appear, but when I want to use one of the CSV fields as a MySQL date, it isn't recognised as a date and is stored as 0000-00-00. e.g. I think this issue is something to do with encoding of the CSV? Can anyone offer any advice? If it helps here is my import script, and the encode type is ASCII according to mb_detect_encoding Code: [Select] <?php include 'config.php'; include 'opendb.php'; ini_set("auto_detect_line_endings", true); $row = 0; $tmpName = $_FILES['csv']['tmp_name']; if (($handle = fopen($tmpName, "r")) !== FALSE) { $num = count($data); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $noQuotes = str_replace("\"", '', $data); $originalDate = $noQuotes[1]; //$delivery_date = date('Y-m-d', strtotime($originalDate)); $parts = explode('/', $originalDate); $delivery_date = $parts[2] . '-' . $parts[1] . '-' . $parts[0]; $row++; $import="INSERT into dispatch (delivery_note_number, delivery_date, dispatch_date, customer_delivery_date, delivery_line, produce, variety, quantity, pallets, count, depot, customer, grower, haulier, status) values ('$noQuotes[0]', '$delivery_date', '$noQuotes[2]', '$noQuotes[3]', '$noQuotes[4]', '$noQuotes[5]', '$noQuotes[6]', '$noQuotes[7]', '$noQuotes[8]', '$noQuotes[9]', '$noQuotes[10]', '$noQuotes[11]', '$noQuotes[12]', '$noQuotes[13]', '$noQuotes[14]')"; echo $import; mysql_query($import) or die(mysql_error()); } //header("location:list_dispatch.php?st=recordsadded"); fclose($handle); } ?> |