-1

I spend six hours on trying to figure out how to upload images in my database and to re-size images. I new in php so anyone can help me.

I research this post: How to upload images into MySQL database using PHP code

but I dont know what BLOBS is nor I do not think this post answer my question. I may be wrong so please explain if I am.

Also, I research this Post: How to add an image to php mysql database?

::::CHANGES::::

I made changes to my php. I add my variables into my php code and now it not sending images to my server side folder "photo" how can I get that happen?

PHP code:

<?php

$filetmp = $_FILES["file_img"]["tmp_name"];
$filename = $_FILES["file_img"]["name"];
$filetype = $_FILES["file_img"]["type"];
$filesize = $_FILES["file_img"]["size"];
$fileinfo = getimagesize($_FILES["file_img"]["tmp_name"]);
$filewidth = $fileinfo[0];
$fileheight = $fileinfo[1];
$filepath = "~warren/mysql2/photo/".$filename;
$filepath_thumb = "~warren/mysql2/photo/thumb/".$filename;
chmod($filepath,0777);
chmod($filepath_thumb,0777);


if(isset($_POST['btn_upload']))
{
    $sPhotoFileName = $filename;
    $nPhotoSize = $filesize;
    $sTempFileName = $filetmp;

if ($sPhotoFileName) // file uploaded
{   $aFileNameParts = explode(".", $sPhotoFileName);
    $sFileExtension = end($aFileNameParts); // part behind last dot
    if ($sFileExtension != "jpg"
        && $sFileExtension != "png"
        && $sFileExtension != "gif")
    {   die ("Choose a JPG for the photo");
    }

    if ($nPhotoSize == 0)
    {   die ("Sorry. The upload of $sPhotoFileName has failed.
Search a photo smaller than 100K, using the button.");
    }
    if ($nPhotoSize > 10240000000)
    {   die ("Sorry.
The file $sPhotoFileName is larger than 100K.
Advice: reduce the photo using a drawing tool.");
    }
    // read photo

    $oTempFile = fopen($sTempFileName, "r");
    $sBinaryPhoto = fread($oTempFile, fileSize($sTempFileName));
    // Try to read image
    $nOldErrorReporting = error_reporting(E_ALL & ~(E_WARNING)); // ingore warnings
    $oSourceImage = imagecreatefromstring($sBinaryPhoto); // try to create image
    error_reporting($nOldErrorReporting);
    if (!$oSourceImage) // error, image is not a valid jpg
    { die ("Sorry.
It was not possible to read photo $sPhotoFileName.
Choose another photo in JPG format.");
    }
}
 $nWidth = imagesx($oSourceImage); // get original source image width
        $nHeight = imagesy($oSourceImage); // and height
        // create small thumbnail
        $nDestinationWidth = 80;
        $nDestinationHeight = 60;
    //$oDestinationImage = imagecreatetruecolor($nDestinationWidth, $nDestinationHeight);
        $oDestinationImage = imagecreate($nDestinationWidth, $nDestinationHeight);
    /*$oResult = imagecopyresampled(
        $oDestinationImage, $oSourceImage,
        0, 0, 0, 0,
        $nDestinationWidth, $nDestinationHeight,
        $nWidth, $nHeight); // resize the image
    */
        imagecopyresized($oDestinationImage, $oSourceImage,0, 0, 0, 0,$nDestinationWidth, $nDestinationHeight,$nWidth, $nHeight); // resize the image
        ob_start(); // Start capturing stdout.
        imageJPEG($oDestinationImage); // As though output to browser.
        $sBinaryThumbnail = ob_get_contents(); // the raw jpeg image data.
        ob_end_clean(); // Dump the stdout so it does not screw other output.

    if($_POST['btn_upload'])
    {

    $sBinaryThumbnail = addslashes($sBinaryThumbnail);
    $oDatabase = $link;
    mysql_select_db("upload", $oDatabase);
    $sQuery = "insert into image (thumbnail) values ('$sBinaryThumbnail')";
    echo $sQuery;
    mysql_query($sQuery, $oDatabase);
    }
}

    ?>

Here my html:

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

<form action="test4.php" method="post" enctype="multipart/form-data">
<input type="file" name="file_img" />
<input type="submit" name="btn_upload" value="Upload">  
</form>
</body>
</html>
Community
  • 1
  • 1
Kashad
  • 19
  • 6
  • is there value in your db ? Or there is problem in both upload and inserting value? – Niroj Adhikary May 04 '16 at 09:33
  • @niroj adhikary in my database i got img_id, img_name, img_path, img_type – Kashad May 04 '16 at 09:38
  • your upload paths are incorrrect give path from root . echo __DIR__ (magic constant) to know root path. It should be like D:\xampp\htdocs\...... (your folder path) do not use path like $filepath = ".../photo/{$filename}".$filename; and at last i think you should not use {$filename}".$filename; after photo/ – Niroj Adhikary May 04 '16 at 09:46
  • @NirojAdhikary I tried it. it still not posted database. this what i have tried: **$filepath = "http://thing.cs.usm.maine.edu/~warren/mysql2/photo".$filename; $filepath_thumb = "http://thing.cs.usm.maine.edu\~warren\mysql2\photo\thumb".$filename;** – Kashad May 04 '16 at 10:07
  • check this link http://www.w3schools.com/php/php_file_upload.asp – Niroj Adhikary May 04 '16 at 10:27

2 Answers2

2

PHP CODE:

Photo upload, validation at server

<?php
$sPhotoFileName = $_FILES['photo']['name']; // get client side file name
if ($sPhotoFileName) // file uploaded
{   $aFileNameParts = explode(".", $sPhotoFileName);
    $sFileExtension = end($aFileNameParts); // part behind last dot
    if ($sFileExtension != "jpg"
        && $sFileExtension != "JPEG"
        && $sFileExtension != "JPG")
    {   die ("Choose a JPG for the photo");
    }
    $nPhotoSize = $_FILES['photo']['size']; // size of uploaded file
    if ($nPhotoSize == 0)
    {   die ("Sorry. The upload of $sPhotoFileName has failed.
Search a photo smaller than 100K, using the button.");
    }
    if ($nPhotoSize > 10240000000)
    {   die ("Sorry.
The file $sPhotoFileName is larger than 100K.
Advice: reduce the photo using a drawing tool.");
    }
    // read photo
    $sTempFileName = $_FILES['photo']['tmp_name']; // temporary file at server side
    $oTempFile = fopen($sTempFileName, "r");
    $sBinaryPhoto = fread($oTempFile, fileSize($sTempFileName));
    // Try to read image
    $nOldErrorReporting = error_reporting(E_ALL & ~(E_WARNING)); // ingore warnings
    $oSourceImage = imagecreatefromstring($sBinaryPhoto); // try to create image
    error_reporting($nOldErrorReporting);
    if (!$oSourceImage) // error, image is not a valid jpg
    { die ("Sorry.
It was not possible to read photo $sPhotoFileName.
Choose another photo in JPG format.");
    }
}

Create thumbnail

    $nWidth = imagesx($oSourceImage); // get original source image width
        $nHeight = imagesy($oSourceImage); // and height
        // create small thumbnail
        $nDestinationWidth = 80;
        $nDestinationHeight = 60;
    //$oDestinationImage = imagecreatetruecolor($nDestinationWidth, $nDestinationHeight);
        $oDestinationImage = imagecreate($nDestinationWidth, $nDestinationHeight);
    /*$oResult = imagecopyresampled(
        $oDestinationImage, $oSourceImage,
        0, 0, 0, 0,
        $nDestinationWidth, $nDestinationHeight,
        $nWidth, $nHeight); // resize the image
    */
        imagecopyresized($oDestinationImage, $oSourceImage,0, 0, 0, 0,$nDestinationWidth, $nDestinationHeight,$nWidth, $nHeight); // resize the image
        ob_start(); // Start capturing stdout.
        imageJPEG($oDestinationImage); // As though output to browser.
        $sBinaryThumbnail = ob_get_contents(); // the raw jpeg image data.
        ob_end_clean(); // Dump the stdout so it does not screw other output.

Store Thumbnail in database

 if($_POST['send'])
    {
    $sBinaryThumbnail = addslashes($sBinaryThumbnail);
    $oDatabase = mysql_connect("localhost", "root", "karma");
    mysql_select_db("upload", $oDatabase);
    $sQuery = "insert into image (thumbnail) values ('$sBinaryThumbnail')";
    echo $sQuery;
    mysql_query($sQuery, $oDatabase);
    }
    ?>
Hitarthi Panchal
  • 392
  • 2
  • 11
0

As you have image data in $image_p (after resizing), you need to put this variable in your database.

PHP does not provide a way to convert the image object to it's data, so we need to output in buffer and save in variable:

ob_start();
$imagecreate($image_p);
$contents =  ob_get_contents(); //this is your image contents
ob_end_clean();

Now insert this contents in your database. Make sure the contents column is of BLOB Type. For example:

$sql = "INSERT INTO Uploadimg (img_id,img_name,img_path,img_type,contents) VALUES ('0','$filename','$filepath','$filetype', '$contents')";

When you want to show the image in HTML src tag use this:

<img src="data:image/png;base64,<?php echo base64_encode($image_contents_from_db); ?>">
Ghulam Ali
  • 1,935
  • 14
  • 15