Add PNG Image in MySQL Database

Started by eyeshiv, October 18, 2014, 04:51:58 AM

Previous topic - Next topic

eyeshiv

Hi friends,
Following code is for inserting PNG image in My SQL Server.
But It is not working for PNG transparent Image. Image is inserting well but it is showing black background.

can anybody help me to resolve this problem.

<?php
ini_set("display_errors",0);
include_once("connection.php");
//define a maxim size for the uploaded images in Kb
define ("MAX_SIZE","9000");
 
//This function reads the extension of the file. It is used to determine if the file  is an image by checking the extension for more visit  hxxp:eyeshiv.com [nonactive].
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 errro occures. 
//If the error occures 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['image']['name'];
    //if it is not empty
    if ($image)
    {
    //get the original name of the file from the clients machine
        $filename = stripslashes($_FILES['image']['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 and will not  upload the file, 
    //otherwise we will do more tests
    if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
        {
        //print error message
            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=filesize($_FILES['image']['tmp_name']);
 
            //compare the size with the maxim size we defined and print error if bigger
            if ($size > MAX_SIZE*1024)
            {
                echo '<h1>You have exceeded the size limit!</h1>';
                $errors=1;
            }
 
            else{
         
         if(($extension == "jpg") || ($extension == "jpeg"))
         {
            //the new name will be containing the full path where will be stored (images folder)
            $temp=resizeImage($_FILES['image']['tmp_name'],120,318);
            $imgfile1="../porduct/thumb/".time().$image;
         $aa=imagejpeg ( $temp, $imgfile1);
            //$small=mysql_query("insert into product_small_image values('Null','$imgfile','".$_GET['id']."','".time()."')");
            if(imagejpeg ( $temp, $imgfile1 ))
         {
         
         $temp=resizeImage($_FILES['image']['tmp_name'],413,1093);
            $imgfile="../porduct/big/".time().$image;
         
         imagejpeg ( $temp, $imgfile);
         $big=mysql_query("insert into product_image values('Null','$imgfile','$imgfile1','".$_GET['id']."','".time()."')");
         }
         ///////////
         }
         else
         {
          $temp=resizeImage2($_FILES['image']['tmp_name'],120,318);
            $imgfile1="../porduct/thumb/".time().$image;
         $aa=imagejpeg ( $temp, $imgfile1);
            //$small=mysql_query("insert into product_small_image values('Null','$imgfile','".$_GET['id']."','".time()."')");
            if(imagejpeg ( $temp, $imgfile1 ))
         {
         
         $temp=resizeImage2($_FILES['image']['tmp_name'],413,1093);
            $imgfile="../porduct/big/".time().$image;
         
         imagejpeg ( $temp, $imgfile);
         $big=mysql_query("insert into product_image values('Null','$imgfile','$imgfile1','".$_GET['id']."','".time()."')");
         }
         
         
         }
         
         
            }
 
 
        }
    }
 
    else
    {
        echo "<h1>Select Image File</h1>";
        $errors=1;
    }
}
 
//If no errors registred, print the success message
if(isset($_POST['Submit']) && !$errors)
{
    echo "<h1>File Uploaded Successfully! Try again!</h1>";
}
 
   

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "hxxp:www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd [nonactive]">
<html xmlns="hxxp:www.w3.org/1999/xhtml [nonactive]">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

</head>

<body>
<div style="padding:0px 100px 0px 100px;">
<table width="400px" height="400px" border="0" cellspacing="0" cellpadding="0" align="center">
<tr><td valign="top" align="center" style="font-size:20px" height="80px"><strong>Add Image</strong></td></tr>
<tr>
<td valign="top">
<form name="newad" method="post" enctype="multipart/form-data"  action="">
<table>
<tr><td>Select Image :</td><td valign="top"><input type="file" name="image"></td></tr>





<tr><td colspan="2" height="10px"></td></tr>
<tr><td>&nbsp;</td><td><input name="Submit" type="submit" value="Upload image"></td></tr>
</table>
</form>
</td>
</tr>
</table>
</div>
</body>
</html>

vbgamer45

I generally don't recommend storing files inside the database.

But if you are make sure your column type is set to binary
Community Suite for SMF - Take your forum to the next level built for SMF, Gallery,Store,Classifieds,Downloads,more!

SMFHacks.com -  Paid Modifications for SMF

Mods:
EzPortal - Portal System for SMF
SMF Gallery Pro
SMF Store SMF Classifieds Ad Seller Pro

JBlaze

Make sure you are using the imagesavealpha() function when saving transparent .png images.
Jason Clemons
Former Team Member 2009 - 2012

Advertisement: