duandi8838 2014-09-14 08:03
浏览 37
已采纳

上传时使用脚本优化图像

Am try upload image to server while uploading i need the image to be optimized

  • i need script to optimized
  • types of file(jpeg,png,gif)


i found many online tool like http://www.smushit.com/ysmush.it/ but i need script its possible to use script

<input name="file" type="file" id="ufile" /> this the input method am using

  • how to optimize image and save to custom folder

Upload script

<form action="for.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
         <input name="file" type="file" id="ufile" size="50" /><br>
            <input type="submit" name="submit">
        </form> 
  • 写回答

1条回答 默认 最新

  • dongxing5525 2014-10-19 03:57
    关注

    try this function am using this in my program it works fine

    <?php
    ############ Configuration ##############
    $thumb_square_size      = 200; //Thumbnails will be cropped to 200x200 pixels
    $max_image_size         = 5000; //Maximum image size (height and width)
    $thumb_prefix           = "thumb_"; //Normal thumb Prefix
    $destination_folder     = 'uploads/'; //upload directory ends with / (slash)
    $jpeg_quality           = 90; //jpeg quality
    ##########################################
    
    //continue only if $_POST is set and it is a Ajax request
    if(isset($_POST) && isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&     strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
    
    // check $_FILES['ImageFile'] not empty
    if(!isset($_FILES['image_file']) || !is_uploaded_file($_FILES['image_file']    ['tmp_name'])){
            die('Image file is Missing!'); // output error when above checks     fail.
    }
    
    //uploaded file info we need to proceed
    $image_name = $_FILES['image_file']['name']; //file 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
    
    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
    
        //create a random name for new image (Eg: fileName_293749.jpg) ;
        $new_file_name = $image_name_only. '_' .  rand(0, 9999999999) . '.' .     $image_extension;
    
        //folder path to save resized images and thumbnails
        $thumb_save_folder  = $destination_folder . $thumb_prefix .     $new_file_name; 
        $image_save_folder  = $destination_folder . $new_file_name;
    
        //call normal_resize_image() function to proportionally resize image
        if(normal_resize_image($image_res, $image_save_folder, $image_type,     $max_image_size, $image_width, $image_height, $jpeg_quality))
        {
            //call crop_image_square() function to create square thumbnails
            if(!crop_image_square($image_res, $thumb_save_folder, $image_type,     $thumb_square_size, $image_width, $image_height, $jpeg_quality))
            {
                die('Error Creating thumbnail');
            }
    
            /* We have succesfully resized and created thumbnail image
            We can now output image to user's browser or store information in     the database*/
            echo '<div align="center">';
            echo '<img src="uploads/'.$thumb_prefix . $new_file_name.'"     alt="Thumbnail">';
            echo '<br />';
            echo '<img src="uploads/'. $new_file_name.'" alt="Resized Image">';
            echo '</div>';
        }
    
        imagedestroy($image_res); //freeup memory
    }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序