image - Creating a thumbnail in PHP store in different folder -
hello looking @ creating thumbnail along main image. stored in different folder original , maximum height of 400px. have had search around seems complex. looking simple. new file uploading stuff. below code have.
// configuration $allowed_filetypes = array('.jpg', '.jpg', '.bmp', '.png', '.gif'); $max_filesize = 100000000; $upload_path = 'artimages/'; $filename = $_files['userfile']['name']; $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // random number $randomnumber = rand(1, 1000000); $filename = $randomnumber.$filename; // file size if(filesize($_files['userfile']['tmp_name']) > $max_filesize) die('the file attempted upload large.'); // file type if(!in_array($ext,$allowed_filetypes)) die('the file attempted upload not allowed.'); // check chmod 777 if(!is_writable($upload_path)) die('fail'); // upload file if(move_uploaded_file($_files['userfile']['tmp_name'],$upload_path . $filename)) echo 'success'; else echo 'fail';
the code below require php-gd
// load image , image size $img = imagecreatefromjpeg( "{$pathtoimages}{$fname}" ); $width = imagesx( $img ); $height = imagesy( $img ); // calculate thumbnail size $new_width = $thumbwidth; $new_height = floor( $height * ( $thumbwidth / $width ) ); // create new temporary image $tmp_img = imagecreatetruecolor( $new_width, $new_height ); // re-create , resize old image new image imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height ); // save thumbnail file imagejpeg( $tmp_img, "{$pathtothumbs}{$fname}" ); }
php image file upload
No comments:
Post a Comment