PHP - .png Background Is White, Need Transparent? - Php Gd
So I'm new to PHP GD, I cant understand why the PNG image will not go transparent, it's just showing a white background.
Could anyone help me out with this?
Spoiler
Similar Tutorialssession_start(); $text = rand(10000,99999); $_SESSION["vercode"] = $text; $height = 25; $width = 65; $image_p = imagecreate($width, $height); $black = imagecolorallocate($image_p, 255, 255, 255); $white = imagecolorallocate($image_p, 0, 0, 0); $font_size = 14; imagestring($image_p, $font_size, 5, 5, $text, $white); imagejpeg($image_p, null, 80);The code above creates an image with white background. How to change it to create transparent background? 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); } ?> 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 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? I am using the following code to give my image round corners, but i am using an image that just cuts the corner with a white shape. Is it possible to make it cut the corner with a transparent color? function roundcorners($image, $topleft, $topright = null, $bottomright = null, $bottomleft = null) { if($topright == null) $topright = $topleft; if($bottomright == null) $bottomright = $topleft; if($bottomleft == null) $bottomleft = $topleft; if($topleft >= 2) { $tl = imagecreatefrompng("corner.png"); imagecopyresampled($image, $tl, 0, 0, 0, 0, $topleft, $topleft, 485, 485); } if($topright >= 2) { $tr = imagerotate(imagecreatefrompng("corner.png"), 270, 0); imagecopyresampled($image, $tr, imagesx($image)-$topright, 0, 0, 0, $topright, $topright, 485, 485); } if($bottomright >= 2) { $br = imagerotate(imagecreatefrompng("corner.png"), 180, 0); imagecopyresampled($image, $br, imagesx($image)-$bottomright, imagesy($image)-$bottomright, 0, 0, $bottomright, $bottomright, 485, 485); } if($bottomleft >= 2) { $bl = imagerotate(imagecreatefrompng("corner.png"), 90, 0); imagecopyresampled($image, $bl, 0, imagesy($image)-$bottomleft, 0, 0, $bottomleft, $bottomleft, 485, 485); } } I'm wondering if the GD library is capable of coloring ONLY the non-transparent areas in an image. I had this image white, black, and every other color under the moon, but could only get GD to color the entire canvas (width and height) of the image with an opacity over it. I'm merging many layers on top of each other so I can't afford to color the entire square and lower the opacity and do some weird multiply stuff.
That's the image above. Then I have some lineart, markings, eye color, etc. So I had all my possible colors white areas that I layer on top of eachother. I want to color the entire non-transparent areas with a color (dynamic).. so I can't just color it in Photoshop and then upload it or else I would literally have millions of possibilities for every single color and shade..
Thanks for any help. I was trying to go with ImageMagick if GD isn't capable, but I can't even get that downloaded in my Wamp. I'm using 64 bit Wamp so I have no idea if that does something or whatever. I can get it installed on my computer and run it via command line, but I can't get it to work and render images in my Wamp files... can't ever find a .dll that works.
hello, here is what i have. i feel like im close. Code: [Select] $result4 = mysql_query("SELECT * FROM schedule WHERE eid = '$posteid' AND status = 1 OR 2"); Hello.
The first page works, but when form actioning to the next i just get a white screen.
This is the white screen page:
<?php session_start(); include 'details.php'; $username = $_POST['username']; $password = $_POST['password']; $pwswdrd = md5($password); if(!empty($_POST['username'])) { $query = mysqli_query($con, "SELECT * FROM users where name = '$username' AND pass = '$pwswdrd'") or die(mysqli_connect_error()); $row = mysqli_fetch_array($query); if(!empty($row['name']) AND !empty($row['pass'])) { $_SESSION['username'] = $row['username']; $_SESSION['id'] = $row['id']; header('Location: index.php'); } } else { echo "You failed to log in."; } ?> - details.php <?php $one=mysqli_connect('.......webhost.com','aee','aaa') or die("Failed to connect to MySQL: " . mysqli_error() ); $db=mysqli_select_db($one,'aee') or die("Failed to connect to MySQL: " . mysqli_error() ); ?> -- What can possibly be wrong?! I’m trying to work out how I can use php gd to take an image, in this case it’s a rectangle that has text in the middle and the rest is transparent, so it’s a png with just text in the centre.
I have another image which is a texture. Its also a rectangle of the same size. I have managed to merge the two together so the texture on top of the text image but it covers the entire image rather than just the text in the middle.
what I need it to do it apply the texture to the text in the image and ignore the surrounding transparent pixels.
I have tried to look google, but was unable to find anything. and the search daemon is giving me errors on this site. So, when I am calling lines from a text file, I am getting a problem. to explain, I will use a # to show were this "white space" is I am getting This is the text file Code: [Select] url.com url2.com url3.com when PHP reads the file, and I output it into PHP its reading it like this Code: [Select] url1.com# url2.com# url3.com url1.com and url2.com both are getting this "white space" at the end of them, and the last entry does not. I have tried to use str_replace(" ","",$line) and str_replace("\r","",$line) but nothing. can anyone advise me as to what character this is that is being outputed. my code I'm using $f = fopen ("list.txt", "r"); while ($line= fgets ($f)) { if($url == $line){echo $line;} } fclose ($f); I'm familiar with 'trim' and even tried the following: $datanew = str_replace(array("\n", "\r", "\t", " ", "\o", "\xOB"), '', $data); but for the life of me I can't remove any white space within the source code which is messing everything else up. This is what I need: What people see.... Code: [Select] word1 word2 word3 The source... Code: [Select] word1 word2 word3 This is the current situation What people see... Code: [Select] word1 word2 word3 The source... Code: [Select] word1 word2 word3 For whatever reason, if I can strip the whitespace from the source code then I can use the content accordingly. How would do you strip the white space? Does this even make sense? Okay, I can't seem to figure out why the form isn't pulling up. Here's what I'm trying to do. Administrator clicks on Add New Item to Inventory, pulling up the shops that are available to add inventory to. (This part appears to be working, because it does list those shops available). Then, the administrator will click on a shop name, opening up a form that will allow them to type in the name of the item they want to add. Instead, it goes to a blank screen, no errors even with error reporting on. adminpanel.php is the main file that houses all these functions. If you're familiar with EZRPG, it's the same basic setup, just with these additions to the list. Any help would be greatly appreciated! Thank you!! function addinv() { $query = doquery("SELECT id,name FROM shop ORDER BY name", "shop"); $page = "<b><u>Add Inventory</u></b><br />Click to add an item to a shop's inventory.<br /><br /><table width=\"50%\">\n"; $count = 1; while ($row = mysql_fetch_array($query)) { if ($count == 1) { $page .= "<tr><td width=\"8%\" style=\"background-color: #eeeeee;\">".$row["id"]."</td><td style=\"background-color: #eeeeee;\"><a href=\"admin_panel.php?do=addshopinv:".$row["id"]."\">".$row["name"]."</a></td></tr>\n"; $count = 2; } else { $page .= "<tr><td width=\"8%\" style=\"background-color: #ffffff;\">".$row["id"]."</td><td style=\"background-color: #ffffff;\"><a href=\"admin_panel.php?do=addshopinv:".$row["id"]."\">".$row["name"]."</a></td></tr>\n"; $count = 1; } } if (mysql_num_rows($query) == 0) { $page .= "<tr><td width=\"8%\" style=\"background-color: #eeeeee;\">No items found.</td></tr>\n"; } $page .= "</table>"; admindisplay($page, "Add Shop Inventory"); } function addshopinv($name) { if (isset($_POST["submit"])) { extract($_POST); $errors = 0; $errorlist = ""; if ($name == "") { $errors++; $errorlist .= "Name is required.<br />"; } else if ($errors == 0) { $query1 = doquery("SELECT * FROM items WHERE name=$name"); $item_id = mysql_fetch_array($query1); $query = doquery("INSERT INTO `sale` SET shop_id=".$row["id"].", item_id='$item_id',"); admindisplay("Inventory Item Added.","Add New Inventory Item"); } else { admindisplay("<b>Errors:</b><br /><div style=\"color:red;\">$errorlist</div><br />Please go back and try again.", "Add New Item to Shop"); } } $name = $page = <<<END <b><u>Add New Inventory Item</u></b><br /><br /> <form action="admin_panel.php?do=addshopinv:$name" method="post"> <table width="90%"> <tr><td width="20%">Name:</td><td><input type="text" name="name" size="30" maxlength="255" value="" />*255 character max</td></tr> </table> <input type="submit" name="submit" value="Submit" /> <input type="reset" name="reset" value="Reset" /> </form> END; $page = parsetemplate($page, $row); admindisplay($page, "Add New Inventory Item"); } Code: [Select] Address: 351 Hougang Ave 3 (769057) Contact No: 6752 5513 Operating Hours: 8.00am - 9.30pm (Thursday to Tuesday & Public Holidays) Background: Opened to the public on 30 Mar 88 Hi good day, if I paste the above address into one of the mysql field, I found out that I cannot fetch it using php. It give me a blank page. Code: [Select] Address: 351 Hougang Ave 3 (769057) Contact No: 6752 5513 Operating Hours: 8.00am - 9.30pm (Thursday to Tuesday & Public Holidays) Background: Opened to the public on 30 Mar 88 When I sort it together then i can see mysql working, very strange.. Can anyone help? 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('','',''); } ?> Hello! Incredibly new to PHP, so if this problem is a clear error i apologise. I am developing a page which will display a product based on what is clicked as a result of a search, my problem is that no matter that happens I cannot display anything from the Products table in my database: Code: [Select] <?php session_start(); include "connect.php"; $prodsql="SELECT product_ID, product_Name, Category, Price, Information from Product ;"; $prodresult = mysqli_query($_SESSION['conn'], $prodsql); while ($prow = mysql_fetch_array($prodresult, MYSQL_NUM)) { echo "<p> $prow['product_Name']"; } else{ } What am i missing? The connect.php works as I can successfully login and register members. Thank you I made my website on my laptop (localserver) and it was working fine. But when I uploaded to my hosting account and tried to view it in my browser, all I get is a blank white screen. No errors, no messages. Just blank screen. I tried different hosts ... same problem ... but the website runs fine on my localhost. what could be the problem ??? I even contacted me hosting company and they told me that PHP server is running fine. Hi all.
i am trying to use php to include a javascript onto different pages, and then "sort of " pass it a var.
The bulk of the code will be in an included footer php file.
The var will be set in the main page.
i have it working as follows: by just using echo 3 times.. the 1st with the first part of the script, the 2nd is the variable, and the 3rd is the rest of the script.
The same endcode.php file needs to also be used for pages that wont have a var set, and wont be using the script - hense the isset.
<!-- mainPage.php --> <?php $sion_gallery_id = '450'; ?> <?php include(endcode.php); ?> <!-- endcode.php --> <?php if (isset($sion_gallery_id)) {echo "start of javascript.......album/"; echo $sion_gallery_id; echo"/end of script"; } else { echo "var not on set";} ?> Hello, I have the following code writting IP,session, and date to a text file (it works) but I want to add a white space after the IP address. fwrite($fp, $_SERVER['REMOTE_ADDR']. $_SESSION["sess_name"]. " $dateTime\r\n"); At the moment it prints like this 16.17.51.41SESSION_ID 2011/01/23 12:53:32 thanks in advance. Keep going over this and can't find the stupid error. Need some fresh eyes for some help. Thanks Code: [Select] <?php ob_start(); session_start(); include("include/db_connect.php"); // Define $username and $password $username = $_REQUEST['username']; $password = $_REQUEST['password']; // To protect MySQL injection $username = stripslashes($username); $password = stripslashes($password); $username = mysql_real_escape_string($username); $password = mysql_real_escape_string($password); $sql="SELECT * FROM login WHERE username = '$username' AND password = '$password'"; $result=mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_array($result); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched, table row must be 1 row if($count!=0) { // Register session variables and redirect to file "dashboard.php" $_SESSION['id'] = $row['id']; $_SESSION['franchise_id'] = $row['franchise_id']; $_SESSION['dealer_id'] = $row['dealer_id]'; $_SESSION['first'] = $row['first']; $_SESSION['last'] = $row['last']; header("location:dashboard.php"); } else { header("location:not_logged_in.php"); } ob_end_flush(); ?> Hi im having trouble with uploading images with spaces in as they dont show up in the browser. Here is the code: Code: [Select] $image = addslashes($_FILES['image']['name']); $target = "images/"; $target = $target . basename( $_FILES['image']['name']); //Writes the photo to the server if(move_uploaded_file($_FILES['image']['tmp_name'], $target)) { Any help would be appreciated. |