Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Thumbnail from a blob

Status
Not open for further replies.

Icelizard

Programmer
Feb 9, 2006
17
GB
OK, i am having a problem, i have got lots of images stored on my database, and i want to display them, easy enough, but how would i auto thumbnail then?

I can thumnail images, and i can retrieve images from database but how would i do them together?!

Cheers for any help
 
at its most basic level:

1. retrieve the image from the database.
2. store it in the filesystem (fopen + fwrite)
3. perform a thumbnail operation on the stored image
4. write the thumbnail to the db as a form of cache

then display whichever one you want and clean up the filesystem

i suspect that some graphics libraries do not need a file handle but the extra step is minimal.
 
The best way, first of all, is to only store a link of the image in the database (i.e. images/pic1.jpg instead of the binary data of the pic1.jpg)

then use this function:
Code:
//thanks to phpgarage.com
function ResizeImage($im,$maxwidth,$maxheight,$name){
	$width = imagesx($im);
	$height = imagesy($im);
	if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
		if($maxwidth && $width > $maxwidth){
			$widthratio = $maxwidth/$width;
			$RESIZEWIDTH=true;
		}
		if($maxheight && $height > $maxheight){
			$heightratio = $maxheight/$height;
			$RESIZEHEIGHT=true;
		}
		if($RESIZEWIDTH && $RESIZEHEIGHT){
			if($widthratio < $heightratio){
				$ratio = $widthratio;
			}else{
				$ratio = $heightratio;
			}
		}elseif($RESIZEWIDTH){
			$ratio = $widthratio;
		}elseif($RESIZEHEIGHT){
			$ratio = $heightratio;
		}
    	$newwidth = $width * $ratio;
        $newheight = $height * $ratio;
		if(function_exists("imagecopyresampled")){
      		$newim = imagecreatetruecolor($newwidth, $newheight);
      		imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
		}else{
			$newim = imagecreate($newwidth, $newheight);
      		imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
		}
        ImageJpeg ($newim,"thumb/".$name . ".jpg");
		ImageDestroy ($newim);
	}else{
		ImageJpeg ($im,"thumb/".$name . ".jpg");
	}
}

and this would be a basic call of the function and image upload. You'll need to do a bit of modifications.
Code:
if ($_FILES['image']['name'] == "") {
    echo "Please select a file to upload!\n";
    exit;
}
    $uploads = "uploads";
    $types_array = array("image/gif","image/pjpeg","image/x-png","image/jpeg","image/jpg",);
    $file1name = "$_POST[description].jpg";

if (!in_array($_FILES['image']['type'], $types_array)) {
    echo "That file type is not allowed!\n";
    exit;
}
  
    $max_filesize = 999999;

if ($_FILES['image']['size'] > $max_filesize) {
    echo "That file type is too large!\n";
    exit;
}
    $imagesize = getimagesize($_FILES['image']['tmp_name']);
    $imagewidth = $size[0];
    $imageheight = $size[1];
    
    $maxwidth = 250;
    $maxheight = 250;

if($imagewidth > $maxwidth || $imageheight > $maxheight) {
    echo "That file type is too large!\n";
    exit;
}





// Filename to store image as (no extension)
$FILENAME=$idmax;

// Width to resize image to (in pixels) 
$RESIZEWIDTH=100;

// Width to resize image to (in pixels) 
$RESIZEHEIGHT=100;

// DO NOT EDIT BELOW HERE -----------------------------------------

include("function.php");

if($_FILES['image']['size']){
	if($_FILES['image']['type'] == "image/pjpeg"){
		$im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
	}elseif($_FILES['image']['type'] == "image/x-png"){
		$im = imagecreatefrompng($_FILES['image']['tmp_name']);
	}elseif($_FILES['image']['type'] == "image/gif"){
		$im = imagecreatefromgif($_FILES['image']['tmp_name']);
	}elseif($_FILES['image']['type'] == "image/jpeg"){
		$im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
	}elseif($_FILES['image']['type'] == "image/jpg"){
		$im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
	}
	if($im){
		if(file_exists("$FILENAME.jpg")){
			unlink("$FILENAME.jpg");
		}
    	ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME);
    	
	}
}

move_uploaded_file($_FILES['image']['tmp_name'], "".$uploads."/".$idmax.".jpg") or die ("Couldn't upload file!");

Reality is built on a foundation of dreams.
 
Hello Icelizard,

Hey, I am working on tring to upload images into my own database. but I am having some issues, and I am not sure what kind of data I should be looking for in my database, once it is uploaded. So my question to you is either A) can you share your code with me or B) can you look at my posting under:
imryn (Programmer)11 replies18 Apr 06 (5 Apr 06)
Uploading images to mysql with php - issues

and give me some pointers - it would be greatly appreciated
Thanks,
imryn
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top