FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Image store in mysql





DanielXP
I have this script i made.

SQL:
Code:
CREATE TABLE `pictures` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`pfl_id` varchar(100),
`pic_title` varchar(50),
`pic_desc` varchar(50),
`pic_format` varchar(5),
`pic_width` int(11),
`pic_height` int(11),
`content` BLOB,
`thumb_content` BLOB,
PRIMARY KEY (`id`)
) TYPE = MYISAM;


And heres the imageupload

Code:
<?php
include("header.php");
if(!$account[id]) {
header("Location: " . $siteurl);
}else if(is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) {
if(substr($HTTP_POST_FILES['userfile']['type'],0,5) == image) {
$pic_format = explode("/", $HTTP_POST_FILES['userfile']['type']);
$vcode = gencode("32");
$filename = "temp/" . $vcode . "." . $pic_format[1];
if(@copy($_FILES['userfile']['tmp_name'], $filename)) {
$PSize = filesize($filename);
$contents = addslashes(fread(fopen($filename, "r"), $PSize));
list($width, $height, $type, $attr) = getimagesize($filename);

//Create thumbnail
if($pic_format[1] == "gif") {
$im=ImageCreateFromgif($filename);
}elseif($pic_format[1] == "png") {
$im=ImageCreateFrompng($filename);
}elseif($pic_format[1] == "jpg") {
$im=ImageCreateFromjpeg($filename);
}
$width=ImageSx($im);              // Original picture width is stored
$height=ImageSy($im);            // Original picture height is stored
$newimage=imagecreatetruecolor($thumb_width,$thumb_height);
imageCopyResized($newimage,$im,0,0,0,0,$thumb_width,$thumb_height,$width,$height);
$filename_thumb = "temp/thumb_" . $vcode . "." . $pic_format[1];
if (function_exists("imagegif")) {
Imagegif($newimage,$filename_thumb);
}elseif (function_exists("imagejpeg")) {
ImageJPEG($newimage,$filename_thumb);
}elseif (function_exists("imagepng")) {
Imagepng($newimage,$filename_thumb);
}
$PSize_thumb = filesize($filename_thumb);
$contents_thumb = addslashes(fread(fopen($filename_thumb, "r"), $PSize_thumb));
//finished creating thumbnail

$insert = mysql_query("INSERT INTO `pictures` (`pfl_id`,`pic_title`,`pic_desc`,`pic_format`,`pic_width`,`pic_height`,`content`,`thumb_content`) VALUES ('" . $account[id] . "','" . protect($_POST[pic_title]) . "','" . protect($_POST[pic_desc]) . "','" . protect($pic_format[1]) . "','" . $width . "','" . $height . "','" . $contents . "','" . $contents_thumb . "')");

unlink($_SERVER["DOCUMENT_ROOT"] . $prefix . "/temp/" . $vcode . "." . $pic_format[1]);
unlink($_SERVER["DOCUMENT_ROOT"] . $prefix . "/temp/thumb_" . $vcode . "." . $pic_format[1]);
if($insert) {
$msgtitle = "Success";
$message = "Image uploaded.";

}else{
$msgtitle = "Unknown Error";
$message = "Image could not be inserted in the database.<br />If this continues to happern please report this.";
}
}else{
$msgtitle = "Unknown Error";
$message = "Image could not be uploaded.<br />If this continues to happern please report this.";
}
}else{
$msgtitle = "Error";
$message = "File must be a image.";
}
?>
<table cellspacing="0">
   <tr>
      <td class="cleft">

      
        <table class="tb" id="spa">
           <tr>
               <td class="bar"><?=$msgtitle?></td>
            </tr>
            <tr>
               <td class="panel"><?=$message?></td>
            </tr>
        </table>
</td>
      <td class="cright" width="200" align="center"><? include("advert2.php"); ?></td>
   </tr>
</table>
<?
}else{
?>
<table cellspacing="0">
   <tr>
      <td class="cleft">

      
        <table class="tb" id="spa">
           <tr>
               <td class="bar">Upload Picture</td>
            </tr>
            <tr>
               <td class="panel"><form method="post" enctype="multipart/form-data">
<table>
   <tr>
      <td>File:</td>
      <td><input type="file" name="userfile" size="45" /></td>
   </tr>
   <tr>
      <td>Image Title:</td>
      <td><input type="text" style="width:250px;" name="pic_title" /></td>
   </tr>
   <tr>
      <td valign="top">Image Description:</td>
      <td><textarea style="width:450px;" name="pic_desc"></textarea></td>
   </tr>
   <tr>
      <td colspan="2"><input type="submit" value="Upload" onclick="this.value='Uploading...'"></td>
   </tr>
</table>
</form></td>
            </tr>
        </table>
</td>
      <td class="cright" width="200" align="center"><? include("advert2.php"); ?></td>
   </tr>
</table>
<?
}
include("footer.php");
?>


