adamjblakey
Nov 10th 2008, 5:46 am
Hi,
I am having a problem with using the FTP function within php while resizing the image.
I am using the following for uploading the image:
function ftpupload($file, $img_tmp, $path){
$message = '';
if ($ftp = ftp_connect($_SESSION['ddomain'])) {
if (ftp_login($ftp, $_SESSION['dname'], $_SESSION['dpass'])) {
ftp_pasv($ftp, true);
if (ftp_put($ftp, $path . $file,
$img_tmp, FTP_BINARY)) {
$message = "File uploaded";
} else {
die("Could not upload file");
}
} else {
die("Could not login to FTP account");
}
} else {
die("Could not connect to FTP server");
}
}
Now this works fine when you pass it details for ['name'] and ['tmp_name'] but i want to use this with a image re-sizing/re-naming function which is:
function uploadimage($value){
$file_type = $value['type'];
$file_name = $value['name'];
$file_size = $value['size'];
$file_tmp = $value['tmp_name'];
//check file extension
$ext = strrchr($file_name,'.');
$ext = strtolower($ext);
if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
echo "Wrong file extension. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
exit();
}
//get the file extension.
$getExt = explode ('.', $file_name);
$file_ext = $getExt[count($getExt)-1];
//create a random file name
$rand_name = md5(time());
$rand_name= rand(0,999999999);
//get the new width variable.
$ThumbWidth = 175;
$ImgWidth = 700;
//keep image type
if($file_size){
if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
$new_img = imagecreatefromjpeg($file_tmp);
}elseif($file_type == "image/x-png" || $file_type == "image/png"){
$new_img = imagecreatefrompng($file_tmp);
}elseif($file_type == "image/gif"){
$new_img = imagecreatefromgif($file_tmp);
}
//list width and height and keep height ratio.
list($width, $height) = getimagesize($file_tmp);
$imgratio=$width/$height;
if ($imgratio>1){
$newwidth = $ThumbWidth;
$newheight = $ThumbWidth/$imgratio;
$newwidth2 = $ImgWidth;
$newheight2 = $ImgWidth/$imgratio;
}else{
$newheight = $ThumbWidth;
$newwidth = $ThumbWidth*$imgratio;
$newheight2 = $ImgWidth;
$newwidth2 = $ImgWidth*$imgratio;
}
$resized_img = imagecreatetruecolor($newwidth, $newheight);
$resized_img2 = imagecreatetruecolor($newwidth2, $newheight2);
imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagecopyresized($resized_img2, $new_img, 0, 0, 0, 0, $newwidth2, $newheight2, $width, $height);
//save image
ftpupload($file_name, $file_tmp, "httpdocs/uploads/images/thumbs/");
ftpupload($file_name, $file_tmp, "httpdocs/uploads/images/big/");
imagedestroy($resized_img);
imagedestroy($resized_img2);
imagedestroy($new_img);
}
//$value = "$rand_name.$file_ext";
$value = $file_name;
return $value;
}
At the moment this is all working fine and sends the image to the but just does not resize or rename. How can i get this to work how i want it to?
Cheers,
Adam
I am having a problem with using the FTP function within php while resizing the image.
I am using the following for uploading the image:
function ftpupload($file, $img_tmp, $path){
$message = '';
if ($ftp = ftp_connect($_SESSION['ddomain'])) {
if (ftp_login($ftp, $_SESSION['dname'], $_SESSION['dpass'])) {
ftp_pasv($ftp, true);
if (ftp_put($ftp, $path . $file,
$img_tmp, FTP_BINARY)) {
$message = "File uploaded";
} else {
die("Could not upload file");
}
} else {
die("Could not login to FTP account");
}
} else {
die("Could not connect to FTP server");
}
}
Now this works fine when you pass it details for ['name'] and ['tmp_name'] but i want to use this with a image re-sizing/re-naming function which is:
function uploadimage($value){
$file_type = $value['type'];
$file_name = $value['name'];
$file_size = $value['size'];
$file_tmp = $value['tmp_name'];
//check file extension
$ext = strrchr($file_name,'.');
$ext = strtolower($ext);
if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
echo "Wrong file extension. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
exit();
}
//get the file extension.
$getExt = explode ('.', $file_name);
$file_ext = $getExt[count($getExt)-1];
//create a random file name
$rand_name = md5(time());
$rand_name= rand(0,999999999);
//get the new width variable.
$ThumbWidth = 175;
$ImgWidth = 700;
//keep image type
if($file_size){
if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
$new_img = imagecreatefromjpeg($file_tmp);
}elseif($file_type == "image/x-png" || $file_type == "image/png"){
$new_img = imagecreatefrompng($file_tmp);
}elseif($file_type == "image/gif"){
$new_img = imagecreatefromgif($file_tmp);
}
//list width and height and keep height ratio.
list($width, $height) = getimagesize($file_tmp);
$imgratio=$width/$height;
if ($imgratio>1){
$newwidth = $ThumbWidth;
$newheight = $ThumbWidth/$imgratio;
$newwidth2 = $ImgWidth;
$newheight2 = $ImgWidth/$imgratio;
}else{
$newheight = $ThumbWidth;
$newwidth = $ThumbWidth*$imgratio;
$newheight2 = $ImgWidth;
$newwidth2 = $ImgWidth*$imgratio;
}
$resized_img = imagecreatetruecolor($newwidth, $newheight);
$resized_img2 = imagecreatetruecolor($newwidth2, $newheight2);
imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagecopyresized($resized_img2, $new_img, 0, 0, 0, 0, $newwidth2, $newheight2, $width, $height);
//save image
ftpupload($file_name, $file_tmp, "httpdocs/uploads/images/thumbs/");
ftpupload($file_name, $file_tmp, "httpdocs/uploads/images/big/");
imagedestroy($resized_img);
imagedestroy($resized_img2);
imagedestroy($new_img);
}
//$value = "$rand_name.$file_ext";
$value = $file_name;
return $value;
}
At the moment this is all working fine and sends the image to the but just does not resize or rename. How can i get this to work how i want it to?
Cheers,
Adam