Forum Moderators: coopster
[phpclasses.org...]
dc
<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
$target_path = "";
$target_path = $target_path . $filename;
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "You have successfully added a photo for Item # ".$row_Upload['Room'];
} else{
echo "There was an error uploading the file, or you have not yet selected a file to upload...please try again!";
}
?>
Where should I place code that will resize the image?
1. upload image and echo message
2. IF upload was succesfull, I call a function that resizes the image in the upload dir and inside the same function, the original is deleted
<?
function func_makethumb_avatar_upload($uid){
//describe dynamic image resize function for uploading user avatars and delete original
ob_start(); //output buffering
$thumb_quality = 80; // JPEG image quality (0-100) for thumbnails
$thumb_width = 80; // resized images will have this width
$req_dir = getcwd();// get current working directory
chdir('/home/public_html/');//this is just here so there is no need for those pesky relative URLs ( ../../ ), set it to your base directory, the directory your html files are in
$avatar_url = "user/avatars/$uid.jpg";//path to image
if (file_exists($avatar_url)){
list($width, $height, $type, $attr) = getimagesize($avatar_url); //...get its size and stuff like that and...
if ($width!= $thumb_width){ //...if the thumbnail is not the same size as $thumb_width we need to resize it so...
$image_handle = imagecreatefromjpeg($avatar_url); //
$thumb_height = round(($thumb_width / $width) * $height); //resizing etc
$thumbnail = imagecreatetruecolor($thumb_width, $thumb_height) or $thumbnail = imagecreate($thumb_width, $thumb_height);
imagecopyresampled($thumbnail, $image_handle, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height); //resample the original onto $thumbnail (which is a blank image)
unlink("user/avatars/$uid.jpg");//now delete the original
imagejpeg($thumbnail, $avatar_url, $thumb_quality); //actually create a jpg from $thumbnail, store it in location $avatar_url, with quality $thumb_quality
imagedestroy($image_handle); //clear buffer #*$!
imagedestroy($thumbnail);
ob_end_flush();//end output buffering
}
}
chdir($req_dir); //change back to original working directory
}//end function?>
you can extend the function even further, with 2 more variables you can pass it the path and image name so you dont have to hardcode it into the function; maximum flexibility ;-)
[edited by: dmmh at 10:35 pm (utc) on Jan. 4, 2006]
[edited by: tedster at 11:11 pm (utc) on Jan. 4, 2006]
[edit reason] fix side scroll [/edit]
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
--> HERE <--
echo "You have successfully added a photo for Item # ".$row_Upload['Room'];
} else{
echo "There was an error uploading the file, or you have not yet selected a file to upload...please try again!";
}
Or somewhere else in the code?
<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
$target_path = "";
$target_path = $target_path . $filename;
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
function func_makethumb_avatar_upload($uid){
//describe dynamic image resize function for uploading user avatars and delete original
ob_start(); //output buffering
$thumb_quality = 60; // JPEG image quality (0-100) for thumbnails
$thumb_width = 400; // resized images will have this width
$req_dir = getcwd();// get current working directory
$avatar_url = $filename;//path to image
if (file_exists($avatar_url)){
list($width, $height, $type, $attr) = getimagesize($avatar_url); //...get its size and stuff like that and...
if ($width!= $thumb_width){ //...if the thumbnail is not the same size as $thumb_width we need to resize it so...
$image_handle = imagecreatefromjpeg($avatar_url); //
$thumb_height = round(($thumb_width / $width) * $height); //resizing etc
$thumbnail = imagecreatetruecolor($thumb_width, $thumb_height) or $thumbnail = imagecreate($thumb_width, $thumb_height);
imagecopyresampled($thumbnail, $image_handle, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height); //resample the original onto $thumbnail (which is a blank image)
unlink("user/avatars/$uid.jpg");//now delete the original
imagejpeg($thumbnail, $avatar_url, $thumb_quality); //actually create a jpg from $thumbnail, store it in location $avatar_url, with quality $thumb_quality
imagedestroy($image_handle); //clear buffer #*$!
imagedestroy($thumbnail);
ob_end_flush();//end output buffering
}
}
}
echo "You have successfully added a photo for Item # ".$row_Upload['Room']. ". You have stored ".$filename;
} else{
echo "There was an error uploading the file, or you have not yet selected a file to upload...please try again!";
}
?>
<center>
<p><font color=red>
<?=$_REQUEST[message]?>
</font> </p>
<p> </p>
<p><br>
You are currently editing Item # <?php echo $row_Upload['Room'];?></p>
<p>
</p>
<form name="upload" id="upload" ENCTYPE="multipart/form-data" method="post">
<p>
<input name="filename" type="radio" value="<?php echo $row_Upload['Photo1'];?>">
Photo1
<br>
<input name="filename" type="radio" value="<?php echo $row_Upload['Photo2'];?>">
Photo2 <br>
<input name="filename" type="radio" value="<?php echo $row_Upload['Photo3'];?>">
Photo3 <br>
<label> </label>
</p>
<p>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
Choose a file to upload:
<input name="uploadedfile" type="file" />
<br />
<input type="submit" value="Upload File" />
</p>
</form>
<p> </p>
<p><a href="roomadmin.php">RETURN TO ADMIN</a></p>
<p> </p>
</center>
<HEAD>
<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
<?php
mysql_free_result($Upload);
?>
But this doesn't work...it uploads the image, but there is no resize. I have verified that GD is on the server...what it the problem?
Your biggest flaw is you are indeed using the function, but you are merely defining it, not calling it (executing it). Generally, you define the functions in a seperate page and call them when you need them.
one defines a function like:
function func_name($var1){
//lots of code here
}
you call that same function (execute it) by doing:
func_name($array_of_whatever);
so, you need to do it like this:
$target_path = $target_path . $filename;
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
func_make_thumb_avatar_upload($target_path);
}
thats it, then you echo the rest of the page
adjust the function to the stuff below and put it in a external file:
function func_makethumb_avatar_upload($filename){
//describe dynamic image resize function for uploading user avatars and delete original
ob_start(); //output buffering
$thumb_quality = 60; // JPEG image quality (0-100) for thumbnails
$thumb_width = 400; // resized images will have this width
$req_dir = getcwd();// get current working directory
$avatar_url = $filename;//path to image
if (file_exists($avatar_url)){
list($width, $height, $type, $attr) = getimagesize($avatar_url); //...get its size and stuff like that and...
if ($width!= $thumb_width){ //...if the thumbnail is not the same size as $thumb_width we need to resize it so...
$image_handle = imagecreatefromjpeg($avatar_url); //
$thumb_height = round(($thumb_width / $width) * $height); //resizing etc
$thumbnail = imagecreatetruecolor($thumb_width, $thumb_height) or $thumbnail = imagecreate($thumb_width, $thumb_height);
imagecopyresampled($thumbnail, $image_handle, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height); //resample the original onto $thumbnail (which is a blank image)
unlink($avatar_url);//now delete the original
imagejpeg($thumbnail, $avatar_url, $thumb_quality); //actually create a jpg from $thumbnail, store it in location $avatar_url, with quality $thumb_quality
imagedestroy($image_handle); //clear buffer #*$!
imagedestroy($thumbnail);
ob_end_flush();//end output buffering
}
}
Is there code I can add that just does the resize to $filename?
I have tried this:
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $filename)) {
list($width, $hieght) = getimagesize($filename);
imagecopyresampled($filename, $filename, 0, 0, 0, 0, 400, 300, $width, $height);
echo "You have successfully added a photo for Item # ".$row_Upload['Room']. ". You have stored ".$filename;
} else{
echo "There was an error uploading the file, or you have not yet selected a file to upload...please try again!";
}
The file is successfully uploaded, but the resize doesn't work.
} else{
echo "There was an error uploading the file, or you have not yet selected a file to upload...please try again!";
}
typo dude, u used 'hieght' instead of 'height'
but your better of with the code I posted above
seriously, you need to start thinking scalability...scalability ;-)
but hey, whatever rocks your boat