很高兴成为这个论坛的一员。
我正在寻找一个具有触发器并在上传过程中执行的压缩事件 - 这里我尝试使用' file-upload'在上传过程中压缩照片。 jansy bootstrap。
功能:
function compress($source, $destination, $quality) {
$info = getimagesize($source);
if ($info['mime'] == 'image/jpeg')
$image = imagecreatefromjpeg($source);
elseif ($info['mime'] == 'image/gif')
$image = imagecreatefromgif($source);
elseif ($info['mime'] == 'image/png')
$image = imagecreatefrompng($source);
imagejpeg($image, $destination, $quality);
return $destination;
}
jansy-bootstrap代码:
$photo_name = time();
$photo_type = $_FILES['photo']['name'];
$photo_size = $_FILES['photo']['size'];
$photo_tmp_name = $_FILES['photo']['tmp_name'];
$errors = array();
$maxsize = 2097152;
$acceptable = array(
'image/jpeg',
'image/jpg',
'image/gif',
'image/png'
);
if(($_FILES['photo']['size'] >= $maxsize) || ($_FILES["photo"]["size"] == 0)) {
$errors[] = 'File too large. File must be less than 2 megabytes.';
?><script type="text/javascript">window.history.back();</script><?php
}
if(!in_array($_FILES['photo']['type'], $acceptable)) {
$errors[] = 'Invalid file type. Only JPG, GIF and PNG types are accepted.';
?><script type="text/javascript">window.history.back();</script><?php
}
if(count($errors) === 0) {
$source_img = $photo_name.'.jpg';
$source = $source_img;
$destination_img = $photo_name.'_c.jpg';
$d = compress($source_img, $destination_img, 90);
move_uploaded_file($_FILES['photo']['tmp_name'], 'upload/'.$d );
} else {
foreach($errors as $error) {
echo '<script>alert("'.$error.'");</script>';
}
die(); //Ensure no more processing is done
}
无法确定我们出了什么问题 - 任何帮助都会感激不尽......