我正在使用gulp和PHP为上传创建一个图像优化器。
我希望能够将上传文件的文件路径url输出到index.php中,最好的方法是什么。
我尝试使用下面的方法模仿文件路径的输出,但是没有用... - :打印'https://footpatrol.s3.amazonaws.com/images/'。date('Y')。'/'。日期( 'd-M') '/' $ newfilename '';。。。
PHP代码。
<?php
$files = glob('images/*'); // get all file names
foreach($files as $file){ // iterate files
if(is_file($file))
unlink($file); // delete file
}
foreach($_FILES['file']['name'] as $index=>$name){
$filename = $name;
if(!file_exists("images/".$filename)){
$file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
$file_ext = substr($filename, strripos($filename, '.')); // get file name
$newfilename = md5($file_basename) . $file_ext;
move_uploaded_file($_FILES["file"]["tmp_name"][$index],"images/" . $newfilename);
if( move_uploaded_file($_FILES["file"]["tmp_name"][$index],"images/" . $newfilename)){
print '<div class="img">https://footpatrol.s3.amazonaws.com/images/'.date('Y').'/'. date('d-m').'/'.$newfilename.'</div>';
}
}
}
?>
Gulp Code ..
var gulp = require('gulp');
var imagemin = require('gulp-imagemin');
var clean = require('gulp-clean');
var s3 = require('gulp-s3');
var AWS = require('aws-sdk');
var fs = require('fs')
var pngquant = require('imagemin-pngquant');
var jpegrecompress = require('imagemin-jpeg-recompress');
var moment = require('moment');
var runSequence = require('run-sequence');
// Delete files in image folder
gulp.task('cleanTemp', function(cb) {
return gulp.src('dist/images/*', { read: false }).pipe(clean());
cb(err)
});
// Delete files in image folder
gulp.task('delete', ['image'], function() {
return gulp.src('./images/*', { read: false })
.pipe(clean());
});
var year = moment().format('YYYY');
var today = moment().format('MM-DD');
// // Image Optimisation
//console.log('dist/images/'+year+'/'+today)
gulp.task('image',['cleanTemp'], function() {
return gulp.src('images/*')
.pipe(imagemin([
imagemin.gifsicle({ interlaced: true }),
jpegrecompress({
progressive: true,
max: 70,
min: 55
}),
pngquant({ quality: '70-80' })
]))
.pipe(gulp.dest('dist/images/' + year + '/' + today + ''));
});
aws = JSON.parse(fs.readFileSync('./aws.json'));
// // UPLOAD
gulp.task('upload', function() {
return gulp.src('./dist/**')
.pipe(s3(aws))
});
// Watch Image files
gulp.task('watch', function() {
gulp.watch('images/*', function(event) {
runSequence('cleanTemp', ['image','upload']);
});
});
// Default Task
gulp.task('default', ['cleanTemp','image', 'upload']);