WordPress.org

Ready to get started?Download WordPress

Forums

Problem with saving images by webcam on comment (1 post)

  1. Johnny-Pi
    Member
    Posted 11 months ago #

    I'm trying to make a plugin that through a webcam js take a shot of you while commenting and let you to choose a picture to insert as avatar.

    The plugin seems to work except for saving jpg.

    I think the problem is in the main php file (ImageComment.php):

    <?php 
    
    add_action("comment_form","webcamPlugin");
    add_action("comment_form","cancellaIMG");
    add_filter("comment_text", "attachCommentImage");
    add_filter("delete_comment", "deleteComment");
    
    function webcamPlugin()
    {
    	global $wpdb;
    	$last = $wpdb->get_row("SHOW TABLE STATUS LIKE 'wp_comments'");
        $idcommento = $last->Auto_increment;
    	//$idcommento=get_comment_ID()+1;
    	$post = get_the_ID();
    
    	//includo gli script necessari
    	wp_enqueue_script ( 'jquery' ); //jquery c'è già in wordpress, quindi se lo cerca lui
    	wp_enqueue_script ( 'jquerywebcam', plugins_url ( 'static/js/jquery.webcam.js', __FILE__) );
    	wp_enqueue_script ( 'jquerylightbox', plugins_url ( 'static/js/jquery.lightbox.js', __FILE__ ) );
    	wp_enqueue_script ( 'imageComment', plugins_url ( 'imageComment.js', __FILE__ ), array('jquery') );
    
    	//passo le variabili necessarie agli script
    	$params = array ( 'plugins_url' => plugins_url (), 'post' => $post, 'idCommento' => $idcommento, 'content_url' => content_url () );
    	wp_localize_script( 'imageComment', 'variabili', $params );
    
    	//includo gli stili
    	wp_enqueue_style ( 'wic', plugins_url ( 'style.css', __FILE__) );
    	wp_enqueue_style ( 'lightbox', plugins_url ( 'static/css/jquery.lightbox.css', __FILE__) );
    
    	echo'<div id="camera"></div>
    		<div id="status"></div>
    		<div id="imgthumb"></div>
    
    	   <input name="submit" type="button" onclick="InitCamera();" class="Camera" id="showebcam" value="Show Webcam" />
    	   <input style="visibility:hidden" name="submit" type="button" onclick="saveImage();" class="Camera" id="cattura" value="Capture" />
    	   <input style="visibility:hidden" name="submit" type="button" onclick="HideWebcam();" class="Camera" id="hidewebcam"
    	   value="Hide Webcam" />
    	   <input style="visibility:hidden" name="submit" type="button" onclick="deleteAll();" class="Camera" id="delete"
    	   value="Delete All" /> ';
    }
    
    function attachCommentImage($comment = "")
    {
    	$post=get_the_ID();
        $id = get_comment_ID();
        $count=0;
        for($i=0;$i <= $count;$i++){
    	    if (file_exists(ABSPATH."wp-content/uploads/WebcamImageComment/".$post.'-'.$id."-".$count.".jpg"))
    	        {
    	        	$src=content_url()."/uploads/WebcamImageComment/".$post.'-'.$id."-".$count.".jpg";
    	        	$comment .= "\n<a href=".$src." id=\"commentimglink\"> <img src=".$src." class=\"commentimages\" id=\"commentimage\"/> </a>";
    	            $count++;
    	        }
    	 }
        return $comment;
    }
    
    function cancellaIMG(){
    	$post=get_the_ID();
    	$id = get_comment_ID()+1;
    	$count=0;
        for($i=0;$i <= $count;$i++){
    	    if (file_exists(ABSPATH."wp-content/uploads/WebcamImageComment/".$post.'-'.$id."-".$count.".jpg"))
    	        {
    	        	$nome_file=ABSPATH."wp-content/uploads/WebcamImageComment/".$post.'-'.$id."-".$count.".jpg";
    	        	unlink($nome_file);
    	            $count++;
    	        }
    	}
    }
    
    function deleteComment($id)
    {
    	$my_id = $id;
    	$comment_id= get_comment($my_id);
    	$comment_post_id=$comment_id->comment_post_ID;
    	$count=0;
        for($i=0;$i <= $count;$i++){
    	    	if (file_exists(ABSPATH."wp-content/uploads/WebcamImageComment/".$comment_post_id.'-'.$my_id."-".$count.".jpg"))
    	        	{
    		        	$nome_file=ABSPATH."wp-content/uploads/WebcamImageComment/".$comment_post_id.'-'.$my_id."-".$count.".jpg";
    		        	unlink($nome_file);
    		            $count++;
    	            }
    	}
    }
    
    ?>

    or maybe in the upload.php file:

    <?php
    if (isset esc_html(($_REQUEST['idcommento'])))
    	if esc_html(($_REQUEST['idcommento'] != '')) {
    		$name = "img".$_REQUEST['idcommento'].".jpg";
    		$percorso = content_url().'\uploads\WebcamImageComment\\';
    		$str = file_get_contents("php://input");
    
    		$img = imagecreatefromstring (pack("H*",$str)); //creo l'immagine dalla stringa passata dall'swf
    
    		if ($img) //se è stata creata
    			imagejpeg ($img, $percorso.$name);
    	}
    
    ?>

    or, finally, in the js file (ImageComment.js):

    //uso jQuery e non $ perché altri plugin lo deinizializzano
    jQuery(".commentimages").mouseover(function()
    {
    	jQuery(this).css("cursor","pointer");
    	jQuery(this).animate({width:"100%"},"slow");
    });
    jQuery(".commentimages").mouseout(function()
    {
    	jQuery(this).animate({width:"40%"},"slow");
    });
     count= 0;
       function InitCamera() {
    	jQuery(document).ready(function(){
    		jQuery("#status").text("Webcam ready!");
    		document.getElementById("cattura").style.visibility="visible";
    		document.getElementById("showebcam").style.visibility="hidden";
    		document.getElementById("hidewebcam").style.visibility="visible";
    		jQuery("#camera").webcam({
    			width: 320,
    			height: 240,
    			mode: "save",
    			swffile: variabili.plugins_url + '/WebcamImageComment/static/js/jscam.swf',
    			onCapture: function () {
    				webcam.save(variabili.plugins_url + '/WebcamImageComment/upload.php?idcommento=' + variabili.post + '-' + variabili.idCommento + '-' + count);
    				count++;
    				jQuery("#status").html("Image captured!");
    			},
    			onTick: function(remain) {
    				if (0 == remain) {
    					jQuery("#status").text("Cheese!");
    				} else {
    					jQuery("#status").text(remain + " seconds remaining...");
    				}
    			},
    			onSave: function(){
    				imglink=document.createElement("a");
    				imglink.setAttribute("href", variabili.content_url + '/uploads/WebcamImageComment/' + variabili.post + '-' + variabili.idCommento + '-' + (count-1) + ".jpg");
    				imglink.setAttribute("id","thumblink");
    				document.getElementById("imgthumb").appendChild(imglink);
    				immagine=document.createElement("IMG");
    				immagine.setAttribute("src", variabili.content_url + '/uploads/WebcamImageComment/' + variabili.post + '-' + variabili.idCommento + '-' + (count-1) + ".jpg");
    				jQuery("#status").html("Post the comment or take more images!");
    				immagine.setAttribute("id","thumb");
    				document.getElementById("thumblink").appendChild(immagine);
    				document.getElementById("delete").style.visibility="visible";
    				jQuery("#imgthumb").fadeOut(350);
    				jQuery("#imgthumb").fadeIn(350);
    				jQuery("#imgthumb a").lightBox();
    
    			},
    		});
       });
    } 
    
    function cancella()
    {
    	jQuery.post(variabili.plugins_url + '/WebcamImageComment/delete.php', { idcommento: variabili.post + '-' + variabili.idCommento + '-' + (count-1)});
    	jQuery("#imgdiv'.jQuerypost.'-'.jQueryidcommento.'-"+(count-1)).empty();
    }
    
    function deleteAll()
    {
    	index=count;
    	for(i=0;i< index;i++){
    		document.getElementById("delete").style.visibility="hidden";
    		jQuery.post(variabili.plugins_url + '/WebcamImageComment/delete.php', { idcommento: variabili.post + '-' + variabili.idCommento + '-' + (count-1)});
    		count--;
    	}
    	count=0;
    	jQuery("#imgthumb").empty();
    	document.getElementById("delete").style.visibility="hidden";
    }
    
    function HideWebcam()
    {
    	deleteAll();
    	jQuery("#camera").empty();
    	document.getElementById("cattura").style.visibility="hidden";
    	document.getElementById("showebcam").style.visibility="visible";
    	document.getElementById("hidewebcam").style.visibility="hidden";
    	jQuery("#status").text("");
    	jQuery("#imgthumb").empty();
    	document.getElementById("delete").style.visibility="hidden";
    }
    
    function saveImage(){
    	webcam.capture(3);
    }
    
    function cancellaImmagine()
    {
    	jQuery("#imgthumb").empty();
    	document.getElementById("cancella").style.visibility="hidden";
    	jQuery.post(variabili.plugins_url + '/WebcamImageComment/delete.php', { idcommento: variabili.post + '-' + variabili.idcommento + '-' + count});
    	jQuery("#status").html("Images deleted..");
    }

Reply

You must log in to post.

About this Topic