0

So i have a form to upload GIFs images and i would like to keep the Animated GIF IMAGE and convert/create a new one static image from the GIF(getting the first frame) to JPG.

This is my form:

<form action="" id="primaryPostForm" method="POST" enctype="multipart/form-data">

        <fieldset>
    <p>Upload funny pictures, paste pictures or YouTube URL, accepting GIF/JPG/PNG (Max size: 3MB)</p>
            <label for="postTitle"><?php _e('* Title of the image:', 'framework') ?></label>

            <input type="text" name="postTitle" id="postTitle" class="required" value="<?php if ( isset( $_POST['postTitle'] ) ) echo $_POST['postTitle']; ?>" />
        </fieldset>

    <div id="file-input" class="file-field">
    <input type="file" name="thumbnail" id="thumbnail">
    </div>

And this is my validation code :

// validation
wp_register_script( 'validation', 'http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js', array( 'jquery' ) );
wp_enqueue_script( 'validation' );
if ( isset( $_POST['submitted'] ) && isset( $_POST['post_nonce_field'] ) && wp_verify_nonce( $_POST['post_nonce_field'], 'post_nonce' ) ) {
    if ( trim( $_POST['postTitle'] ) === '' ) {
        $postTitleError = 'Please enter a title.';
        $hasError = true;
    }
    $post_information = array(
        'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
        'post_content' => $_POST['postContent'],
        'post_type' => 'post',
        'post_status' => 'pending'
    );
$new_post = wp_insert_post( $post_information );
if (!function_exists('wp_generate_attachment_metadata')){
                require_once(ABSPATH . "wp-admin" . '/includes/image.php');
                require_once(ABSPATH . "wp-admin" . '/includes/file.php');
                require_once(ABSPATH . "wp-admin" . '/includes/media.php');
            }
             if ($_FILES) {
                foreach ($_FILES as $file => $array) {
                    if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
                        return "upload error : " . $_FILES[$file]['error'];
                    }
                    $attach_id = media_handle_upload( $file, $new_post );
                }   
            }
            if ($attach_id > 0){
                //and if you want to set that image as Post  then use:
                update_post_meta($new_post,'_thumbnail_id',$attach_id);
            }
}

How can i accomplish this?

Oscar Batlle
  • 157
  • 2
  • 4
  • 21
  • I assume you have copied the code from somewhere, as you are creating the post initially with `$_POST['postContent']` which does not exist in your form. You are also attaching the upload straight away, which you probably want to do if you need the animated GIF, though you are not checking if it is an animated GIF - which you should probably do before creating a new post. You will need to generate the JPEG from the GIF directly and then attach that to the post as well, possibly setting it as the featured image if you have that enabled for the site. – Orbling Dec 03 '13 at 18:09
  • How do i check if the image that I'm submitting is a .GIF extension to add a conditional tag like If image == .GIF { DO THIS } ? – Oscar Batlle Dec 03 '13 at 19:51
  • And i also have a PHP SCRIPT to generate the JPG IMAGE from a .GIF IMAGE which is located in this another question that i have -> http://stackoverflow.com/questions/20343192/wordpress-function-to-convert-from-gif-to-jpg-and-set-it-as-the-post-thumbnail-f – Oscar Batlle Dec 03 '13 at 19:54
  • Best to [check the file extension on the client side before they upload it](http://stackoverflow.com/a/17355937/438971) as well as in PHP, the original name is in one of the fields of `$_FILES` which you are processing. Note checking the extension alone is insufficient, you will have to run tests on the image to see if it is an animated GIF and has frames, etc. – Orbling Dec 03 '13 at 22:28
  • Easiest to only check the extension .jpg .png .gif and create a conditional tag from it. – Oscar Batlle Dec 03 '13 at 22:31
  • Easiest, yes - but not sufficient. Unless you know all your users will guarantee only uploading nothing that can cause your code any trouble. – Orbling Dec 03 '13 at 22:44
  • So what do you recommend? Any code? Im using wordpress,.. – Oscar Batlle Dec 04 '13 at 00:02

0 Answers0