Debt Consolidation - Wordpress Theme - Find services - Debt Consolidation - Find jobs

PDA

View Full Version : Image Resize and FTP Upload


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

happpy
Nov 10th 2008, 9:11 pm
looks like you upload the big image into "thumb" and "big"

mob4u1
Nov 11th 2008, 2:43 am
//save image
ftpupload($file_name, $file_tmp, "httpdocs/uploads/images/thumbs/");
ftpupload($file_name, $file_tmp, "httpdocs/uploads/images/big/");


shouldn't it be this?:

//save image
ftpupload($resized_img, $file_tmp, "httpdocs/uploads/images/thumbs/");
ftpupload($resized_img2, $file_tmp, "httpdocs/uploads/images/big/");


Daniel

rene7705
Nov 12th 2008, 12:19 am
shouldn't it be this?:

//save image
ftpupload($resized_img, $file_tmp, "httpdocs/uploads/images/thumbs/");
ftpupload($resized_img2, $file_tmp, "httpdocs/uploads/images/big/");


Daniel

You're messing with the wrong parameter ;)

The OP is indeed uploading the original image twice.. (2nd param)

fix:


....
$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);

$somePath = '/path/to/some/safe/temp/dir';
imagejpeg ($resized_img, $somePath.'1.jpg');
imagejpeg ($resized_img2, $somePath.'2.jpg');

//save image
ftpupload($file_name, $somePath.'1.jpg', "httpdocs/uploads/images/thumbs/");
ftpupload($file_name, $somePath.'2.jpg', "httpdocs/uploads/images/big/");

imagedestroy($resized_img);
imagedestroy($resized_img2);
imagedestroy($new_img);
....


you could also use imagepng() instead of imagejpeg() but for non-vector pictures jpeg is simply smaller in filesize at the same quality.

adamjblakey
Nov 17th 2008, 4:07 am
I tried this but it gives me the error of Could not upload file

Subikar
Nov 17th 2008, 4:10 am
Use this script http://www.myblogplanner.com/blog/wp-content/uploads/2008/01/resize.zip
You wil get details from this post

http://www.myblogplanner.com/blog/upload-resize/

Subikar

adamjblakey
Nov 17th 2008, 4:14 am
This does not FTP upload the image as well though and that is what i need as it will be a remote server.

adamjblakey
Nov 17th 2008, 4:28 am
Right i have got a step further. The image will now be uploaded with the name change but i still can't get the resized version uploaded. Can you see anything wrong?


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);


imagejpeg ($resized_img, $rand_name.".".$file_ext);
imagejpeg ($resized_img2, $rand_name.".".$file_ext);

//save images
ftpupload($rand_name.".".$file_ext, $file_tmp, "httpdocs/uploads/images/thumbs/");
ftpupload($rand_name.".".$file_ext, $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;
}