PHP Image upload and Resize problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
buknoii
Forum Newbie
Posts: 4
Joined: Fri Mar 14, 2008 5:49 am

PHP Image upload and Resize problem

Post by buknoii »

Hi there! I've been developing a simple website wherein users on my website can upload their picture, once it is uploaded it will be renamed according to time and its width resized to 470px.

I have been success working on it on my local computer but once I uploaded it on my webhosting service, the resizing script doesnt work.

Please check my code below

Code: Select all

<?php
    $userid=$_POST["userid"];
 
    include("dbconnect.php");   
 
    $result = mysql_query("select * from community_user_info  where user_xid='$userid' ") or
    die (mysql_error());
 
    while ($row = mysql_fetch_array($result))
        {
            echo $row["user_xid"];
            $user_id = $row["user_xid"];
            #echo "<br>-->".$user_id."<br>";
        }
    mysql_free_result($result);
?>
 
 
 
 
 
 
<?php
 
    $target_path = "photos/";
 
    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
 
 
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) 
        {
            echo "The file ".  basename( $_FILES['uploadedfile']['name']). " has been uploaded";
        } 
 
    else
 
        {
            echo "There was an error uploading the file, please try again!";
        }
 
?>
<br><br>
 
 
 
<?php
    
    
 
?>
 
 
 
 
<Br><br>
<?
$newfilename= date(("ydjGHs"));
$oldWD = getcwd();
$path="photos";
chdir($path);
rename("".basename( $_FILES['uploadedfile']['name'])."","".$user_id."-".$newfilename.".jpg");
echo "<input type='text' value='".$user_id."-".$newfilename.".jpg"."'><br>";
chdir($oldWD);
?>
<br><br>
 
<?php
 
echo $photo =  $user_id."-".$newfilename.".jpg";
 
chmod("photos/".$photo."", 0755);
 
 
 
mysql_query("INSERT INTO community_user_photo (user_id,user_photo)  VALUES ('$user_id','$photo') ") 
or die(mysql_error());  
 
 
?>
 
<h2>photo added</h2>
<br><Br><Br><Br><Br>
 
 
 
 
<?php
 
// File and new size
$filename = "photos/".$photo;
 
//echo($filename);
 
// Get new sizes
list($width, $height) = getimagesize($filename);
 
//figure out height ratios
 
$newwidth = 470;
$newheight = ($newwidth/$width) * $height;
 
echo "new height -->".$newheight."<br>";
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
 
echo "thumb -->".$thumb."<br>";
echo "thumb -->".$source."<br>";
 
// resize
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
 
// Save the image as 'mynewpic.jpg'
imagejpeg($thumb, ''.$filename,85);
 
 
// Free up memory
imagedestroy($thumb);
?> 
 
Any help or tips would be appreciated.
Post Reply