Jump to content

Looking for help understanding script behavior


alwaysinit

Recommended Posts

Below is a script that saves a user's pic to MySQL and to my file system. It also creates a thumbnail and does the same for it. The original size pic gets unlinked and updated flawlessly every time.

Problem is that the thumbnail 'unlink' and 'update' is not functioning as well as the other. I upload a pic, it creates and saves it fine 1st time. Then I go to upload a 2nd time, and it unlinks the thumb altogether(no replace). Then on the 3rd attempt it works fine(with warning bout no such file or directory). The 4th attempt unlinks the thumb altogether again. And it goes on in that cycle.

I hope somebody can take a sec to find where the problem is in the code. I've been toying with it for two days now and cannot fix it.
Thanks for your time and knowledge.

[code]<?php
session_start();
header("Cache-control: private");

include("db_connect.php");
function create_resize_save_jpeg( $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 = imagecreatefromjpeg( $path ) ) {
    $_SESSION['errormsg'] = "Cant create photo!";
} else {
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $w, $h, $width_orig, $height_orig);
imagejpeg($image_p, $newpath, 100);
}
imagedestroy($image_p);
imagedestroy($image);
}

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, $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 = imagecreatefrompng( $path ) ) {
    $_SESSION['errormsg'] = "Cant create photo!";
} else {
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $w, $h, $width_orig, $height_orig);
imagepng($image_p, $newpath, 100);
}
imagedestroy($image_p);
imagedestroy($image);
}

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['id'] ) {
echo "You're not logged in!";
include("doh.html");
exit();
}

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

// check if there was a file uploaded
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']);
// if it's there, an okay size and type, copy to server and update the photo value in SQL         
} else {
$ext = file_extention( $_FILES['userphoto']['name'] );

if( $_SESSION['pic_1'] != "nopic.jpg" && $_SESSION['pic_1']!="") {
unlink("user_files/picture1/".$_SESSION['pic_1']);

}

            if( $_SESSION['thumb_1'] != "nopic.jpg" && $_SESSION['thumb_1']!="") {
unlink("user_files/thumbnail1/".$_SESSION['thumb_1']);


}
$newname = $_SESSION['id'].$_FILES['userphoto']['name'];

if( !move_uploaded_file( $_FILES['userphoto']['tmp_name'], "user_files/picture1/".$newname ) ) {
$error = "problems moving uploaded file.<br />";
}
else {
$_SESSION['pic_1'] = $newname;


if( $ext == ".jpg" ) {
create_resize_save_jpeg("user_files/picture1/".$newname, "user_files/thumbnail1/".$newname);
} else if( $ext == ".gif" ) {
create_resize_save_gif("user_files/picture1/".$newname, "user_files/thumbnail1/".$newname);
} else if( $ext == ".png" ) {
create_resize_save_png("user_files/picture1/".$newname, "user_files/thumbnail1/".$newname);
}

mysql_query("UPDATE users SET pic_1='$newname' WHERE id='$id'") or die (mysql_error());
            $_SESSION['pic_1'] = $newname;
    mysql_query("UPDATE users SET thumb_1='$newname' WHERE id='$id'") or die (mysql_error());
            $_SESSION['thumb_1'] = $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=\"changephoto.php\">click here</a>.";
}
?></body></html>[/code]
Link to comment
Share on other sites

Nah, I don't need the unlink. Any way to copy over the old file would work. I'm new to coding, put that script together from other scripts I've found on the net. So I don't really understand where the unlink is failing me. If you have a way to copy over or delete old thumb from MySQL and file system to replace with the new one, I would love to see it. So I can use it instead.

thanks for the reply drifter, and takin' time to look it over. :D
Link to comment
Share on other sites

really if you create a TN and then create a new on with the same name, it will just over write. You may also want to check out phpclasses.org - they have a lot of TN classes - they all come with examples - will make it a lot easier by getting all that messy code out of there.

They are usually set up so you just call something like

$s->new thumbnailer();
$s->('orig.jpp','location/');

That is it and you are done.

Just a hint -you may have luck there. I do really have an code on hand for what you want since it is a very specialized for a specific goal
Link to comment
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.