Jump to content

Recommended Posts

I would like to write a php script that will take the pictures in a folder and display them in the way I specify. For instance, having a page with thumbnails at the bottom and a larger picture right above that, and if you click on a thumbnail it will display that picture in the larger space right above it. I could probably do this in HTML but then each picture would have to be coded in manually. I want to just be able to throw a bunch of pictures in a directory and have them display as thumbnails automatically.

Does that make sense?

 

Any ideas on how to do this? Will I need a SQL database?

Link to comment
https://forums.phpfreaks.com/topic/62506-grab-images/
Share on other sites

you could do it using an SQL database, and that would probably be the preferred method considering that you can store more than just the file name as an attribute, and it makes pagination a whole lot easier.

 

anyway, to do thumbnails you will need a php resizer script.  fortunately i have one that works for JPEG files only.

<?php
header("Content-type: image/jpeg");
//process the desired image size
//usage: showJpg.php?image=(desiredImage)
$image =  $_GET['image'];
$size  =   $_GET['size'];
$resample = $_GET['res'];
$quality = $_GET['qual'];
$src   = @imagecreatefromjpeg($image);

if ($src) {
	list($width, $height) = getimagesize($image);
	if ($width > $height) {
		$factor = ($size / $width);
		$newW = $size; 
		$newH = $height * $factor;
	}else {
		$factor = ($size / $height);
		$newW = $width * $factor; 
		$newH = $size;
	}

	$thumb = imagecreatetruecolor($newW, $newH);
	if ($resample == false) {
		imagecopyresized($thumb, $src, 0, 0, 0, 0, $newW, $newH, $width, $height);
	}else{	
		imagecopyresampled($thumb, $src, 0, 0, 0, 0, $newW, $newH, $width, $height);
	}
}else{
	$thumb = imagecreatetruecolor(128, 128);
	$clr_grey = imagecolorallocate($thumb, 64,64,64);
	$clr_red  = imagecolorallocate($thumb, 255, 32, 0);
	imagefilledrectangle($thumb, 10, 10, 115, 90, $clr_grey);
	imagestring($thumb, 5, 35, 40, "Error_", $clr_red);
}
imagejpeg($thumb, '', $quality);

//destroy the image
imagedestroy($thumb);
imagedestroy($src);
?>

 

if you want to browse through a directory of images, this loop may help:

<?php
$dir = opendir("path/to/picture/library");
while (false !== ($file = readdir($dir))) {
//TODO: handle files
}
closedir($dir);

Link to comment
https://forums.phpfreaks.com/topic/62506-grab-images/#findComment-311116
Share on other sites

Thank you for your help. I'll hack away at it and if I have any problems I'll probably come and ask for help.  ;)

If I was using javascript to bring up photos on the same page, would I write a javascipt function and call it from my php script? Sorry if that sounds thick headed I just haven't integrated javascript in my websites very much.

 

As far as using SQL for this, does anyone have a tutorial on what i would need to do, I've looked around and couldn't find anything on what i want to do here.

 

Thanks for the help.

Link to comment
https://forums.phpfreaks.com/topic/62506-grab-images/#findComment-311810
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.