在ftp_put上调整图像大小(创建缩略图)

时间:2016-01-28 20:08:18

标签: php image ftp put

所以我有一个简单的目标,但我似乎无法让一部分工作。

1)上传图片并创建3种不同尺寸:LG,MD,SM;然后重命名图像。

2)将图像从服务器/网站A移动到服务器/网站B.

我有上传和重命名工作所有三个图像,但我无法弄清楚我在重新调整大小的部分做错了什么。任何帮助将不胜感激。

注意:我目前正试图让一张图片调整大小,我一次认为最聪明一点,以确保我了解正在发生的事情。

代码:

function thumb_resize_image_new($source, $destination, $image_type, $max_size, $image_width, $image_height, $quality){

    if($image_width <= 0 || $image_height <= 0){return false;} //return false if nothing to resize

    //do not resize if image is smaller than max size
    if($image_width <= $max_size && $image_height <= $max_size){
        return true;
    }

    //Construct a proportional size of new image
    $image_scale = min($max_size/$image_width, $max_size/$image_height);
    $new_width = ceil($image_scale * $image_width);
    $new_height = ceil($image_scale * $image_height);

    $new_canvas = imagecreatetruecolor( $new_width, $new_height ); //Create a new true color image
    imagealphablending($new_canvas,true);
    $success = imagecopyresampled($new_canvas, $source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);

    return true;
}
if(isset($_POST['sendFile'])) { 
    $image_name = $_FILES['image_file']['name']; //file name
    $destination_folder = "folder/";
    $sm_square_size = 300; //Thumbnails will ! be cropped 
    $sm_prefix = "_sm"; //Small thumb Prefix
    $md_square_size = 600; //Thumbnails will ! be cropped 
    $md_prefix = "_md"; //Medium thumb Prefix
    $max_image_size = 1200; //Maximum image size (height and width)
    $lg_prefix = "_lg"; //Large thumb Prefix
    $jpeg_quality = 90; //jpeg quality

    if($image_name != "") {
        $image_size = $_FILES['image_file']['size']; //file size
        $image_temp = $_FILES['image_file']['tmp_name']; //file temp
        $image_size_info = getimagesize($image_temp); //get image size
        //echo "Image ".$image_name." Uploaded. ";
        if($image_size_info){
            $image_width = $image_size_info[0]; //image width
            $image_height = $image_size_info[1]; //image height
            $image_type = $image_size_info['mime']; //image type
        } else {
            die("Make sure image file is valid!");
        }

        //switch statement below checks allowed image type 
        //as well as creates new image from given file 
        switch($image_type){
            case 'image/png':
                $image_res =  imagecreatefrompng($image_temp); break;
            case 'image/gif':
                $image_res =  imagecreatefromgif($image_temp); break;
            case 'image/jpeg': case 'image/pjpeg':
                $image_res = imagecreatefromjpeg($image_temp); break;
            default:
                $image_res = false;
        }

        if($image_res){
            //Get file extension and name to construct new file name 
            $image_info = pathinfo($image_name);
            $image_extension = strtolower($image_info["extension"]); //image extension
            $image_name_only = strtolower($image_info["filename"]);//file name only, no extension
            $dateRec = date('d-M-Y-h-i-s');

            //create a random name for new image (Eg: fileName_293749.jpg) ;
            $new_file_name = $dateRec. '_' .  rand(0, 9999);

            $thumb_name = $new_file_name.$sm_prefix.'.'.$image_extension;
            $medium_name = $new_file_name.$md_prefix.'.'.$image_extension;
            $large_name = $new_file_name.$lg_prefix.'.'.$image_extension;

            $thumb_path = $destination_folder.$thumb_name;
            $medium_path = $destination_folder.$medium_name;
            $large_path = $destination_folder.$large_name;


            $ftp_server = "";
            $ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
            $ftp_username = "";
            $ftp_userpass = "";
            $login_conn = ftp_login($ftp_conn, $ftp_username, $ftp_userpass) or die("Could not connect to ftp login");
            if((!$ftp_conn) || (!$login_conn)) {
                print "FTP connection failed!"; exit();
            } else {
                // upload a file

                if(thumb_resize_image_new($image_res, $image_temp, $image_type, $md_square_size, $image_width, $image_height, $jpeg_quality)) { 
                    echo "thumb created. $success <br />";
                } else { 
                    die('Error Creating thumbnail: '.$thumb_path.'<br />'); 
                }

                if(ftp_put($ftp_conn, $thumb_path, $image_temp, FTP_BINARY)) {
                    echo "Successfully uploaded $thumb_name <hr />";
                } else {
                    echo "There was a problem while uploading $thumb_name into $thumb_path <hr />";
                }

                if(ftp_put($ftp_conn, $medium_path, $image_temp, FTP_BINARY)) {
                    echo "Successfully uploaded $medium_name <hr />";
                } else {
                    echo "There was a problem while uploading $medium_name into $medium_path <hr />";
                }

                if(ftp_put($ftp_conn, $large_path, $image_temp, FTP_BINARY)) {
                    echo "Successfully uploaded $large_name <hr />";
                } else {
                    echo "There was a problem while uploading $large_name into $large_path <hr />";
                }

            }
            ftp_close($ftp_conn);   
        }   
    }
}

如果有人知道如何帮助解决这个问题,或者更好的方法,我会永远感激不尽!

0 个答案:

没有答案