Jump to content

Simple Help For A Big Problem


leblanc

Recommended Posts

Hey All,

 

Im still relatively new to php and am trying to learn it at a solid pace, but I have been working with this existing script that automatically creates thumb nails of pictures in the same folder the script is in.

 

When the script generates the thumbnail image, it uses the existing name but adds a tn_ in front of the original name.

So for example

Original File:

"picturename.jpg"

Thumbnail created file

"tn_picturename.jpg"

 

My problem is, I want to get the extension of the thumbnail on the end of the original file name for example

"picturename_tn.jpg" <---- (the _tn is on at the end)

 

I have tried everything and cannot figure this out.

Its a pretty simple script as well.

 

Here is the script. I would really appreciate if someone could help me out on this small problem of mine.

 

 

<?php 
$imagefolder='.';
$thumbsfolder='.';
$pics=directory($imagefolder,"jpg,JPG,JPEG,jpeg,png,PNG");
$pics=ditchtn($pics,"tn_");
if ($pics[0]!="")
{
foreach ($pics as $p)
{
	createthumb($p,"tn_".$p,100,100);
}
}

/*
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;
}
?>


Link to comment
https://forums.phpfreaks.com/topic/177122-simple-help-for-a-big-problem/
Share on other sites

Not tested, but this should worK:

 

Change

<?php 
$imagefolder='.';
$thumbsfolder='.';
$pics=directory($imagefolder,"jpg,JPG,JPEG,jpeg,png,PNG");
$pics=ditchtn($pics,"tn_");
if ($pics[0]!="")
{
foreach ($pics as $p)
{
	createthumb($p,"tn_".$p,100,100);
}
}

 

to:

 

<?php 
$imagefolder='.';
$thumbsfolder='.';
$pics=directory($imagefolder,"jpg,JPG,JPEG,jpeg,png,PNG");
$pics=ditchtn($pics,"_tn");
if ($pics[0]!="")
{
foreach ($pics as $p)
{
	createthumb($p,$p."_tn",100,100);
}
}

 

You are only changing lines 4 and 9

John,

 

I have one more quick question i thought maybe u could look at.

 

When I have this script aka thumbs.php that we are working on in the folder with the images, it works fine as the first two lines of the code is

<?php 
$imagefolder='.';
$thumbsfolder='.';

 

 

When I put the thumb.php in a folder one up and try to call it from there, i get a million and one warnings

<?php 
$imagefolder='images/';
$thumbsfolder='images/'; 

 

I get these error messages when I try to call from a folder down from the thumbs.php.

 

 

Warning: imagecreatefromjpeg(1958bsa.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in /mnt/w1000/d25/s27/b0142d1b/www/gallery/thumb3.php on line 41

 

Warning: imagesx(): supplied argument is not a valid Image resource in /mnt/w1000/d25/s27/b0142d1b/www/gallery/thumb.php on line 43

 

Warning: imagesy(): supplied argument is not a valid Image resource in /mnt/w1000/d25/s27/b0142d1b/www/gallery/thumb.php on line 44

 

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /mnt/w1000/d25/s27/b0142d1b/www/gallery/thumb.php on line 61

 

Warning: imagedestroy(): supplied argument is not a valid Image resource in /mnt/w1000/d25/s27/b0142d1b/www/gallery/thumb3.php on line 69

 

 

Its actually pulling the images out of the folder "images/"

and creating the thumbnails in one folder up from it where the thumbs.php is

and giving me all those errors?

Does that make sense? LOL Thanks

 

Try:

 

$imagefolder='../images/';

$thumbsfolder='../images/';

 

The above probably might work. Below definitely will:

 

$imagefolder='/mnt/w1000/d25/s27/b0142d1b/www/images/';

$thumbsfolder='/mnt/w1000/d25/s27/b0142d1b/www/images/';

 

Or whatever the path is to this folder.

 

The path above will work if the images folder is stored in the web root (i.e www.yourwebsite.com/images)

 

If images is somewhere else, you will need to change this.

 

Let me know where the images folder is, and I can help.

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.