Thumbnail mod effects upload

Discussion in 'PHP' started by chrisj, Mar 10, 2015.

  1. #1
    I'm using the PHP script for a video web site, and have added the ability to
    show the user's avatar (that he adds to his User Profile) as his uploaded video's Thumbnail image,
    in place of the thumbnail that's generated by the uploaded video itself
    (if the User leaves his avatar blank, the thumbnail generated by the video will show).

    It works successfully when the videos uploaded are short and not when the videos are longer.

    When I comment-out the added code (the nine commented-out lines near the bottom of the convertor.php code -posted below), all files upload successfully and the video-generated thumbnail appears.

    When I remove the commenting-out of those lines of code, only shorter videos show the avatar thumbnail, and only shorter videos(200KB for example) upload at all. Longer videos (25,000KB for example) don't appear to have uploaded.

    Any insight or suggestion will be appreciated.

    
    //_____CREATE THUMBNAIL IMAGE_____________________
        $output_file = $base_path.'/uploads/thumbs/'.$file_name_no_extension.'.jpg';
        $player_output_file = $base_path.'/uploads/player_thumbs/'.$file_name_no_extension.'.jpg';
    
    //__create standard thumb_______
        $cmd = "$config[path_to_ffmpeg] -i $new_mp4 -ss $thumb_position -vframes 1 -s 120x90 -r 1 -f mjpeg $output_file";
        $output = '';
        @exec("$cmd 2>&1",$output);
    capture_output($output,$cmd);
    
    //__create large thumb for better player image_______
        $cmd = "$config[path_to_ffmpeg] -i $new_mp4 -ss $thumb_position -vframes 1 -s 560x420 -r 1 -f mjpeg $player_output_file";
        $output = '';
        @exec("$cmd 2>&1",$output);
        capture_output($output,$cmd);
    
    //__check if thumbnail was created_______
        if(!file_exists($output_file)) {
    
          $cmd = "$config[path_to_ffmpeg] -i $new_mp4 -ss $thumb_position -vframes 1 -s 120x90 -r 1 -f image2 $output_file";
            $output = '';
           @exec("$cmd 2>&1",$output);
            capture_output($output,$cmd);
        }
    
    //__check if thumbnail is 0 bytes_________
        if(filesize($output_file) == 0) {
          $second = '00:00:04';
            $cmd = "$config[path_to_ffmpeg] -i $new_mp4 -deinterlace -an -ss $second -vframes 1 -s 120x90 -r 1 -y -vcodec mjpeg -f mjpeg $output_file";
            $output = '';
            @exec("$cmd 2>&1",$output);
            capture_output($output,$cmd);
        }
    
    //___JUMP START DATABASE_________________________________
    $sql_test = 'SELECT * FROM videos LIMIT 1';
    
        if(!mysql_query($sql_test)) {
          include_once ('classes/mysql.inc.php');
            @mysql_close();
                @mysql_connect($config["hostname"],$config["dbusername"],$config["dbpassword"],true);
            @mysql_select_db($config["dbname"]);
        }
    //___UPDATE THE DATABASE_____________________________________________
    
        //__set to just pending______
        $sql = "UPDATE videos SET approved='pending' WHERE video_id = '$raw_video'";
        @mysql_query($sql);
    
    //__reset video id - remove extension______
        $sql = "UPDATE videos SET video_id='$file_name_no_extension' WHERE video_id = '$raw_video'";
        @mysql_query($sql);
    
    //__set video duration_______
        $sql = "UPDATE videos SET video_length='$duration' WHERE video_id = '$file_name_no_extension'";
        @mysql_query($sql);
    
    //__auto approve________
        if($config["auto_approve_videos"] == "yes") {
          $sql = "UPDATE videos SET approved='yes' WHERE video_id = '$file_name_no_extension'";
           @mysql_query($sql);
        }
    
    //___DELETE ORIGINAL FILE_________________________________
    
        $original_file = $raw_video_path;
    
        if($config['delete_original'] == 'yes') {
            if(@file_exists("$new_mp4") && @file_exists("$raw_video_path")) {
                if($new_mp4 != $raw_video_path) {
                        @unlink($raw_video_path);
                }
            }
        }
    
        //        if($config['delete_avi'] == 'yes') if(@file_exists("$new_mp4") && @file_exists("$avi_file")) @unlink($avi_file);
    
    //$sql = "SELECT file_name FROM pictures WHERE user_id = $user_id LIMIT 1";
    //$result = mysql_query($sql);
        //$pic_file = $row['file_name'];
        //$output_file = $base_path."/uploads/thumbs/".$file_name_no_extension.'.jpg';
        //$input_file = $base_path."/pictures/".$pic_file;
        //echo "Input file = ".$input_file;
        //echo " and Output file = ".$output_file."<br>";
        //copy($input_file, $output_file);
    @mysql_close();
    }
    // end while
    PHP:
     
    chrisj, Mar 10, 2015 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Any code would be appreciated...
     
    PoPSiCLe, Mar 10, 2015 IP
  3. chrisj

    chrisj Well-Known Member

    Messages:
    606
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    Here's the code attached
     

    Attached Files:

    chrisj, Mar 10, 2015 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Tip for next post: use the CODE-tags to post the code directly in the post - attachments make it unnecessary complicated.
     
    PoPSiCLe, Mar 10, 2015 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    Well, the code is a bit of a mess, hence a bit hard to decode, but first of, you have no check to see if there is actually an uploaded file - you also reassign the $output_file with the exact same value as before - no need for that (unless there are other if-conditions not showing in the partial code).
    Besides that, you're using mysql_ to hook up to the database, instead of using a proper DB-handler like mysqli_ or PDO, with prepared statements. That being said, again, it doesn't seem like you check if the file exists at all - hence it won't work if the user doesn't have an uploaded picture.
     
    PoPSiCLe, Mar 10, 2015 IP
  6. chrisj

    chrisj Well-Known Member

    Messages:
    606
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #6
    Thanks for your reply. I tried first, several times to use the code tags dialog box, without success. Can you please help me add some code that would "check to see if there is actually an uploaded file - and also reassign the $output_file with the exact same value as before", please?

    This is the thumbnail mod code:
            $sql = "SELECT file_name FROM pictures WHERE user_id = $user_id LIMIT 1";
            $result = mysql_query($sql);
            $row = mysql_fetch_array($result);
            $pic_file = $row['file_name'];
            $output_file = $base_path."/uploads/thumbs/".$file_name_no_extension.'.jpg';
            $input_file = $base_path."/pictures/".$pic_file;
            echo "Input file = ".$input_file;
            echo " and Output file = ".$output_file."<br>";
            copy($input_file, $output_file);
    PHP:
    Any help will be appreciated.
     
    chrisj, Mar 11, 2015 IP
  7. chrisj

    chrisj Well-Known Member

    Messages:
    606
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #7
    Thanks for your reply. I'm apparently having problems with the code dialog box here.

    Can you please help me with some code to "check to see if there is actually an uploaded file - and also reassign the $output_file with the exact same value as before", please?

    
            $sql = "SELECT file_name FROM pictures WHERE user_id = $user_id LIMIT 1";
            $result = mysql_query($sql);
            $row = mysql_fetch_array($result);
            $pic_file = $row['file_name'];
            $output_file = $base_path."/uploads/thumbs/".$file_name_no_extension.'.jpg';
            $input_file = $base_path."/pictures/".$pic_file;
            echo "Input file = ".$input_file;
            echo " and Output file = ".$output_file."<br>";
            copy($input_file, $output_file); 
    PHP:
    Any help will be appreciated.
     
    chrisj, Mar 11, 2015 IP
  8. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #8
    Well... definitely not tested, and I haven't used mysql_ for... I dunno - at least 5 years, so I'm a bit rusty, so no guarantee this works at all, but it might give you an idea, at least:
        $sql = mysql_query("SELECT file_name FROM pictures WHERE user_id = $user_id LIMIT 1");
        $pic_file = '';
        while ($row = mysql_fetch_field($sql)) {
            $pic_file = $row['file_name'];
        }
    
        if ($pic_file != '') {
            $input_file = $base_path."/pictures/".$pic_file;
            echo "Input file = ".$input_file;
            echo " and Output file = ".$output_file."<br>";
            copy($input_file, $output_file);
        }
    
    Code (markup):
     
    Last edited: Mar 11, 2015
    PoPSiCLe, Mar 11, 2015 IP
  9. chrisj

    chrisj Well-Known Member

    Messages:
    606
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #9
    Thanks for your message and suggestion, however it was unsuccessful.
    Any other help will be appreciated
     
    chrisj, Mar 14, 2015 IP