<?php
$imagefolder='./img/';
$thumbsfolder='./thumb_img/';
$pics=directory($imagefolder,"jpg,JPG,JPEG,jpeg,png,PNG");
$pics=ditchtn($pics,"tn_");
if ($pics[0]!="")
{
foreach ($pics as $p)
{
createthumb($p,"tn_".$p,150,150);
}
}
/*
Function ditchtn($arr,$thumbname)
filters out thumbnails
*/
function ditchtn($arr,$thumbname)
{
foreach ($arr as $item)
{
if (!preg_match("/^".$thumbname."/",$item)){$tmparr[]=$item;}
}
return $tmparr;
}
/*
Function createthumb($name,$filename,$new_w,$new_h)
creates a resized image
variables:
$name Original filename
$filename Filename of the resized image
$new_w width of resized image
$new_h height of resized image
*/
function createthumb($name,$filename,$new_w,$new_h)
{
$system=explode(".",$name);
if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y)
{
$thumb_w=$new_w;
$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y)
{
$thumb_w=$old_x*($new_w/$old_y);
$thumb_h=$new_h;
}
if ($old_x == $old_y)
{
$thumb_w=$new_w;
$thumb_h=$new_h;
}
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
if (preg_match("/png/",$system[1]))
{
imagepng($dst_img,$filename);
} else {
imagejpeg($dst_img,$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}
/*
Function directory($directory,$filters)
reads the content of $directory, takes the files that apply to $filter
and returns an array of the filenames.
You can specify which files to read, for example
$files = directory(".","jpg,gif");
gets all jpg and gif files in this directory.
$files = directory(".","all");
gets all files.
*/
function directory($dir,$filters)
{
$handle=opendir($dir);
$files=array();
if ($filters == "all"){while(($file = readdir($handle))!==false){$files[] = $file;}}
if ($filters != "all")
{
$filters=explode(",",$filters);
while (($file = readdir($handle))!==false)
{
for ($f=0;$f<sizeof($filters);$f++):
$system=explode(".",$file);
if ($system[1] == $filters[$f]){$files[] = $file;}
endfor;
}
}
closedir($handle);
return $files;
}
?>
Last edited by Dopple; 03-27-2006 at 08:11 AM.
Reason: Resolved
Really what I'm after is for it to take any image after uploading and resize it. I found your example on this thread but couldn't work out how to get that working either (no offence, I'm still generally new to the PHP game). If you think your one there would be better, let me know and I'll post my question on that thread if you'd be so good as to explain it a bit for me? (just so that it's on the appropriate thread)
Thanks
Let's continue here now you have started, rather than drag an old thread out of the archive. Using that function may not be the best thing for what you want. Because it produces a scaled down version of the source image the images can be all different sizes and shapes and this might not be a good thing for your html/css layout. This is demonstrate well by this example which used a function like that to build the thumbnails. I recently wrote a function to get round that problem and it was used to create the thumbnails at this page which are all exactly the same size. This allowed a completely liquid layout of the thumbnails and captions using only CSS and no tables. As you will see, when the page is resized the number of columns changes to fit the page width, something completely impossible with tables.
I can help you produce either of these styles or something different but you need to be very specific about your needs.
It would be the second style that I would be after. What I have is one folder (thumb_img) for the thumbnails and one (img) for the actual images. What I would like, and I'm not sure if this is the most logical and space friendly way, is to have the user upload the image to the folder "img" which also inserts the details (name, caption, filename) of the upload into a mysql database (I have this part working already) and during the upload I would like the thumb to be resized to roughly the size that the thumbs are on the second link you posted and then saved to the "thumb_img" folder. The thumbs would be the same name as the img so that I could use the mysql to display the contents of both folders. To display I would just output them in one long line, allowing css to space them. I should be ok to get the output working on my own. I already have the page displaying the full sized images.
Put that in a file and require() when you want to use the function. If you can't figure out how to use it consult the examples in post #6 of this thread. It you still can't figure it out come back and I will write something specific for you.
Gotcha. Well the weekends just started and I fancy a cold one. Going to see V for Vendetta tonight. I'll try that out on Sunday and if I have any problems I'll let you know.
Much appreciated Bokeh.
Ok I'm calling the function like so. $imgname is just the file name. I am getting the directory permission error though. Both folders, img and thumb_img have 755 permissions, which is fine for just uploading the image. Should the thumbnail directory be 777? Also does it look like I'm declaring the variables correctly?
PHP Code:
# Image location... (Can be HTTP or file path)
$image_location = '../img/' . $imgname;
Bookmarks