# MYSQL image database

**URL:** https://forum.kirupa.com/t/mysql-image-database/185381
**Category:** Uncategorized
**Created:** [April 12, 2006, 10:28pm UTC](https://forum.kirupa.com/t/mysql-image-database/185381 "2006-04-12T22:28:12Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![The\_Vulcan](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/the_vulcan/32/145_2.png) [@The\_Vulcan](https://forum.kirupa.com/u/The_Vulcan)
#### Post date: [April 12, 2006, 10:28pm UTC](https://forum.kirupa.com/t/mysql-image-database/185381/1 "2006-04-12T22:28:12Z")

</div>

Simple question.

[SIZE=3]\*\* If you are creating a large image database do you:\*\*[/SIZE]

**A.** Upload the image into MYSQL ?

\*\*B. \*\*Upload the image to a folder and store only the address in MYSQL ?

\*\*A=\> \*\*MYSQL does have a table size limit. (though perhpas larger than I need to worry about)

\*\*A=\> \*\*Will not have to worry about images with the same name as you would not be using that as the key.

\*\*B=\> \*\*Will save MYSQL from having to pull images from database.

\*\*B=\> \*\*Will have to save each image with a different name.

Ok these are my first and only thoughts on the subject. Was hoping someone knows which is the better method and why ?

Any tips much appreciated.

---

<div class="post-metadata">

### Author: ![hl1](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hl1/32/849_2.png) [@hl1](https://forum.kirupa.com/u/hl1)
#### Post date: [April 12, 2006, 10:42pm UTC](https://forum.kirupa.com/t/mysql-image-database/185381/2 "2006-04-12T22:42:37Z")

</div>

You can normally compress it a bit if you store it in the database using some encryption algorithm. It’ll take a tiny bit less space on your server, and accessing will be a bit more dynamic.

It might take some extra code, but it can be worth it.

---

<div class="post-metadata">

### Author: ![Jeff\_Wheeler](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@Jeff\_Wheeler](https://forum.kirupa.com/u/Jeff_Wheeler)
#### Post date: [April 12, 2006, 10:50pm UTC](https://forum.kirupa.com/t/mysql-image-database/185381/3 "2006-04-12T22:50:09Z")

</div>

I would definitely suggest files and folders with links in the db. It’s much cleaner in my opinion, and really, the naming files differently can be handled in php, and probably most any other language, automatically (temp names are given, and I’m sure you can do something with this).

It just seems much better…

---

<div class="post-metadata">

### Author: ![The\_Vulcan](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/the_vulcan/32/145_2.png) [@The\_Vulcan](https://forum.kirupa.com/u/The_Vulcan)
#### Post date: [April 12, 2006, 10:56pm UTC](https://forum.kirupa.com/t/mysql-image-database/185381/4 "2006-04-12T22:56:22Z")

</div>

Not that it makes much difference but I am also going to have to resize and create thumbnails, and perhaps water mark each image with PHP before I either store it in MYSQL or save it in a folder.

---

<div class="post-metadata">

### Author: ![JoshuaJonah](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/joshuajonah/32/1775_2.png) [@JoshuaJonah](https://forum.kirupa.com/u/JoshuaJonah)
#### Post date: [April 12, 2006, 10:58pm UTC](https://forum.kirupa.com/t/mysql-image-database/185381/5 "2006-04-12T22:58:33Z")

</div>

It actually really slow to keep binary in your database, I do not recommend doing it. I would definately recommend you store the images on your server and put a path fieled in your database. It’s a little hard to keep track of, but it’s much faster.

Edit: after reading your last post i should tell you, i am really familiar with doing this, i even have a some code for you to play with:

```php
<?php

	$MAXIMUM_FILESIZE = 1024 * 1500; // 200KB
	$MAXIMUM_FILE_COUNT = 1000; // keep maximum 1000 files on server
	$filename = $_GET['filename'];
	$galleryname = $_GET['gallery'];
	
	if ($_FILES['Filedata']['size'] <= $MAXIMUM_FILESIZE) {
		unset($_FILES['Filedata']['name']);
		$_FILES['Filedata']['name'] = $filename.".jpg";
		mkdir("./gallery/".$galleryname."/");
		mkdir("./gallery/".$galleryname."/full/");
		mkdir("./gallery/".$galleryname."/thumbs/");
		move_uploaded_file($_FILES['Filedata']['tmp_name'], "./temporary/".$_FILES['Filedata']['name']);
		rename("./temporary/".$_FILES['Filedata']['name'], "./gallery/".$galleryname."/full/".$_FILES['Filedata']['name']);		
	};
		$fullsize = "./gallery/".$galleryname."/full/".$_FILES['Filedata']['name'];
		$thumbsize = "./gallery/".$galleryname."/thumbs/".$_FILES['Filedata']['name'];
		// The file
		$filein = $fullsize; // File in
		$fileout = $thumbsize; // Fileout - optional 
		
		$imagethumbsize_w = 95; // thumbnail size (area cropped in middle of image)
		$imagethumbsize_h = 64; // thumbnail size (area cropped in middle of image)
		resize_then_crop( $filein,$fileout,$imagethumbsize_w,
		$imagethumbsize_h,/*rgb*/"255","255","255");
		
		function resize_then_crop( $filein,$fileout,$imagethumbsize_w,$imagethumbsize_h,$red,$green,$blue){
		
		// Get new dimensions
		list($width, $height) = getimagesize($filein);
		$new_width = $width * $percent;
		$new_height = $height * $percent;
		
		   if(preg_match("/.jpg/i", "$filein"))
		   {
			   $format = 'image/jpeg';
		   };
		   if (preg_match("/.gif/i", "$filein"))
		   {
			   $format = 'image/gif';
		   };
		   if(preg_match("/.png/i", "$filein"))
		   {
			   $format = 'image/png';
		   };
		   
			   switch($format)
			   {
				   case 'image/jpeg':
				   $image = imagecreatefromjpeg($filein);
				   break;
				   case 'image/gif';
				   $image = imagecreatefromgif($filein);
				   break;
				   case 'image/png':
				   $image = imagecreatefrompng($filein);
				   break;
			   };
		
		$width = $imagethumbsize_w ;
		$height = $imagethumbsize_h ;
		list($width_orig, $height_orig) = getimagesize($filein);
		
		if ($width_orig < $height_orig) {
		  $height = ($imagethumbsize_w / $width_orig) * $height_orig;
		} else {
		   $width = ($imagethumbsize_h / $height_orig) * $width_orig;
		};
		
		if ($width < $imagethumbsize_w)
		//if the width is smaller than supplied thumbnail size 
		{
		$width = $imagethumbsize_w;
		$height = ($imagethumbsize_w/ $width_orig) * $height_orig;;
		};
		
		if ($height < $imagethumbsize_h)
		//if the height is smaller than supplied thumbnail size 
		{
		$height = $imagethumbsize_h;
		$width = ($imagethumbsize_h / $height_orig) * $width_orig;
		}
		
		$thumb = imagecreatetruecolor($width , $height);  
		$bgcolor = imagecolorallocate($thumb, $red, $green, $blue);  
		ImageFilledRectangle($thumb, 0, 0, $width, $height, $bgcolor);
		imagealphablending($thumb, true);
		
		imagecopyresampled($thumb, $image, 0, 0, 0, 0,
		$width, $height, $width_orig, $height_orig);
		$thumb2 = imagecreatetruecolor($imagethumbsize_w , $imagethumbsize_h);
		// true color for best quality
		$bgcolor = imagecolorallocate($thumb2, $red, $green, $blue);  
		ImageFilledRectangle($thumb2, 0, 0,
		$imagethumbsize_w , $imagethumbsize_h , $white);
		imagealphablending($thumb2, true);
		
		$w1 =($width/2) - ($imagethumbsize_w/2);
		$h1 = ($height/2) - ($imagethumbsize_h/2);
		
		imagecopyresampled($thumb2, $thumb, 0,0, $w1, $h1,
		$imagethumbsize_w , $imagethumbsize_h ,$imagethumbsize_w, $imagethumbsize_h);
		
		// Output
		//header('Content-type: image/gif');
		//imagegif($thumb); //output to browser first image when testing
		
		if ($fileout !="")imagegif($thumb2, $fileout); //write to file
		header('Content-type: image/gif');
		imagegif($thumb2); //output to browser
		};
//------start thumbnailer
   $thumbsize=300;
   $imgfile = $fullsize;
   header('Content-type: image/jpeg');
   list($width, $height) = getimagesize($imgfile);
   $imgratio=$width/$height;
   if ($imgratio>1){
     $newwidth = $thumbsize;
     $newheight = $thumbsize/$imgratio;}
   else{
     $newheight = $thumbsize;
     $newwidth = $thumbsize*$imgratio;}
   $thumb = ImageCreateTrueColor($newwidth,$newheight);
   $source = imagecreatefromjpeg($imgfile);
   imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb,$fullsize,100);
?>

```

---

<div class="post-metadata">

### Author: ![bwh2](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@bwh2](https://forum.kirupa.com/u/bwh2)
#### Post date: [April 12, 2006, 11:01pm UTC](https://forum.kirupa.com/t/mysql-image-database/185381/6 "2006-04-12T23:01:48Z")

</div>

i’ve never stored images in a mysql db. seemed a bit heavy and dirty. anyone know what the path looks like when you store an image in a db (ie when you right click on the image and goto properties)?

---

<div class="post-metadata">

### Author: ![The\_Vulcan](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/the_vulcan/32/145_2.png) [@The\_Vulcan](https://forum.kirupa.com/u/The_Vulcan)
#### Post date: [April 12, 2006, 11:14pm UTC](https://forum.kirupa.com/t/mysql-image-database/185381/7 "2006-04-12T23:14:22Z")

</div>

Thanks for the Posts guys.

And thanks for that piece of code **Defective** I will definitely try to put it to good use.

If anyone else has anything to add to the subject please do.

---

<div class="post-metadata">

### Author: ![Jeff\_Wheeler](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@Jeff\_Wheeler](https://forum.kirupa.com/u/Jeff_Wheeler)
#### Post date: [April 13, 2006, 12:11am UTC](https://forum.kirupa.com/t/mysql-image-database/185381/8 "2006-04-13T00:11:57Z")

</div>

> [@Defective](#):
>
> It actually really slow to keep binary in your database, I do not recommend doing it. I would definately recommend you store the images on your server and put a path fieled in your database. It’s a little hard to keep track of, but it’s much faster.

It seemed like something like that should be true… I just couldn’t say it definitively. :thumb:

---

<div class="post-metadata">

### Author: ![Sniper\_Jo](https://avatars.discourse-cdn.com/v4/letter/s/fbc32d/32.png) [@Sniper\_Jo](https://forum.kirupa.com/u/Sniper_Jo)
#### Post date: [April 13, 2006, 12:04pm UTC](https://forum.kirupa.com/t/mysql-image-database/185381/9 "2006-04-13T12:04:46Z")

</div>

i once used mysql to store images, never again its just a pain in the \*\*\* i would always use paths

---

<div class="post-metadata">

### Author: ![The\_Vulcan](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/the_vulcan/32/145_2.png) [@The\_Vulcan](https://forum.kirupa.com/u/The_Vulcan)
#### Post date: [April 15, 2006, 1:31am UTC](https://forum.kirupa.com/t/mysql-image-database/185381/10 "2006-04-15T01:31:05Z")

</div>

Well I think that the votes are in and Paths are the way.

---

<div class="post-metadata">

### Author: ![hl1](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hl1/32/849_2.png) [@hl1](https://forum.kirupa.com/u/hl1)
#### Post date: [April 15, 2006, 3:13am UTC](https://forum.kirupa.com/t/mysql-image-database/185381/11 "2006-04-15T03:13:11Z")

</div>

> [@The\_Vulcan](#):
>
> Well I think that the votes are in and Paths are the way.

Of course, the fun way is to store the image in the database 🙂

(Anything that requires more PHP is fun :D)
