tags:

views:

103

answers:

2

Hello! I want to know the best way of writing out my "$imagepath" in to this input

This is my upload script

<?php
        if(isset($_POST['submit'])){
          if (isset ($_FILES['new_image'])){
              $imagename = $_FILES['new_image']['name'];
              $source = $_FILES['new_image']['tmp_name'];
              $target = "temporary_images/".$imagename;
              move_uploaded_file($source, $target);

              $imagepath = $imagename;
              $save = "temporary_images/" . $imagepath; //This is the new file you saving
              $file = "temporary_images/" . $imagepath; //This is the original file

              list($width, $height) = getimagesize($file) ; 

              $modwidth = 350;                         
              $modheight = 100; 

              $tn = imagecreatetruecolor($modwidth, $modheight) ; 
              $image = imagecreatefromjpeg($file) ; 
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 

              imagejpeg($tn, $save, 100) ; 

              $save = "temporary_images/sml_" . $imagepath; //This is the new file you saving
              $file = "temporary_images/" . $imagepath; //This is the original file

              list($width, $height) = getimagesize($file) ; 

              $modwidth = 80; 
              $modheight = 100; 

              $tn = imagecreatetruecolor($modwidth, $modheight) ; 
              $image = imagecreatefromjpeg($file) ; 
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 

              imagejpeg($tn, $save, 100) ; 
            echo "Large image: <img src='temporary_images/".$imagepath."'><br>"; 
            echo "$imagepath"
          }
        }

And this is my form

<form>
 <input name="animeinput" id="animeinput" size="20" class="textbox">
</form>
+1  A: 

If you've got the var available to the markup:

<form>
   <input name="animeinput" id="animeinput" size="20" class="textbox" value="<?php echo htmlspecialchars($imagePath); ?>" />
</form>
David Archer
Use `htmlspecialchars` on it.
Gumbo
My "script" is an uploading script.. And since you do not have an image uploaded when you load the page you cant write out the $imagepath..I would like it to write out $imagePath in the input when an image has been uploaded so it displays the name of the picture.How do i do this?
edited, thanks :-)
David Archer
@Per, if your form is e.g in the same script file as your upload code above, then $imagePath will be available when you build the markup. You could test it for null to see if you should add it to the markup
David Archer
A: 

Note that there can often be an issue with caching and timing when doing this (so if you are having problems actually seeing your image after submitting your form then see below), I guess this has something to do with the image not being in place by the time the resulting page is loaded. I got around this issue by updating my image with jquery/ajax.

Once the form is posted and image uploaded I save a reference to the image in a hidden tag #large_image. Then use this jquery....

$(document).ready(function(){

    //read in hidden reference to image URL
var large_photo = $("#large_image").val();

   //display relevent image (use placeholder if no image uploaded yet)
if (large_photo != "") {
    $("#photo_holder").html('<img src="' + large_photo + '" />');
} else {
    $("#photo_holder").html('<img src="noimagefound.png" />'); 
}

});

In your case then, instead of echoing the image src you would echo a hidden tag with a path to the image.

Hope this helps :)

Julian Young
I dont think you got what i meant. http://www.anitard.org/siggen/siggen_stripes/The picture shows up when you've uploaded an image.But i need the script to write the name of the file in the form<form> <input name="animeinput" id="animeinput" size="20" class="textbox"></form>