Basicly i uploads the image file to temp/ folder. gets the binary and saves it in the mysql folder (as well as creates a thumbnail and saves that into the sql as well)

But if you upload a png image it don't seem to display.

http://rmb-scripting.com/rmb-space/userdata/picture/1/ Thats a png (as you can see it wont display)

http://rmb-scripting.com/rmb-space/userdata/picture/2/ Gif works fine
http://rmb-scripting.com/rmb-space/userdata/picture/2/ jpg works fine

Also with the thumbnail maker it don't work when making a thumbnail for jpg.

This is the error i get
Quote:

Warning: imagesx(): supplied argument is not a valid Image resource in /home/rmb/public_html/rmb-space/uploadphoto.php on line 23

Warning: imagesy(): supplied argument is not a valid Image resource in /home/rmb/public_html/rmb-space/uploadphoto.php on line 24

Warning: imagecopyresized(): supplied argument is not a valid Image resource in /home/rmb/public_html/rmb-space/uploadphoto.php on line 26


I hope you can help on these 2 problems.

Thanks,
Daniel
alalex
I can only give you a little tip that could both make easier picture management and thumbnail creation.
It is called Asido (http://asido.info/)
With it you can ensure that the thumbnails will be created properly.

The thing with png's is that they might be uploaded wrongly, maybe you could try to convert all the images to jpg for example and then format them...
DanielXP
Yer i was thinking of converting all the images to .gif

Any one know a good tutorial/script what i could use to do this?

I have no idea lol Razz

Cheers
Daniel
AOP Web Development
why don't you just upload the files on the server and save the file name, so that it would be easier to retrieve... i guess putting the image into mysql by chunking take a lot of time, and might be burden to your server...
golles
AOP Web Development wrote:
why don't you just upload the files on the server and save the file name, so that it would be easier to retrieve... i guess putting the image into mysql by chunking take a lot of time, and might be burden to your server...

sometimes it will be usefull, some hosts have file data limit but no database data limit Rolling Eyes
AOP Web Development
golles wrote:
AOP Web Development wrote:
why don't you just upload the files on the server and save the file name, so that it would be easier to retrieve... i guess putting the image into mysql by chunking take a lot of time, and might be burden to your server...

sometimes it will be usefull, some hosts have file data limit but no database data limit Rolling Eyes


ah yes. for small diskspace it might be useful, but it will burden your server... Well anyway, it's just upon on the situation..... so for as long as where you are comfortable with then go for it.
DanielXP
Thanks for your replys.

I think i will just upload it into a directory, i can't be asked converting the images to put in mysql.
Related topics
E-Cards with PHP & MySQL
Make search engine With PHP and mySQL, for your site
News management system...
PHP Image Upload (MySQL/Folder)
Images...
Jamatus' Dynamic image store
Storing and handling image in mysql.
whats the best?
Image only hosts?
uploading and retriving images from mysql
putting image to mysql. "MySQL Gone Away"
Web Layer and Application Layer
Web and Application Layers
What MySQL Data type would you use to store a Webpage
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.