doubi9615 2016-01-28 20:08
浏览 72

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

So I have a simple goal but I can't seem to get the one part to work.

1) Upload an image and create 3 different sizes: LG, MD, SM; then rename images.

2) Move the images from server/website A to server/website B.

I have the uploading and renaming working for all three images, but I can't figure out what I'm doing wrong with the re-sizing part. Any help would be greatly appreciated.

Note: I am currently trying to just get one image to resize, I figured one at a time would be smartest to make sure I understand what is happening.

Code:

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

If anyone knows how to help fix this, or a better way of doing it, I'd forever be thankful!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么