Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    33
    Thanks
    18
    Thanked 0 Times in 0 Posts

    image update with jquery $.ajax

    Heya,

    on every button click I apply a filter on an image. I want to update the image thumbnail and fancybox image too whenever the filter has been applied.

    I'm trying to achieve this with jquery $.ajax

    My current method works. But only for the very first click. For every additional click, the filter will apply but both the thumbnail and the fancybox image won't update until I refresh the browser.

    (#image+rowid is my thumbnail and #fancy+rowid is my fancybox image)
    Code:
    function filterBrigthness(fname,rowid,directory)
    {
        $.ajax({ url: "updatefile.php",
            data: {"filename":fname,"directory":directory,"action":"brightness"},
            type: 'post',
            success: function(output) {
    			$("#image"+rowid).attr("src", directory+"/"+fname);
    			$("#fancy"+rowid).attr("href", directory+"/"+fname);
            }
        });
    }
    Code:
    <?php
    $filename = $_POST['filename'];
    $path = $_POST['directory'];
    $action = $_POST['action'];
    if(file_exists($path."/".$filename)) {
    
    	$im = imagecreatefromjpeg($path."/".$filename) or imagecreatefrompng($path."/".$filename) or imagecreatefromgif($path."/".$filename);
    	if($action == "brightness")
    	{
    		if ($im && imagefilter($im, IMG_FILTER_BRIGHTNESS, 40))
    		{
    			imagejpeg($im, $path."/".$filename);
    		} else {
    			echo 'Image brightness change failed.';
    		}
    	imagedestroy($im);
    	}
    I was first thinking cache was the issue, but post should not rely on cache anyway no? :/

    any help is appreciated, thanks in advance!

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,349
    Thanks
    11
    Thanked 589 Times in 570 Posts
    probably is cache, if the img src is not different than the last time you set it.
    same path=no re-try for aggressive browsers like chrome.



    you can probably do this to get a new url each time:

    Code:
    $("#image"+rowid).attr("src", directory+"/"+fname+"?"+ (+new Date())  );
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • Users who have thanked rnd me for this post:

    coffeecup (04-14-2013)

  • #3
    New Coder
    Join Date
    Jul 2012
    Posts
    33
    Thanks
    18
    Thanked 0 Times in 0 Posts
    fixed it! thanks alot!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •