Re: php with ajax - uploading pictures

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, April 9, 2006 3:21 pm, Merlin wrote:
> I am searching for a good upload framework that works with PHP and
> AJAX
> to provide an upload interface that uploads one picture instantly,
> shows
> the thumbnail and displays another upload formfield to select the next
> picture for upload.
>
> Is there something like this, or similar around?

I dunno where you plan to put the AJAX part...

upload.php
<?php
  $destination = "/path/to/some/directory/I/can/write/";
  //untested code:
  if (isset($_FILES) && count($_FILES)){
    if (move_uploaded_file($_FILES['image']),
"$destination/$_FILES[image_name]"){
      echo "<img src=\"thumbnail.php/$_FILES[image_name]\" /><br />\n";
    }
  }
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"
enctype="multipart/form-data">
  Upload an image: <input type="file" name="image" /><input
type="submit" value="Go" />
</form>

thumbnail.php
<?php
  $destination = "/path/to/some/directory/I/can/write/";
  $filename = substr($_SERVER['PATH_INFO'], 1);
  $image = imagecreatefromjpeg($filename);
  $width = imagesx($image);
  $height = imagesy($image);
  $thumb_height = round(50 * $height/$width);
  $thumb = imagecreatetruecolor(50, $thumb_height);
  imagecopyresized($thumb, $image, 0, 0, 0, 0, $width, $height, 50,
$thumb_height);
  $thumbfile = str_replace('.jpg', '_thumb.jpeg', $filename);
  imagejpeg($thumb, "$destination/$thumbfile");

  header("Content-type: image/jpeg");
  imagejpeg($thumb);
?>

You're on your own to add the error checking, test the error setting
of $_FILES, validate the image to avoid being a warez site, handling
goofballs who name their .jpg files as something else like '.jpeg', or
handling other image formats or...

But, really, it's not exactly a complicated script...

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux