Jump to content


Photo

Old thumbnails are lingering in my file system,


  • Please log in to reply
8 replies to this topic

#1 alwaysinit

alwaysinit
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 25 October 2006 - 06:06 PM

Could some one show me how to go about unlinking, or replacing my user's old thumbnails. They're all sticking around taking up space.

I tried to do it as it is done for the original size image(original unlinks fine) in this script by adding:
<?php
if( $_SESSION['thumb'] != "nopic.jpg" ) { 
				unlink("shared_files/thumbs/".$_SESSION['thumb']);
				 
			}
?>
Which gives me the warning: "shared_files/thumbs/" Is a directory in /home/myname/mydomain/changephotoparse.php
So below is the code if I can impose upon you( I've commented where I tried to place the above code)
<?php 
session_start(); 
header("Cache-control: private"); 

include("db_connect.php"); 

function create_resize_save_jpeg( $path, $newpath ) {
	
	list( $width_orig, $height_orig ) = getimagesize( $path );
	$image_p = imagecreatetruecolor( 150, 150);
	if( !$image = imagecreatefromjpeg( $path ) ) {
		$_SESSION['errormsg'] = "Cant create photo!";
	} else {	
		imagecopyresampled($image_p, $image, 0, 0, 0, 0, 150, 150, $width_orig, $height_orig);
		imagejpeg($image_p, $newpath, 100);
	}
}

function create_resize_save_gif( $path, $newpath, $max = 150) 
{
	$w = $h = $max;
	list( $width_orig, $height_orig ) = getimagesize( $path );
	if($width_orig < $height_orig) $w = round(($h / $height_orig) * $width_orig);
	else $h = round(($w / $width_orig) * $height_orig);	
	$image_p = imagecreatetruecolor($w, $h);
	if( !$image = imagecreatefromgif( $path ) ) {
	    $_SESSION['errormsg'] = "Cant create photo!";
	} else {	
		imagecopyresampled($image_p, $image, 0, 0, 0, 0, $w, $h, $width_orig, $height_orig);
		imagegif($image_p, $newpath, 100);
	}
	imagedestroy($image_p);
	imagedestroy($image);	
}

function create_resize_save_png( $path, $newpath ) {
	
	list( $width_orig, $height_orig ) = getimagesize( $path );
	$image_p = imagecreatetruecolor( 150, 150);
	if( !$image = imagecreatefrompng( $path ) ) {
		$_SESSION['errormsg'] = "Cant create proto!";
	} else {	
		imagecopyresampled($image_p, $image, 0, 0, 0, 0, 150, 150, $width_orig, $height_orig);
		imagepng($image_p, $newpath, 100);
	}
}

function file_extention( $str ) {
	$size = strlen($str);
	$x=0;

	while($x < $size)
	{
		if($str[$x] == '.')
		{
			$ext = "";
			$ext_len = ($size - $x);
			$y=$x;
			while($y < $size)
			{
				$ext .= $str[$y];
				$y++;
			}
		}
		$x++;
	}
return $ext;
}

if( !$_SESSION['artname'] ) { 
	echo "You're not logged in!"; 
	include("login.html"); 
	exit(); 
} 

$artname = $_SESSION['artname']; 
$maxfilesize = 81920; 


if( !is_uploaded_file( $_FILES['userphoto']['tmp_name'] ) ) { 
	$error = "you didn't select a file to upload.<br />"; 
} else { 
	if( $_FILES['userphoto']['size'] > $maxfilesize ) { 
		$error = "your image file was too large.<br />"; 
		unlink($_FILES['userphoto']['tmp_name']); 
	} else { 
		if( !preg_match("/\.(gif|jpg|png)$/i", $_FILES['userphoto']['name'] ) ) {
			$error = "your file was an unacceptable type.<br />"; 
			unlink($_FILES['userphoto']['tmp_name']); 
			          
		} else { 
			$ext = file_extention( $_FILES['userphoto']['name'] );
			
			if( $_SESSION['photo'] != "nopic.jpg" ) { 
				unlink("shared_files/photos/".$_SESSION['photo']);
				 
			}
			// **********This is where I tried to pop that code in************
                                      // ****************************************************
			$newname = $_SESSION['artname'].$_FILES['userphoto']['name']; 
			
			if( !move_uploaded_file( $_FILES['userphoto']['tmp_name'], "shared_files/photos/".$newname ) ) {
				$error = "problems moving uploaded file.<br />";
			} else {
				$_SESSION['photo'] = $newname;
				
				if( $ext == ".jpg" ) {
					create_resize_save_jpeg("shared_files/photos/".$newname, "shared_files/thumbs/".$newname);
				} else if( $ext == ".gif" ) {
					create_resize_save_gif("shared_files/photos/".$newname, "shared_files/thumbs/".$newname);
				} else if( $ext == ".png" ) {
					create_resize_save_png("shared_files/photos/".$newname, "shared_files/thumbs/".$newname);
				}
				
				mysql_query("UPDATE peepsignup SET photo='$newname' WHERE artname='$artname'") or die (mysql_error()); 
            $_SESSION['photo'] = $newname;
			    mysql_query("UPDATE peepsignup SET thumb='$newname' WHERE artname='$artname'") or die (mysql_error()); 
            $_SESSION['photo'] = $newname;
			}
		} 
	} 
}

?> 
<html><head><title>Change Photo Result</title></head><body> 
<h1>Change Photo Result</h1> 
<?php 
if ($error) { 
    echo "Your photo could not be changed because ".$error."."; 
} else { 
    echo "Your photo was successfully uploaded.  To view your updated profile, <a href=\"memberarea.php\">click here</a>."; 
} 
?></body></html>
Thanks for any push in the right direction.

#2 ksteuber

ksteuber
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts
  • LocationCalifornia, USA

Posted 25 October 2006 - 06:11 PM

well if it is giving you that error message, it means that $_SESSION['thumb'] is not set. Maybe you want

<?php
if( $_SESSION['thumb'] != "nopic.jpg" && $_SESSION['thumb']!="") { 
				unlink("shared_files/thumbs/".$_SESSION['thumb']);
?>


#3 alwaysinit

alwaysinit
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 25 October 2006 - 06:23 PM

That fixed the warning, thanks

But It still won't replace the old thumb like it does the old original size, thumbs still lingering
I just can't see why, the original size gets swapped out just perfectly.
Thumbs don't swap out, they build up,lol. hmmmm

Any more ideas?

#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 October 2006 - 06:35 PM

I can't see where you've defined
$_SESSION['thumb']
?

My guess is that you've put the thumbs into a directory called thumbs, but given them the same name as the large image, in which case try this:

<?php
if( $_SESSION['photo'] != "nopic.jpg" && $_SESSION['photo']!="") { 
				unlink("shared_files/thumbs/".$_SESSION['photo']);
?>

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#5 alwaysinit

alwaysinit
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 25 October 2006 - 06:46 PM

I'll work it out, But you guys got me a lot closer a lot quicker, thanks

#6 alwaysinit

alwaysinit
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 25 October 2006 - 07:11 PM

Nope I was wrong, Whatever you guys did worked for me!

Now, could I push for a brief explanation?(what did this "&& $_SESSION['thumb']!="")" do ?

(Huggie, I defined $_SESSION['thumb'] in my first block of code up there)

Thank you, thank you, thank you
Another satisfied customer

#7 ksteuber

ksteuber
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts
  • LocationCalifornia, USA

Posted 25 October 2006 - 07:14 PM

I agree with HuggieBear, I dont see where you defined $_Session['thumb']

#8 alwaysinit

alwaysinit
  • Members
  • PipPipPip
  • Advanced Member
  • 60 posts

Posted 25 October 2006 - 07:16 PM

Ok, now I'm just confused about this thumb session bit, What do you guys mean?



#9 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 October 2006 - 10:51 PM

When I say define, I mean assign a value to it, something like this:

$_SESSION['thumb'] = "smallthumb.jpg";

Nowhere in your code do you assign anything to a session variable called 'thumb'.  So by looking at the code, I assumed you'd just copied one block that deleted the photo and changed the values to try and delete the thumbnails.  The theory is almost right, but the practice not quite, hence why I got you to change the name to
$_SESSION['photo']
as I know it existed, and you were calling your thumbs by the same name.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users