Ok, I have an odd situation here that I can't seem to get my head around.
I've got a script that takes an image upload from a form and copies it to a remote resource via FTP. I've also got a script to create a resized version of an image.
Separately they work just fine, but I can't figure out how to make them work together.
Basically what I need to do is create three versions of the uploaded image, and upload them all to the FTP resource. So if the user uploaded "image.jpg", the script would create "image-tn.jpg" and "image-lg.jpg" and transfer them along with the original "image.jpg" to the FTP resource.
This portion of the script receives the uploaded file, renames it to a unique id based on the last database insert id, and transfers it to the FTP resource:
$product = mysql_insert_id();
$ftp_dump_dir="public_html/catalog/"; //change to destination directory
if(is_uploaded_file($_FILES['userfiles']['tmp_name'])){
$conn_id = ftp_connect($_SESSION['ftp_server']);
$login_result = ftp_login($conn_id, $_SESSION['ftp_user_name'], $_SESSION['ftp_user_pass']);
if ((!$conn_id) || (!$login_result)) {
echo "<img src='images/color_red.gif'> FTP connection has failed!<br>";
echo "Attempted to connect to $ftp_server for user $ftp_user_name";
exit;
}else{
if(!ftp_pasv($conn_id,TRUE)){
echo "<img src='images/color_red.gif'> Could not enter PASV mode!";
}
$filename = $_FILES['userfiles']['name'];
if (@ftp_chdir($conn_id, $ftp_dump_dir)) {
//
}else{
echo "<img src='images/color_red.gif'> Couldn't change directory\n";
}
if(ftp_put($conn_id,$filename,$_FILES['userfiles']['tmp_name'],FTP_BINARY)){
echo "<img src='images/color_green.gif'> File ".$_FILES['userfiles']['name']." was sent successfully<br>";
$old_file = $_FILES['userfiles']['name'];
$extension = get_extension($_FILES['userfiles']['name']);
$new_file = $product.'.'.$extension;
if (ftp_rename($conn_id, $old_file, $new_file)) {
echo '<img src="images/color_green.gif"> Renaming successful<br>';
}
} else {
echo '<img src="images/color_red.gif"> Renaming failed<br>';
}
}else{
echo "<img src='images/color_red.gif'> There was a problem sending file ".$_FILES['userfiles']['name']."<br>";
}
}
ftp_close($conn_id);
}else{
echo"<img src='images/color_red.gif'> File was not uploaded!<br>";
}
Now I also have this function to create a "thumbnail", but I can't figure out how to make it use the FTP path for the file, then publish the resized copy back to the FTP path:
function createthumb($name,$filename,$new_w,$new_h){
$size = getimagesize($name);
global $gd2;
$system=explode(".",$name);
if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
$old_x = $size[0];
$old_y = $size[1];
if($old_x <= 180 && $old_y <= 220){
$thumb_w = $old_x;
$thumb_h = $old_y;
}else{
$imagesize_x = $size[0];
$imagesize_y = $size[1];
$thumbsize_x = 180;
$thumbsize_y = 220;
$thumb_w = round(($imagesize_x * $thumbsize_y)/$imagesize_y);
if ($thumb_w > $thumbsize_x) {
$thumb_w = $thumbsize_x;
$thumb_h = round(($imagesize_y * $thumbsize_x)/$imagesize_x);
}else{
$thumb_h = $thumbsize_y;
}
}
$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
if (preg_match("/png/",$system[1])){
imagepng($dst_img,$filename);
} else {
imagejpeg($dst_img,$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}